Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notifications #3

Merged
merged 5 commits into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/core/autostar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
core::emoji::{EmojiCommon, SimpleEmoji},
database::AutoStarChannel,
unwrap_id,
utils::notify,
};

use super::has_image::has_image;
Expand Down Expand Up @@ -39,12 +40,22 @@ pub async fn handle(bot: StarboardBot, event: Box<MessageCreate>) -> 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;
}

Expand Down
17 changes: 17 additions & 0 deletions src/interactions/components/dismiss.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use twilight_model::application::interaction::MessageComponentInteraction;

use crate::client::bot::StarboardBot;

pub async fn handle_dismiss(
bot: StarboardBot,
interaction: Box<MessageComponentInteraction>,
) -> anyhow::Result<()> {
assert!(interaction.is_dm());

bot.http
.delete_message(interaction.message.channel_id, interaction.message.id)
.exec()
.await?;

Ok(())
}
17 changes: 17 additions & 0 deletions src/interactions/components/handle.rs
Original file line number Diff line number Diff line change
@@ -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<MessageComponentInteraction>,
) -> anyhow::Result<()> {
match interaction.data.custom_id.as_str() {
"stateless::dismiss_notification" => handle_dismiss(bot, interaction).await?,
_ => {}
}

Ok(())
}
4 changes: 4 additions & 0 deletions src/interactions/components/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Handling for stateless components.

pub mod dismiss;
pub mod handle;
4 changes: 3 additions & 1 deletion src/interactions/handle.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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?,
_ => {}
}

Expand Down
1 change: 1 addition & 0 deletions src/interactions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod autocomplete;
pub mod commands;
pub mod components;
pub mod handle;
20 changes: 20 additions & 0 deletions src/utils/dm.rs
Original file line number Diff line number Diff line change
@@ -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<UserMarker>,
) -> Result<CreateMessage, twilight_http::Error> {
let dm_channel = bot
.http
.create_private_channel(user_id)
.exec()
.await?
.model()
.await
.unwrap()
.id;
Ok(bot.http.create_message(dm_channel))
}
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod dm;
pub mod embed;
pub mod notify;
35 changes: 35 additions & 0 deletions src/utils/notify.rs
Original file line number Diff line number Diff line change
@@ -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<UserMarker>, 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;
}