Add command permission checks

Level 0: Every user - Public commands
Level 1: Guild owners or members with respective admin role - Elevated guild commands
Level 2: Global admins - Every command including levels below
This commit is contained in:
2022-08-19 19:18:01 +02:00
parent 6a7e3f6647
commit eb4ffae173
8 changed files with 147 additions and 4 deletions

View File

@@ -13,7 +13,7 @@ module.exports = {
.setDescription("The command to debug")
.setRequired(false)
),
permissionLevel: 2,
async execute(interaction) {
const identifier = CardUtils.generateIdentifier();
let user = await UserUtils.getUserByDiscordId(interaction.member.id);
@@ -70,6 +70,13 @@ module.exports = {
content: `Reset cooldowns`,
ephemeral: false
});
break;
default:
interaction.reply({
content: `Your permission level is ${await UserUtils.getPermissionLevel(interaction.member)}`,
ephemeral: false
});
break;
}
}
}

View File

@@ -12,7 +12,7 @@ module.exports = {
.setDescription("The id of the character to drop")
.setRequired(true)
),
permissionLevel: 2,
async execute(interaction) {
//get user id from database given the userID
const user = await User.findOne({

61
commands/guildSettings.js Normal file
View File

@@ -0,0 +1,61 @@
const { SlashCommandBuilder, ComponentType, ActionRowBuilder, ButtonBuilder, ButtonStyle, SelectMenuBuilder } = require("discord.js");
const { GeneralUtils, GuildUtils } = require("../util");
module.exports = {
data: new SlashCommandBuilder()
.setName("settings")
.setDescription("Change Guild Settings"),
permissionLevel: 1,
async execute(interaction) {
let reply = "Guild settings:\n";
const row = new ActionRowBuilder();
row.addComponents(
new ButtonBuilder()
.setCustomId(`set-admin-role`)
.setLabel('Set Admin Role')
.setStyle(ButtonStyle.Primary)
);
const message = await interaction.reply({ content: reply, components: [row], fetchReply: true });
const filter = (m) => m.author.id === message.author.id;
const collector = message.createMessageComponentCollector(filter, { componentType: ComponentType.Button, time: 120000 });
//BUGBUG: end callback does not trigger
collector.on('end', collected => {
console.log(`Collected ${collected.size} interactions.`);
message.edit({ components: [] });
});
collector.on('collect', async m => {
switch (m.customId) {
case 'set-admin-role':
//Build select menu with every role in the guild
const row = new ActionRowBuilder()
.addComponents(
new SelectMenuBuilder()
.setCustomId('select')
.setPlaceholder('Nothing selected')
.addOptions(
m.guild.roles.cache.map(role => ({
label: role.name,
description: role.id,
value: role.id
}))
),
);
const message = await m.reply({ content: 'Select a role', components: [row], fetchReply: true });
const filter = (m) => m.author.id === message.author.id;
const replyCollector = message.createMessageComponentCollector(filter, { componentType: ComponentType.SelectMenu, time: 25000 });
replyCollector.on('collect', async r => {
const role = m.guild.roles.cache.find(role => role.id === r.values[0]);
m.deleteReply(); //Delete select menu message
GuildUtils.setProperty(m.guild.id, 'adminRoleId', role.id);
r.reply({ content: `Selected role: ${role.name} \n(${role.id})`, components: [] });
});
break;
}
});
}
}

View File

@@ -4,6 +4,7 @@ module.exports = {
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("Ping, yes"),
permissionLevel: 1,
async execute(interaction) {
interaction.reply({
content: "Pong!",