From 592604aaa840ab537a1dfdd0709e4134bc942a9e Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Thu, 23 Nov 2023 22:36:10 +0100 Subject: [PATCH] feat: unset a channel as a logger Closes #3 --- src/db/mod.rs | 27 ++++++++++++++++++++++++- src/discord/commands.rs | 44 ++++++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index e8eb31d..551a206 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -39,7 +39,9 @@ WHERE guild_id = ?1 .fetch_all(&self.pool) .await .map_err(|e| { - error!("Error getting logging channels for guild {guild_id}: {e:?}"); + error!( + "Error getting logging channels for guild {guild_id}: {e:?}" + ); e })?; Ok(channels.iter().map(|id| id.channel_id as u64).collect()) @@ -70,4 +72,27 @@ VALUES ( ?1, ?2 ) }) .map(|_| ()) } + + pub async fn remove_logging_channel( + &self, + guild_id: GuildId, + channel_id: ChannelId, + ) -> Result<()> { + let guild_id = guild_id.0 as i64; + let channel_id = channel_id.0 as i64; + let mut conn = self.pool.acquire().await?; + sqlx::query!(r#" +DELETE FROM guild_log_channels +WHERE guild_id = ?1 AND channel_id = ?2 + "#, + guild_id, + channel_id) + .execute(&mut *conn) + .await + .map_err(|e| { + error!("Error removing channel {channel_id} as a logger for guild {guild_id}: {e:?}"); + e + }) + .map(|_| ()) + } } diff --git a/src/discord/commands.rs b/src/discord/commands.rs index cbc2162..5fb00c5 100644 --- a/src/discord/commands.rs +++ b/src/discord/commands.rs @@ -2,21 +2,23 @@ use super::{Context, Error}; use super::utils::serenity; +type Result = ::std::result::Result<(), Error>; + #[allow(clippy::unused_async)] #[poise::command( slash_command, - subcommands("add_channel", "list_channels"), + subcommands("add_channel", "list_channels", "remove_channel"), required_permissions = "ADMINISTRATOR" )] -pub async fn logging(_ctx: Context<'_>) -> Result<(), Error> { +pub async fn logging(_ctx: Context<'_>) -> Result { Ok(()) } -#[poise::command(slash_command, aliases("add-channel"))] +#[poise::command(slash_command)] pub async fn add_channel( ctx: Context<'_>, #[description = "New logging channel"] channel: serenity::Channel, -) -> Result<(), Error> { +) -> Result { let channel_id = channel.id(); let response = match ctx.guild_id() { None => "Error: Could not determine the guild's ID".to_owned(), @@ -50,12 +52,13 @@ pub async fn add_channel( Ok(()) } -#[poise::command(slash_command, aliases("list-channels"))] -pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> { +#[poise::command(slash_command)] +pub async fn list_channels(ctx: Context<'_>) -> Result { let response = match ctx.guild_id() { None => "Error: Could not determine the guild's ID".to_owned(), Some(guild_id) => { match ctx.data().database.get_logging_channel(guild_id).await { + Err(e) => format!("Could not retrieve loggers: {e:?}"), Ok(channels) => { if channels.is_empty() { "No channels registered as loggers".to_owned() @@ -70,7 +73,34 @@ pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> { ) } } - Err(e) => format!("Could not retrieve loggers: {e:?}"), + } + } + }; + ctx.say(response).await?; + Ok(()) +} + +#[poise::command(slash_command)] +pub async fn remove_channel( + ctx: Context<'_>, + #[description = "Logger channel to remove"] channel: serenity::Channel, +) -> Result { + let channel_id = channel.id(); + let response = match ctx.guild_id() { + None => "Error: Could not determine the guild's ID".to_owned(), + Some(guild_id) => { + match ctx + .data() + .database + .remove_logging_channel(guild_id, channel_id) + .await + { + Ok(()) => { + format!("Removed channel <#{channel_id}> as a logger") + } + Err(e) => { + format!("Could not remove channel as a logger: {e:?}") + } } } };