diff --git a/src/commands/checkperms.ts b/src/commands/checkperms.ts new file mode 100644 index 0000000..9cfe42b --- /dev/null +++ b/src/commands/checkperms.ts @@ -0,0 +1,48 @@ +import { + type CommandInteraction, + SlashCommandBuilder, EmbedBuilder, type GuildMember +} from 'discord.js' +import { execute, role2obj } from '../db' + +module.exports = { + data: new SlashCommandBuilder() + .setName('checkperms') + .addUserOption((option) => + option + .setName('user') + .setDescription('The user to check the roles of') + .setRequired(true)) + .setDescription('Check permission of an user for goobot'), + async execute (interaction: CommandInteraction) { + if (interaction.guild === null) { + void interaction.reply({ content: 'I am sorry dave, I cannot do that\nCommand not available outside of guilds', ephemeral: true }) + return + } + let member = interaction.member as GuildMember | null + if (interaction.options.get('user') !== undefined) member = interaction.options.getMember('user') as GuildMember | null + + if (member === null) { + void interaction.reply({ content: 'Can`t find member', ephemeral: true }) + return + } + + const allPermissions = await execute('SELECT * FROM permissions WHERE guild=$guild', { $guild: interaction.guildId }) as Array<{ guild: string, role: string, permissions: number }> + console.log(allPermissions) + const userPermsBitField = allPermissions.reduce((acc, val) => + member?.roles.cache.some(r => r.id === val.role.toString()) === true ? acc | val.permissions : acc + , 0) + + const permissions = role2obj(userPermsBitField) + const statusUpdate = new EmbedBuilder() + .setColor(0x0099FF) + .setTitle('List of roles') + .setTimestamp() + .addFields( + { name: 'User', value: `<@${member.id}>` }, + { name: 'Can add goobs', value: permissions.create ? '🟩' : '🟥', inline: true }, + { name: 'Can delete goobs', value: permissions.delete ? '🟩' : '🟥', inline: true } + ) + + await interaction.reply({ embeds: [statusUpdate], ephemeral: true }) + } +} diff --git a/src/commands/checkroles.ts b/src/commands/checkroles.ts index f58f363..7dbeb0b 100644 --- a/src/commands/checkroles.ts +++ b/src/commands/checkroles.ts @@ -9,6 +9,11 @@ module.exports = { .setName('checkroles') .setDescription('Prints all roles permissions'), async execute (interaction: CommandInteraction) { + if (interaction.guild === null) { + void interaction.reply({ content: 'I am sorry dave, I cannot do that\nCommand not available outside of guilds', ephemeral: true }) + return + } + if ((interaction.memberPermissions?.has(PermissionsBitField.Flags.ManageRoles, true)) !== true) { await interaction.reply({ content: 'I am sorry dave, I cannot do that\nYou don\'t have the permission to manage roles on this server', ephemeral: true }) return diff --git a/src/commands/delrole.ts b/src/commands/delrole.ts new file mode 100644 index 0000000..ed66d29 --- /dev/null +++ b/src/commands/delrole.ts @@ -0,0 +1,34 @@ +import { + type CommandInteraction, + SlashCommandBuilder, + PermissionsBitField +} from 'discord.js' +import { execute } from '../db' + +module.exports = { + data: new SlashCommandBuilder() + .setName('delrole') + .setDescription('Delete a role from the database') + .addRoleOption((option) => + option + .setName('role') + .setDescription('The role to delete') + .setRequired(true) + ), + async execute (interaction: CommandInteraction) { + if (interaction.guild === null) { + void interaction.reply({ content: 'I am sorry dave, I cannot do that\nCommand not available outside of guilds', ephemeral: true }) + return + } + + if ( + interaction.memberPermissions?.has(PermissionsBitField.Flags.ManageRoles, true) !== true) { + await interaction.reply({ content: "I am sorry dave, I cannot do that\nYou don't have the permission to manage roles on this server", ephemeral: true }) + return + } + + await execute('DELETE FROM permissions WHERE role=$role AND guild=$guild', { $role: interaction.options.get('role')?.value, $guild: interaction.guildId }) + + await interaction.client.commands.get('checkroles')?.execute(interaction) + } +} diff --git a/src/commands/setrole.ts b/src/commands/setrole.ts index 4ac0127..410fb35 100644 --- a/src/commands/setrole.ts +++ b/src/commands/setrole.ts @@ -29,11 +29,11 @@ module.exports = { const role = interaction.options.get('role')?.value if (role === undefined) { - await interaction.reply({ content: 'Cannot find role', ephemeral: true }) + void interaction.reply({ content: 'Cannot find role', ephemeral: true }) return } if ((interaction.memberPermissions?.has(PermissionsBitField.Flags.ManageRoles, true)) !== true) { - await interaction.reply({ content: 'I am sorry dave, I cannot do that\nYou don\'t have the permission to manage roles on this server', ephemeral: true }) + void interaction.reply({ content: 'I am sorry dave, I cannot do that\nYou don\'t have the permission to manage roles on this server', ephemeral: true }) return } // Set Role