reminder-bot/src/commands/moderation_cmds.rs

158 lines
4.3 KiB
Rust
Raw Normal View History

2020-08-18 19:09:21 +00:00
use regex_command_attr::command;
use serenity::{
client::Context,
model::{
channel::{
Message,
},
},
framework::standard::CommandResult,
};
2020-08-26 17:26:28 +00:00
use regex::Regex;
2020-08-27 20:37:44 +00:00
use chrono_tz::Tz;
2020-08-22 00:24:12 +00:00
use crate::{
2020-08-27 11:15:20 +00:00
models::{
ChannelData,
UserData,
2020-08-29 19:57:11 +00:00
GuildData,
2020-08-27 11:15:20 +00:00
},
2020-08-22 00:24:12 +00:00
SQLPool,
2020-08-29 17:07:15 +00:00
framework::SendFromDb,
2020-08-22 00:24:12 +00:00
};
2020-08-18 19:09:21 +00:00
2020-08-26 17:26:28 +00:00
lazy_static! {
static ref REGEX_CHANNEL: Regex = Regex::new(r#"^\s*<#(\d+)>\s*$"#).unwrap();
}
2020-08-18 19:09:21 +00:00
#[command]
#[supports_dm(false)]
#[permission_level(Restricted)]
2020-08-25 16:19:08 +00:00
#[can_blacklist(false)]
2020-08-18 19:09:21 +00:00
async fn blacklist(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
2020-08-26 17:26:28 +00:00
let capture_opt = REGEX_CHANNEL.captures(&args).map(|cap| cap.get(1)).flatten();
let mut channel = match capture_opt {
Some(capture) =>
ChannelData::from_id(capture.as_str().parse::<u64>().unwrap(), pool.clone()).await.unwrap(),
None =>
ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool.clone()).await.unwrap(),
};
2020-08-22 00:24:12 +00:00
channel.blacklisted = !channel.blacklisted;
channel.commit_changes(pool).await;
if channel.blacklisted {
2020-08-25 16:19:08 +00:00
let _ = msg.channel_id.say(&ctx, "Blacklisted").await;
2020-08-22 00:24:12 +00:00
}
else {
2020-08-25 16:19:08 +00:00
let _ = msg.channel_id.say(&ctx, "Unblacklisted").await;
2020-08-22 00:24:12 +00:00
}
2020-08-18 19:09:21 +00:00
Ok(())
}
2020-08-27 11:15:20 +00:00
#[command]
async fn timezone(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
2020-08-29 17:07:15 +00:00
let mut user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap();
2020-08-27 20:37:44 +00:00
2020-08-29 17:07:15 +00:00
if args.len() > 0 {
match args.parse::<Tz>() {
Ok(_) => {
user_data.timezone = args;
2020-08-27 20:37:44 +00:00
2020-08-29 17:07:15 +00:00
user_data.commit_changes(pool).await;
2020-08-27 20:37:44 +00:00
2020-08-29 17:07:15 +00:00
let _ = msg.channel_id.say_named(&ctx, user_data.language, "timezone/set_p").await;
}
2020-08-27 20:37:44 +00:00
2020-08-29 17:07:15 +00:00
Err(_) => {
let _ = msg.channel_id.say_named(&ctx, user_data.language, "timezone/no_timezone").await;
}
2020-08-27 20:37:44 +00:00
}
}
2020-08-29 17:07:15 +00:00
else {
let _ = msg.channel_id.say_named(&ctx, user_data.language, "timezone/no_argument").await;
}
2020-08-27 20:37:44 +00:00
Ok(())
}
#[command]
async fn language(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let mut user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap();
2020-08-29 17:07:15 +00:00
match sqlx::query!(
"
SELECT code FROM languages WHERE code = ? OR name = ?
", args, args)
.fetch_one(&pool)
.await {
Ok(row) => {
user_data.language = row.code;
user_data.commit_changes(pool).await;
let _ = msg.channel_id.say_named(&ctx, user_data.language, "lang/set_p").await;
},
Err(_) => {
let _ = msg.channel_id.say_named(&ctx, user_data.language, "lang/invalid").await;
},
}
2020-08-27 11:15:20 +00:00
Ok(())
}
2020-08-29 19:57:11 +00:00
#[command]
async fn prefix(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let mut guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), pool.clone()).await.unwrap();
let user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap();
if args.len() > 5 {
let _ = msg.channel_id.say_named(&ctx, user_data.language, "prefix/too_long").await;
}
else if args.len() == 0 {
let _ = msg.channel_id.say_named(&ctx, user_data.language, "prefix/no_argument").await;
}
else {
guild_data.prefix = args;
guild_data.commit_changes(pool).await;
let _ = msg.channel_id.say_named(&ctx, user_data.language, "prefix/success").await;
}
Ok(())
}
#[command]
async fn pause(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
2020-08-30 20:08:08 +00:00
let channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool.clone()).await.unwrap();
channel.commit_changes(pool).await;
Ok(())
}