This commit is contained in:
2024-08-06 02:08:45 +02:00
parent fb92cfe581
commit 0d6b60acb2

View File

@@ -2,12 +2,12 @@ pub mod eventsub;
pub mod helix; pub mod helix;
pub mod auth; pub mod auth;
use chrono::{DateTime, Utc}; use chrono::{format, DateTime, Utc};
use futures::StreamExt; use futures::StreamExt;
use async_trait::async_trait; use async_trait::async_trait;
use auth::Token; use auth::Token;
use crate::config; use crate::{config, notifiers::{discord::send_to_discord, telegram::send_to_telegram}};
pub struct TokenStorage { pub struct TokenStorage {
@@ -47,12 +47,18 @@ pub struct State {
pub struct TwitchBot {} pub struct TwitchBot {}
pub async fn notify_game_change(_title: String, old_game: String, new_game: String) { pub async fn notify_game_change(title: String, _old_game: String, new_game: String) {
println!("Game changed: {} -> {}", old_game, new_game); let msg = format!("HafMC сменил игру на {} ({})!", new_game, title);
send_to_discord(&msg).await;
// send_to_telegram(&msg).await;
} }
pub async fn notify_stream_online(title: String) { pub async fn notify_stream_online(title: String, game: String) {
println!("Stream online: {}", title); let msg = format!("HafMC сейчас стримит {} ({})! \nПрисоединяйся: https://twitch.tv/hafmc", title, game);
send_to_discord(&msg).await;
// send_to_telegram(&msg).await;
} }
@@ -124,12 +130,33 @@ impl TwitchBot {
updated_at: chrono::offset::Utc::now() updated_at: chrono::offset::Utc::now()
}); });
}, },
eventsub::NotificationType::StreamOnline(_) => { eventsub::NotificationType::StreamOnline(data) => {
if (chrono::offset::Utc::now() - current_state.as_ref().unwrap().updated_at).num_seconds() > 15 * 60 { if (chrono::offset::Utc::now() - current_state.as_ref().unwrap().updated_at).num_seconds() > 15 * 60 || current_state.is_none() {
notify_stream_online(current_state.as_ref().unwrap().title.clone()).await; let new_state: Option<State> = {
} let stream = client.get_stream(config::CONFIG.twitch_channel_id.clone()).await;
current_state.as_mut().unwrap().updated_at = chrono::offset::Utc::now(); match stream {
Ok(stream) => {
Some(State {
title: stream.title,
game: stream.game_name,
updated_at: chrono::offset::Utc::now()
})
},
Err(_) => {
None
}
}
};
match new_state {
Some(state) => {
notify_stream_online(state.title.clone(), state.game.clone()).await;
current_state = Some(state);
},
None => {}
}
}
}, },
} }
} }