Merge pull request 'feat: unset a channel as a logger' (#12) from feature/remove-logging-channel into develop

Reviewed-on: #12
This commit is contained in:
Lucien Cartier-Tilet 2023-11-23 21:41:28 +00:00
commit 621e1d97ed
2 changed files with 63 additions and 8 deletions

View File

@ -39,7 +39,9 @@ WHERE guild_id = ?1
.fetch_all(&self.pool) .fetch_all(&self.pool)
.await .await
.map_err(|e| { .map_err(|e| {
error!("Error getting logging channels for guild {guild_id}: {e:?}"); error!(
"Error getting logging channels for guild {guild_id}: {e:?}"
);
e e
})?; })?;
Ok(channels.iter().map(|id| id.channel_id as u64).collect()) Ok(channels.iter().map(|id| id.channel_id as u64).collect())
@ -70,4 +72,27 @@ VALUES ( ?1, ?2 )
}) })
.map(|_| ()) .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(|_| ())
}
} }

View File

@ -2,21 +2,23 @@ use super::{Context, Error};
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,
subcommands("add_channel", "list_channels"), subcommands("add_channel", "list_channels", "remove_channel"),
required_permissions = "ADMINISTRATOR" required_permissions = "ADMINISTRATOR"
)] )]
pub async fn logging(_ctx: Context<'_>) -> Result<(), Error> { pub async fn logging(_ctx: Context<'_>) -> Result {
Ok(()) Ok(())
} }
#[poise::command(slash_command, aliases("add-channel"))] #[poise::command(slash_command)]
pub async fn add_channel( pub async fn add_channel(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "New logging channel"] channel: serenity::Channel, #[description = "New logging channel"] channel: serenity::Channel,
) -> Result<(), Error> { ) -> Result {
let channel_id = channel.id(); let channel_id = channel.id();
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(),
@ -50,12 +52,13 @@ pub async fn add_channel(
Ok(()) Ok(())
} }
#[poise::command(slash_command, aliases("list-channels"))] #[poise::command(slash_command)]
pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> { 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_channel(guild_id).await {
Err(e) => format!("Could not retrieve loggers: {e:?}"),
Ok(channels) => { Ok(channels) => {
if channels.is_empty() { if channels.is_empty() {
"No channels registered as loggers".to_owned() "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:?}")
}
} }
} }
}; };