From ebd1efa990aa4df1fb4437a64569f157ed08b156 Mon Sep 17 00:00:00 2001 From: jellywx Date: Sat, 13 Nov 2021 22:30:18 +0000 Subject: [PATCH] added check for guild only commands --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/moderation_cmds.rs | 3 ++- src/component_models/mod.rs | 4 +++- src/hooks.rs | 20 ++++++++++++++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9156770..9fe02d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1187,7 +1187,7 @@ dependencies = [ [[package]] name = "reminder_rs" -version = "1.6.0-beta0" +version = "1.6.0-beta1" dependencies = [ "base64", "chrono", diff --git a/Cargo.toml b/Cargo.toml index eefe802..eaf93ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reminder_rs" -version = "1.6.0-beta0" +version = "1.6.0-beta1" authors = ["jellywx "] edition = "2018" diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index aecfffd..cada0e4 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -8,7 +8,7 @@ use crate::{ component_models::pager::{MacroPager, Pager}, consts::{EMBED_DESCRIPTION_MAX_LENGTH, THEME_COLOR}, framework::{CommandInvoke, CommandOptions, CreateGenericResponse, OptionValue}, - hooks::CHECK_GUILD_PERMISSIONS_HOOK, + hooks::{CHECK_GUILD_PERMISSIONS_HOOK, GUILD_ONLY_HOOK}, models::{command_macro::CommandMacro, CtxData}, PopularTimezones, RecordingMacros, RegexFramework, SQLPool, }; @@ -146,6 +146,7 @@ You may want to use one of the popular timezones below, otherwise click [here](h #[description("Delete a recorded macro")] #[arg(name = "name", description = "Name of the macro to delete", kind = "String", required = true)] #[supports_dm(false)] +#[hook(GUILD_ONLY_HOOK)] #[hook(CHECK_GUILD_PERMISSIONS_HOOK)] async fn macro_cmd(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) { let pool = ctx.data.read().await.get::().cloned().unwrap(); diff --git a/src/component_models/mod.rs b/src/component_models/mod.rs index baf925d..373e405 100644 --- a/src/component_models/mod.rs +++ b/src/component_models/mod.rs @@ -48,7 +48,9 @@ impl ComponentDataModel { } pub fn from_custom_id(data: &String) -> Self { - let buf = base64::decode(data).unwrap(); + let buf = base64::decode(data) + .map_err(|e| format!("Could not decode `custom_id' {}: {:?}", data, e)) + .unwrap(); let cur = Cursor::new(buf); rmp_serde::from_read(cur).unwrap() } diff --git a/src/hooks.rs b/src/hooks.rs index 00336bf..212d4e4 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -7,6 +7,26 @@ use crate::{ moderation_cmds, RecordingMacros, }; +#[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 + } +} + #[check] pub async fn macro_check( ctx: &Context,