Allow clean on DM channels

This commit is contained in:
Natsu Kagami 2019-12-23 11:56:02 -05:00
parent 5f328829c5
commit 48bbb070be
2 changed files with 27 additions and 7 deletions

View file

@ -4,7 +4,10 @@ use serenity::{
macros::{command, group},
Args, CommandResult,
},
model::{channel::Message, id::UserId},
model::{
channel::{Channel, Message},
id::UserId,
},
};
use soft_ban::{SOFT_BAN_COMMAND, SOFT_BAN_INIT_COMMAND};
use std::{thread::sleep, time::Duration};
@ -15,7 +18,6 @@ pub use soft_ban::watch_soft_bans;
group!({
name: "admin",
options: {
only_in: "guilds",
description: "Administrative commands for the server.",
},
commands: [clean, ban, kick, soft_ban, soft_ban_init],
@ -24,7 +26,7 @@ group!({
#[command]
#[aliases("cleanall")]
#[required_permissions(MANAGE_MESSAGES)]
#[description = "Clean at most X latest messages from the current channel. Defaults to 10."]
#[description = "Clean at most X latest messages from the current channel (only clean Youmu's messages in DMs). Defaults to 10."]
#[usage = "clean 50"]
#[min_args(0)]
#[max_args(1)]
@ -33,11 +35,25 @@ fn clean(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let messages = msg
.channel_id
.messages(&ctx.http, |b| b.before(msg.id).limit(limit))?;
msg.channel_id.delete_messages(&ctx.http, messages.iter())?;
let channel = msg.channel_id.to_channel(&ctx)?;
match &channel {
Channel::Private(_) | Channel::Group(_) => {
let self_id = ctx.http.get_current_application_info()?.id;
messages
.into_iter()
.filter(|v| v.author.id == self_id)
.try_for_each(|m| m.delete(&ctx))?;
}
_ => {
msg.channel_id
.delete_messages(&ctx.http, messages.into_iter())?;
}
};
msg.react(&ctx, "🌋")?;
sleep(Duration::from_secs(2));
msg.delete(&ctx)?;
if let Channel::Guild(_) = &channel {
sleep(Duration::from_secs(2));
msg.delete(&ctx)?;
}
Ok(())
}
@ -48,6 +64,7 @@ fn clean(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
#[usage = "ban user#1234 spam"]
#[min_args(1)]
#[max_args(2)]
#[only_in("guilds")]
fn ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let user = args.single::<UserId>()?.to_user(&ctx)?;
let reason = args
@ -73,6 +90,7 @@ fn ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
#[usage = "kick user#1234 spam"]
#[min_args(1)]
#[max_args(2)]
#[only_in("guilds")]
fn kick(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let user = args.single::<UserId>()?.to_user(&ctx)?;
let reason = args

View file

@ -20,6 +20,7 @@ use std::cmp::max;
#[example = "user#1234 5s"]
#[min_args(1)]
#[max_args(2)]
#[only_in("guilds")]
pub fn soft_ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let user = args.single::<UserId>()?.to_user(&ctx)?;
let duration = if args.is_empty() {
@ -84,6 +85,7 @@ pub fn soft_ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResu
#[description = "Sets up the soft-ban command. This command can only be run once.\nThe soft-ban command assigns a role, temporarily, to a user."]
#[usage = "{soft_ban_role_id}"]
#[num_args(1)]
#[only_in("guilds")]
pub fn soft_ban_init(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let role_id = args.single::<RoleId>()?;
let guild = msg.guild(&ctx).ok_or(Error::from("Guild-only command"))?;