Skip to content

Commit

Permalink
testing perms for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
trent-001 committed Jul 23, 2024
1 parent 10b3a80 commit d22e354
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
12 changes: 11 additions & 1 deletion events/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AddButtonDataTwitch, commands, discord } from "..";
import { db } from "../db";
import * as schema from "../db/schema";
import { randomUUID } from "crypto";
import { checkPermissions, PermissionResult } from "../lib/checkPermissions";
discord.on(
Events.InteractionCreate,
async (interaction: Interaction): Promise<any> => {
Expand Down Expand Up @@ -87,7 +88,16 @@ discord.on(

if (!command) return;
try {
command.execute(interaction as ChatInputCommandInteraction);
const permissionsCheck: PermissionResult = await checkPermissions(
command,
interaction
);

if (permissionsCheck.result) {
command.execute(interaction as ChatInputCommandInteraction);
} else {
throw new Error(permissionsCheck.missing.join(", "));
}
} catch (error: any) {
console.error(error);
if (error.message.includes("permissions")) {
Expand Down
22 changes: 22 additions & 0 deletions lib/checkPermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ChatInputCommandInteraction, PermissionResolvable } from "discord.js";

export interface PermissionResult {
result: boolean;
missing: string[];
}

export async function checkPermissions(
command: any,
interaction: ChatInputCommandInteraction
): Promise<PermissionResult> {
const member = await interaction.guild!.members.fetch({
user: interaction.client.user!.id,
});
const requiredPermissions = command.permissions as PermissionResolvable[];

if (!command.permissions) return { result: true, missing: [] };

const missing = member.permissions.missing(requiredPermissions);

return { result: !Boolean(missing.length), missing };
}

0 comments on commit d22e354

Please sign in to comment.