feat: refund
This commit is contained in:
parent
fa56fc5c2e
commit
27d6997fc9
|
@ -1,21 +1,33 @@
|
||||||
use twitch_api2::{
|
use std::sync::Arc;
|
||||||
TwitchClient,
|
use std::time::Instant;
|
||||||
types::UserId,
|
|
||||||
twitch_oauth2::UserToken,
|
|
||||||
pubsub::channel_points::Redemption,
|
|
||||||
};
|
|
||||||
use irc::{
|
use irc::{
|
||||||
client::prelude::Message,
|
client::prelude::Message,
|
||||||
proto::{Command, Response},
|
proto::{Command, Response},
|
||||||
};
|
};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
use tokio_tungstenite::tungstenite::Message as WsMessage;
|
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::{
|
use crate::app::{
|
||||||
State,
|
|
||||||
config::CommandExecutor,
|
config::CommandExecutor,
|
||||||
rhai_tools::ExecutorState,
|
rhai_tools::ExecutorState,
|
||||||
|
State,
|
||||||
};
|
};
|
||||||
use std::sync::Arc;
|
use crate::app::config::Config;
|
||||||
|
|
||||||
pub struct Twitch {
|
pub struct Twitch {
|
||||||
pub client: TwitchClient<'static, reqwest::Client>,
|
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<()> {
|
async fn on_privmsg(state: Arc<State>, event: &Message, message: &str) -> anyhow::Result<()> {
|
||||||
let initiator = event.source_nickname()
|
let initiator = event.source_nickname()
|
||||||
.ok_or_else(|| anyhow::anyhow!("missing source"))?
|
.ok_or_else(|| anyhow::anyhow!("missing source"))?
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let tags = event.tags.as_ref().ok_or_else(|| anyhow::anyhow!("missing tags"))?;
|
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(())
|
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<()> {
|
pub async fn handle_pubsub(state: Arc<State>, event: WsMessage) -> anyhow::Result<()> {
|
||||||
let json = match event {
|
let json = match event {
|
||||||
WsMessage::Text(json) => json,
|
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)?;
|
let response = twitch_api2::pubsub::Response::parse(&json)?;
|
||||||
|
|
||||||
match response {
|
match response {
|
||||||
PubSubResponse::Message {data} => match data {
|
PubSubResponse::Message { data } => match data {
|
||||||
TopicData::ChannelPointsChannelV1 { reply, .. } => handle_channel_points_reply(state, reply).await,
|
TopicData::ChannelPointsChannelV1 { reply, .. } => handle_channel_points_reply(state, reply).await,
|
||||||
TopicData::VideoPlaybackById { reply, .. } => handle_stream_status(state, reply).await,
|
TopicData::VideoPlaybackById { reply, .. } => handle_stream_status(state, reply).await,
|
||||||
_ => Ok(()),
|
_ => Ok(()),
|
||||||
|
@ -198,13 +200,40 @@ async fn rename_a_split(state: Arc<State>, redemption: &Redemption) {
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = reqwest::Client::new()
|
let fulfilled = match reqwest::Client::new()
|
||||||
.post(&state.user_config.livesplit.server)
|
.post(&format!("{}/rename", state.user_config.livesplit.server))
|
||||||
.body(format!("{}\n{}", old.trim(), new.trim()))
|
.body(format!("{}\n{}", old.trim(), new.trim()))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue