mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-20 01:08:55 +00:00
Core: asyncify Admin group
This commit is contained in:
parent
07a8bc5579
commit
8f65f63f00
1 changed files with 48 additions and 30 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
use futures_util::{stream, TryStreamExt};
|
||||||
use serenity::{
|
use serenity::{
|
||||||
framework::standard::{
|
framework::standard::{
|
||||||
macros::{command, group},
|
macros::{command, group},
|
||||||
|
@ -9,7 +10,6 @@ use serenity::{
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use soft_ban::{SOFT_BAN_COMMAND, SOFT_BAN_INIT_COMMAND};
|
use soft_ban::{SOFT_BAN_COMMAND, SOFT_BAN_INIT_COMMAND};
|
||||||
use std::{thread::sleep, time::Duration};
|
|
||||||
use youmubot_prelude::*;
|
use youmubot_prelude::*;
|
||||||
|
|
||||||
mod soft_ban;
|
mod soft_ban;
|
||||||
|
@ -27,29 +27,34 @@ struct Admin;
|
||||||
#[usage = "clean 50"]
|
#[usage = "clean 50"]
|
||||||
#[min_args(0)]
|
#[min_args(0)]
|
||||||
#[max_args(1)]
|
#[max_args(1)]
|
||||||
fn clean(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
async fn clean(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
let limit = args.single().unwrap_or(10);
|
let limit = args.single().unwrap_or(10);
|
||||||
let messages = msg
|
let messages = msg
|
||||||
.channel_id
|
.channel_id
|
||||||
.messages(&ctx.http, |b| b.before(msg.id).limit(limit))?;
|
.messages(&ctx.http, |b| b.before(msg.id).limit(limit))
|
||||||
let channel = msg.channel_id.to_channel(&ctx)?;
|
.await?;
|
||||||
|
let channel = msg.channel_id.to_channel(&ctx).await?;
|
||||||
match &channel {
|
match &channel {
|
||||||
Channel::Private(_) | Channel::Group(_) => {
|
Channel::Private(_) => {
|
||||||
let self_id = ctx.http.get_current_application_info()?.id;
|
let self_id = ctx.http.get_current_application_info().await?.id;
|
||||||
messages
|
messages
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|v| v.author.id == self_id)
|
.filter(|v| v.author.id == self_id)
|
||||||
.try_for_each(|m| m.delete(&ctx))?;
|
.map(|m| m.delete(&ctx))
|
||||||
|
.collect::<stream::FuturesUnordered<_>>()
|
||||||
|
.try_collect::<()>()
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
msg.channel_id
|
msg.channel_id
|
||||||
.delete_messages(&ctx.http, messages.into_iter())?;
|
.delete_messages(&ctx.http, messages.into_iter())
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
msg.react(&ctx, "🌋")?;
|
msg.react(&ctx, '🌋').await?;
|
||||||
if let Channel::Guild(_) = &channel {
|
if let Channel::Guild(_) = &channel {
|
||||||
sleep(Duration::from_secs(2));
|
tokio::time::delay_for(Duration::from_secs(2)).await;
|
||||||
msg.delete(&ctx)?;
|
msg.delete(&ctx).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -58,25 +63,36 @@ fn clean(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
#[command]
|
#[command]
|
||||||
#[required_permissions(ADMINISTRATOR)]
|
#[required_permissions(ADMINISTRATOR)]
|
||||||
#[description = "Ban an user with a certain reason."]
|
#[description = "Ban an user with a certain reason."]
|
||||||
#[usage = "@user#1234/spam"]
|
#[usage = "tag user/[reason = none]/[days of messages to delete = 0]"]
|
||||||
#[min_args(1)]
|
#[min_args(1)]
|
||||||
#[max_args(2)]
|
#[max_args(2)]
|
||||||
#[only_in("guilds")]
|
#[only_in("guilds")]
|
||||||
fn ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
async fn ban(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
let user = args.single::<UserId>()?.to_user(&ctx)?;
|
let user = args.single::<UserId>()?.to_user(&ctx).await?;
|
||||||
let reason = args
|
let reason = args.single::<String>().map(|v| format!("`{}`", v)).ok();
|
||||||
.remains()
|
let dmds = args.single::<u8>().unwrap_or(0);
|
||||||
.map(|v| format!("`{}`", v))
|
|
||||||
.unwrap_or("no provided reason".to_owned());
|
|
||||||
|
|
||||||
|
match reason {
|
||||||
|
Some(reason) => {
|
||||||
msg.reply(
|
msg.reply(
|
||||||
&ctx,
|
&ctx,
|
||||||
format!("🔨 Banning user {} for reason `{}`.", user.tag(), reason),
|
format!("🔨 Banning user {} for reason `{}`.", user.tag(), reason),
|
||||||
)?;
|
)
|
||||||
|
.await?;
|
||||||
msg.guild_id
|
msg.guild_id
|
||||||
.ok_or("Can't get guild from message?")? // we had a contract
|
.ok_or(Error::msg("Can't get guild from message?"))? // we had a contract
|
||||||
.ban(&ctx.http, user, &reason)?;
|
.ban_with_reason(&ctx.http, user, dmds, &reason)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
msg.reply(&ctx, format!("🔨 Banning user {}.", user.tag()))
|
||||||
|
.await?;
|
||||||
|
msg.guild_id
|
||||||
|
.ok_or(Error::msg("Can't get guild from message?"))? // we had a contract
|
||||||
|
.ban(&ctx.http, user, dmds)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -87,14 +103,16 @@ fn ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
#[usage = "@user#1234"]
|
#[usage = "@user#1234"]
|
||||||
#[num_args(1)]
|
#[num_args(1)]
|
||||||
#[only_in("guilds")]
|
#[only_in("guilds")]
|
||||||
fn kick(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
async fn kick(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
let user = args.single::<UserId>()?.to_user(&ctx)?;
|
let user = args.single::<UserId>()?.to_user(&ctx).await?;
|
||||||
|
|
||||||
msg.reply(&ctx, format!("🔫 Kicking user {}.", user.tag()))?;
|
msg.reply(&ctx, format!("🔫 Kicking user {}.", user.tag()))
|
||||||
|
.await?;
|
||||||
|
|
||||||
msg.guild_id
|
msg.guild_id
|
||||||
.ok_or("Can't get guild from message?")? // we had a contract
|
.ok_or("Can't get guild from message?")? // we had a contract
|
||||||
.kick(&ctx.http, user)?;
|
.kick(&ctx.http, user)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue