2020-08-06 14:22:13 +00:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
use serenity::{
|
2020-10-12 20:01:27 +00:00
|
|
|
client::Context,
|
2020-10-12 18:12:33 +00:00
|
|
|
constants::MESSAGE_CODE_LIMIT,
|
2020-10-26 11:10:00 +00:00
|
|
|
framework::Framework,
|
2020-10-12 20:01:27 +00:00
|
|
|
futures::prelude::future::BoxFuture,
|
2020-10-01 17:07:27 +00:00
|
|
|
http::Http,
|
2020-08-07 00:02:01 +00:00
|
|
|
model::{
|
2020-10-12 20:01:27 +00:00
|
|
|
channel::{Channel, GuildChannel, Message},
|
|
|
|
guild::{Guild, Member},
|
2020-10-12 18:12:33 +00:00
|
|
|
id::ChannelId,
|
2020-08-07 00:02:01 +00:00
|
|
|
},
|
2020-10-12 20:01:27 +00:00
|
|
|
Result as SerenityResult,
|
2020-08-07 00:02:01 +00:00
|
|
|
};
|
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
use log::{error, info, warn};
|
2020-08-06 14:22:13 +00:00
|
|
|
|
2020-10-22 09:31:47 +00:00
|
|
|
use regex::{Match, Regex, RegexBuilder};
|
2020-08-07 00:02:01 +00:00
|
|
|
|
2020-10-13 13:35:13 +00:00
|
|
|
use std::{collections::HashMap, fmt};
|
2020-08-06 14:22:13 +00:00
|
|
|
|
2020-11-22 01:31:50 +00:00
|
|
|
use crate::language_manager::LanguageManager;
|
2020-10-23 22:04:37 +00:00
|
|
|
use crate::models::{GuildData, UserData};
|
2020-10-13 13:35:13 +00:00
|
|
|
use crate::{models::ChannelData, SQLPool};
|
2020-08-06 14:22:13 +00:00
|
|
|
|
2020-10-26 11:10:00 +00:00
|
|
|
type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, String) -> BoxFuture<'fut, ()>;
|
2020-08-10 21:12:26 +00:00
|
|
|
|
2020-10-11 17:56:27 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-08-06 14:22:13 +00:00
|
|
|
pub enum PermissionLevel {
|
|
|
|
Unrestricted,
|
|
|
|
Managed,
|
|
|
|
Restricted,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Command {
|
2020-08-06 18:18:30 +00:00
|
|
|
pub name: &'static str,
|
|
|
|
pub required_perms: PermissionLevel,
|
|
|
|
pub supports_dm: bool,
|
2020-08-06 19:39:05 +00:00
|
|
|
pub can_blacklist: bool,
|
2020-08-06 18:18:30 +00:00
|
|
|
pub func: CommandFn,
|
2020-08-06 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:01:50 +00:00
|
|
|
impl Command {
|
|
|
|
async fn check_permissions(&self, ctx: &Context, guild: &Guild, member: &Member) -> bool {
|
2020-10-11 17:56:27 +00:00
|
|
|
if self.required_perms == PermissionLevel::Unrestricted {
|
|
|
|
true
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2020-10-25 11:19:21 +00:00
|
|
|
let permissions = guild.member_permissions(&ctx, &member.user).await.unwrap();
|
|
|
|
|
|
|
|
if permissions.manage_guild()
|
|
|
|
|| (permissions.manage_messages()
|
|
|
|
&& self.required_perms == PermissionLevel::Managed)
|
2020-10-13 10:36:20 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-11 17:56:27 +00:00
|
|
|
if self.required_perms == PermissionLevel::Managed {
|
2020-10-12 20:01:27 +00:00
|
|
|
let pool = ctx
|
|
|
|
.data
|
|
|
|
.read()
|
|
|
|
.await
|
|
|
|
.get::<SQLPool>()
|
|
|
|
.cloned()
|
|
|
|
.expect("Could not get SQLPool from data");
|
2020-08-09 20:01:50 +00:00
|
|
|
|
2020-10-11 17:56:27 +00:00
|
|
|
match sqlx::query!(
|
|
|
|
"
|
2020-08-09 20:01:50 +00:00
|
|
|
SELECT
|
|
|
|
role
|
|
|
|
FROM
|
|
|
|
roles
|
|
|
|
INNER JOIN
|
|
|
|
command_restrictions ON roles.id = command_restrictions.role_id
|
|
|
|
WHERE
|
|
|
|
command_restrictions.command = ? AND
|
2020-09-02 16:13:17 +00:00
|
|
|
roles.guild_id = (
|
2020-08-09 20:01:50 +00:00
|
|
|
SELECT
|
|
|
|
id
|
|
|
|
FROM
|
|
|
|
guilds
|
|
|
|
WHERE
|
2020-10-11 17:56:27 +00:00
|
|
|
guild = ?)
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
self.name,
|
|
|
|
guild.id.as_u64()
|
|
|
|
)
|
|
|
|
.fetch_all(&pool)
|
|
|
|
.await
|
|
|
|
{
|
2020-08-09 20:01:50 +00:00
|
|
|
Ok(rows) => {
|
2020-10-12 20:01:27 +00:00
|
|
|
let role_ids = member
|
|
|
|
.roles
|
|
|
|
.iter()
|
|
|
|
.map(|r| *r.as_u64())
|
|
|
|
.collect::<Vec<u64>>();
|
2020-08-09 20:01:50 +00:00
|
|
|
|
|
|
|
for row in rows {
|
|
|
|
if role_ids.contains(&row.role) {
|
2020-10-12 20:01:27 +00:00
|
|
|
return true;
|
2020-08-09 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
Err(sqlx::Error::RowNotFound) => false,
|
2020-08-09 20:01:50 +00:00
|
|
|
|
|
|
|
Err(e) => {
|
2020-10-12 20:01:27 +00:00
|
|
|
warn!(
|
|
|
|
"Unexpected error occurred querying command_restrictions: {:?}",
|
|
|
|
e
|
|
|
|
);
|
2020-08-09 20:01:50 +00:00
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2020-08-09 20:01:50 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 19:39:05 +00:00
|
|
|
impl fmt::Debug for Command {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("Command")
|
2020-10-12 20:01:27 +00:00
|
|
|
.field("name", &self.name)
|
|
|
|
.field("required_perms", &self.required_perms)
|
|
|
|
.field("supports_dm", &self.supports_dm)
|
|
|
|
.field("can_blacklist", &self.can_blacklist)
|
|
|
|
.finish()
|
2020-08-06 19:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:07:27 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait SendIterator {
|
2020-10-12 20:01:27 +00:00
|
|
|
async fn say_lines(
|
|
|
|
self,
|
|
|
|
http: impl AsRef<Http> + Send + Sync + 'async_trait,
|
|
|
|
content: impl Iterator<Item = String> + Send + 'async_trait,
|
|
|
|
) -> SerenityResult<()>;
|
2020-10-01 17:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl SendIterator for ChannelId {
|
2020-10-12 20:01:27 +00:00
|
|
|
async fn say_lines(
|
|
|
|
self,
|
|
|
|
http: impl AsRef<Http> + Send + Sync + 'async_trait,
|
|
|
|
content: impl Iterator<Item = String> + Send + 'async_trait,
|
|
|
|
) -> SerenityResult<()> {
|
2020-10-01 17:07:27 +00:00
|
|
|
let mut current_content = String::new();
|
|
|
|
|
|
|
|
for line in content {
|
2020-10-12 18:12:33 +00:00
|
|
|
if current_content.len() + line.len() > MESSAGE_CODE_LIMIT as usize {
|
2020-10-13 23:19:41 +00:00
|
|
|
self.send_message(&http, |m| {
|
|
|
|
m.allowed_mentions(|am| am.empty_parse())
|
|
|
|
.content(¤t_content)
|
|
|
|
})
|
|
|
|
.await?;
|
2020-10-01 17:07:27 +00:00
|
|
|
|
|
|
|
current_content = line;
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2020-10-01 17:07:27 +00:00
|
|
|
current_content = format!("{}\n{}", current_content, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !current_content.is_empty() {
|
2020-10-13 23:19:41 +00:00
|
|
|
self.send_message(&http, |m| {
|
|
|
|
m.allowed_mentions(|am| am.empty_parse())
|
|
|
|
.content(¤t_content)
|
|
|
|
})
|
|
|
|
.await?;
|
2020-10-01 17:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 09:31:47 +00:00
|
|
|
pub struct RegexFramework {
|
|
|
|
commands: HashMap<String, &'static Command>,
|
|
|
|
command_matcher: Regex,
|
|
|
|
dm_regex_matcher: Regex,
|
|
|
|
default_prefix: String,
|
|
|
|
client_id: u64,
|
|
|
|
ignore_bots: bool,
|
|
|
|
case_insensitive: bool,
|
|
|
|
}
|
|
|
|
|
2020-08-06 14:22:13 +00:00
|
|
|
impl RegexFramework {
|
2020-10-12 17:37:14 +00:00
|
|
|
pub fn new<T: Into<u64>>(client_id: T) -> Self {
|
2020-08-06 14:22:13 +00:00
|
|
|
Self {
|
2020-08-06 18:18:30 +00:00
|
|
|
commands: HashMap::new(),
|
2020-09-03 23:29:19 +00:00
|
|
|
command_matcher: Regex::new(r#"^$"#).unwrap(),
|
2020-08-07 22:45:34 +00:00
|
|
|
dm_regex_matcher: Regex::new(r#"^$"#).unwrap(),
|
2020-10-13 13:35:13 +00:00
|
|
|
default_prefix: "".to_string(),
|
2020-10-12 17:37:14 +00:00
|
|
|
client_id: client_id.into(),
|
2020-08-06 14:22:13 +00:00
|
|
|
ignore_bots: true,
|
2020-10-22 09:31:47 +00:00
|
|
|
case_insensitive: true,
|
2020-08-06 14:22:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 09:31:47 +00:00
|
|
|
pub fn case_insensitive(mut self, case_insensitive: bool) -> Self {
|
|
|
|
self.case_insensitive = case_insensitive;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-28 12:42:20 +00:00
|
|
|
pub fn default_prefix<T: ToString>(mut self, new_prefix: T) -> Self {
|
2020-08-06 14:22:13 +00:00
|
|
|
self.default_prefix = new_prefix.to_string();
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ignore_bots(mut self, ignore_bots: bool) -> Self {
|
|
|
|
self.ignore_bots = ignore_bots;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-12 17:37:14 +00:00
|
|
|
pub fn add_command<S: ToString>(mut self, name: S, command: &'static Command) -> Self {
|
2020-08-09 22:59:31 +00:00
|
|
|
self.commands.insert(name.to_string(), command);
|
2020-08-06 14:22:13 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(mut self) -> Self {
|
2020-08-07 15:45:19 +00:00
|
|
|
{
|
2020-08-07 22:45:34 +00:00
|
|
|
let command_names;
|
|
|
|
|
|
|
|
{
|
2020-10-12 20:01:27 +00:00
|
|
|
let mut command_names_vec =
|
|
|
|
self.commands.keys().map(|k| &k[..]).collect::<Vec<&str>>();
|
2020-08-07 22:45:34 +00:00
|
|
|
|
|
|
|
command_names_vec.sort_unstable_by(|a, b| b.len().cmp(&a.len()));
|
|
|
|
|
|
|
|
command_names = command_names_vec.join("|");
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("Command names: {}", command_names);
|
2020-08-07 15:45:19 +00:00
|
|
|
|
2020-08-07 22:45:34 +00:00
|
|
|
{
|
2020-12-07 12:36:33 +00:00
|
|
|
let match_string = r#"^(?:(?:<@ID>\s*)|(?:<@!ID>\s*)|(?P<prefix>\S{1,5}?))(?P<cmd>COMMANDS)(?:$|\s+(?P<args>.*))$"#
|
2020-08-07 22:45:34 +00:00
|
|
|
.replace("COMMANDS", command_names.as_str())
|
|
|
|
.replace("ID", self.client_id.to_string().as_str());
|
2020-08-07 15:45:19 +00:00
|
|
|
|
2020-10-22 09:31:47 +00:00
|
|
|
self.command_matcher = RegexBuilder::new(match_string.as_str())
|
|
|
|
.case_insensitive(self.case_insensitive)
|
2020-10-26 10:16:38 +00:00
|
|
|
.dot_matches_new_line(true)
|
2020-10-22 09:31:47 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
2020-08-07 22:45:34 +00:00
|
|
|
}
|
2020-08-07 15:45:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 22:45:34 +00:00
|
|
|
{
|
|
|
|
let dm_command_names;
|
|
|
|
|
|
|
|
{
|
2020-10-12 20:01:27 +00:00
|
|
|
let mut command_names_vec = self
|
|
|
|
.commands
|
2020-08-07 22:45:34 +00:00
|
|
|
.iter()
|
|
|
|
.filter_map(|(key, command)| {
|
|
|
|
if command.supports_dm {
|
|
|
|
Some(&key[..])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<&str>>();
|
|
|
|
|
|
|
|
command_names_vec.sort_unstable_by(|a, b| b.len().cmp(&a.len()));
|
|
|
|
|
|
|
|
dm_command_names = command_names_vec.join("|");
|
|
|
|
}
|
2020-08-06 14:22:13 +00:00
|
|
|
|
2020-08-07 22:45:34 +00:00
|
|
|
{
|
|
|
|
let match_string = r#"^(?:(?:<@ID>\s+)|(?:<@!ID>\s+)|(\$)|())(?P<cmd>COMMANDS)(?:$|\s+(?P<args>.*))$"#
|
|
|
|
.replace("COMMANDS", dm_command_names.as_str())
|
|
|
|
.replace("ID", self.client_id.to_string().as_str());
|
2020-08-07 00:02:01 +00:00
|
|
|
|
2020-10-22 09:31:47 +00:00
|
|
|
self.dm_regex_matcher = RegexBuilder::new(match_string.as_str())
|
|
|
|
.case_insensitive(self.case_insensitive)
|
2020-10-26 10:16:38 +00:00
|
|
|
.dot_matches_new_line(true)
|
2020-10-22 09:31:47 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
2020-08-07 22:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-07 00:02:01 +00:00
|
|
|
|
2020-08-06 14:22:13 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 00:02:01 +00:00
|
|
|
enum PermissionCheck {
|
2020-11-23 16:04:42 +00:00
|
|
|
None, // No permissions
|
|
|
|
Basic(bool, bool, bool, bool), // Send + Embed permissions (sufficient to reply)
|
|
|
|
All, // Above + Manage Webhooks (sufficient to operate)
|
2020-08-07 00:02:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 14:22:13 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Framework for RegexFramework {
|
|
|
|
async fn dispatch(&self, ctx: Context, msg: Message) {
|
2020-10-12 20:01:27 +00:00
|
|
|
async fn check_self_permissions(
|
|
|
|
ctx: &Context,
|
|
|
|
guild: &Guild,
|
|
|
|
channel: &GuildChannel,
|
2020-10-25 11:19:21 +00:00
|
|
|
) -> SerenityResult<PermissionCheck> {
|
2020-08-07 00:02:01 +00:00
|
|
|
let user_id = ctx.cache.current_user_id().await;
|
|
|
|
|
2020-10-25 11:19:21 +00:00
|
|
|
let guild_perms = guild.member_permissions(&ctx, user_id).await?;
|
2020-11-20 16:37:39 +00:00
|
|
|
let channel_perms = channel.permissions_for_user(ctx, user_id).await?;
|
2020-08-07 00:02:01 +00:00
|
|
|
|
2020-11-20 16:37:39 +00:00
|
|
|
let basic_perms = channel_perms.send_messages();
|
2020-10-23 22:04:37 +00:00
|
|
|
|
|
|
|
Ok(
|
2020-11-20 16:37:39 +00:00
|
|
|
if basic_perms && guild_perms.manage_webhooks() && channel_perms.embed_links() {
|
2020-10-23 22:04:37 +00:00
|
|
|
PermissionCheck::All
|
|
|
|
} else if basic_perms {
|
2020-11-20 16:37:39 +00:00
|
|
|
PermissionCheck::Basic(
|
|
|
|
guild_perms.manage_webhooks(),
|
|
|
|
channel_perms.embed_links(),
|
2020-11-23 16:04:42 +00:00
|
|
|
channel_perms.add_reactions(),
|
|
|
|
channel_perms.manage_messages(),
|
2020-11-20 16:37:39 +00:00
|
|
|
)
|
2020-10-23 22:04:37 +00:00
|
|
|
} else {
|
|
|
|
PermissionCheck::None
|
|
|
|
},
|
|
|
|
)
|
2020-08-07 00:02:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:37 +00:00
|
|
|
async fn check_prefix(ctx: &Context, guild: &Guild, prefix_opt: Option<Match<'_>>) -> bool {
|
2020-08-07 15:45:19 +00:00
|
|
|
if let Some(prefix) = prefix_opt {
|
2020-10-12 20:01:27 +00:00
|
|
|
let pool = ctx
|
|
|
|
.data
|
|
|
|
.read()
|
|
|
|
.await
|
|
|
|
.get::<SQLPool>()
|
|
|
|
.cloned()
|
|
|
|
.expect("Could not get SQLPool from data");
|
|
|
|
|
2020-10-23 22:04:37 +00:00
|
|
|
let guild_prefix = GuildData::prefix_from_id(Some(guild.id), &pool).await;
|
2020-08-07 15:45:19 +00:00
|
|
|
|
2020-10-23 22:04:37 +00:00
|
|
|
guild_prefix.as_str() == prefix.as_str()
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2020-08-07 15:45:19 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 00:02:01 +00:00
|
|
|
// gate to prevent analysing messages unnecessarily
|
2020-12-16 19:20:46 +00:00
|
|
|
if (msg.author.bot && self.ignore_bots) || msg.content.is_empty() {
|
2020-10-12 20:01:27 +00:00
|
|
|
}
|
2020-08-07 00:02:01 +00:00
|
|
|
// Guild Command
|
2020-10-12 20:01:27 +00:00
|
|
|
else if let (Some(guild), Some(Channel::Guild(channel))) =
|
|
|
|
(msg.guild(&ctx).await, msg.channel(&ctx).await)
|
|
|
|
{
|
2020-12-22 14:28:18 +00:00
|
|
|
let data = ctx.data.read().await;
|
|
|
|
|
|
|
|
let pool = data
|
|
|
|
.get::<SQLPool>()
|
|
|
|
.cloned()
|
|
|
|
.expect("Could not get SQLPool from data");
|
2020-12-22 14:44:45 +00:00
|
|
|
{
|
|
|
|
let guild = GuildData::from_guild(guild.clone(), &pool).await.unwrap();
|
2020-11-22 01:31:50 +00:00
|
|
|
|
2020-12-22 14:44:45 +00:00
|
|
|
let _ = sqlx::query!(
|
|
|
|
"UPDATE channels SET guild_id = ? WHERE channel = ?",
|
|
|
|
guild.id,
|
|
|
|
msg.channel_id.as_u64(),
|
|
|
|
)
|
|
|
|
.execute(&pool)
|
|
|
|
.await;
|
|
|
|
}
|
2020-10-23 22:04:37 +00:00
|
|
|
|
2020-12-22 14:28:18 +00:00
|
|
|
if let Some(full_match) = self.command_matcher.captures(&msg.content) {
|
|
|
|
if check_prefix(&ctx, &guild, full_match.name("prefix")).await {
|
2020-11-22 01:31:50 +00:00
|
|
|
let lm = data.get::<LanguageManager>().unwrap();
|
|
|
|
|
|
|
|
let language = UserData::language_of(&msg.author, &pool).await;
|
2020-10-23 22:04:37 +00:00
|
|
|
|
2020-08-07 15:45:19 +00:00
|
|
|
match check_self_permissions(&ctx, &guild, &channel).await {
|
|
|
|
Ok(perms) => match perms {
|
2020-08-07 22:45:34 +00:00
|
|
|
PermissionCheck::All => {
|
2020-10-12 20:01:27 +00:00
|
|
|
let command = self
|
|
|
|
.commands
|
2020-10-22 09:31:47 +00:00
|
|
|
.get(&full_match.name("cmd").unwrap().as_str().to_lowercase())
|
2020-10-12 20:01:27 +00:00
|
|
|
.unwrap();
|
2020-10-26 12:33:38 +00:00
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
let channel_data = ChannelData::from_channel(
|
|
|
|
msg.channel(&ctx).await.unwrap(),
|
|
|
|
&pool,
|
|
|
|
)
|
2020-10-27 11:37:55 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-10-12 20:01:27 +00:00
|
|
|
|
2020-10-27 11:37:55 +00:00
|
|
|
if !command.can_blacklist || !channel_data.blacklisted {
|
2020-10-12 20:01:27 +00:00
|
|
|
let args = full_match
|
|
|
|
.name("args")
|
2020-08-25 16:19:08 +00:00
|
|
|
.map(|m| m.as_str())
|
|
|
|
.unwrap_or("")
|
|
|
|
.to_string();
|
2020-08-07 22:45:34 +00:00
|
|
|
|
2020-10-26 12:33:38 +00:00
|
|
|
let member = guild.member(&ctx, &msg.author).await.unwrap();
|
|
|
|
|
2020-08-25 16:19:08 +00:00
|
|
|
if command.check_permissions(&ctx, &guild, &member).await {
|
2020-10-26 11:10:00 +00:00
|
|
|
(command.func)(&ctx, &msg, args).await;
|
2020-10-12 20:01:27 +00:00
|
|
|
} else if command.required_perms == PermissionLevel::Restricted
|
|
|
|
{
|
2020-10-23 22:04:37 +00:00
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
2020-11-22 01:31:50 +00:00
|
|
|
.say(&ctx, lm.get(&language, "no_perms_restricted"))
|
2020-10-23 22:04:37 +00:00
|
|
|
.await;
|
2020-10-12 20:01:27 +00:00
|
|
|
} else if command.required_perms == PermissionLevel::Managed {
|
2020-10-23 22:04:37 +00:00
|
|
|
let _ = msg
|
|
|
|
.channel_id
|
|
|
|
.say(
|
|
|
|
&ctx,
|
2020-11-22 01:31:50 +00:00
|
|
|
lm.get(&language, "no_perms_managed").replace(
|
|
|
|
"{prefix}",
|
|
|
|
&GuildData::prefix_from_id(msg.guild_id, &pool)
|
2020-10-23 22:04:37 +00:00
|
|
|
.await,
|
2020-11-22 01:31:50 +00:00
|
|
|
),
|
2020-10-23 22:04:37 +00:00
|
|
|
)
|
|
|
|
.await;
|
2020-10-11 17:56:27 +00:00
|
|
|
}
|
2020-08-09 20:01:50 +00:00
|
|
|
}
|
2020-08-07 22:45:34 +00:00
|
|
|
}
|
2020-08-07 00:02:01 +00:00
|
|
|
|
2020-11-23 16:04:42 +00:00
|
|
|
PermissionCheck::Basic(
|
|
|
|
manage_webhooks,
|
|
|
|
embed_links,
|
|
|
|
add_reactions,
|
|
|
|
manage_messages,
|
|
|
|
) => {
|
2020-11-22 01:31:50 +00:00
|
|
|
let response = lm
|
|
|
|
.get(&language, "no_perms_general")
|
2020-11-20 16:37:39 +00:00
|
|
|
.replace(
|
|
|
|
"{manage_webhooks}",
|
|
|
|
if manage_webhooks { "✅" } else { "❌" },
|
|
|
|
)
|
2020-11-23 16:04:42 +00:00
|
|
|
.replace("{embed_links}", if embed_links { "✅" } else { "❌" })
|
2020-11-20 16:37:39 +00:00
|
|
|
.replace(
|
2020-11-23 16:04:42 +00:00
|
|
|
"{add_reactions}",
|
|
|
|
if add_reactions { "✅" } else { "❌" },
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
"{manage_messages}",
|
|
|
|
if manage_messages { "✅" } else { "❌" },
|
2020-11-20 16:37:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let _ = msg.channel_id.say(&ctx, response).await;
|
2020-08-07 22:45:34 +00:00
|
|
|
}
|
2020-08-07 00:02:01 +00:00
|
|
|
|
2020-08-07 15:45:19 +00:00
|
|
|
PermissionCheck::None => {
|
|
|
|
warn!("Missing enough permissions for guild {}", guild.id);
|
|
|
|
}
|
2020-10-12 20:01:27 +00:00
|
|
|
},
|
2020-08-07 00:02:01 +00:00
|
|
|
|
2020-08-07 15:45:19 +00:00
|
|
|
Err(e) => {
|
2020-10-12 20:01:27 +00:00
|
|
|
error!(
|
|
|
|
"Error occurred getting permissions in guild {}: {:?}",
|
|
|
|
guild.id, e
|
|
|
|
);
|
2020-08-07 15:45:19 +00:00
|
|
|
}
|
2020-08-07 00:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// DM Command
|
2020-09-25 22:07:22 +00:00
|
|
|
else if let Some(full_match) = self.dm_regex_matcher.captures(&msg.content[..]) {
|
2020-10-12 20:01:27 +00:00
|
|
|
let command = self
|
|
|
|
.commands
|
2020-10-22 09:31:47 +00:00
|
|
|
.get(&full_match.name("cmd").unwrap().as_str().to_lowercase())
|
2020-10-12 20:01:27 +00:00
|
|
|
.unwrap();
|
|
|
|
let args = full_match
|
|
|
|
.name("args")
|
2020-09-25 22:07:22 +00:00
|
|
|
.map(|m| m.as_str())
|
|
|
|
.unwrap_or("")
|
|
|
|
.to_string();
|
|
|
|
|
2020-10-26 11:10:00 +00:00
|
|
|
(command.func)(&ctx, &msg, args).await;
|
2020-08-07 00:02:01 +00:00
|
|
|
}
|
2020-08-06 14:22:13 +00:00
|
|
|
}
|
|
|
|
}
|