diff --git a/src/app/twitch.rs b/src/app/twitch.rs index 2583734..626445e 100644 --- a/src/app/twitch.rs +++ b/src/app/twitch.rs @@ -1,21 +1,33 @@ -use twitch_api2::{ - TwitchClient, - types::UserId, - twitch_oauth2::UserToken, - pubsub::channel_points::Redemption, -}; +use std::sync::Arc; +use std::time::Instant; + use irc::{ client::prelude::Message, proto::{Command, Response}, }; use tokio::sync::RwLock; use tokio_tungstenite::tungstenite::Message as WsMessage; +use twitch_api2::{ + pubsub::channel_points::Redemption, + twitch_oauth2::UserToken, + TwitchClient, + types::UserId, +}; +use twitch_api2::helix::points::{CustomRewardRedemptionStatus, UpdateRedemptionStatusBody, UpdateRedemptionStatusRequest}; +use twitch_api2::pubsub::{ + channel_points::ChannelPointsChannelV1Reply, + Response as PubSubResponse, + TopicData, +}; +use twitch_api2::pubsub::video_playback::VideoPlaybackReply; +use twitch_api2::twitch_oauth2::TwitchToken; + use crate::app::{ - State, config::CommandExecutor, rhai_tools::ExecutorState, + State, }; -use std::sync::Arc; +use crate::app::config::Config; pub struct Twitch { pub client: TwitchClient<'static, reqwest::Client>, @@ -73,7 +85,7 @@ pub async fn handle_irc_event(state: Arc, event: Message) -> anyhow::Resu async fn on_privmsg(state: Arc, event: &Message, message: &str) -> anyhow::Result<()> { let initiator = event.source_nickname() .ok_or_else(|| anyhow::anyhow!("missing source"))? - .to_string(); + .to_string(); let tags = event.tags.as_ref().ok_or_else(|| anyhow::anyhow!("missing tags"))?; @@ -158,16 +170,6 @@ async fn on_privmsg(state: Arc, event: &Message, message: &str) -> anyhow Ok(()) } -use twitch_api2::pubsub::{ - Response as PubSubResponse, - TopicData, - channel_points::ChannelPointsChannelV1Reply, -}; -use std::time::Instant; -use twitch_api2::pubsub::video_playback::VideoPlaybackReply; -use crate::app::config::Config; -use twitch_api2::twitch_oauth2::TwitchToken; - pub async fn handle_pubsub(state: Arc, event: WsMessage) -> anyhow::Result<()> { let json = match event { WsMessage::Text(json) => json, @@ -177,7 +179,7 @@ pub async fn handle_pubsub(state: Arc, event: WsMessage) -> anyhow::Resul let response = twitch_api2::pubsub::Response::parse(&json)?; match response { - PubSubResponse::Message {data} => match data { + PubSubResponse::Message { data } => match data { TopicData::ChannelPointsChannelV1 { reply, .. } => handle_channel_points_reply(state, reply).await, TopicData::VideoPlaybackById { reply, .. } => handle_stream_status(state, reply).await, _ => Ok(()), @@ -198,13 +200,40 @@ async fn rename_a_split(state: Arc, redemption: &Redemption) { None => return, }; - if let Err(e) = reqwest::Client::new() - .post(&state.user_config.livesplit.server) + let fulfilled = match reqwest::Client::new() + .post(&format!("{}/rename", state.user_config.livesplit.server)) .body(format!("{}\n{}", old.trim(), new.trim())) .send() .await + .and_then(|resp| resp.error_for_status()) { - eprintln!("could not do split rename: {:?}", e); + Ok(_) => true, + Err(e) => { + eprintln!("could not do split rename: {:?}", e); + false + } + }; + + let status = if fulfilled { + CustomRewardRedemptionStatus::Fulfilled + } else { + CustomRewardRedemptionStatus::Canceled + }; + + let req = UpdateRedemptionStatusRequest::builder() + .broadcaster_id(state.user_config.twitch.channel_id.to_string()) + .reward_id(redemption.reward.id.clone()) + .id(redemption.id.clone()) + .build(); + + let body = UpdateRedemptionStatusBody::builder() + .status(status) + .build(); + + if let Ok(token) = state.twitch.user_token().await { + if let Err(e) = state.twitch.client.helix.req_patch(req, body, &token).await { + eprintln!("could not update redemption status: {:?}", e); + } } }