feat: refund

This commit is contained in:
Anna 2022-03-19 20:07:59 -04:00
parent 9ea46a65a5
commit add0e4197b
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
1 changed files with 52 additions and 23 deletions

View File

@ -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<State>, event: Message) -> anyhow::Resu
async fn on_privmsg(state: Arc<State>, 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<State>, 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<State>, event: WsMessage) -> anyhow::Result<()> {
let json = match event {
WsMessage::Text(json) => json,
@ -177,7 +179,7 @@ pub async fn handle_pubsub(state: Arc<State>, 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<State>, 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);
}
}
}