Files
artcraft/src/include.rs
T

100 lines
2.7 KiB
Rust

#![allow(dead_code)]
use anyhow::Context as _;
use twitchchat::{messages, AsyncRunner, Status, UserConfig};
use crate::filesystem::secrets::Secrets;
// some helpers for the demo
// fn get_env_var(key: &str) -> anyhow::Result<String> {
// std::env::var(key).with_context(|| format!("please set `{}`", key))
// }
pub fn get_user_config(secrets: &Secrets) -> anyhow::Result<twitchchat::UserConfig> {
let name = "testytest512".to_string(); //get_env_var("TWITCH_NAME")?;
let token = secrets.oauth_access_token.to_string();
// you need a `UserConfig` to connect to Twitch
let config = UserConfig::builder()
// the name of the associated twitch account
.name(name)
// and the provided OAuth token
.token(token)
// and enable all of the advanced message signaling from Twitch
.enable_all_capabilities()
.build()?;
Ok(config)
}
// channels can be either in the form of '#museun' or 'museun'. the crate will internally add the missing #
pub fn channels_to_join() -> anyhow::Result<Vec<String>> {
let channels = "#vocodes,#testytest512,#ech310n" //get_env_var("TWITCH_CHANNEL")?
.split(',')
.map(ToString::to_string)
.collect();
Ok(channels)
}
// a 'main loop'
pub async fn main_loop(mut runner: AsyncRunner) -> anyhow::Result<()> {
loop {
match runner.next_message().await? {
// this is the parsed message -- across all channels (and notifications from Twitch)
Status::Message(msg) => {
println!("Handle message time!");
handle_message(msg).await;
}
// you signaled a quit
Status::Quit => {
println!("we signaled we wanted to quit");
break;
}
// the connection closed normally
Status::Eof => {
println!("we got a 'normal' eof");
break;
}
}
}
Ok(())
}
// you can generally ignore the lifetime for these types.
async fn handle_message(msg: messages::Commands<'_>) {
use messages::Commands::*;
// All sorts of messages
match msg {
// This is the one users send to channels
Privmsg(msg) => println!("[{}] {}: {}", msg.channel(), msg.name(), msg.data()),
// This one is special, if twitch adds any new message
// types, this will catch it until future releases of
// this crate add them.
Raw(_) => {}
// These happen when you initially connect
IrcReady(_) => {}
Ready(_) => {}
Cap(_) => {}
// and a bunch of other messages you may be interested in
ClearChat(_) => {}
ClearMsg(_) => {}
GlobalUserState(_) => {}
HostTarget(_) => {}
Join(_) => {}
Notice(_) => {}
Part(_) => {}
Ping(_) => {}
Pong(_) => {}
Reconnect(_) => {}
RoomState(_) => {}
UserNotice(_) => {}
UserState(_) => {}
Whisper(_) => {}
_ => {}
}
}