From f1bfc11160d580bac6079600e9155a7912c97bcf Mon Sep 17 00:00:00 2001 From: jellywx Date: Tue, 2 Nov 2021 20:10:10 +0000 Subject: [PATCH] removed all remaining restriction code --- src/commands/moderation_cmds.rs | 11 ++-- src/commands/reminder_cmds.rs | 10 ++-- src/component_models/mod.rs | 1 - src/framework.rs | 7 --- src/hooks.rs | 96 +-------------------------------- 5 files changed, 10 insertions(+), 115 deletions(-) diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index be659ea..aecfffd 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -2,16 +2,13 @@ use chrono::offset::Utc; use chrono_tz::{Tz, TZ_VARIANTS}; use levenshtein::levenshtein; use regex_command_attr::command; -use serenity::{client::Context, model::misc::Mentionable}; +use serenity::client::Context; use crate::{ - component_models::{ - pager::{MacroPager, Pager}, - ComponentDataModel, Restrict, - }, + component_models::pager::{MacroPager, Pager}, consts::{EMBED_DESCRIPTION_MAX_LENGTH, THEME_COLOR}, framework::{CommandInvoke, CommandOptions, CreateGenericResponse, OptionValue}, - hooks::{CHECK_GUILD_PERMISSIONS_HOOK, CHECK_MANAGED_PERMISSIONS_HOOK}, + hooks::CHECK_GUILD_PERMISSIONS_HOOK, models::{command_macro::CommandMacro, CtxData}, PopularTimezones, RecordingMacros, RegexFramework, SQLPool, }; @@ -149,7 +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(CHECK_MANAGED_PERMISSIONS_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/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index e24fbd8..e006c81 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -17,7 +17,7 @@ use crate::{ }, consts::{EMBED_DESCRIPTION_MAX_LENGTH, REGEX_CHANNEL_USER, SELECT_MAX_ENTRIES, THEME_COLOR}, framework::{CommandInvoke, CommandOptions, CreateGenericResponse, OptionValue}, - hooks::{CHECK_GUILD_PERMISSIONS_HOOK, CHECK_MANAGED_PERMISSIONS_HOOK}, + hooks::CHECK_GUILD_PERMISSIONS_HOOK, models::{ reminder::{ builder::{MultiReminderBuilder, ReminderScope}, @@ -258,7 +258,7 @@ async fn nudge(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) kind = "Boolean", required = false )] -#[hook(CHECK_MANAGED_PERMISSIONS_HOOK)] +#[hook(CHECK_GUILD_PERMISSIONS_HOOK)] async fn look(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) { let pool = ctx.data.read().await.get::().cloned().unwrap(); @@ -351,7 +351,7 @@ async fn look(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) { #[command("del")] #[description("Delete reminders")] -#[hook(CHECK_MANAGED_PERMISSIONS_HOOK)] +#[hook(CHECK_GUILD_PERMISSIONS_HOOK)] async fn delete(ctx: &Context, invoke: &mut CommandInvoke, _args: CommandOptions) { let timezone = ctx.timezone(invoke.author_id()).await; @@ -497,7 +497,7 @@ pub fn show_delete_page( #[subcommand("delete")] #[description("Delete a timer")] #[arg(name = "name", description = "Name of the timer to delete", kind = "String", required = true)] -#[hook(CHECK_MANAGED_PERMISSIONS_HOOK)] +#[hook(CHECK_GUILD_PERMISSIONS_HOOK)] async fn timer(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) { fn time_difference(start_time: NaiveDateTime) -> String { let unix_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64; @@ -663,7 +663,7 @@ DELETE FROM timers WHERE owner = ? AND name = ? kind = "Boolean", required = false )] -#[hook(CHECK_MANAGED_PERMISSIONS_HOOK)] +#[hook(CHECK_GUILD_PERMISSIONS_HOOK)] async fn remind(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) { invoke.defer(&ctx).await; diff --git a/src/component_models/mod.rs b/src/component_models/mod.rs index 3632d1b..baf925d 100644 --- a/src/component_models/mod.rs +++ b/src/component_models/mod.rs @@ -10,7 +10,6 @@ use serenity::{ client::Context, model::{ channel::Channel, - id::{GuildId, RoleId, UserId}, interactions::{message_component::MessageComponentInteraction, InteractionResponseType}, prelude::InteractionApplicationCommandCallbackDataFlags, }, diff --git a/src/framework.rs b/src/framework.rs index 2876b32..970105f 100644 --- a/src/framework.rs +++ b/src/framework.rs @@ -156,13 +156,6 @@ impl CommandInvoke { } } - pub fn member(&self) -> Option { - match &self.model { - InvokeModel::Slash(i) => i.member.clone(), - InvokeModel::Component(i) => i.member.clone(), - } - } - pub async fn respond( &mut self, http: impl AsRef, diff --git a/src/hooks.rs b/src/hooks.rs index 9354166..00336bf 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -4,7 +4,7 @@ use serenity::{client::Context, model::channel::Channel}; use crate::{ framework::{CommandInvoke, CommandOptions, CreateGenericResponse, HookResult}, - moderation_cmds, RecordingMacros, SQLPool, + moderation_cmds, RecordingMacros, }; #[check] @@ -103,100 +103,6 @@ pub async fn check_self_permissions( } } -#[check] -pub async fn check_managed_permissions( - ctx: &Context, - invoke: &mut CommandInvoke, - args: &CommandOptions, -) -> HookResult { - if let Some(guild) = invoke.guild(&ctx) { - let permissions = guild.member_permissions(&ctx, invoke.author_id()).await.unwrap(); - - if permissions.manage_messages() { - return HookResult::Continue; - } - - let member = invoke.member().unwrap(); - - let pool = ctx - .data - .read() - .await - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - match sqlx::query!( - " -SELECT - role -FROM - roles -INNER JOIN - command_restrictions ON roles.id = command_restrictions.role_id -WHERE - command_restrictions.command = ? AND - roles.guild_id = ( - SELECT - id - FROM - guilds - WHERE - guild = ?) - ", - args.command, - guild.id.as_u64() - ) - .fetch_all(&pool) - .await - { - Ok(rows) => { - let role_ids = member.roles.iter().map(|r| *r.as_u64()).collect::>(); - - for row in rows { - if role_ids.contains(&row.role) { - return HookResult::Continue; - } - } - - let _ = invoke - .respond( - &ctx, - CreateGenericResponse::new().content( - "You must have \"Manage Messages\" or have a role capable of sending reminders to that channel. \ -Please talk to your server admin, and ask them to use the `/restrict` command to specify allowed roles.", - ), - ) - .await; - - HookResult::Halt - } - - Err(sqlx::Error::RowNotFound) => { - let _ = invoke - .respond( - &ctx, - CreateGenericResponse::new().content( - "You must have \"Manage Messages\" or have a role capable of sending reminders to that channel. \ -Please talk to your server admin, and ask them to use the `/restrict` command to specify allowed roles.", - ), - ) - .await; - - HookResult::Halt - } - - Err(e) => { - warn!("Unexpected error occurred querying command_restrictions: {:?}", e); - - HookResult::Halt - } - } - } else { - HookResult::Continue - } -} - #[check] pub async fn check_guild_permissions( ctx: &Context,