feat: send in logger channels mentions to everyone #13

Merged
phundrak merged 1 commits from feature/log-everyone into develop 2023-11-23 22:55:33 +00:00
4 changed files with 82 additions and 5 deletions

View File

@ -23,7 +23,7 @@ impl Database {
}) })
} }
pub async fn get_logging_channel( pub async fn get_logging_channels(
&self, &self,
guild_id: GuildId, guild_id: GuildId,
) -> Result<Vec<u64>> { ) -> Result<Vec<u64>> {

View File

@ -1,9 +1,7 @@
use super::{Context, Error}; use super::{Context, Result};
use super::utils::serenity; use super::utils::serenity;
type Result = ::std::result::Result<(), Error>;
#[allow(clippy::unused_async)] #[allow(clippy::unused_async)]
#[poise::command( #[poise::command(
slash_command, slash_command,
@ -57,7 +55,7 @@ pub async fn list_channels(ctx: Context<'_>) -> Result {
let response = match ctx.guild_id() { let response = match ctx.guild_id() {
None => "Error: Could not determine the guild's ID".to_owned(), None => "Error: Could not determine the guild's ID".to_owned(),
Some(guild_id) => { Some(guild_id) => {
match ctx.data().database.get_logging_channel(guild_id).await { match ctx.data().database.get_logging_channels(guild_id).await {
Err(e) => format!("Could not retrieve loggers: {e:?}"), Err(e) => format!("Could not retrieve loggers: {e:?}"),
Ok(channels) => { Ok(channels) => {
if channels.is_empty() { if channels.is_empty() {

View File

@ -0,0 +1,72 @@
use crate::db::Database;
use super::{utils::BotData, Error, Result};
use poise::{serenity_prelude as serenity, Event};
use tracing::{error, info};
async fn handle_everyone_mention(
ctx: &serenity::Context,
database: &Database,
message: &serenity::Message,
) -> Result {
use serenity::ChannelId;
if let Some(guild_id) = message.guild_id {
if message.mention_everyone {
let author = message.author.clone();
let message_channel = message.channel_id;
let channels: Vec<ChannelId> = database
.get_logging_channels(guild_id)
.await?
.iter()
.map(|channel_id| serenity::ChannelId(channel_id.to_owned()))
.collect();
for channel in &channels {
channel
.send_message(&ctx, |m| {
m.embed(|e| {
e.title("Someone mentioned everyone!")
.field("Author", author.clone(), true)
.field(
"When",
message.timestamp.naive_local().to_string(),
true,
)
.field(
"Channel",
format!("<#{message_channel}>"),
true,
)
.field("Link", format!("https://discord.com/channels/{guild_id}/{}/{}", channel.0, message.id), false)
})
})
.await
.map_err(|e| {
error!("Failed to send message: {e:?}");
e
})?;
}
}
} else {
error!("Could not determine guild id of message {message:?}");
}
Ok(())
}
pub async fn event_handler(
ctx: &serenity::Context,
event: &Event<'_>,
_framework: poise::FrameworkContext<'_, BotData, Error>,
data: &BotData,
) -> Result {
match event {
Event::Ready { data_about_bot } => {
info!("Logged in as {}", data_about_bot.user.name);
}
Event::Message { new_message } => {
handle_everyone_mention(ctx, &data.database, new_message).await?;
}
_ => {}
}
Ok(())
}

View File

@ -8,10 +8,17 @@ use utils::serenity;
use commands::logging; use commands::logging;
use utils::{BotData, Context, Error}; use utils::{BotData, Context, Error};
use self::events::event_handler;
pub type Result = ::std::result::Result<(), Error>;
pub fn make_bot() -> FrameworkBuilder<BotData, Error> { pub fn make_bot() -> FrameworkBuilder<BotData, Error> {
poise::Framework::builder() poise::Framework::builder()
.options(poise::FrameworkOptions { .options(poise::FrameworkOptions {
commands: vec![logging()], commands: vec![logging()],
event_handler: |ctx, event, framework, data| {
Box::pin(event_handler(ctx, event, framework, data))
},
..Default::default() ..Default::default()
}) })
.token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN")) .token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"))