From b8bbfbfade9c4752824e83d25bf819aac60905a5 Mon Sep 17 00:00:00 2001 From: jellywx Date: Fri, 11 Jun 2021 14:47:13 +0100 Subject: [PATCH] fixed an overflow issue. perm checks on slash cmds --- src/event_handlers.rs | 10 ++++++++- src/framework.rs | 47 ++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 19 ++++++++--------- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/src/event_handlers.rs b/src/event_handlers.rs index 35b2ddd..1c20b2e 100644 --- a/src/event_handlers.rs +++ b/src/event_handlers.rs @@ -171,6 +171,14 @@ SELECT name, id, plays, public, server_id, uploader_id } async fn interaction_create(&self, ctx: Context, interaction: Interaction) { - // + let framework = ctx + .data + .read() + .await + .get::() + .cloned() + .expect("RegexFramework not found in context"); + + framework.execute(ctx, interaction).await; } } diff --git a/src/framework.rs b/src/framework.rs index 7c8cc67..98d9550 100644 --- a/src/framework.rs +++ b/src/framework.rs @@ -27,6 +27,7 @@ use regex::{Match, Regex, RegexBuilder}; use std::{collections::HashMap, env, fmt}; use crate::{guild_data::CtxGuildData, MySQL}; +use serenity::model::prelude::InteractionType; use std::sync::Arc; type CommandFn = for<'fut> fn( @@ -142,7 +143,11 @@ impl CommandInvoke for Interaction { } async fn guild(&self, cache: Arc) -> Option { - self.guild(cache).await + if let Some(guild_id) = self.guild_id { + guild_id.to_guild_cached(cache).await + } else { + None + } } fn author_id(&self) -> UserId { @@ -403,6 +408,46 @@ impl RegexFramework { info!("{} slash commands built! Ready to go", count); } + + pub async fn execute(&self, ctx: Context, interaction: Interaction) { + if interaction.kind == InteractionType::ApplicationCommand { + let command = { + let name = &interaction.data.as_ref().unwrap().name; + + self.commands + .get(name) + .expect(&format!("Received invalid command: {}", name)) + }; + + if command + .check_permissions( + &ctx, + &interaction.guild(ctx.cache.clone()).await.unwrap(), + &interaction.member(&ctx).await.unwrap(), + ) + .await + { + (command.fun)(&ctx, &interaction, Args::new("", &[Delimiter::Single(' ')])) + .await + .unwrap(); + } else if command.required_permissions == PermissionLevel::Managed { + let _ = interaction + .respond( + ctx.http.clone(), + CreateGenericResponse::new().content("You must either be an Admin or have a role specified in `?roles` to do this command") + ) + .await; + } else if command.required_permissions == PermissionLevel::Restricted { + let _ = interaction + .respond( + ctx.http.clone(), + CreateGenericResponse::new() + .content("You must be an Admin to do this command"), + ) + .await; + } + } + } } enum PermissionCheck { diff --git a/src/main.rs b/src/main.rs index 88ceb47..195ae1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -315,9 +315,9 @@ async fn help( }; invoke - .channel_id() - .send_message(&ctx, |m| { - m.embed(|e| { + .respond( + ctx.http.clone(), + CreateGenericResponse::new().embed(|e| { e.title("Help") .color(THEME_COLOR) .description(description) @@ -327,8 +327,8 @@ async fn help( .field("Settings", "`prefix` `roles` `volume` `allow_greet`", false) .field("Search", "`search` `random` `popular`", false) .field("Other", "`greet` `ambience`", false) - }) - }) + }), + ) .await?; } else { let body = match args.rest().to_lowercase().as_str() { @@ -415,10 +415,11 @@ Please select a category from the following: }; invoke - .channel_id() - .send_message(&ctx, |m| { - m.embed(|e| e.title("Help").color(THEME_COLOR).description(body)) - }) + .respond( + ctx.http.clone(), + CreateGenericResponse::new() + .embed(|e| e.title("Help").color(THEME_COLOR).description(body)), + ) .await?; }