clemsbot/src/app/task/stream_status.rs

41 lines
1.2 KiB
Rust

use chrono::Duration;
use twitch_api2::{
types::UserId,
helix::streams::{
GetStreamsRequest,
get_streams::Stream,
},
};
use crate::app::State;
use std::sync::Arc;
pub async fn start_task(state: Arc<State>) {
tokio::task::spawn(async move {
loop {
let req = GetStreamsRequest::builder()
.user_id(vec![
UserId::new(state.user_config.twitch.channel_id.to_string()),
])
.build();
let token = match state.twitch.bot_token().await {
Ok(token) => token,
Err(e) => {
eprintln!("could not get bot token: {:?}", e);
continue;
},
};
match state.twitch.client.helix.req_get(req, &token).await {
Ok(resp) => {
let streams: Vec<Stream> = resp.data;
state.store_live_status(!streams.is_empty()).await;
}
Err(e) => {
eprintln!("could not get stream status: {:?}", e);
}
}
tokio::time::sleep(Duration::minutes(5).to_std().unwrap()).await;
}
});
}