mirror of
https://github.com/kurbezz/discord-bot.git
synced 2025-12-06 15:15:37 +01:00
Update twitch integration
This commit is contained in:
23
src/main.rs
23
src/main.rs
@@ -1,8 +1,7 @@
|
||||
use futures::StreamExt;
|
||||
use serenity::all::ActivityData;
|
||||
use serenity::prelude::*;
|
||||
|
||||
use twitch_handler::{auth::{self}, helix};
|
||||
use twitch_handler::TwitchBot;
|
||||
|
||||
use tokio::join;
|
||||
use rustls;
|
||||
@@ -35,25 +34,7 @@ async fn start_discord_bot() {
|
||||
}
|
||||
|
||||
async fn start_twitch_bot() {
|
||||
println!("Starting Twitch bot...");
|
||||
|
||||
let token_storage = auth::VoidStorage {};
|
||||
|
||||
let mut client = helix::Client::from_get_app_token(
|
||||
config::CONFIG.twitch_client_id.clone(),
|
||||
config::CONFIG.twitch_client_secret.clone(),
|
||||
token_storage,
|
||||
).await.unwrap();
|
||||
|
||||
let mut t = client.connect_eventsub(vec![
|
||||
("stream.online".to_string(), "1".to_string()),
|
||||
("stream.offline".to_string(), "1".to_string()),
|
||||
("channel.update".to_string(), "2".to_string())
|
||||
], config::CONFIG.twitch_channel_id.clone()).await.unwrap();
|
||||
|
||||
while let Some(event) = t.next().await {
|
||||
println!("{:?}", event);
|
||||
}
|
||||
TwitchBot::start().await;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -267,10 +267,7 @@ impl<T: auth::TokenStorage> helix::Client<T> {
|
||||
r#type: subtype,
|
||||
version: version,
|
||||
condition: helix::EventSubCondition {
|
||||
broadcaster_id: Some(broadcaster_id.clone()),
|
||||
broadcaster_user_id: Some(broadcaster_id.clone()),
|
||||
moderator_user_id: Some(broadcaster_id.clone()),
|
||||
user_id: Some(broadcaster_id.clone()),
|
||||
..Default::default()
|
||||
},
|
||||
transport: helix::EventSubTransport {
|
||||
|
||||
@@ -421,6 +421,7 @@ impl<T: TokenStorage> Client<T> {
|
||||
let res = self
|
||||
.request::<T2>(method, uri, data_json, data_form)
|
||||
.await?;
|
||||
|
||||
Ok(res.json::<T1>().await?)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,77 @@
|
||||
pub mod eventsub;
|
||||
pub mod helix;
|
||||
pub mod auth;
|
||||
|
||||
use futures::StreamExt;
|
||||
use async_trait::async_trait;
|
||||
use auth::Token;
|
||||
|
||||
use crate::config;
|
||||
|
||||
|
||||
pub struct TokenStorage {
|
||||
pub filepath: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl auth::TokenStorage for TokenStorage {
|
||||
async fn save(&mut self, token: &Token) -> anyhow::Result<()> {
|
||||
let token_json = serde_json::to_string(&token).unwrap();
|
||||
|
||||
std::fs::write(&self.filepath, token_json).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl TokenStorage {
|
||||
pub async fn load(&self) -> anyhow::Result<Token> {
|
||||
let token_json = std::fs::read_to_string(&self.filepath).unwrap();
|
||||
|
||||
let token: Token = serde_json::from_str(&token_json).unwrap();
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct TwitchBot {}
|
||||
|
||||
|
||||
impl TwitchBot {
|
||||
pub async fn start() {
|
||||
println!("Starting Twitch bot...");
|
||||
|
||||
let token_storage = TokenStorage {
|
||||
filepath: "/secrets/twitch_token.json".to_string()
|
||||
};
|
||||
let token = token_storage.load().await.unwrap();
|
||||
|
||||
let mut client = helix::Client::from_token(
|
||||
config::CONFIG.twitch_client_id.clone(),
|
||||
config::CONFIG.twitch_client_secret.clone(),
|
||||
token_storage,
|
||||
token
|
||||
).await.unwrap();
|
||||
|
||||
let mut eventsub_client = client.connect_eventsub(
|
||||
vec![
|
||||
("stream.online".to_string(), "1".to_string()),
|
||||
("stream.offline".to_string(), "1".to_string()),
|
||||
("channel.update".to_string(), "2".to_string())
|
||||
],
|
||||
config::CONFIG.twitch_channel_id.clone()
|
||||
).await.unwrap();
|
||||
|
||||
println!("Connected to Twitch EventSub...");
|
||||
client.refresh_token().await.unwrap();
|
||||
|
||||
loop {
|
||||
if let Some(event) = eventsub_client.next().await {
|
||||
println!("{:?}", event);
|
||||
}
|
||||
|
||||
client.validate_token().await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user