er/src/commands/admin.rs
Louis Hollingworth 46c6a25164
(#8) Reporting now added, admin commands are also here
Signed-off-by: Louis Hollingworth <louis@hollingworth.nl>
2023-10-29 14:40:12 +00:00

70 lines
2.3 KiB
Rust

// SPDX-FileCopyrightText: 2023 Louis Hollingworth <louis@hollingworth.nl>
//
// SPDX-License-Identifier: GPL-3.0-or-later
use poise::serenity_prelude as serenity;
#[poise::command(
slash_command,
subcommands("admin_reports", "admin_setup"),
subcommand_required,
guild_only,
default_member_permissions = "ADMINISTRATOR"
)]
pub async fn admin(_: crate::Context<'_>) -> Result<(), crate::Error> {
Ok(())
}
/// Set or get the configured channel for reports
#[poise::command(slash_command, rename = "reports")]
pub async fn admin_reports(
ctx: crate::Context<'_>,
#[description = "Set where the reports should be sent"] reports_channel: Option<
serenity::Channel,
>,
) -> Result<(), crate::Error> {
ctx.defer_ephemeral().await.unwrap();
let pool = &ctx.framework().user_data.db_pool;
match reports_channel {
None => {
let dbg = crate::models::db::guild::Guild::get_by_id(
ctx.guild_id().unwrap().to_string(),
pool.clone(),
)
.await
.unwrap();
ctx.reply(format!(
"<#{}> is currently receiving reports for this guild.",
dbg.reports_channel_id.unwrap_or("0000000".to_string())
))
.await
.unwrap();
}
Some(cnl) => {
crate::models::db::guild::Guild::update_reports_channel(
ctx.guild_id().unwrap().to_string(),
Some(cnl.id().to_string()),
pool.clone(),
)
.await
.unwrap();
ctx.reply(format!(
"<#{}> is now setup to receive reports.",
cnl.id().to_string()
))
.await
.unwrap();
}
};
Ok(())
}
/// This command shouldn't be needed for anything other than development
#[poise::command(slash_command, rename = "setup")]
pub async fn admin_setup(ctx: crate::Context<'_>) -> Result<(), crate::Error> {
let pool = &ctx.framework().user_data.db_pool;
let g = ctx.guild().unwrap();
crate::models::db::guild::Guild::create(g.id.to_string(), g.name, pool.clone()).await?;
ctx.reply("Setup complete, never run this again. See the [issue tracker](https://git.ludoviko.ch/lucxjo/er/issues) if you are still having issues").await.unwrap();
Ok(())
}