badgey/src/badgey/commands/custom_responses.rs

74 lines
2.6 KiB
Rust

use poise::serenity_prelude as serenity;
use manifold::error::{ManifoldError, ManifoldResult};
use manifold::{ManifoldContext, ManifoldData};
use poise::serenity_prelude::Mentionable;
use crate::badgey::models::custom_response::{CustomResponse, CustomResponseInserter};
#[poise::command(slash_command, prefix_command, required_permissions = "MODERATE_MEMBERS" )]
async fn add_custom_response(ctx: ManifoldContext<'_>, target: serenity::User, trigger: String, response: String) -> ManifoldResult<()> {
let db = &ctx.data().database;
match CustomResponseInserter::new(trigger, response, target, ctx.author().clone()).insert(db) {
Ok(_) => ctx.reply(t!("commands.custom_response.added")).await?,
Err(e) => ctx.reply(t!("commands.custom_response.added.error", error = e)).await?,
};
Ok(())
}
#[poise::command(slash_command, prefix_command, required_permissions = "MODERATE_MEMBERS")]
async fn remove_custom_response(ctx: ManifoldContext<'_>, id: i32) -> ManifoldResult<()> {
let db = &ctx.data().database;
let mut response = CustomResponse::find_by_id(db, &id)?;
response.deleted = Some(true);
response.insert(db)?;
ctx.reply(t!("commands.custom_response.marked_deleted", response_id=id)).await?;
Ok(())
}
#[poise::command(slash_command, prefix_command, required_permissions = "MODERATE_MEMBERS")]
async fn undelete_custom_response(ctx: ManifoldContext<'_>, id: i32) -> ManifoldResult<()> {
let db = &ctx.data().database;
let mut response = CustomResponse::find_by_id(db, &id)?;
if let Some(_) = response.deleted {
response.deleted = None;
response.insert(db)?;
ctx.reply(t!("commands.custom_response.marked_undeleted", response_id=id)).await?;
} else {
ctx.reply(t!("commands.custom_response.undelete_error_not_deleted", response_id=id)).await?;
}
Ok(())
}
#[poise::command(slash_command, prefix_command, required_permissions = "MODERATE_MEMBERS")]
async fn list_custom_responses_for_user(ctx: ManifoldContext<'_>, target: serenity::User) -> ManifoldResult<()> {
let db = &ctx.data().database;
let mut answer: String = "".to_string();
CustomResponse::find_by_user(db, &target)?.iter().for_each(|f| {
answer.push_str(format!("{}{}", "\n", f.to_string()).as_str());
});
if answer.len() == 0 {
answer.push_str("\nNone found");
}
ctx.reply(t!("commands.custom_response.list_user", user = &target.mention(), response_list = answer)).await?;
Ok(())
}
pub fn commands() -> [poise::Command<ManifoldData, ManifoldError>; 4] {
[add_custom_response(), remove_custom_response(), undelete_custom_response(), list_custom_responses_for_user()]
}