Files
reminder-bot/src/hooks.rs

145 lines
6.5 KiB
Rust
Raw Normal View History

2025-06-18 22:08:32 +01:00
use crate::consts::THEME_COLOR;
use poise::{serenity_prelude::CreateEmbed, CommandInteractionType, CreateReply};
use serenity::builder::CreateEmbedFooter;
use crate::{consts::MACRO_MAX_COMMANDS, models::command_macro::RecordedCommand, Context, Error};
2022-02-19 14:32:03 +00:00
async fn macro_check(ctx: Context<'_>) -> bool {
if let Context::Application(app_ctx) = ctx {
2024-02-15 17:28:43 +00:00
if app_ctx.interaction_type != CommandInteractionType::Command {
return true;
}
2024-01-06 19:48:17 +00:00
if let Some(guild_id) = ctx.guild_id() {
if ctx.command().identifying_name != "finish_macro" {
2024-02-09 17:03:04 +00:00
let mut lock = ctx.data().recording_macros.write().await;
if let Some(command_macro) = lock.get_mut(&(guild_id, ctx.author().id)) {
2024-01-06 19:48:17 +00:00
if command_macro.commands.len() >= MACRO_MAX_COMMANDS {
let _ = ctx
2024-02-06 20:08:59 +00:00
.send(
CreateReply::default()
.ephemeral(true)
2025-06-18 22:08:32 +01:00
.embed(CreateEmbed::new()
.title("💾 Currently recording macro")
.description(
format!("{} commands already recorded. Please use `/macro finish` to end recording.", MACRO_MAX_COMMANDS),
)
.footer(
CreateEmbedFooter::new(
"Any commands performed during recording won't take any actual action- they are only captured for the macro"
)
)
.color(*THEME_COLOR),
),
2024-02-06 20:08:59 +00:00
)
.await;
2024-01-06 19:48:17 +00:00
} else {
match RecordedCommand::from_context(app_ctx) {
Some(recorded) => {
command_macro.commands.push(recorded);
2022-02-19 22:11:21 +00:00
let _ = ctx
.send(
2025-06-18 22:08:32 +01:00
CreateReply::default().ephemeral(true).embed(
CreateEmbed::new()
.title("💾 Currently recording macro")
.description(
"Command recorded. Use `/macro finish` to end recording.",
)
.footer(
CreateEmbedFooter::new(
"Any commands performed during recording won't take any actual action- they are only captured for the macro"
)
)
.color(*THEME_COLOR),
),
)
.await;
}
None => {
let _ = ctx
.send(
2025-06-18 22:08:32 +01:00
CreateReply::default().ephemeral(true).embed(
CreateEmbed::new()
.title("💾 Currently recording macro")
.description(
"This command is not supported in macros, so it hasn't been recorded. Use `/macro finish` to end recording.",
)
.footer(
CreateEmbedFooter::new(
"Any commands performed during recording won't take any actual action- they are only captured for the macro"
)
)
.color(*THEME_COLOR),
),
)
.await;
}
}
}
2024-01-06 19:48:17 +00:00
return false;
2022-02-19 14:32:03 +00:00
}
}
}
}
true
}
2022-02-19 14:32:03 +00:00
async fn check_self_permissions(ctx: Context<'_>) -> bool {
2024-02-09 17:03:04 +00:00
let user_id = ctx.serenity_context().cache.current_user().id;
2024-06-12 17:44:55 +01:00
let app_permissions = match ctx {
Context::Application(app_ctx) => app_ctx.interaction.app_permissions,
_ => None,
};
2024-02-09 17:03:04 +00:00
match ctx.guild().map(|g| g.to_owned()) {
Some(guild) => {
let manage_webhooks = guild
.member(&ctx, user_id)
.await
.map_or(false, |m| m.permissions(&ctx).map_or(false, |p| p.manage_webhooks()));
2024-06-12 17:44:55 +01:00
if let Some(permissions) = app_permissions {
return if permissions.send_messages()
&& permissions.embed_links()
&& manage_webhooks
2025-06-18 22:08:32 +01:00
&& permissions.view_channel()
2024-06-12 17:44:55 +01:00
{
true
} else {
let _ = ctx
.send(CreateReply::default().content(format!(
"The bot appears to be missing some permissions:
2025-06-18 22:08:32 +01:00
{} **View Channels**
{} **Send Message**
{} **Embed Links**
2024-06-12 17:44:55 +01:00
{} **Manage Webhooks**
2025-06-18 22:08:32 +01:00
Please check the bot's roles, and any channel overrides. Alternatively, giving the bot \"Administrator\" will bypass permission checks",
if permissions.view_channel() { "" } else { "" },
2024-06-12 17:44:55 +01:00
if permissions.send_messages() { "" } else { "" },
if permissions.embed_links() { "" } else { "" },
if manage_webhooks { "" } else { "" },
)))
.await;
false
};
}
2024-06-12 17:44:55 +01:00
manage_webhooks
}
2025-06-18 22:08:32 +01:00
None => true,
}
}
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)
}