feat: add listing logger channels in a guild
This commit also allows in the database to hold more than one channel per guild and introduces clippy linting. Closes #2 BREAKING CHANGES: The database schema changed from its root. All databases should be dropped and recreated before running this new version.
This commit is contained in:
@@ -2,12 +2,13 @@ use super::{Context, Error};
|
||||
|
||||
use super::utils::serenity;
|
||||
|
||||
#[allow(clippy::unused_async)]
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
subcommands("add_channel"),
|
||||
subcommands("add_channel", "list_channels"),
|
||||
required_permissions = "ADMINISTRATOR"
|
||||
)]
|
||||
pub async fn logging(_ctx: Context<'_>, _arg: String) -> Result<(), Error> {
|
||||
pub async fn logging(_ctx: Context<'_>) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -18,7 +19,7 @@ pub async fn add_channel(
|
||||
) -> Result<(), Error> {
|
||||
let channel_id = channel.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) => {
|
||||
match ctx
|
||||
.data()
|
||||
@@ -26,7 +27,7 @@ pub async fn add_channel(
|
||||
.set_logging_channel(guild_id, channel_id)
|
||||
.await
|
||||
{
|
||||
Ok(_) => format!(
|
||||
Ok(()) => format!(
|
||||
"Added channel <#{channel_id}> as a logging channel"
|
||||
),
|
||||
Err(e) => {
|
||||
@@ -34,7 +35,7 @@ pub async fn add_channel(
|
||||
if db_error.is_unique_violation() {
|
||||
format!("Channel <#{channel_id}> is already a logging channel")
|
||||
} else {
|
||||
format!("Error: {:?}", e)
|
||||
format!("Error: {e:?}")
|
||||
}
|
||||
} else {
|
||||
format!(
|
||||
@@ -48,3 +49,31 @@ pub async fn add_channel(
|
||||
ctx.say(response).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[poise::command(slash_command, aliases("list-channels"))]
|
||||
pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> {
|
||||
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 {
|
||||
Ok(channels) => {
|
||||
if channels.is_empty() {
|
||||
"No channels registered as loggers".to_owned()
|
||||
} else {
|
||||
format!(
|
||||
"Here are the channels currently set as loggers:\n{}",
|
||||
channels
|
||||
.iter()
|
||||
.map(|channel| format!("- <#{channel}>"))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
)
|
||||
}
|
||||
}
|
||||
Err(e) => format!("Could not retrieve loggers: {e:?}"),
|
||||
}
|
||||
}
|
||||
};
|
||||
ctx.say(response).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -8,9 +8,8 @@ use utils::serenity;
|
||||
use commands::logging;
|
||||
use utils::{BotData, Context, Error};
|
||||
|
||||
pub async fn make_bot() -> color_eyre::Result<FrameworkBuilder<BotData, Error>>
|
||||
{
|
||||
let framework = poise::Framework::builder()
|
||||
pub fn make_bot() -> FrameworkBuilder<BotData, Error> {
|
||||
poise::Framework::builder()
|
||||
.options(poise::FrameworkOptions {
|
||||
commands: vec![logging()],
|
||||
..Default::default()
|
||||
@@ -26,6 +25,5 @@ pub async fn make_bot() -> color_eyre::Result<FrameworkBuilder<BotData, Error>>
|
||||
.await?;
|
||||
Ok(BotData::new().await?)
|
||||
})
|
||||
});
|
||||
Ok(framework)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user