2020-08-18 19:09:21 +00:00
|
|
|
use regex_command_attr::command;
|
|
|
|
|
|
|
|
use serenity::{
|
2021-06-27 15:31:54 +00:00
|
|
|
builder::CreateActionRow,
|
2020-08-18 19:09:21 +00:00
|
|
|
client::Context,
|
2020-10-26 11:10:00 +00:00
|
|
|
framework::Framework,
|
2021-01-03 14:48:18 +00:00
|
|
|
model::{
|
2021-06-27 15:55:59 +00:00
|
|
|
channel::Message,
|
2021-06-27 15:31:54 +00:00
|
|
|
id::{ChannelId, MessageId, RoleId},
|
|
|
|
interactions::ButtonStyle,
|
2021-01-03 14:48:18 +00:00
|
|
|
},
|
2020-08-18 19:09:21 +00:00
|
|
|
};
|
|
|
|
|
2020-11-22 23:58:46 +00:00
|
|
|
use chrono_tz::{Tz, TZ_VARIANTS};
|
2020-08-27 20:37:44 +00:00
|
|
|
|
2020-10-11 00:42:19 +00:00
|
|
|
use chrono::offset::Utc;
|
|
|
|
|
|
|
|
use inflector::Inflector;
|
|
|
|
|
2020-11-22 23:58:46 +00:00
|
|
|
use levenshtein::levenshtein;
|
|
|
|
|
2020-08-22 00:24:12 +00:00
|
|
|
use crate::{
|
2020-12-18 17:41:36 +00:00
|
|
|
command_help,
|
2020-11-22 23:58:46 +00:00
|
|
|
consts::{REGEX_ALIAS, REGEX_CHANNEL, REGEX_COMMANDS, REGEX_ROLE, THEME_COLOR},
|
2020-10-01 17:07:27 +00:00
|
|
|
framework::SendIterator,
|
2020-12-18 11:46:22 +00:00
|
|
|
get_ctx_data,
|
2021-07-16 20:28:51 +00:00
|
|
|
models::{channel_data::ChannelData, guild_data::GuildData, user_data::UserData, CtxGuildData},
|
2021-01-19 12:19:20 +00:00
|
|
|
FrameworkCtx, PopularTimezones,
|
2020-08-22 00:24:12 +00:00
|
|
|
};
|
2020-09-28 15:11:27 +00:00
|
|
|
|
2021-06-27 15:55:59 +00:00
|
|
|
use std::{collections::HashMap, iter};
|
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-10-26 11:10:00 +00:00
|
|
|
async fn blacklist(ctx: &Context, msg: &Message, args: String) {
|
2020-12-18 11:46:22 +00:00
|
|
|
let (pool, lm) = get_ctx_data(&ctx).await;
|
2020-10-12 20:01:27 +00:00
|
|
|
|
2020-11-22 12:09:57 +00:00
|
|
|
let language = UserData::language_of(&msg.author, &pool).await;
|
2020-11-19 21:31:31 +00:00
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
let capture_opt = REGEX_CHANNEL
|
|
|
|
.captures(&args)
|
|
|
|
.map(|cap| cap.get(1))
|
|
|
|
.flatten();
|
2020-08-26 17:26:28 +00:00
|
|
|
|
2020-11-19 21:31:31 +00:00
|
|
|
let (channel, local) = match capture_opt {
|
|
|
|
Some(capture) => (
|
|
|
|
ChannelId(capture.as_str().parse::<u64>().unwrap())
|
|
|
|
.to_channel_cached(&ctx)
|
|
|
|
.await,
|
|
|
|
false,
|
|
|
|
),
|
2020-08-26 17:26:28 +00:00
|
|
|
|
2020-11-19 21:31:31 +00:00
|
|
|
None => (msg.channel(&ctx).await, true),
|
2020-08-26 17:26:28 +00:00
|
|
|
};
|
2020-08-22 00:24:12 +00:00
|
|
|
|
2020-11-19 21:31:31 +00:00
|
|
|
let mut channel_data = ChannelData::from_channel(channel.unwrap(), &pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
channel_data.blacklisted = !channel_data.blacklisted;
|
|
|
|
channel_data.commit_changes(&pool).await;
|
|
|
|
|
|
|
|
if channel_data.blacklisted {
|
|
|
|
if local {
|
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 12:09:57 +00:00
|
|
|
.say(&ctx, lm.get(&language, "blacklist/added"))
|
2020-11-19 21:31:31 +00:00
|
|
|
.await;
|
|
|
|
} else {
|
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 12:09:57 +00:00
|
|
|
.say(&ctx, lm.get(&language, "blacklist/added_from"))
|
2020-11-19 21:31:31 +00:00
|
|
|
.await;
|
|
|
|
}
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2020-11-19 21:31:31 +00:00
|
|
|
if local {
|
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 12:09:57 +00:00
|
|
|
.say(&ctx, lm.get(&language, "blacklist/removed"))
|
2020-11-19 21:31:31 +00:00
|
|
|
.await;
|
|
|
|
} else {
|
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 12:09:57 +00:00
|
|
|
.say(&ctx, lm.get(&language, "blacklist/removed_from"))
|
2020-11-19 21:31:31 +00:00
|
|
|
.await;
|
|
|
|
}
|
2020-08-22 00:24:12 +00:00
|
|
|
}
|
2020-08-18 19:09:21 +00:00
|
|
|
}
|
2020-08-27 11:15:20 +00:00
|
|
|
|
|
|
|
#[command]
|
2020-10-26 11:10:00 +00:00
|
|
|
async fn timezone(ctx: &Context, msg: &Message, args: String) {
|
2020-12-18 11:46:22 +00:00
|
|
|
let (pool, lm) = get_ctx_data(&ctx).await;
|
2020-08-27 11:15:20 +00:00
|
|
|
|
2020-09-11 16:41:15 +00:00
|
|
|
let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
|
2020-08-27 20:37:44 +00:00
|
|
|
|
2020-11-22 23:58:46 +00:00
|
|
|
let footer_text = lm.get(&user_data.language, "timezone/footer").replacen(
|
|
|
|
"{timezone}",
|
|
|
|
&user_data.timezone,
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
|
2020-09-25 11:48:44 +00:00
|
|
|
if !args.is_empty() {
|
2020-08-29 17:07:15 +00:00
|
|
|
match args.parse::<Tz>() {
|
|
|
|
Ok(_) => {
|
|
|
|
user_data.timezone = args;
|
2020-09-01 16:07:51 +00:00
|
|
|
user_data.commit_changes(&pool).await;
|
2020-08-27 20:37:44 +00:00
|
|
|
|
2020-10-11 00:42:19 +00:00
|
|
|
let now = Utc::now().with_timezone(&user_data.timezone());
|
|
|
|
|
2020-11-22 01:31:50 +00:00
|
|
|
let content = lm
|
|
|
|
.get(&user_data.language, "timezone/set_p")
|
2020-10-11 00:42:19 +00:00
|
|
|
.replacen("{timezone}", &user_data.timezone, 1)
|
2021-07-16 20:28:51 +00:00
|
|
|
.replacen("{time}", &now.format("%H:%M").to_string(), 1);
|
2020-10-11 00:42:19 +00:00
|
|
|
|
2020-11-23 00:06:46 +00:00
|
|
|
let _ =
|
|
|
|
msg.channel_id
|
|
|
|
.send_message(&ctx, |m| {
|
|
|
|
m.embed(|e| {
|
|
|
|
e.title(lm.get(&user_data.language, "timezone/set_p_title"))
|
|
|
|
.description(content)
|
|
|
|
.color(*THEME_COLOR)
|
|
|
|
.footer(|f| {
|
|
|
|
f.text(
|
|
|
|
lm.get(&user_data.language, "timezone/footer")
|
|
|
|
.replacen("{timezone}", &user_data.timezone, 1),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.await;
|
2020-08-29 17:07:15 +00:00
|
|
|
}
|
2020-08-27 20:37:44 +00:00
|
|
|
|
2020-08-29 17:07:15 +00:00
|
|
|
Err(_) => {
|
2020-11-22 23:58:46 +00:00
|
|
|
let filtered_tz = TZ_VARIANTS
|
|
|
|
.iter()
|
2021-06-27 15:31:54 +00:00
|
|
|
.filter(|tz| {
|
|
|
|
args.contains(&tz.to_string())
|
|
|
|
|| tz.to_string().contains(&args)
|
|
|
|
|| levenshtein(&tz.to_string(), &args) < 4
|
|
|
|
})
|
2020-11-22 23:58:46 +00:00
|
|
|
.take(25)
|
2021-06-27 15:31:54 +00:00
|
|
|
.map(|t| t.to_owned())
|
|
|
|
.collect::<Vec<Tz>>();
|
|
|
|
|
|
|
|
let fields = filtered_tz.iter().map(|tz| {
|
|
|
|
(
|
|
|
|
tz.to_string(),
|
|
|
|
format!(
|
|
|
|
"🕗 `{}`",
|
2021-07-16 20:28:51 +00:00
|
|
|
Utc::now().with_timezone(tz).format("%H:%M").to_string()
|
2021-06-27 15:31:54 +00:00
|
|
|
),
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
});
|
2020-11-22 23:58:46 +00:00
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 23:58:46 +00:00
|
|
|
.send_message(&ctx, |m| {
|
|
|
|
m.embed(|e| {
|
|
|
|
e.title(lm.get(&user_data.language, "timezone/no_timezone_title"))
|
|
|
|
.description(lm.get(&user_data.language, "timezone/no_timezone"))
|
|
|
|
.color(*THEME_COLOR)
|
2021-06-27 15:31:54 +00:00
|
|
|
.fields(fields)
|
2020-11-22 23:58:46 +00:00
|
|
|
.footer(|f| f.text(footer_text))
|
|
|
|
.url("https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee")
|
2021-06-27 15:31:54 +00:00
|
|
|
}).components(|c| {
|
|
|
|
for row in filtered_tz.as_slice().chunks(5) {
|
|
|
|
let mut action_row = CreateActionRow::default();
|
|
|
|
for timezone in row {
|
|
|
|
action_row.create_button(|b| {
|
|
|
|
b.style(ButtonStyle::Secondary)
|
|
|
|
.label(timezone.to_string())
|
|
|
|
.custom_id(format!("timezone:{}", timezone.to_string()))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
c.add_action_row(action_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
c
|
2020-11-22 23:58:46 +00:00
|
|
|
})
|
|
|
|
})
|
2020-10-12 20:01:27 +00:00
|
|
|
.await;
|
2020-08-29 17:07:15 +00:00
|
|
|
}
|
2020-08-27 20:37:44 +00:00
|
|
|
}
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2021-04-12 21:33:02 +00:00
|
|
|
let content = lm
|
|
|
|
.get(&user_data.language, "timezone/no_argument")
|
|
|
|
.replace("{prefix}", &ctx.prefix(msg.guild_id).await);
|
2020-11-22 23:58:46 +00:00
|
|
|
|
2021-01-19 12:19:20 +00:00
|
|
|
let popular_timezones = ctx
|
|
|
|
.data
|
|
|
|
.read()
|
|
|
|
.await
|
|
|
|
.get::<PopularTimezones>()
|
|
|
|
.cloned()
|
|
|
|
.unwrap();
|
2020-10-11 00:42:19 +00:00
|
|
|
|
2020-11-22 23:58:46 +00:00
|
|
|
let popular_timezones_iter = popular_timezones.iter().map(|t| {
|
|
|
|
(
|
2021-01-19 12:19:20 +00:00
|
|
|
t.to_string(),
|
2020-11-22 23:58:46 +00:00
|
|
|
format!(
|
|
|
|
"🕗 `{}`",
|
2021-07-16 20:28:51 +00:00
|
|
|
Utc::now().with_timezone(t).format("%H:%M").to_string()
|
2020-11-22 23:58:46 +00:00
|
|
|
),
|
2020-11-23 14:11:57 +00:00
|
|
|
true,
|
2020-11-22 23:58:46 +00:00
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
|
|
|
.send_message(&ctx, |m| {
|
|
|
|
m.embed(|e| {
|
|
|
|
e.title(lm.get(&user_data.language, "timezone/no_argument_title"))
|
|
|
|
.description(content)
|
|
|
|
.color(*THEME_COLOR)
|
|
|
|
.fields(popular_timezones_iter)
|
|
|
|
.footer(|f| f.text(footer_text))
|
|
|
|
.url("https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee")
|
|
|
|
})
|
2021-06-27 15:31:54 +00:00
|
|
|
.components(|c| {
|
|
|
|
for row in popular_timezones.as_slice().chunks(5) {
|
|
|
|
let mut action_row = CreateActionRow::default();
|
|
|
|
for timezone in row {
|
|
|
|
action_row.create_button(|b| {
|
|
|
|
b.style(ButtonStyle::Secondary)
|
|
|
|
.label(timezone.to_string())
|
|
|
|
.custom_id(format!("timezone:{}", timezone.to_string()))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
c.add_action_row(action_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
c
|
|
|
|
})
|
2020-11-22 23:58:46 +00:00
|
|
|
})
|
|
|
|
.await;
|
2020-08-29 17:07:15 +00:00
|
|
|
}
|
2020-08-27 20:37:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:45:25 +00:00
|
|
|
#[command("lang")]
|
2020-10-26 11:10:00 +00:00
|
|
|
async fn language(ctx: &Context, msg: &Message, args: String) {
|
2020-12-18 11:46:22 +00:00
|
|
|
let (pool, lm) = get_ctx_data(&ctx).await;
|
2020-08-27 20:37:44 +00:00
|
|
|
|
2020-09-11 16:41:15 +00:00
|
|
|
let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
|
2020-08-27 20:37:44 +00:00
|
|
|
|
2020-11-23 14:11:57 +00:00
|
|
|
if !args.is_empty() {
|
|
|
|
match lm.get_language(&args) {
|
|
|
|
Some(lang) => {
|
|
|
|
user_data.language = lang.to_string();
|
2020-08-29 17:07:15 +00:00
|
|
|
|
2020-11-23 14:11:57 +00:00
|
|
|
user_data.commit_changes(&pool).await;
|
2020-08-29 17:07:15 +00:00
|
|
|
|
2020-11-23 14:11:57 +00:00
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
|
|
|
.send_message(&ctx, |m| {
|
|
|
|
m.embed(|e| {
|
|
|
|
e.title(lm.get(&user_data.language, "lang/set_p_title"))
|
|
|
|
.color(*THEME_COLOR)
|
|
|
|
.description(lm.get(&user_data.language, "lang/set_p"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
None => {
|
|
|
|
let language_codes = lm.all_languages().map(|(k, v)| {
|
|
|
|
(
|
|
|
|
format!("{} {}", lm.get(k, "flag"), v.to_title_case()),
|
|
|
|
format!("`$lang {}`", k.to_uppercase()),
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
|
|
|
.send_message(&ctx, |m| {
|
|
|
|
m.embed(|e| {
|
|
|
|
e.title(lm.get(&user_data.language, "lang/invalid_title"))
|
|
|
|
.color(*THEME_COLOR)
|
|
|
|
.description(lm.get(&user_data.language, "lang/invalid"))
|
|
|
|
.fields(language_codes)
|
|
|
|
})
|
2021-06-27 15:55:59 +00:00
|
|
|
.components(|c| {
|
|
|
|
for row in lm
|
|
|
|
.all_languages()
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
.collect::<Vec<(String, String)>>()
|
|
|
|
.as_slice()
|
|
|
|
.chunks(5)
|
|
|
|
{
|
|
|
|
let mut action_row = CreateActionRow::default();
|
|
|
|
for (code, name) in row {
|
|
|
|
action_row.create_button(|b| {
|
|
|
|
b.style(ButtonStyle::Primary)
|
|
|
|
.label(name.to_title_case())
|
|
|
|
.custom_id(format!("lang:{}", code.to_uppercase()))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
c.add_action_row(action_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
c
|
|
|
|
})
|
2020-11-23 14:11:57 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
2020-10-12 20:01:27 +00:00
|
|
|
}
|
2020-11-23 14:11:57 +00:00
|
|
|
} else {
|
|
|
|
let language_codes = lm.all_languages().map(|(k, v)| {
|
|
|
|
(
|
|
|
|
format!("{} {}", lm.get(k, "flag"), v.to_title_case()),
|
|
|
|
format!("`$lang {}`", k.to_uppercase()),
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2021-06-27 15:55:59 +00:00
|
|
|
let _ = msg
|
2020-11-23 14:11:57 +00:00
|
|
|
.channel_id
|
|
|
|
.send_message(&ctx, |m| {
|
|
|
|
m.embed(|e| {
|
|
|
|
e.title(lm.get(&user_data.language, "lang/select_title"))
|
|
|
|
.color(*THEME_COLOR)
|
|
|
|
.description(lm.get(&user_data.language, "lang/select"))
|
|
|
|
.fields(language_codes)
|
2021-06-27 15:55:59 +00:00
|
|
|
})
|
|
|
|
.components(|c| {
|
|
|
|
for row in lm
|
|
|
|
.all_languages()
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
.collect::<Vec<(String, String)>>()
|
|
|
|
.as_slice()
|
|
|
|
.chunks(5)
|
|
|
|
{
|
|
|
|
let mut action_row = CreateActionRow::default();
|
|
|
|
for (code, name) in row {
|
|
|
|
action_row.create_button(|b| {
|
|
|
|
b.style(ButtonStyle::Primary)
|
|
|
|
.label(name.to_title_case())
|
|
|
|
.custom_id(format!("lang:{}", code.to_uppercase()))
|
|
|
|
});
|
|
|
|
}
|
2020-11-23 16:04:42 +00:00
|
|
|
|
2021-06-27 15:55:59 +00:00
|
|
|
c.add_action_row(action_row);
|
|
|
|
}
|
2020-11-23 16:04:42 +00:00
|
|
|
|
2021-06-27 15:55:59 +00:00
|
|
|
c
|
|
|
|
})
|
2020-11-23 14:11:57 +00:00
|
|
|
})
|
|
|
|
.await;
|
2020-08-29 17:07:15 +00:00
|
|
|
}
|
2020-08-27 11:15:20 +00:00
|
|
|
}
|
2020-08-29 19:57:11 +00:00
|
|
|
|
|
|
|
#[command]
|
2020-09-01 17:48:40 +00:00
|
|
|
#[supports_dm(false)]
|
|
|
|
#[permission_level(Restricted)]
|
2020-10-26 11:10:00 +00:00
|
|
|
async fn prefix(ctx: &Context, msg: &Message, args: String) {
|
2020-12-18 11:46:22 +00:00
|
|
|
let (pool, lm) = get_ctx_data(&ctx).await;
|
2020-10-12 20:01:27 +00:00
|
|
|
|
2021-05-23 09:59:06 +00:00
|
|
|
let guild_data = ctx.guild_data(msg.guild_id.unwrap()).await.unwrap();
|
2020-11-22 12:09:57 +00:00
|
|
|
let language = UserData::language_of(&msg.author, &pool).await;
|
2020-08-29 19:57:11 +00:00
|
|
|
|
|
|
|
if args.len() > 5 {
|
2020-10-12 20:01:27 +00:00
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 12:09:57 +00:00
|
|
|
.say(&ctx, lm.get(&language, "prefix/too_long"))
|
2020-10-12 20:01:27 +00:00
|
|
|
.await;
|
|
|
|
} else if args.is_empty() {
|
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 12:09:57 +00:00
|
|
|
.say(&ctx, lm.get(&language, "prefix/no_argument"))
|
2020-10-12 20:01:27 +00:00
|
|
|
.await;
|
|
|
|
} else {
|
2021-05-23 09:59:06 +00:00
|
|
|
guild_data.write().await.prefix = args;
|
2021-03-24 13:10:57 +00:00
|
|
|
|
2021-05-23 09:59:06 +00:00
|
|
|
guild_data.read().await.commit_changes(&pool).await;
|
2020-08-29 19:57:11 +00:00
|
|
|
|
2021-05-23 09:59:06 +00:00
|
|
|
let content = lm.get(&language, "prefix/success").replacen(
|
|
|
|
"{prefix}",
|
|
|
|
&guild_data.read().await.prefix,
|
|
|
|
1,
|
|
|
|
);
|
2020-10-11 00:42:19 +00:00
|
|
|
|
|
|
|
let _ = msg.channel_id.say(&ctx, content).await;
|
2020-08-29 19:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-01 18:00:56 +00:00
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[supports_dm(false)]
|
|
|
|
#[permission_level(Restricted)]
|
2020-10-26 11:10:00 +00:00
|
|
|
async fn restrict(ctx: &Context, msg: &Message, args: String) {
|
2020-12-18 11:46:22 +00:00
|
|
|
let (pool, lm) = get_ctx_data(&ctx).await;
|
2020-09-02 16:13:17 +00:00
|
|
|
|
2020-11-22 12:09:57 +00:00
|
|
|
let language = UserData::language_of(&msg.author, &pool).await;
|
2020-10-12 20:01:27 +00:00
|
|
|
let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-02 16:13:17 +00:00
|
|
|
|
|
|
|
let role_tag_match = REGEX_ROLE.find(&args);
|
|
|
|
|
|
|
|
if let Some(role_tag) = role_tag_match {
|
2020-10-12 20:01:27 +00:00
|
|
|
let commands = REGEX_COMMANDS
|
|
|
|
.find_iter(&args.to_lowercase())
|
|
|
|
.map(|c| c.as_str().to_string())
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
let role_id = RoleId(
|
|
|
|
role_tag.as_str()[3..role_tag.as_str().len() - 1]
|
|
|
|
.parse::<u64>()
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2020-09-02 16:13:17 +00:00
|
|
|
|
|
|
|
let role_opt = role_id.to_role_cached(&ctx).await;
|
|
|
|
|
|
|
|
if let Some(role) = role_opt {
|
2020-10-11 17:56:27 +00:00
|
|
|
let _ = sqlx::query!(
|
|
|
|
"
|
2020-09-02 16:13:17 +00:00
|
|
|
DELETE FROM command_restrictions WHERE role_id = (SELECT id FROM roles WHERE role = ?)
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
role.id.as_u64()
|
|
|
|
)
|
|
|
|
.execute(&pool)
|
|
|
|
.await;
|
2020-09-02 16:13:17 +00:00
|
|
|
|
2020-10-11 17:56:27 +00:00
|
|
|
if commands.is_empty() {
|
2020-10-12 20:01:27 +00:00
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 12:09:57 +00:00
|
|
|
.say(&ctx, lm.get(&language, "restrict/disabled"))
|
2020-10-12 20:01:27 +00:00
|
|
|
.await;
|
|
|
|
} else {
|
2020-09-02 16:13:17 +00:00
|
|
|
let _ = sqlx::query!(
|
|
|
|
"
|
|
|
|
INSERT IGNORE INTO roles (role, name, guild_id) VALUES (?, ?, ?)
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
role.id.as_u64(),
|
|
|
|
role.name,
|
|
|
|
guild_data.id
|
|
|
|
)
|
|
|
|
.execute(&pool)
|
|
|
|
.await;
|
2020-09-02 16:13:17 +00:00
|
|
|
|
|
|
|
for command in commands {
|
|
|
|
let res = sqlx::query!(
|
|
|
|
"
|
|
|
|
INSERT INTO command_restrictions (role_id, command) VALUES ((SELECT id FROM roles WHERE role = ?), ?)
|
|
|
|
", role.id.as_u64(), command)
|
|
|
|
.execute(&pool)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if res.is_err() {
|
2020-10-11 17:56:27 +00:00
|
|
|
println!("{:?}", res);
|
|
|
|
|
2020-11-22 12:09:57 +00:00
|
|
|
let content = lm.get(&language, "restrict/failure").replacen(
|
2020-11-22 01:31:50 +00:00
|
|
|
"{command}",
|
|
|
|
&command,
|
|
|
|
1,
|
|
|
|
);
|
2020-10-11 17:56:27 +00:00
|
|
|
|
|
|
|
let _ = msg.channel_id.say(&ctx, content).await;
|
2020-09-02 16:13:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 12:09:57 +00:00
|
|
|
.say(&ctx, lm.get(&language, "restrict/enabled"))
|
2020-10-12 20:01:27 +00:00
|
|
|
.await;
|
2020-09-02 16:13:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 20:01:27 +00:00
|
|
|
} else if args.is_empty() {
|
2020-09-25 11:48:44 +00:00
|
|
|
let guild_id = msg.guild_id.unwrap().as_u64().to_owned();
|
2020-09-02 16:13:17 +00:00
|
|
|
|
|
|
|
let rows = sqlx::query!(
|
|
|
|
"
|
|
|
|
SELECT
|
|
|
|
roles.role, command_restrictions.command
|
|
|
|
FROM
|
|
|
|
command_restrictions
|
|
|
|
INNER JOIN
|
|
|
|
roles
|
|
|
|
ON
|
|
|
|
roles.id = command_restrictions.role_id
|
|
|
|
WHERE
|
|
|
|
roles.guild_id = (SELECT id FROM guilds WHERE guild = ?)
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
guild_id
|
|
|
|
)
|
|
|
|
.fetch_all(&pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-12-17 19:59:02 +00:00
|
|
|
let mut commands_roles: HashMap<&str, Vec<String>> = HashMap::new();
|
|
|
|
|
|
|
|
rows.iter().for_each(|row| {
|
|
|
|
if let Some(vec) = commands_roles.get_mut(&row.command.as_str()) {
|
|
|
|
vec.push(format!("<@&{}>", row.role));
|
|
|
|
} else {
|
|
|
|
commands_roles.insert(&row.command, vec![format!("<@&{}>", row.role)]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let fields = commands_roles
|
2020-10-12 20:01:27 +00:00
|
|
|
.iter()
|
2020-12-17 19:59:02 +00:00
|
|
|
.map(|(key, value)| (key.to_title_case(), value.join("\n"), true));
|
|
|
|
|
|
|
|
let title = lm.get(&language, "restrict/title");
|
|
|
|
|
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
|
|
|
.send_message(&ctx, |m| {
|
|
|
|
m.embed(|e| e.title(title).fields(fields).color(*THEME_COLOR))
|
|
|
|
})
|
|
|
|
.await;
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2021-04-12 21:33:02 +00:00
|
|
|
let prefix = ctx.prefix(msg.guild_id).await;
|
2020-11-30 21:55:34 +00:00
|
|
|
|
2020-12-18 17:41:36 +00:00
|
|
|
command_help(ctx, msg, lm, &prefix, &language, "restrict").await;
|
2020-09-02 16:13:17 +00:00
|
|
|
}
|
2020-09-01 18:00:56 +00:00
|
|
|
}
|
2020-09-03 23:29:19 +00:00
|
|
|
|
2020-10-26 19:50:51 +00:00
|
|
|
#[command("alias")]
|
2020-09-03 23:29:19 +00:00
|
|
|
#[supports_dm(false)]
|
|
|
|
#[permission_level(Managed)]
|
2020-10-26 11:10:00 +00:00
|
|
|
async fn alias(ctx: &Context, msg: &Message, args: String) {
|
2020-12-18 11:46:22 +00:00
|
|
|
let (pool, lm) = get_ctx_data(&ctx).await;
|
2020-09-03 23:29:19 +00:00
|
|
|
|
2020-11-22 12:09:57 +00:00
|
|
|
let language = UserData::language_of(&msg.author, &pool).await;
|
2020-09-03 23:29:19 +00:00
|
|
|
|
|
|
|
let guild_id = msg.guild_id.unwrap().as_u64().to_owned();
|
|
|
|
|
|
|
|
let matches_opt = REGEX_ALIAS.captures(&args);
|
|
|
|
|
|
|
|
if let Some(matches) = matches_opt {
|
|
|
|
let name = matches.name("name").unwrap().as_str();
|
|
|
|
let command_opt = matches.name("cmd").map(|m| m.as_str());
|
|
|
|
|
|
|
|
match name {
|
|
|
|
"list" => {
|
|
|
|
let aliases = sqlx::query!(
|
|
|
|
"
|
|
|
|
SELECT name, command FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?)
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
guild_id
|
|
|
|
)
|
|
|
|
.fetch_all(&pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-03 23:29:19 +00:00
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
let content = iter::once("Aliases:".to_string()).chain(
|
|
|
|
aliases
|
|
|
|
.iter()
|
|
|
|
.map(|row| format!("**{}**: `{}`", row.name, row.command)),
|
|
|
|
);
|
2020-09-28 15:11:27 +00:00
|
|
|
|
2020-10-01 17:07:27 +00:00
|
|
|
let _ = msg.channel_id.say_lines(&ctx, content).await;
|
2020-10-12 20:01:27 +00:00
|
|
|
}
|
2020-09-03 23:29:19 +00:00
|
|
|
|
|
|
|
"remove" => {
|
|
|
|
if let Some(command) = command_opt {
|
2020-09-28 15:11:27 +00:00
|
|
|
let deleted_count = sqlx::query!(
|
|
|
|
"
|
|
|
|
SELECT COUNT(1) AS count FROM command_aliases WHERE name = ? AND guild_id = (SELECT id FROM guilds WHERE guild = ?)
|
|
|
|
", command, guild_id)
|
|
|
|
.fetch_one(&pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-09-03 23:29:19 +00:00
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
DELETE FROM command_aliases WHERE name = ? AND guild_id = (SELECT id FROM guilds WHERE guild = ?)
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
command,
|
|
|
|
guild_id
|
|
|
|
)
|
|
|
|
.execute(&pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-03 23:29:19 +00:00
|
|
|
|
2020-11-22 01:31:50 +00:00
|
|
|
let content = lm
|
2020-11-22 12:09:57 +00:00
|
|
|
.get(&language, "alias/removed")
|
2020-10-12 20:01:27 +00:00
|
|
|
.replace("{count}", &deleted_count.count.to_string());
|
2020-09-28 15:11:27 +00:00
|
|
|
|
|
|
|
let _ = msg.channel_id.say(&ctx, content).await;
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 12:09:57 +00:00
|
|
|
.say(&ctx, lm.get(&language, "alias/help"))
|
2020-10-12 20:01:27 +00:00
|
|
|
.await;
|
2020-09-03 23:29:19 +00:00
|
|
|
}
|
2020-10-12 20:01:27 +00:00
|
|
|
}
|
2020-09-03 23:29:19 +00:00
|
|
|
|
|
|
|
name => {
|
|
|
|
if let Some(command) = command_opt {
|
|
|
|
let res = sqlx::query!(
|
|
|
|
"
|
|
|
|
INSERT INTO command_aliases (guild_id, name, command) VALUES ((SELECT id FROM guilds WHERE guild = ?), ?, ?)
|
|
|
|
", guild_id, name, command)
|
|
|
|
.execute(&pool)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if res.is_err() {
|
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
UPDATE command_aliases SET command = ? WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?) AND name = ?
|
|
|
|
", command, guild_id, name)
|
|
|
|
.execute(&pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2020-11-22 12:09:57 +00:00
|
|
|
let content = lm.get(&language, "alias/created").replace("{name}", name);
|
2020-09-28 15:11:27 +00:00
|
|
|
|
|
|
|
let _ = msg.channel_id.say(&ctx, content).await;
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2020-09-03 23:29:19 +00:00
|
|
|
match sqlx::query!(
|
|
|
|
"
|
2020-09-04 20:21:47 +00:00
|
|
|
SELECT command FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?) AND name = ?
|
2020-09-03 23:29:19 +00:00
|
|
|
", guild_id, name)
|
|
|
|
.fetch_one(&pool)
|
|
|
|
.await {
|
|
|
|
|
|
|
|
Ok(row) => {
|
2020-09-04 20:21:47 +00:00
|
|
|
let framework = ctx.data.read().await
|
|
|
|
.get::<FrameworkCtx>().cloned().expect("Could not get FrameworkCtx from data");
|
|
|
|
|
|
|
|
let mut new_msg = msg.clone();
|
|
|
|
new_msg.content = format!("<@{}> {}", &ctx.cache.current_user_id().await, row.command);
|
2021-05-13 19:20:53 +00:00
|
|
|
new_msg.id = MessageId(0);
|
2020-09-04 20:21:47 +00:00
|
|
|
|
|
|
|
framework.dispatch(ctx.clone(), new_msg).await;
|
2020-09-03 23:29:19 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Err(_) => {
|
2020-11-22 12:09:57 +00:00
|
|
|
let content = lm.get(&language, "alias/not_found").replace("{name}", name);
|
2020-09-28 15:11:27 +00:00
|
|
|
|
|
|
|
let _ = msg.channel_id.say(&ctx, content).await;
|
2020-09-03 23:29:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2021-04-12 21:33:02 +00:00
|
|
|
let prefix = ctx.prefix(msg.guild_id).await;
|
2020-09-28 12:42:20 +00:00
|
|
|
|
2020-12-18 17:41:36 +00:00
|
|
|
command_help(ctx, msg, lm, &prefix, &language, "alias").await;
|
2020-09-03 23:29:19 +00:00
|
|
|
}
|
|
|
|
}
|