feat: unset a channel as a logger

Closes #3
This commit is contained in:
Lucien Cartier-Tilet 2023-11-23 22:36:10 +01:00
parent 3bcef90fb9
commit 592604aaa8
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
2 changed files with 63 additions and 8 deletions

View File

@ -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(|_| ())
}
}

View File

@ -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:?}")
}
}
}
};