removed all remaining restriction code

This commit is contained in:
jellywx 2021-11-02 20:10:10 +00:00
parent 72228911f2
commit f1bfc11160
5 changed files with 10 additions and 115 deletions

View File

@ -2,16 +2,13 @@ use chrono::offset::Utc;
use chrono_tz::{Tz, TZ_VARIANTS}; use chrono_tz::{Tz, TZ_VARIANTS};
use levenshtein::levenshtein; use levenshtein::levenshtein;
use regex_command_attr::command; use regex_command_attr::command;
use serenity::{client::Context, model::misc::Mentionable}; use serenity::client::Context;
use crate::{ use crate::{
component_models::{ component_models::pager::{MacroPager, Pager},
pager::{MacroPager, Pager},
ComponentDataModel, Restrict,
},
consts::{EMBED_DESCRIPTION_MAX_LENGTH, THEME_COLOR}, consts::{EMBED_DESCRIPTION_MAX_LENGTH, THEME_COLOR},
framework::{CommandInvoke, CommandOptions, CreateGenericResponse, OptionValue}, framework::{CommandInvoke, CommandOptions, CreateGenericResponse, OptionValue},
hooks::{CHECK_GUILD_PERMISSIONS_HOOK, CHECK_MANAGED_PERMISSIONS_HOOK}, hooks::CHECK_GUILD_PERMISSIONS_HOOK,
models::{command_macro::CommandMacro, CtxData}, models::{command_macro::CommandMacro, CtxData},
PopularTimezones, RecordingMacros, RegexFramework, SQLPool, 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")] #[description("Delete a recorded macro")]
#[arg(name = "name", description = "Name of the macro to delete", kind = "String", required = true)] #[arg(name = "name", description = "Name of the macro to delete", kind = "String", required = true)]
#[supports_dm(false)] #[supports_dm(false)]
#[hook(CHECK_MANAGED_PERMISSIONS_HOOK)] #[hook(CHECK_GUILD_PERMISSIONS_HOOK)]
async fn macro_cmd(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) { async fn macro_cmd(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) {
let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap(); let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap();

View File

@ -17,7 +17,7 @@ use crate::{
}, },
consts::{EMBED_DESCRIPTION_MAX_LENGTH, REGEX_CHANNEL_USER, SELECT_MAX_ENTRIES, THEME_COLOR}, consts::{EMBED_DESCRIPTION_MAX_LENGTH, REGEX_CHANNEL_USER, SELECT_MAX_ENTRIES, THEME_COLOR},
framework::{CommandInvoke, CommandOptions, CreateGenericResponse, OptionValue}, framework::{CommandInvoke, CommandOptions, CreateGenericResponse, OptionValue},
hooks::{CHECK_GUILD_PERMISSIONS_HOOK, CHECK_MANAGED_PERMISSIONS_HOOK}, hooks::CHECK_GUILD_PERMISSIONS_HOOK,
models::{ models::{
reminder::{ reminder::{
builder::{MultiReminderBuilder, ReminderScope}, builder::{MultiReminderBuilder, ReminderScope},
@ -258,7 +258,7 @@ async fn nudge(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions)
kind = "Boolean", kind = "Boolean",
required = false required = false
)] )]
#[hook(CHECK_MANAGED_PERMISSIONS_HOOK)] #[hook(CHECK_GUILD_PERMISSIONS_HOOK)]
async fn look(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) { async fn look(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) {
let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap(); let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap();
@ -351,7 +351,7 @@ async fn look(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) {
#[command("del")] #[command("del")]
#[description("Delete reminders")] #[description("Delete reminders")]
#[hook(CHECK_MANAGED_PERMISSIONS_HOOK)] #[hook(CHECK_GUILD_PERMISSIONS_HOOK)]
async fn delete(ctx: &Context, invoke: &mut CommandInvoke, _args: CommandOptions) { async fn delete(ctx: &Context, invoke: &mut CommandInvoke, _args: CommandOptions) {
let timezone = ctx.timezone(invoke.author_id()).await; let timezone = ctx.timezone(invoke.author_id()).await;
@ -497,7 +497,7 @@ pub fn show_delete_page(
#[subcommand("delete")] #[subcommand("delete")]
#[description("Delete a timer")] #[description("Delete a timer")]
#[arg(name = "name", description = "Name of the timer to delete", kind = "String", required = true)] #[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) { async fn timer(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) {
fn time_difference(start_time: NaiveDateTime) -> String { fn time_difference(start_time: NaiveDateTime) -> String {
let unix_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64; 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", kind = "Boolean",
required = false required = false
)] )]
#[hook(CHECK_MANAGED_PERMISSIONS_HOOK)] #[hook(CHECK_GUILD_PERMISSIONS_HOOK)]
async fn remind(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) { async fn remind(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) {
invoke.defer(&ctx).await; invoke.defer(&ctx).await;

View File

@ -10,7 +10,6 @@ use serenity::{
client::Context, client::Context,
model::{ model::{
channel::Channel, channel::Channel,
id::{GuildId, RoleId, UserId},
interactions::{message_component::MessageComponentInteraction, InteractionResponseType}, interactions::{message_component::MessageComponentInteraction, InteractionResponseType},
prelude::InteractionApplicationCommandCallbackDataFlags, prelude::InteractionApplicationCommandCallbackDataFlags,
}, },

View File

@ -156,13 +156,6 @@ impl CommandInvoke {
} }
} }
pub fn member(&self) -> Option<Member> {
match &self.model {
InvokeModel::Slash(i) => i.member.clone(),
InvokeModel::Component(i) => i.member.clone(),
}
}
pub async fn respond( pub async fn respond(
&mut self, &mut self,
http: impl AsRef<Http>, http: impl AsRef<Http>,

View File

@ -4,7 +4,7 @@ use serenity::{client::Context, model::channel::Channel};
use crate::{ use crate::{
framework::{CommandInvoke, CommandOptions, CreateGenericResponse, HookResult}, framework::{CommandInvoke, CommandOptions, CreateGenericResponse, HookResult},
moderation_cmds, RecordingMacros, SQLPool, moderation_cmds, RecordingMacros,
}; };
#[check] #[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::<SQLPool>()
.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::<Vec<u64>>();
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] #[check]
pub async fn check_guild_permissions( pub async fn check_guild_permissions(
ctx: &Context, ctx: &Context,