Add support for extras, conditionally displaying commands in the help command, and start working on moderation commands
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::{
|
||||
database::models::UserIncident,
|
||||
extras::Extras,
|
||||
framework::{
|
||||
commands::{
|
||||
Command, CommandError,
|
||||
arguments::{Arg, Argument, ArgumentParsingError, ParseArguments},
|
||||
context::CommandContext,
|
||||
permissions::Permission,
|
||||
},
|
||||
runtime::Runtime,
|
||||
},
|
||||
groups::moderation::{
|
||||
arguments::UserArgument,
|
||||
permissions::{BotModeratorPermission, BotOwnerPermission},
|
||||
},
|
||||
state::State,
|
||||
utils::Bye,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ModeratorCommand;
|
||||
|
||||
#[async_trait]
|
||||
impl Command<State, Extras> for ModeratorCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"moderator"
|
||||
}
|
||||
|
||||
fn aliases(&self) -> &'static [&'static str] {
|
||||
&["mod"]
|
||||
}
|
||||
|
||||
fn permissions(&self) -> Vec<Box<dyn Permission<State, Extras> + Send + Sync + 'static>> {
|
||||
vec![Box::new(BotOwnerPermission)]
|
||||
}
|
||||
|
||||
fn show_in_help(&self, rt: &Runtime<State, Extras>, extras: &Extras) -> bool {
|
||||
extras.user.id == rt.state.bot_owner_id
|
||||
}
|
||||
|
||||
fn arguments(&self) -> &'static [Argument] {
|
||||
&[Argument {
|
||||
name: "user",
|
||||
is_optional: false,
|
||||
is_greedy: false,
|
||||
}]
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
ctx: CommandContext<'_, State, Extras>,
|
||||
args: &str,
|
||||
) -> Result<(), CommandError> {
|
||||
let Arg(mut user) = match Arg::<UserArgument>::parse(args, &ctx).await {
|
||||
Ok(arg) => arg,
|
||||
Err(e) => match e {
|
||||
ArgumentParsingError::InvalidArgument => {
|
||||
ctx.send()
|
||||
.content("❌ User not found in DB.")
|
||||
.await
|
||||
.map_err(|_| CommandError::MessageSendingFailed)?;
|
||||
return Ok(());
|
||||
}
|
||||
ArgumentParsingError::DatabaseFailed => {
|
||||
return Err(CommandError::DatabaseFailed);
|
||||
}
|
||||
e => {
|
||||
unreachable!("Unexpected error: {e}");
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
user.is_moderator = !user.is_moderator;
|
||||
|
||||
user.insert(&ctx.rt.state.db)
|
||||
.await
|
||||
.map_err(|_| CommandError::DatabaseFailed)?;
|
||||
|
||||
ctx.send()
|
||||
.content(format!(
|
||||
"👍 Done. <@{}> {}",
|
||||
user.id,
|
||||
if user.is_moderator {
|
||||
"**is now** a moderator"
|
||||
} else {
|
||||
"**is no longer** a moderator"
|
||||
}
|
||||
))
|
||||
.await
|
||||
.map_err(|_| CommandError::MessageSendingFailed)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct IncidentsCommand;
|
||||
|
||||
#[async_trait]
|
||||
impl Command<State, Extras> for IncidentsCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"incidents"
|
||||
}
|
||||
|
||||
fn aliases(&self) -> &'static [&'static str] {
|
||||
&["i"]
|
||||
}
|
||||
|
||||
fn permissions(&self) -> Vec<Box<dyn Permission<State, Extras> + Send + Sync + 'static>> {
|
||||
vec![Box::new(BotModeratorPermission)]
|
||||
}
|
||||
|
||||
fn show_in_help(&self, _rt: &Runtime<State, Extras>, extras: &Extras) -> bool {
|
||||
extras.user.is_moderator
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
ctx: CommandContext<'_, State, Extras>,
|
||||
args: &str,
|
||||
) -> Result<(), CommandError> {
|
||||
let user = match Arg::<UserArgument>::parse(args, &ctx).await {
|
||||
Ok(Arg(user)) => user,
|
||||
Err(e) => match e {
|
||||
ArgumentParsingError::InvalidArgument => {
|
||||
ctx.send()
|
||||
.content("❌ User not found in DB.")
|
||||
.await
|
||||
.map_err(|_| CommandError::MessageSendingFailed)?;
|
||||
return Ok(());
|
||||
}
|
||||
ArgumentParsingError::DatabaseFailed => {
|
||||
return Err(CommandError::DatabaseFailed);
|
||||
}
|
||||
e => {
|
||||
unreachable!("Unexpected error: {e}");
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
let incidents = UserIncident::get_for_user(user.id, &ctx.rt.state.db)
|
||||
.await
|
||||
.map_err(|_| CommandError::DatabaseFailed)?;
|
||||
|
||||
ctx.send()
|
||||
.content(format!(
|
||||
"Great! The user fetched from the DB is {}",
|
||||
user.name
|
||||
))
|
||||
.await
|
||||
.bye();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user