feat: add a channel as a logging channel

Closes #1, fixes #10
This commit is contained in:
2023-11-23 02:09:45 +01:00
parent 47dd2b02c4
commit c7bf7ec9e6
7 changed files with 114 additions and 73 deletions

View File

@@ -2,16 +2,47 @@ use super::{Context, Error};
use super::utils::serenity;
#[poise::command(slash_command)]
pub async fn add_logging_channel(
#[poise::command(
slash_command,
subcommands("add_channel"),
required_permissions = "ADMINISTRATOR"
)]
pub async fn logging(_ctx: Context<'_>, _arg: String) -> Result<(), Error> {
Ok(())
}
#[poise::command(slash_command, aliases("add-channel"))]
pub async fn add_channel(
ctx: Context<'_>,
#[description = "Selected channel"] channel: Option<serenity::Channel>,
#[description = "New logging channel"] channel: serenity::Channel,
) -> Result<(), Error> {
let response = match channel {
None => "No channel selected. Please select one.".to_owned(),
Some(chan) => {
let channel_id = chan.id();
format!("Selected channel <#{channel_id}>")
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
.set_logging_channel(guild_id, channel_id)
.await
{
Ok(_) => format!(
"Added channel <#{channel_id}> as a logging channel"
),
Err(e) => {
if let Some(db_error) = e.as_database_error() {
if db_error.is_unique_violation() {
format!("Channel <#{channel_id}> is already a logging channel")
} else {
format!("Error: {:?}", e)
}
} else {
format!(
"Something bad happened with the database: {e:?}"
)
}
}
}
}
};
ctx.say(response).await?;

View File

@@ -5,14 +5,14 @@ pub mod utils;
use poise::FrameworkBuilder;
use utils::serenity;
use commands::add_logging_channel;
use commands::logging;
use utils::{BotData, Context, Error};
pub async fn make_bot() -> color_eyre::Result<FrameworkBuilder<BotData, Error>>
{
let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![add_logging_channel()],
commands: vec![logging()],
..Default::default()
})
.token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"))

View File

@@ -2,7 +2,7 @@ use crate::db::Database;
pub use poise::serenity_prelude as serenity;
pub struct BotData {
database: Database,
pub database: Database,
}
impl BotData {