From 46da0ffb23ec3fbe3b646e15a1a99254635a5046 Mon Sep 17 00:00:00 2001 From: Taken Date: Thu, 21 Aug 2025 16:53:24 +0200 Subject: [PATCH] Added role based perm checks --- src/commands/anime.ts | 1 + src/commands/ban.ts | 7 +----- src/commands/botinfo.ts | 1 + src/commands/check.ts | 1 + src/commands/clear.ts | 1 + src/commands/config.ts | 1 + src/commands/find.ts | 1 + src/commands/forceunverify.ts | 1 + src/commands/forceupdate.ts | 1 + src/commands/forceverify.ts | 1 + src/commands/guild.ts | 1 + src/commands/help.ts | 1 + src/commands/kick.ts | 7 +----- src/commands/ping.ts | 1 + src/commands/pp.ts | 1 + src/commands/remove.ts | 1 + src/commands/reqs.ts | 1 + src/commands/send.ts | 1 + src/commands/setnick.ts | 1 + src/commands/setup.ts | 3 ++- src/commands/slowmode.ts | 1 + src/commands/staff.ts | 1 + src/commands/timeout.ts | 1 + src/commands/unban.ts | 1 + src/commands/update.ts | 1 + src/commands/uuid.ts | 1 + src/commands/verify.ts | 1 + src/commands/whoami.ts | 1 + src/commands/whois.ts | 1 + src/typings/Discord.ts | 1 + src/utils/Events/command.ts | 40 ++++++++++++++++++++++++++++++++++- src/utils/Functions/staff.ts | 14 ++++++++++++ 32 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 src/utils/Functions/staff.ts diff --git a/src/commands/anime.ts b/src/commands/anime.ts index 1b12721..e649f7b 100644 --- a/src/commands/anime.ts +++ b/src/commands/anime.ts @@ -8,6 +8,7 @@ export default { description: "Anime subcommands", public: true, dev: false, + requiredRole: "none", data: new SlashCommandBuilder() .setName("anime") diff --git a/src/commands/ban.ts b/src/commands/ban.ts index 815db09..6ba3e2f 100644 --- a/src/commands/ban.ts +++ b/src/commands/ban.ts @@ -9,6 +9,7 @@ export default { description: "Ban a user", dev: false, public: false, + requiredRole: "admin", data: new SlashCommandBuilder() .setName("ban") @@ -55,12 +56,6 @@ export default { const mod = await interaction.guild!.members.fetch(interaction.user.id) const memberRoles = member.roles.cache.map(role => role.id) - const modRoles = mod.roles.cache.map(role => role.id) - - if (!modRoles.includes(admin)) { - await interaction.editReply("You do not have permission to use this command.") - return - } if (interaction.guild!.members.me!.roles.highest.position <= member.roles.highest.position) { await interaction.editReply("I cannot ban this member.") diff --git a/src/commands/botinfo.ts b/src/commands/botinfo.ts index 85fd515..58c015b 100644 --- a/src/commands/botinfo.ts +++ b/src/commands/botinfo.ts @@ -11,6 +11,7 @@ export default { description: "Get information about the bot", dev: false, public: true, + requiredRole: "none", data: new SlashCommandBuilder() .setName("botinfo") diff --git a/src/commands/check.ts b/src/commands/check.ts index b39cf43..a457482 100644 --- a/src/commands/check.ts +++ b/src/commands/check.ts @@ -10,6 +10,7 @@ export default { description: "Check a player's stats.", dev: false, public: true, + requiredRole: "none", data: new SlashCommandBuilder() .setName("check") diff --git a/src/commands/clear.ts b/src/commands/clear.ts index 1b64d72..7745a45 100644 --- a/src/commands/clear.ts +++ b/src/commands/clear.ts @@ -8,6 +8,7 @@ export default { description: "Clears messages", dev: false, public: false, + requiredRole: "admin", data: new SlashCommandBuilder() .setName("clear") diff --git a/src/commands/config.ts b/src/commands/config.ts index 7d36a51..61cbdcc 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -8,6 +8,7 @@ export default { description: "Configure the bot", dev: false, public: false, + requiredRole: "superadmin", data: new SlashCommandBuilder() .setName("config") diff --git a/src/commands/find.ts b/src/commands/find.ts index 0985760..9543a44 100644 --- a/src/commands/find.ts +++ b/src/commands/find.ts @@ -9,6 +9,7 @@ export default { description: "Find a person by the ign", dev: false, public: false, + requiredRole: "helper", data: new SlashCommandBuilder() .setName("find") diff --git a/src/commands/forceunverify.ts b/src/commands/forceunverify.ts index d0ef0db..1ae3ac2 100644 --- a/src/commands/forceunverify.ts +++ b/src/commands/forceunverify.ts @@ -11,6 +11,7 @@ export default { description: "Force unverify a user", dev: false, public: false, + requiredRole: "superadmin", data: new SlashCommandBuilder() .setName("forceunverify") diff --git a/src/commands/forceupdate.ts b/src/commands/forceupdate.ts index e4535d6..78de211 100644 --- a/src/commands/forceupdate.ts +++ b/src/commands/forceupdate.ts @@ -12,6 +12,7 @@ export default { description: "Force update the user", dev: false, public: false, + requiredRole: "helper", data: new SlashCommandBuilder() .setName("forceupdate") diff --git a/src/commands/forceverify.ts b/src/commands/forceverify.ts index 5f8c5a1..4ad8610 100644 --- a/src/commands/forceverify.ts +++ b/src/commands/forceverify.ts @@ -13,6 +13,7 @@ export default { description: "Force verify a user.", dev: false, public: false, + requiredRole: "admin", data: new SlashCommandBuilder() .setName("forceverify") diff --git a/src/commands/guild.ts b/src/commands/guild.ts index c5a03e5..cdca41c 100644 --- a/src/commands/guild.ts +++ b/src/commands/guild.ts @@ -11,6 +11,7 @@ export default { dev: false, public: true, subcommands: true, + requiredRole: "none", data: new SlashCommandBuilder() .setName("guild") diff --git a/src/commands/help.ts b/src/commands/help.ts index d4f3d46..066815f 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -7,6 +7,7 @@ export default { description: "Help command", dev: false, public: true, + requiredRole: "none", data: new SlashCommandBuilder() .setName("help") diff --git a/src/commands/kick.ts b/src/commands/kick.ts index f649652..3f70c55 100644 --- a/src/commands/kick.ts +++ b/src/commands/kick.ts @@ -9,6 +9,7 @@ export default { description: "Kick a member from the server.", dev: false, public: false, + requiredRole: "helper", data: new SlashCommandBuilder() .setName("kick") @@ -34,12 +35,6 @@ export default { const reason = interaction.options.getString("reason") ?? "No reason provided." const mod = await interaction.guild!.members.fetch(interaction.user.id) const memberRoles = member.roles.cache.map(role => role.id) - const modRoles = mod.roles.cache.map(role => role.id) - - if (!modRoles.includes(helper) && !modRoles.includes(admin)) { - await interaction.editReply("You do not have permission to use this command.") - return - } if (member.id === interaction.applicationId) { await interaction.editReply("I cannot kick myself.") diff --git a/src/commands/ping.ts b/src/commands/ping.ts index 97f3907..5280740 100644 --- a/src/commands/ping.ts +++ b/src/commands/ping.ts @@ -7,6 +7,7 @@ export default { description: "Get the bot's ping.", dev: false, public: true, + requiredRole: "none", data: new SlashCommandBuilder() .setName("ping") diff --git a/src/commands/pp.ts b/src/commands/pp.ts index c9da0a8..e498d7a 100644 --- a/src/commands/pp.ts +++ b/src/commands/pp.ts @@ -8,6 +8,7 @@ export default { description: "Shows pp size", public: true, dev: false, + requiredRole: "none", data: new SlashCommandBuilder() .setName("pp") diff --git a/src/commands/remove.ts b/src/commands/remove.ts index df9fbc4..8315675 100644 --- a/src/commands/remove.ts +++ b/src/commands/remove.ts @@ -10,6 +10,7 @@ export default { description: "Remove a person on the waiting list.", dev: false, public: false, + requiredRole: "helper", data: new SlashCommandBuilder() .setName("remove") diff --git a/src/commands/reqs.ts b/src/commands/reqs.ts index ac69817..a780f9f 100644 --- a/src/commands/reqs.ts +++ b/src/commands/reqs.ts @@ -8,6 +8,7 @@ export default { description: "Displays the requirements for the guild.", dev: false, public: true, + requiredRole: "none", data: new SlashCommandBuilder() .setName("reqs") diff --git a/src/commands/send.ts b/src/commands/send.ts index c5077ce..845215b 100644 --- a/src/commands/send.ts +++ b/src/commands/send.ts @@ -7,6 +7,7 @@ export default { description: "Send a message to a channel.", dev: false, public: false, + requiredRole: "superadmin", data: new SlashCommandBuilder() .setName("send") diff --git a/src/commands/setnick.ts b/src/commands/setnick.ts index 5dba861..7aa7d19 100644 --- a/src/commands/setnick.ts +++ b/src/commands/setnick.ts @@ -8,6 +8,7 @@ export default { description: "Set your nickname", dev: false, public: false, + requiredRole: "admin", data: new SlashCommandBuilder() .setName("setnick") diff --git a/src/commands/setup.ts b/src/commands/setup.ts index b978354..54e331d 100644 --- a/src/commands/setup.ts +++ b/src/commands/setup.ts @@ -15,9 +15,10 @@ import { ICommand } from "~/typings" export default { name: "setup", description: "Used for setup of the bot.", - dev: true, + dev: false, public: false, subcommands: true, + requiredRole: "superadmin", data: new SlashCommandBuilder() .setName("setup") diff --git a/src/commands/slowmode.ts b/src/commands/slowmode.ts index eaa891a..024aac5 100644 --- a/src/commands/slowmode.ts +++ b/src/commands/slowmode.ts @@ -17,6 +17,7 @@ export default { description: "Set the slowmode of a channel.", dev: false, public: false, + requiredRole: "admin", data: new SlashCommandBuilder() .setName("slowmode") diff --git a/src/commands/staff.ts b/src/commands/staff.ts index 85a11dd..3793188 100644 --- a/src/commands/staff.ts +++ b/src/commands/staff.ts @@ -13,6 +13,7 @@ export default { dev: false, public: false, subcommands: true, + requiredRole: "helper", data: new SlashCommandBuilder() .setName("staff") diff --git a/src/commands/timeout.ts b/src/commands/timeout.ts index a6c0f78..ade4e46 100644 --- a/src/commands/timeout.ts +++ b/src/commands/timeout.ts @@ -10,6 +10,7 @@ export default { description: "Times out a memeber", dev: false, public: false, + requiredRole: "helper", data: new SlashCommandBuilder() .setName("timeout") diff --git a/src/commands/unban.ts b/src/commands/unban.ts index 6a5c633..0331b37 100644 --- a/src/commands/unban.ts +++ b/src/commands/unban.ts @@ -9,6 +9,7 @@ export default { description: "Unban a user from the server", dev: false, public: false, + requiredRole: "admin", data: new SlashCommandBuilder() .setName("unban") diff --git a/src/commands/update.ts b/src/commands/update.ts index 325f63e..ab5fdc5 100644 --- a/src/commands/update.ts +++ b/src/commands/update.ts @@ -12,6 +12,7 @@ export default { description: "Update your guild rank.", dev: false, public: true, + requiredRole: "none", data: new SlashCommandBuilder() .setName("update") diff --git a/src/commands/uuid.ts b/src/commands/uuid.ts index 34a8a23..721dd5f 100644 --- a/src/commands/uuid.ts +++ b/src/commands/uuid.ts @@ -9,6 +9,7 @@ export default { description: "Get a player's UUID", dev: false, public: true, + requiredRole: "none", data: new SlashCommandBuilder() .setName("uuid") diff --git a/src/commands/verify.ts b/src/commands/verify.ts index 5811824..643ee55 100644 --- a/src/commands/verify.ts +++ b/src/commands/verify.ts @@ -12,6 +12,7 @@ export default { description: "Verify yourself as a member of the server.", dev: false, public: true, + requiredRole: "none", data: new SlashCommandBuilder() .setName("verify") diff --git a/src/commands/whoami.ts b/src/commands/whoami.ts index fe90508..211a183 100644 --- a/src/commands/whoami.ts +++ b/src/commands/whoami.ts @@ -9,6 +9,7 @@ export default { description: "Get your user info", public: true, dev: false, + requiredRole: "none", data: new SlashCommandBuilder() .setName("whoami") diff --git a/src/commands/whois.ts b/src/commands/whois.ts index 3dad50b..876ef49 100644 --- a/src/commands/whois.ts +++ b/src/commands/whois.ts @@ -9,6 +9,7 @@ export default { description: "Get's the ign of a user.", dev: false, public: false, + requiredRole: "helper", data: new SlashCommandBuilder() .setName("whois") diff --git a/src/typings/Discord.ts b/src/typings/Discord.ts index 07ba8d6..e1de7a0 100644 --- a/src/typings/Discord.ts +++ b/src/typings/Discord.ts @@ -16,6 +16,7 @@ export interface ICommand { dev?: boolean public: boolean subcommands?: boolean + requiredRole?: "none" | "helper" | "admin" | "superadmin" data: SlashCommandBuilder execute: (arg: { interaction: ChatInputCommandInteraction, client: ExtendedClient }) => Promise } diff --git a/src/utils/Events/command.ts b/src/utils/Events/command.ts index ddde10b..bccd86d 100644 --- a/src/utils/Events/command.ts +++ b/src/utils/Events/command.ts @@ -1,10 +1,11 @@ -import { Events, MessageFlags } from "discord.js" +import { Events, GuildMember, MessageFlags } from "discord.js" import fs from "fs" import path from "path" import { embedColor } from "~/config/options" import { ICommand, LoadEventsOptions } from "~/typings" import { ExtendedClient as Client } from "~/utils/Client" import logToChannel from "~/utils/Functions/logtochannel" +import { isAdmin, isHelper, isSuperAdmin } from "../Functions/staff" import tryCatch from "../Functions/trycatch" import { log } from "../Logger" @@ -33,6 +34,43 @@ export default async function loadSlashCommandsEvents(client: Client, { ft, dir return } + if (!command.requiredRole && !isSuperAdmin(interaction.member as GuildMember)) { + await interaction.reply({ + content: "You do not have permission to use this command.", + flags: MessageFlags.Ephemeral + }) + return + } + + switch (command.requiredRole) { + case "superadmin": + if (!isSuperAdmin(interaction.member as GuildMember)) { + await interaction.reply({ + content: "You do not have permission to use this command.", + flags: MessageFlags.Ephemeral + }) + } + break + case "admin": + if (!isAdmin(interaction.member as GuildMember)) { + await interaction.reply({ + content: "You do not have permission to use this command.", + flags: MessageFlags.Ephemeral + }) + } + break + case "helper": + if (!isHelper(interaction.member as GuildMember)) { + await interaction.reply({ + content: "You do not have permission to use this command.", + flags: MessageFlags.Ephemeral + }) + } + break + case "none": + break + } + const [error] = await tryCatch(command.execute({ interaction, client })) if (!error) return diff --git a/src/utils/Functions/staff.ts b/src/utils/Functions/staff.ts new file mode 100644 index 0000000..abfe597 --- /dev/null +++ b/src/utils/Functions/staff.ts @@ -0,0 +1,14 @@ +import { GuildMember, PermissionFlagsBits } from "discord.js" +import { admin, helper } from "~/config/roles" + +export function isSuperAdmin(member: GuildMember) { + return member.permissions.has(PermissionFlagsBits.Administrator) +} + +export function isAdmin(member: GuildMember) { + return member.roles.cache.has(admin) || member.permissions.has(PermissionFlagsBits.Administrator) +} + +export function isHelper(member: GuildMember) { + return member.roles.cache.has(helper) || member.roles.cache.has(admin) || member.permissions.has(PermissionFlagsBits.Administrator) +}