107 lines
3.0 KiB
Rust
107 lines
3.0 KiB
Rust
use twitch_api2::{
|
|
types::{UserId, UserName},
|
|
helix::channels::{GetChannelInformationRequest, ChannelInformation},
|
|
};
|
|
use crate::app::State;
|
|
use std::sync::Arc;
|
|
|
|
#[derive(Default, Clone)]
|
|
pub struct ExecutorOutput {
|
|
pub to_send: Vec<String>,
|
|
}
|
|
|
|
impl ExecutorOutput {
|
|
pub fn send<S: Into<String>>(&mut self, s: S) {
|
|
self.to_send.push(s.into());
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ExecutorState {
|
|
pub state: Arc<State>,
|
|
|
|
pub args: Vec<rhai::Dynamic>,
|
|
|
|
pub initiator: String,
|
|
pub initiator_id: UserId,
|
|
/// Always false if this is a channel point reward event
|
|
pub broadcaster: bool,
|
|
/// Always false if this is a channel point reward event
|
|
pub moderator: bool,
|
|
/// Always false if this is a channel point reward event
|
|
pub vip: bool,
|
|
/// Always false if this is a channel point reward event
|
|
pub subscriber: bool,
|
|
}
|
|
|
|
impl ExecutorState {
|
|
// FIXME: make this return &str
|
|
pub fn initiator(&mut self) -> String {
|
|
self.initiator.clone()
|
|
}
|
|
|
|
pub fn initiator_id(&mut self) -> String {
|
|
self.initiator_id.to_string()
|
|
}
|
|
|
|
pub fn args(&mut self) -> rhai::Array {
|
|
self.args.clone()
|
|
}
|
|
|
|
pub fn broadcaster(&mut self) -> bool {
|
|
self.broadcaster
|
|
}
|
|
|
|
pub fn moderator(&mut self) -> bool {
|
|
self.moderator
|
|
}
|
|
|
|
pub fn vip(&mut self) -> bool {
|
|
self.vip
|
|
}
|
|
|
|
pub fn subscriber(&mut self) -> bool {
|
|
self.subscriber
|
|
}
|
|
|
|
pub fn get_username<S: Into<UserId>>(&mut self, id: S) -> rhai::Dynamic {
|
|
self.state.runtime.block_on(self.state.get_user_from_id(id))
|
|
.ok()
|
|
.flatten()
|
|
.map(|user| user.login.to_string())
|
|
.map(rhai::Dynamic::from)
|
|
.unwrap_or_else(|| rhai::Dynamic::from(()))
|
|
}
|
|
|
|
pub fn get_user_id<S: Into<UserName>>(&mut self, username: S) -> rhai::Dynamic {
|
|
self.state.runtime.block_on(self.state.get_user_from_login(username))
|
|
.ok()
|
|
.flatten()
|
|
.map(|user| user.id.to_string())
|
|
.map(rhai::Dynamic::from)
|
|
.unwrap_or_else(|| rhai::Dynamic::from(()))
|
|
}
|
|
|
|
async fn internal_get_channel_info<S: Into<UserId>>(&self, id: S) -> Option<(String, String, String)> {
|
|
let req = GetChannelInformationRequest::builder()
|
|
.broadcaster_id(id.into())
|
|
.build();
|
|
self.state.twitch.client.helix.req_get(req, &self.state.twitch.bot_token)
|
|
.await
|
|
.ok()?
|
|
.data
|
|
.map(|info: ChannelInformation| (info.broadcaster_name.to_string(), info.broadcaster_login.to_string(), info.game_name.to_string()))
|
|
}
|
|
|
|
pub fn get_channel_info<S: Into<UserId>>(&mut self, id: S) -> rhai::Dynamic {
|
|
match self.state.runtime.block_on(self.internal_get_channel_info(id)) {
|
|
Some(x) => rhai::Dynamic::from(vec![
|
|
rhai::Dynamic::from(x.0),
|
|
rhai::Dynamic::from(x.1),
|
|
rhai::Dynamic::from(x.2),
|
|
]),
|
|
None => rhai::Dynamic::from(()),
|
|
}
|
|
}
|
|
}
|