2021-09-22 20:12:29 +00:00
|
|
|
use regex_command_attr::check;
|
|
|
|
use serenity::{client::Context, model::channel::Channel};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
framework::{CommandInvoke, CommandOptions, CreateGenericResponse, HookResult},
|
2021-11-02 20:10:10 +00:00
|
|
|
moderation_cmds, RecordingMacros,
|
2021-09-22 20:12:29 +00:00
|
|
|
};
|
|
|
|
|
2021-11-13 22:30:18 +00:00
|
|
|
#[check]
|
|
|
|
pub async fn guild_only(
|
|
|
|
ctx: &Context,
|
|
|
|
invoke: &mut CommandInvoke,
|
|
|
|
_args: &CommandOptions,
|
|
|
|
) -> HookResult {
|
|
|
|
if invoke.guild_id().is_some() {
|
|
|
|
HookResult::Continue
|
|
|
|
} else {
|
|
|
|
let _ = invoke
|
|
|
|
.respond(
|
|
|
|
&ctx,
|
|
|
|
CreateGenericResponse::new().content("This command can only be used in servers"),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
HookResult::Halt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 20:12:29 +00:00
|
|
|
#[check]
|
|
|
|
pub async fn macro_check(
|
|
|
|
ctx: &Context,
|
2021-10-13 15:37:15 +00:00
|
|
|
invoke: &mut CommandInvoke,
|
2021-09-22 20:12:29 +00:00
|
|
|
args: &CommandOptions,
|
|
|
|
) -> HookResult {
|
|
|
|
if let Some(guild_id) = invoke.guild_id() {
|
|
|
|
if args.command != moderation_cmds::MACRO_CMD_COMMAND.names[0] {
|
|
|
|
let active_recordings =
|
|
|
|
ctx.data.read().await.get::<RecordingMacros>().cloned().unwrap();
|
|
|
|
let mut lock = active_recordings.write().await;
|
|
|
|
|
|
|
|
if let Some(command_macro) = lock.get_mut(&(guild_id, invoke.author_id())) {
|
2021-10-30 19:57:33 +00:00
|
|
|
if command_macro.commands.len() >= 5 {
|
|
|
|
let _ = invoke
|
|
|
|
.respond(
|
|
|
|
&ctx,
|
|
|
|
CreateGenericResponse::new().content("5 commands already recorded. Please use `/macro finish` to end recording."),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
} else {
|
|
|
|
command_macro.commands.push(args.clone());
|
2021-09-22 20:12:29 +00:00
|
|
|
|
2021-10-30 19:57:33 +00:00
|
|
|
let _ = invoke
|
|
|
|
.respond(
|
|
|
|
&ctx,
|
|
|
|
CreateGenericResponse::new().content("Command recorded to macro"),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
}
|
2021-09-22 20:12:29 +00:00
|
|
|
|
|
|
|
HookResult::Halt
|
|
|
|
} else {
|
|
|
|
HookResult::Continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HookResult::Continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HookResult::Continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[check]
|
|
|
|
pub async fn check_self_permissions(
|
|
|
|
ctx: &Context,
|
2021-10-13 15:37:15 +00:00
|
|
|
invoke: &mut CommandInvoke,
|
2021-09-22 20:12:29 +00:00
|
|
|
_args: &CommandOptions,
|
|
|
|
) -> HookResult {
|
|
|
|
if let Some(guild) = invoke.guild(&ctx) {
|
|
|
|
let user_id = ctx.cache.current_user_id();
|
|
|
|
|
|
|
|
let manage_webhooks =
|
|
|
|
guild.member_permissions(&ctx, user_id).await.map_or(false, |p| p.manage_webhooks());
|
2021-11-18 21:05:49 +00:00
|
|
|
let (view_channel, send_messages, embed_links) = invoke
|
2021-09-22 20:12:29 +00:00
|
|
|
.channel_id()
|
|
|
|
.to_channel_cached(&ctx)
|
|
|
|
.map(|c| {
|
|
|
|
if let Channel::Guild(channel) = c {
|
|
|
|
channel.permissions_for_user(ctx, user_id).ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten()
|
2021-11-18 21:05:49 +00:00
|
|
|
.map_or((false, false, false), |p| {
|
|
|
|
(p.read_messages(), p.send_messages(), p.embed_links())
|
|
|
|
});
|
2021-09-22 20:12:29 +00:00
|
|
|
|
|
|
|
if manage_webhooks && send_messages && embed_links {
|
|
|
|
HookResult::Continue
|
|
|
|
} else {
|
2021-11-18 21:05:49 +00:00
|
|
|
let _ = invoke
|
|
|
|
.respond(
|
|
|
|
&ctx,
|
|
|
|
CreateGenericResponse::new().content(format!(
|
|
|
|
"Please ensure the bot has the correct permissions:
|
2021-09-22 20:12:29 +00:00
|
|
|
|
2021-11-18 21:05:49 +00:00
|
|
|
{} **View Channel**
|
|
|
|
{} **Send Message**
|
2021-09-22 20:12:29 +00:00
|
|
|
{} **Embed Links**
|
|
|
|
{} **Manage Webhooks**",
|
2021-11-18 21:05:49 +00:00
|
|
|
if view_channel { "✅" } else { "❌" },
|
|
|
|
if send_messages { "✅" } else { "❌" },
|
|
|
|
if manage_webhooks { "✅" } else { "❌" },
|
|
|
|
if embed_links { "✅" } else { "❌" },
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
.await;
|
2021-09-22 20:12:29 +00:00
|
|
|
|
|
|
|
HookResult::Halt
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HookResult::Continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[check]
|
|
|
|
pub async fn check_guild_permissions(
|
|
|
|
ctx: &Context,
|
2021-10-13 15:37:15 +00:00
|
|
|
invoke: &mut CommandInvoke,
|
2021-09-22 20:12:29 +00:00
|
|
|
_args: &CommandOptions,
|
|
|
|
) -> HookResult {
|
|
|
|
if let Some(guild) = invoke.guild(&ctx) {
|
|
|
|
let permissions = guild.member_permissions(&ctx, invoke.author_id()).await.unwrap();
|
|
|
|
|
|
|
|
if !permissions.manage_guild() {
|
|
|
|
let _ = invoke
|
|
|
|
.respond(
|
|
|
|
&ctx,
|
|
|
|
CreateGenericResponse::new().content(
|
|
|
|
"You must have the \"Manage Server\" permission to use this command",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
HookResult::Halt
|
|
|
|
} else {
|
|
|
|
HookResult::Continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HookResult::Continue
|
|
|
|
}
|
|
|
|
}
|