From 4f6a0a231622f5a1903482fa32bc5f28aeb96193 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Sun, 3 Nov 2019 16:47:23 -0500 Subject: [PATCH] admin.rs --- youmubot/src/commands/admin.rs | 90 ++++++++++++++++++++++++++++++ youmubot/src/commands/admin/mod.rs | 44 --------------- 2 files changed, 90 insertions(+), 44 deletions(-) create mode 100644 youmubot/src/commands/admin.rs delete mode 100644 youmubot/src/commands/admin/mod.rs diff --git a/youmubot/src/commands/admin.rs b/youmubot/src/commands/admin.rs new file mode 100644 index 0000000..9131252 --- /dev/null +++ b/youmubot/src/commands/admin.rs @@ -0,0 +1,90 @@ +use serenity::prelude::*; +use serenity::{ + framework::standard::{ + macros::{command, group}, + Args, CommandResult, + }, + model::{channel::Message, id::UserId}, +}; +use std::{thread::sleep, time::Duration}; + +group!({ + name: "admin", + options: { + only_in: "guilds", + prefixes: ["admin", "a"], + description: "Administrative commands for the server.", + }, + commands: [clean, ban, kick], +}); + +#[command] +#[aliases("cleanall")] +#[required_permissions(MANAGE_MESSAGES)] +#[description = "Clean at most X latest messages from the current channel. Defaults to 10."] +#[usage = "clean 50"] +#[min_args(0)] +#[max_args(1)] +fn clean(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { + let limit = args.single().unwrap_or(10); + let messages = msg + .channel_id + .messages(&ctx.http, |b| b.before(msg.id).limit(limit))?; + msg.channel_id.delete_messages(&ctx.http, messages.iter())?; + msg.react(&ctx, "🌋")?; + + sleep(Duration::from_secs(2)); + msg.delete(&ctx)?; + + Ok(()) +} + +#[command] +#[required_permissions(ADMINISTRATOR)] +#[description = "Ban an user with a certain reason."] +#[usage = "ban user#1234 spam"] +#[min_args(1)] +#[max_args(2)] +fn ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { + let user = args.single::()?.to_user(&ctx)?; + let reason = args + .remains() + .map(|v| format!("`{}`", v)) + .unwrap_or("no provided reason".to_owned()); + + msg.reply( + &ctx, + format!("🔨 Banning user {} for reason `{}`.", user.tag(), reason), + )?; + + msg.guild_id + .ok_or("Can't get guild from message?")? // we had a contract + .ban(&ctx.http, user, &reason)?; + + Ok(()) +} + +#[command] +#[required_permissions(ADMINISTRATOR)] +#[description = "Kick an user with a certain reason."] +#[usage = "kick user#1234 spam"] +#[min_args(1)] +#[max_args(2)] +fn kick(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { + let user = args.single::()?.to_user(&ctx)?; + let reason = args + .remains() + .map(|v| format!("`{}`", v)) + .unwrap_or("no provided reason".to_owned()); + + msg.reply( + &ctx, + format!("🔫 Kicking user {} for {}.", user.tag(), reason), + )?; + + msg.guild_id + .ok_or("Can't get guild from message?")? // we had a contract + .ban(&ctx.http, user, &reason)?; + + Ok(()) +} diff --git a/youmubot/src/commands/admin/mod.rs b/youmubot/src/commands/admin/mod.rs deleted file mode 100644 index e311bce..0000000 --- a/youmubot/src/commands/admin/mod.rs +++ /dev/null @@ -1,44 +0,0 @@ -use serenity::prelude::*; -use serenity::{ - framework::standard::{ - macros::{command, group}, - Args, CommandResult, - }, - model::channel::Message, -}; -use std::{thread::sleep, time::Duration}; - -group!({ - name: "admin", - options: { - only_in: "guilds", - prefixes: ["admin", "a"], - description: "Administrative commands for the server.", - }, - commands: [clean, ban], -}); - -#[command] -#[aliases("cleanall")] -#[required_permissions(MANAGE_MESSAGES)] -#[description = "Clean at most X latest messages from the current channel. Defaults to 10."] -#[usage = "clean 50"] -fn clean(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { - let limit = args.single().unwrap_or(10); - let messages = msg - .channel_id - .messages(&ctx.http, |b| b.before(msg.id).limit(limit))?; - msg.channel_id.delete_messages(&ctx.http, messages.iter())?; - msg.react(&ctx, "🌋")?; - - sleep(Duration::from_secs(2)); - msg.delete(&ctx)?; - - Ok(()) -} - -#[command] -#[required_permissions(ADMINISTRATOR)] -#[description = "Ban an user with a certain reason."] -#[usage = "ban user#1234 spam"] -fn ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {}