fixed an overflow issue. perm checks on slash cmds

This commit is contained in:
jellywx 2021-06-11 14:47:13 +01:00
parent 1f61b72cc5
commit b8bbfbfade
3 changed files with 65 additions and 11 deletions

View File

@ -171,6 +171,14 @@ SELECT name, id, plays, public, server_id, uploader_id
} }
async fn interaction_create(&self, ctx: Context, interaction: Interaction) { async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
// let framework = ctx
.data
.read()
.await
.get::<RegexFramework>()
.cloned()
.expect("RegexFramework not found in context");
framework.execute(ctx, interaction).await;
} }
} }

View File

@ -27,6 +27,7 @@ use regex::{Match, Regex, RegexBuilder};
use std::{collections::HashMap, env, fmt}; use std::{collections::HashMap, env, fmt};
use crate::{guild_data::CtxGuildData, MySQL}; use crate::{guild_data::CtxGuildData, MySQL};
use serenity::model::prelude::InteractionType;
use std::sync::Arc; use std::sync::Arc;
type CommandFn = for<'fut> fn( type CommandFn = for<'fut> fn(
@ -142,7 +143,11 @@ impl CommandInvoke for Interaction {
} }
async fn guild(&self, cache: Arc<Cache>) -> Option<Guild> { async fn guild(&self, cache: Arc<Cache>) -> Option<Guild> {
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 { fn author_id(&self) -> UserId {
@ -403,6 +408,46 @@ impl RegexFramework {
info!("{} slash commands built! Ready to go", count); 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 { enum PermissionCheck {

View File

@ -315,9 +315,9 @@ async fn help(
}; };
invoke invoke
.channel_id() .respond(
.send_message(&ctx, |m| { ctx.http.clone(),
m.embed(|e| { CreateGenericResponse::new().embed(|e| {
e.title("Help") e.title("Help")
.color(THEME_COLOR) .color(THEME_COLOR)
.description(description) .description(description)
@ -327,8 +327,8 @@ async fn help(
.field("Settings", "`prefix` `roles` `volume` `allow_greet`", false) .field("Settings", "`prefix` `roles` `volume` `allow_greet`", false)
.field("Search", "`search` `random` `popular`", false) .field("Search", "`search` `random` `popular`", false)
.field("Other", "`greet` `ambience`", false) .field("Other", "`greet` `ambience`", false)
}) }),
}) )
.await?; .await?;
} else { } else {
let body = match args.rest().to_lowercase().as_str() { let body = match args.rest().to_lowercase().as_str() {
@ -415,10 +415,11 @@ Please select a category from the following:
}; };
invoke invoke
.channel_id() .respond(
.send_message(&ctx, |m| { ctx.http.clone(),
m.embed(|e| e.title("Help").color(THEME_COLOR).description(body)) CreateGenericResponse::new()
}) .embed(|e| e.title("Help").color(THEME_COLOR).description(body)),
)
.await?; .await?;
} }