Skip to content

Commit

Permalink
refactor: permission checks to role (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinbengeorge authored Mar 19, 2024
1 parent 99b4305 commit 421b925
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ DISCORD_TOKEN=<string: 'Discord Bot Token'>
GUILD_ID=<string: 'Discord Server ID'>
DISCORD_CLIENT_ID=<string: 'Discord App Client ID'>
DATABASE_URI=<string: 'MongoDB URI'>
MOD_CHANNEL_ID=<string 'Channel ID'>
MOD_CHANNEL_ID=<string 'Channel ID'>
MOD_ROLE_ID=<string 'Mod Role ID'>
2 changes: 0 additions & 2 deletions src/commands/announce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SlashCommandChannelOption,
TextInputBuilder,
TextInputStyle,
PermissionFlagsBits,
} from "discord.js";
import { Command } from "../interface";

Expand All @@ -15,7 +14,6 @@ export default {
.setName("announce")
.setDescription("announcement the world something")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.addChannelOption((option: SlashCommandChannelOption) => {
return option
.setName("channel")
Expand Down
2 changes: 0 additions & 2 deletions src/commands/echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SlashCommandChannelOption,
TextInputBuilder,
TextInputStyle,
PermissionFlagsBits,
} from "discord.js";
import { Command } from "../interface";

Expand All @@ -15,7 +14,6 @@ export default {
.setName("echo")
.setDescription("Announce a message to a channel")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.addChannelOption((option: SlashCommandChannelOption) => {
return option
.setName("channel")
Expand Down
2 changes: 0 additions & 2 deletions src/commands/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SlashCommandChannelOption,
TextInputBuilder,
TextInputStyle,
PermissionFlagsBits,
} from "discord.js";
import { Command } from "../interface";

Expand All @@ -15,7 +14,6 @@ export default {
.setName("images")
.setDescription("send images")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.addChannelOption((option: SlashCommandChannelOption) => {
return option
.setName("channel")
Expand Down
2 changes: 0 additions & 2 deletions src/commands/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
StringSelectMenuBuilder,
SlashCommandSubcommandBuilder,
ChannelType,
PermissionFlagsBits,
} from "discord.js";
import { Command } from "../interface";
import db from "../utils/database";
Expand All @@ -17,7 +16,6 @@ export default {
.setName("template")
.setDescription("provides us with the templates")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addSubcommand((subcommand: SlashCommandSubcommandBuilder) =>
subcommand.setName("create").setDescription("Creates Templates"),
)
Expand Down
2 changes: 2 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const envSchema = z.object({
NODE_ENV: z.string().optional().default("development"),
DATABASE_URI: z.string(),
MOD_CHANNEL_ID: z.string(),
MOD_ROLE_ID: z.string(),
});

const env = envSchema.parse(process.env);
Expand All @@ -22,4 +23,5 @@ export default {
NODE_ENV: env.NODE_ENV,
DB_URI: env.DATABASE_URI,
MOD_CHANNEL_ID: env.MOD_CHANNEL_ID,
MOD_ROLE_ID: env.MOD_ROLE_ID,
};
11 changes: 10 additions & 1 deletion src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection, Events, Interaction } from "discord.js";
import { Collection, Events, GuildMemberRoleManager, Interaction } from "discord.js";
import { Command } from "../interface";
import env from "../config/index";

Expand All @@ -18,6 +18,7 @@ export default {

const interactionChannelId = interaction.channelId;
const envChannelId = env.MOD_CHANNEL_ID;
const modRoleId = env.MOD_ROLE_ID;
if (command.isMod && interactionChannelId !== envChannelId) {
await interaction.reply({
content: "This command can only be executed in a specific channel.",
Expand All @@ -26,6 +27,14 @@ export default {
return;
}

if (command.isMod && !(interaction.member?.roles as GuildMemberRoleManager).resolve(modRoleId)) {
await interaction.reply({
content: "You do not have the required permissions to execute this command.",
ephemeral: true,
});
return;
}

try {
command.execute(interaction);
} catch (err) {
Expand Down

0 comments on commit 421b925

Please sign in to comment.