177 lines
5.3 KiB
Rust
177 lines
5.3 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,
|
|
|
|
pub speedgame_name: Option<String>,
|
|
pub speedgame_category: Option<String>,
|
|
}
|
|
|
|
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 speedgame_name(&mut self) -> rhai::Dynamic {
|
|
match &self.speedgame_name {
|
|
Some(name) => name.clone().into(),
|
|
None => ().into(),
|
|
}
|
|
}
|
|
|
|
pub fn speedgame_category(&mut self) -> rhai::Dynamic {
|
|
match &self.speedgame_category {
|
|
Some(cat) => cat.clone().into(),
|
|
None => ().into(),
|
|
}
|
|
}
|
|
|
|
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()?)
|
|
.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(()),
|
|
}
|
|
}
|
|
|
|
// async fn get_record(&self, name: &str, category: &str) -> Option<(String, String)> {
|
|
// use serde::Deserialize;
|
|
//
|
|
// #[derive(Deserialize)]
|
|
// struct Game {
|
|
// id: String,
|
|
// }
|
|
//
|
|
// #[derive(Deserialize)]
|
|
// struct Category {
|
|
// id: String,
|
|
// name: String,
|
|
// }
|
|
//
|
|
// #[derive(Deserialize)]
|
|
// struct Record {
|
|
// runs: Vec<PlacedRun>,
|
|
// }
|
|
//
|
|
// #[derive(Deserialize)]
|
|
// struct PlacedRun {
|
|
// place: usize,
|
|
// run: Run,
|
|
// }
|
|
//
|
|
// #[derive(Deserialize)]
|
|
// struct Run {
|
|
// players: Vec<Player>,
|
|
// times: Times,
|
|
// }
|
|
//
|
|
// // look up game
|
|
// // look up category
|
|
// // look up records
|
|
// // look up users
|
|
// let mut games_url = Url::parse("https://www.speedrun.com/api/v1/games").unwrap();
|
|
// games_url.query_pairs_mut()
|
|
// .append_pair("name", name);
|
|
// let games: Vec<Game> = self.state.http.get(games_url).send().await.ok()?.json().await.ok()?;
|
|
// let game = games.get(0)?;
|
|
//
|
|
// let mut categories_url = Url::parse("https://www.speedrun.com/api/v1/games/").unwrap()
|
|
// .join(&format!("{}/", game.id)).ok()?
|
|
// .join("categories").ok()?;
|
|
// let categories: Vec<Category> = self.state.http.get(categories_url).send().await.ok()?.json().await.ok()?;
|
|
// let category = categories.iter().find(|cat| cat.name == category)?;
|
|
//
|
|
// let mut records_url = Url::parse("https://www.speedrun.com/api/v1/categories/").unwrap()
|
|
// .join(&format!("{}", category.id)).ok()?
|
|
// .join("records").ok()?;
|
|
//
|
|
// }
|
|
}
|