Skip to content

Commit

Permalink
fix: broken permission handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kk committed Nov 25, 2023
1 parent 97a65ef commit f22c9e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
14 changes: 9 additions & 5 deletions config.json5
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@
// Channel to log messages
loggingChannel: "1055480391147982969",
// Channel to log admin messages
adminChannel: "",
// Administrator role
adminRole: "787929412364533770",
adminChannel: "1055480391147982980",
permissions: [
// Sample permissions
// Available permissions are: ["kick", "accept", "promote", "demote", "invite", "mute", "unmute", "viewxp"]
// You can use the permission ["all"] to allow for all commands
// Available permissions are command names
// You can use the permission "all" to allow for all commands
{
// Sample Moderator role
// Discord role snowflake
roleId: '900477389560234054',
// Permissions you'd like to use - Example moderator permissions
allowList: ['mute',"unmute","restart"]
},
{
// Sample Administrator role
roleId: '787929412364533770',
allowList: ["all"]
}
]
}
Expand Down
40 changes: 8 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class MinecraftBot {
if (process.env.DISCORD_TOKEN) {
logToConsole("info", "Logging in to Discord");
this.discord.login(process.env.DISCORD_TOKEN).then(async () => {
logToConsole("info", "Fetching commands from files");
// Discord Slash command register
const discordCommandPaths = readdirSync(path.join(__dirname, "discord"), { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
Expand All @@ -86,7 +87,7 @@ export class MinecraftBot {
for (const file of files) {
const resolvePath = path.join(discordCommandsPath, file);
const defaultImport = (await import(resolvePath)).default;
const command = new defaultImport(this.discord, this.bot);
const command = new defaultImport(this.discord, this);
discordCommands.push(command.getCommandBuilder());
this.discordCommandMap.set(command.getCommandBuilder().name, command);
}
Expand Down Expand Up @@ -225,6 +226,7 @@ export class MinecraftBot {
this.discord.on("interactionCreate", async (interaction) => {
if (interaction.member && interaction.isCommand() && interaction.isChatInputCommand()) {
interaction = interaction as ChatInputCommandInteraction;
await interaction.deferReply({ fetchReply: true, ephemeral: true });
const guild = this.discord.guilds.cache.get(config.discord.id);
if (guild) {
const member = guild.members.cache.get(interaction.user.id);
Expand All @@ -242,40 +244,14 @@ export class MinecraftBot {
}),
);

await interaction.deferReply({ fetchReply: true, ephemeral: true });
if (interaction.commandName == "kick" && perms.includes("kick")) {
this.bot.chat(`/g kick ${interaction.options.get("player_name")?.value} ${interaction.options.get("reason")?.value}`);
await interaction.editReply("Command has been executed!");
} else if (interaction.commandName == "accept" && perms.includes("accept")) {
this.bot.chat(`/g accept ${interaction.options.get("player_name")?.value}`);
await interaction.editReply("Command has been executed!");
} else if (interaction.commandName == "promote" && perms.includes("promote")) {
this.bot.chat(`/g promote ${interaction.options.get("player_name")?.value}`);
await interaction.editReply("Command has been executed!");
} else if (interaction.commandName == "demote" && perms.includes("demote")) {
this.bot.chat(`/g demote ${interaction.options.get("player_name")?.value}`);
await interaction.editReply("Command has been executed!");
} else if (interaction.commandName == "invite" && perms.includes("invite")) {
this.bot.chat(`/g invite ${interaction.options.get("player_name")?.value}`);
await interaction.editReply("Command has been executed!");
} else if (interaction.commandName == "mute" && perms.includes("mute")) {
this.bot.chat(`/g mute ${interaction.options.get("player_name")?.value} ${interaction.options.get("time_period")?.value}`);
await interaction.editReply("Command has been executed!");
} else if (interaction.commandName == "unmute" && perms.includes("unmute")) {
this.bot.chat(`/g unmute ${interaction.options.get("player_name")?.value}`);
await interaction.editReply("Command has been executed!");
const commandName = interaction.commandName;
if (commandName && this.discordCommandMap.has(commandName)) {
this.discordCommandMap.get(commandName)?.execute({ interaction, discordCommandMap: this.discordCommandMap });
} else {
if (interaction.isChatInputCommand()) {
const commandName = interaction.commandName;
if (commandName && this.discordCommandMap.has(commandName)) {
this.discordCommandMap.get(commandName)?.execute({ interaction, discordCommandMap: this.discordCommandMap });
} else {
await interaction.editReply(`You don't have the required permissions to execute this command! Missing: ${interaction.commandName}`);
}
}
await interaction.editReply(`This command doesn't appear to exist.`);
}
} else {
await interaction.reply(`You don't have the required permissions to execute this command!`);
await interaction.reply(`You don't have the required permissions to execute this command! Missing: ${interaction.commandName}`);
}
} else {
await interaction.reply(`Can't find the member in question. Discord Error.`);
Expand Down
19 changes: 10 additions & 9 deletions src/util/SlashCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type CommandRegister = {
commandBuilder: Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">;
discordClient: Client<true>;
botInstance: MinecraftBot;
permissionNodes?: Set<string>;
};

export type CommandExecution = {
Expand All @@ -18,10 +19,15 @@ export abstract class CommandBase {
private readonly botInstance: CommandRegister["botInstance"];
private readonly permissionNodes = new Set();

protected constructor({ commandBuilder, discordClient, botInstance }: CommandRegister) {
protected constructor({ commandBuilder, discordClient, botInstance, permissionNodes }: CommandRegister) {
this.discordClient = discordClient;
this.slashCommandBuilder = commandBuilder;
this.botInstance = botInstance;
if (permissionNodes) {
this.permissionNodes = permissionNodes;
} else {
this.permissionNodes.add(commandBuilder.name);
}
}

protected getClient = () => this.discordClient;
Expand All @@ -33,7 +39,7 @@ export abstract class CommandBase {
protected getBotInstance = () => this.botInstance;

protected hasPermissionToExecute = async ({ interaction, discordCommandMap }: CommandExecution) => {
const guild = this.discordClient.guilds.cache.get(process.env.DISCORD_GUILD_ID!);
const guild = this.discordClient.guilds.cache.get(config.discord.id);
if (guild) {
const member = guild.members.cache.get(interaction.user.id);
if (member) {
Expand All @@ -44,17 +50,12 @@ export abstract class CommandBase {
resultArray.allowList.forEach((perm) => {
perms.push(perm.toLowerCase());
if (perm.toLowerCase() == "all") {
discordCommandMap.forEach((value) => perms.push(value.getCommandBuilder().name));
["kick", "accept", "promote", "demote", "invite", "mute", "unmute", "viewxp"].forEach((allPerms) => perms.push(allPerms));
discordCommandMap.forEach((value) => perms.push(value.getCommandBuilder().name.toLowerCase()));
}
}),
);

const valid =
perms.filter((value) => {
console.log(value.toLowerCase());
return this.getPermissions().has(value.toLowerCase());
}).length > 0;
const valid = perms.filter((value) => this.getPermissions().has(value.toLowerCase())).length > 0;
if (valid) {
return true;
} else {
Expand Down

0 comments on commit f22c9e4

Please sign in to comment.