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; } 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 new file mode 100644 index 00000000..32e053bf --- /dev/null +++ b/src/interactions/components/handle.rs @@ -0,0 +1,17 @@ +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?, + _ => {} + } + + 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; 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..7d83b558 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1 +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..469843ee --- /dev/null +++ b/src/utils/notify.rs @@ -0,0 +1,35 @@ +use twilight_model::{ + application::component::{button::ButtonStyle, ActionRow, Button, Component}, + 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 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; +}