2022-02-19 14:32:03 +00:00
|
|
|
use poise::{serenity::model::channel::Channel, ApplicationCommandOrAutocompleteInteraction};
|
2021-09-22 20:12:29 +00:00
|
|
|
|
2022-02-19 18:21:11 +00:00
|
|
|
use crate::{consts::MACRO_MAX_COMMANDS, models::command_macro::RecordedCommand, Context, Error};
|
2021-09-22 20:12:29 +00:00
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
pub async fn guild_only(ctx: Context<'_>) -> Result<bool, Error> {
|
|
|
|
if ctx.guild_id().is_some() {
|
|
|
|
Ok(true)
|
2021-11-13 22:30:18 +00:00
|
|
|
} else {
|
2022-02-19 14:32:03 +00:00
|
|
|
let _ = ctx.say("This command can only be used in servers").await;
|
2021-11-13 22:30:18 +00:00
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
Ok(false)
|
2021-11-13 22:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
async fn macro_check(ctx: Context<'_>) -> bool {
|
|
|
|
if let Context::Application(app_ctx) = ctx {
|
|
|
|
if let ApplicationCommandOrAutocompleteInteraction::ApplicationCommand(interaction) =
|
|
|
|
app_ctx.interaction
|
|
|
|
{
|
|
|
|
if let Some(guild_id) = ctx.guild_id() {
|
|
|
|
if ctx.command().identifying_name != "macro_finish" {
|
|
|
|
let mut lock = ctx.data().recording_macros.write().await;
|
2021-09-22 20:12:29 +00:00
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
if let Some(command_macro) = lock.get_mut(&(guild_id, ctx.author().id)) {
|
|
|
|
if command_macro.commands.len() >= MACRO_MAX_COMMANDS {
|
|
|
|
let _ = ctx.send(|m| {
|
|
|
|
m.ephemeral(true).content(
|
2022-02-19 18:21:11 +00:00
|
|
|
format!("{} commands already recorded. Please use `/macro finish` to end recording.", MACRO_MAX_COMMANDS),
|
2022-02-19 14:32:03 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
} else {
|
2022-02-19 18:21:11 +00:00
|
|
|
let recorded = RecordedCommand {
|
|
|
|
action: None,
|
|
|
|
command_name: ctx.command().identifying_name.clone(),
|
|
|
|
options: Vec::from(app_ctx.args),
|
|
|
|
};
|
|
|
|
|
|
|
|
command_macro.commands.push(recorded);
|
2021-09-22 20:12:29 +00:00
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
let _ = ctx
|
|
|
|
.send(|m| m.ephemeral(true).content("Command recorded to macro"))
|
|
|
|
.await;
|
|
|
|
}
|
2021-09-22 20:12:29 +00:00
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
2021-09-22 20:12:29 +00:00
|
|
|
} else {
|
2022-02-19 14:32:03 +00:00
|
|
|
true
|
2021-09-22 20:12:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-19 14:32:03 +00:00
|
|
|
true
|
2021-09-22 20:12:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-19 14:32:03 +00:00
|
|
|
true
|
2021-09-22 20:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
async fn check_self_permissions(ctx: Context<'_>) -> bool {
|
|
|
|
if let Some(guild) = ctx.guild() {
|
|
|
|
let user_id = ctx.discord().cache.current_user_id();
|
2021-09-22 20:12:29 +00:00
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
let manage_webhooks = guild
|
|
|
|
.member_permissions(&ctx.discord(), user_id)
|
|
|
|
.await
|
|
|
|
.map_or(false, |p| p.manage_webhooks());
|
|
|
|
let (view_channel, send_messages, embed_links) = ctx
|
2021-09-22 20:12:29 +00:00
|
|
|
.channel_id()
|
2022-02-19 14:32:03 +00:00
|
|
|
.to_channel_cached(&ctx.discord())
|
2021-09-22 20:12:29 +00:00
|
|
|
.map(|c| {
|
|
|
|
if let Channel::Guild(channel) = c {
|
2022-02-19 14:32:03 +00:00
|
|
|
channel.permissions_for_user(&ctx.discord(), user_id).ok()
|
2021-09-22 20:12:29 +00:00
|
|
|
} 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 {
|
2022-02-19 14:32:03 +00:00
|
|
|
true
|
2021-09-22 20:12:29 +00:00
|
|
|
} else {
|
2022-02-19 14:32:03 +00:00
|
|
|
let _ = ctx
|
|
|
|
.send(|m| {
|
|
|
|
m.content(format!(
|
2021-11-18 21:05:49 +00:00
|
|
|
"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 { "❌" },
|
2022-02-19 14:32:03 +00:00
|
|
|
))
|
|
|
|
})
|
2021-11-18 21:05:49 +00:00
|
|
|
.await;
|
2021-09-22 20:12:29 +00:00
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
false
|
2021-09-22 20:12:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-19 14:32:03 +00:00
|
|
|
true
|
2021-09-22 20:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
pub async fn all_checks(ctx: Context<'_>) -> Result<bool, Error> {
|
|
|
|
Ok(macro_check(ctx).await && check_self_permissions(ctx).await)
|
2021-09-22 20:12:29 +00:00
|
|
|
}
|