Skip to content

Commit

Permalink
Merge pull request #13 from nakajimayoshi/role
Browse files Browse the repository at this point in the history
fix: Close permissions loophole on roleSelect command & update changelog
  • Loading branch information
nakajimayoshi authored May 24, 2023
2 parents e66d2e0 + 8d960a0 commit 0943d81
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# April 2023
- feat: (04/18/2023): Add new role handler & command
# May 2023
- feat: (05/25/2023): Add new role handler, update gpt command, changelog

# March 2023
- feat: (03/07/2023): Integrate slash command api & new cowsay command
Expand Down
2 changes: 1 addition & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Logger, { CommandDefinition } from "../lib";
import { cowsay } from './memes/cowsay';
import { chatgpt } from "./general/chatgpt";
import { roleSelect } from './general/role_assignment';
import { roleSelect } from './moderation/role_assignment';

export const commands: CommandDefinition[] = [
cowsay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const createMenu = (
export const roleSelect: CommandDefinition = {
name: 'roles',
description: 'Creates a messages for users to react & add roles from',
permissions: PermissionsBitField.Flags.BanMembers,
category: CommandCategory.GENERAL,
requiredPermissions: ['BanMembers'],
category: CommandCategory.MODERATION,
response: ResponseType.STATIC,

interaction: async (interaction) => {
Expand Down
8 changes: 6 additions & 2 deletions src/handlers/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
StringSelectMenuBuilder,
Colors
} from 'discord.js';
import { regionEmbed, techEmbed, createMenu } from '../commands/general/role_assignment';
import { regionRoles, techRoles } from '../constants';
import { regionEmbed, techEmbed, createMenu } from '../commands/moderation/role_assignment';
import { regionRoles, techRoles, Channels } from '../constants';

const errorEmbed = new EmbedBuilder({
title: 'Error',
Expand All @@ -16,6 +16,10 @@ const errorEmbed = new EmbedBuilder({
})

export const roleHandler = async (interaction: any) => {
if(interaction.channel.id !== Channels.ROLE_SELECT) {
return;
}

const regionSelectMenu = createMenu(
'regionRole',
'Select a region',
Expand Down
27 changes: 24 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Client, Partials, Colors, CacheType, Interaction} from 'discord.js';
import dotenv from 'dotenv';
import Logger, { createCommand } from './lib';
import Logger, {createCommand, makeLines} from './lib';
import { commands } from './commands/index';
import { Configuration, OpenAIApi } from 'openai';
import { roleHandler } from "./handlers/role";
import { roleSelect } from './commands/general/role_assignment';
import { roleSelect } from './commands/moderation/role_assignment';
import { Channels } from './constants';

dotenv.config();
Expand Down Expand Up @@ -88,13 +88,34 @@ client.on('interactionCreate', async (interaction: any) => {
await roleHandler(interaction)
}



for(const command of commands) {
if (interaction.commandName == command.name) {
if(command.interaction == undefined) {
Logger.error(`Error: ${command.name} has no interaction`);
break;
}
await command.interaction(interaction)

if(command.requiredPermissions) {
// ignore if user doesn't have permissions
if(!interaction.member.permissions.has(command.requiredPermissions)) {
interaction.reply({
content: makeLines([
`you do not have sufficient permissions to use this command.`,
'',
`(missing: ${command.requiredPermissions.join(', ')})`
]),
ephemeral: true
});
Logger.info(`User ${interaction.member.user.username} doesn't have permissions to run ${command.name}`)
return;
}
}

Logger.info(`Running command ${command.name}`)
await command.interaction(interaction)

}
}
});
8 changes: 4 additions & 4 deletions src/lib/CommandDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommandCategory, ResponseType, Roles } from '../constants';
import { CommandCategory, ResponseType } from '../constants';
import {
ChatInputCommandInteraction, Message, PartialUser, PermissionsBitField,
RESTPostAPIChatInputApplicationCommandsJSONBody, User
ChatInputCommandInteraction, PartialUser,
RESTPostAPIChatInputApplicationCommandsJSONBody, User, PermissionsString
} from 'discord.js'
import { InputCommandOptions } from './CommandOptions';

Expand All @@ -10,7 +10,7 @@ export type APIPost = RESTPostAPIChatInputApplicationCommandsJSONBody;
export interface CommandDefinition {
name: string,
description: string,
permissions?: bigint | PermissionsBitField,
requiredPermissions?: PermissionsString[],
category: CommandCategory,
response: ResponseType,
options?: InputCommandOptions[],
Expand Down

0 comments on commit 0943d81

Please sign in to comment.