From 16afd4243f562b1ada807b2a099870c0a654543c Mon Sep 17 00:00:00 2001 From: Circuit Date: Sat, 2 Jul 2022 00:24:39 -0400 Subject: [PATCH 1/5] helper function for dming a user --- src/utils/dm.rs | 20 ++++++++++++++++++++ src/utils/mod.rs | 1 + 2 files changed, 21 insertions(+) create mode 100644 src/utils/dm.rs diff --git a/src/utils/dm.rs b/src/utils/dm.rs new file mode 100644 index 00000000..cc69ccd6 --- /dev/null +++ b/src/utils/dm.rs @@ -0,0 +1,20 @@ +use twilight_http::request::channel::message::CreateMessage; +use twilight_model::id::{marker::UserMarker, Id}; + +use crate::client::bot::StarboardBot; + +pub async fn dm( + bot: &StarboardBot, + user_id: Id, +) -> Result { + let dm_channel = bot + .http + .create_private_channel(user_id) + .exec() + .await? + .model() + .await + .unwrap() + .id; + Ok(bot.http.create_message(dm_channel)) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 057a3a2c..9777d769 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1 +1,2 @@ +pub mod dm; pub mod embed; From 11cca190b196c504a9beb9e52482db869e56b183 Mon Sep 17 00:00:00 2001 From: Circuit Date: Sat, 2 Jul 2022 00:25:53 -0400 Subject: [PATCH 2/5] logic for handling components --- src/interactions/components/handle.rs | 14 ++++++++++++++ src/interactions/components/mod.rs | 4 ++++ src/interactions/handle.rs | 4 +++- src/interactions/mod.rs | 1 + 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/interactions/components/handle.rs create mode 100644 src/interactions/components/mod.rs diff --git a/src/interactions/components/handle.rs b/src/interactions/components/handle.rs new file mode 100644 index 00000000..cdf84ba1 --- /dev/null +++ b/src/interactions/components/handle.rs @@ -0,0 +1,14 @@ +use twilight_model::application::interaction::MessageComponentInteraction; + +use crate::client::bot::StarboardBot; + +pub async fn handle_component( + bot: StarboardBot, + interaction: Box, +) -> anyhow::Result<()> { + match interaction.data.custom_id.as_str() { + _ => {} + } + + Ok(()) +} diff --git a/src/interactions/components/mod.rs b/src/interactions/components/mod.rs new file mode 100644 index 00000000..856b94de --- /dev/null +++ b/src/interactions/components/mod.rs @@ -0,0 +1,4 @@ +//! Handling for stateless components. + +pub mod dismiss; +pub mod handle; diff --git a/src/interactions/handle.rs b/src/interactions/handle.rs index b076cf9c..d8c8b1e0 100644 --- a/src/interactions/handle.rs +++ b/src/interactions/handle.rs @@ -1,9 +1,10 @@ use twilight_model::application::interaction::Interaction; use crate::client::bot::StarboardBot; -use crate::interactions::commands::handle::handle_command; use super::autocomplete::handle::handle_autocomplete; +use super::commands::handle::handle_command; +use super::components::handle::handle_component; pub async fn handle_interaction( shard_id: u64, @@ -17,6 +18,7 @@ pub async fn handle_interaction( Interaction::ApplicationCommandAutocomplete(interaction) => { handle_autocomplete(bot, interaction).await? } + Interaction::MessageComponent(interaction) => handle_component(bot, interaction).await?, _ => {} } diff --git a/src/interactions/mod.rs b/src/interactions/mod.rs index e4479419..038d66a7 100644 --- a/src/interactions/mod.rs +++ b/src/interactions/mod.rs @@ -1,3 +1,4 @@ pub mod autocomplete; pub mod commands; +pub mod components; pub mod handle; From 54bb64c984fca11ac4de681eb785742f87fb5bc4 Mon Sep 17 00:00:00 2001 From: Circuit Date: Sat, 2 Jul 2022 00:26:14 -0400 Subject: [PATCH 3/5] basic notification logic --- src/interactions/components/dismiss.rs | 17 +++++++++++++++++ src/interactions/components/handle.rs | 3 +++ src/utils/mod.rs | 1 + src/utils/notify.rs | 15 +++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 src/interactions/components/dismiss.rs create mode 100644 src/utils/notify.rs diff --git a/src/interactions/components/dismiss.rs b/src/interactions/components/dismiss.rs new file mode 100644 index 00000000..fb76345c --- /dev/null +++ b/src/interactions/components/dismiss.rs @@ -0,0 +1,17 @@ +use twilight_model::application::interaction::MessageComponentInteraction; + +use crate::client::bot::StarboardBot; + +pub async fn handle_dismiss( + bot: StarboardBot, + interaction: Box, +) -> anyhow::Result<()> { + assert!(interaction.is_dm()); + + bot.http + .delete_message(interaction.message.channel_id, interaction.message.id) + .exec() + .await?; + + Ok(()) +} diff --git a/src/interactions/components/handle.rs b/src/interactions/components/handle.rs index cdf84ba1..32e053bf 100644 --- a/src/interactions/components/handle.rs +++ b/src/interactions/components/handle.rs @@ -2,11 +2,14 @@ use twilight_model::application::interaction::MessageComponentInteraction; use crate::client::bot::StarboardBot; +use super::dismiss::handle_dismiss; + pub async fn handle_component( bot: StarboardBot, interaction: Box, ) -> anyhow::Result<()> { match interaction.data.custom_id.as_str() { + "stateless::dismiss_notification" => handle_dismiss(bot, interaction).await?, _ => {} } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 9777d769..7d83b558 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,2 +1,3 @@ pub mod dm; pub mod embed; +pub mod notify; diff --git a/src/utils/notify.rs b/src/utils/notify.rs new file mode 100644 index 00000000..43f6a2e2 --- /dev/null +++ b/src/utils/notify.rs @@ -0,0 +1,15 @@ +use twilight_model::id::{marker::UserMarker, Id}; + +use crate::client::bot::StarboardBot; + +use super::dm; + +pub async fn notify(bot: &StarboardBot, user_id: Id, message: &str) -> () { + let create = dm::dm(bot, user_id).await; + let create = match create { + Err(_) => return, + Ok(create) => create, + }; + + let _ = create.content(message).unwrap().exec().await; +} From 90abc6e18fc18addddb9c10008564a50c2927755 Mon Sep 17 00:00:00 2001 From: Circuit Date: Sat, 2 Jul 2022 00:38:29 -0400 Subject: [PATCH 4/5] add dismiss button to notification --- src/utils/notify.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/utils/notify.rs b/src/utils/notify.rs index 43f6a2e2..469843ee 100644 --- a/src/utils/notify.rs +++ b/src/utils/notify.rs @@ -1,4 +1,7 @@ -use twilight_model::id::{marker::UserMarker, Id}; +use twilight_model::{ + application::component::{button::ButtonStyle, ActionRow, Button, Component}, + id::{marker::UserMarker, Id}, +}; use crate::client::bot::StarboardBot; @@ -11,5 +14,22 @@ pub async fn notify(bot: &StarboardBot, user_id: Id, message: &str) Ok(create) => create, }; - let _ = create.content(message).unwrap().exec().await; + let comp = Component::ActionRow(ActionRow { + components: vec![Component::Button(Button { + label: Some("Dismiss".to_string()), + url: None, + style: ButtonStyle::Secondary, + custom_id: Some("stateless::dismiss_notification".to_string()), + disabled: false, + emoji: None, + })], + }); + + let _ = create + .content(message) + .unwrap() + .components(&[comp]) + .unwrap() + .exec() + .await; } From f865f42804c8ac145a2368e034f22e85149fd130 Mon Sep 17 00:00:00 2001 From: Circuit Date: Sat, 2 Jul 2022 00:46:34 -0400 Subject: [PATCH 5/5] use notifications for autostar channel deletes --- src/core/autostar.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/core/autostar.rs b/src/core/autostar.rs index 74960fcc..3a639d2f 100644 --- a/src/core/autostar.rs +++ b/src/core/autostar.rs @@ -7,6 +7,7 @@ use crate::{ core::emoji::{EmojiCommon, SimpleEmoji}, database::AutoStarChannel, unwrap_id, + utils::notify, }; use super::has_image::has_image; @@ -39,12 +40,22 @@ pub async fn handle(bot: StarboardBot, event: Box) -> anyhow::Res continue; } if let Status::InvalidRemove(reasons) = status { - println!("{:?}", reasons); let _ = bot .http .delete_message(event.channel_id, event.id) .exec() .await; + + if !event.author.bot { + let message = { + format!( + "Your message <#{}> was deleted for the following reason(s):\n", + event.channel_id + ) + &reasons.join("\n - ") + }; + notify::notify(&bot, event.author.id, &message).await; + } + continue; }