From 25980b7db395feac2c2791fa73d9e8f641cf5002 Mon Sep 17 00:00:00 2001 From: Isaac Date: Mon, 3 May 2021 15:01:46 +0100 Subject: [PATCH] Bot permissions check --- src/locales/cs-CZ.json | 2 +- src/locales/de-DE.json | 2 +- src/locales/en-GB.json | 6 ++- src/locales/en-US.json | 2 +- src/locales/es-ES.json | 2 +- src/locales/fr-FR.json | 2 +- src/locales/hi-IN.json | 2 +- src/locales/id-ID.json | 2 +- src/locales/it-IT.json | 2 +- src/locales/no-NO.json | 2 +- src/locales/ru-RU.json | 2 +- src/modules/commands/manager.js | 72 ++++++++++++++++++++++++--------- 12 files changed, 67 insertions(+), 31 deletions(-) diff --git a/src/locales/cs-CZ.json b/src/locales/cs-CZ.json index a38dfb5..ae0ec28 100644 --- a/src/locales/cs-CZ.json +++ b/src/locales/cs-CZ.json @@ -155,7 +155,7 @@ "description": "Během provádění příkazu došlo k neočekávané chybě.\nPožádejte správce serveru, aby zkontroloval výstup konzole / log pro více detailů." }, "message_will_be_deleted_in": "Tato zpráva bude smazána za %d sekund", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "Na provedení tohoto příkazu nemáte dostatečné oprávnění:\n%s" }, diff --git a/src/locales/de-DE.json b/src/locales/de-DE.json index f045068..8d0522d 100644 --- a/src/locales/de-DE.json +++ b/src/locales/de-DE.json @@ -155,7 +155,7 @@ "description": "An unexpected error occurred during command execution.\nPlease ask an administrator to check the console output / logs for details." }, "message_will_be_deleted_in": "This message will be deleted in %d seconds", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "You do not have the permissions required to use this command:\n%s" }, diff --git a/src/locales/en-GB.json b/src/locales/en-GB.json index f045068..c2a93a4 100644 --- a/src/locales/en-GB.json +++ b/src/locales/en-GB.json @@ -1,5 +1,9 @@ { "bot": { + "missing_permissions": { + "title": "⚠️", + "description": "Discord Tickets requires the following permissions:\n%s" + }, "version": "[Discord Tickets](%s) v%s by [eartharoid](%s)" }, "cmd_usage": { @@ -155,7 +159,7 @@ "description": "An unexpected error occurred during command execution.\nPlease ask an administrator to check the console output / logs for details." }, "message_will_be_deleted_in": "This message will be deleted in %d seconds", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "You do not have the permissions required to use this command:\n%s" }, diff --git a/src/locales/en-US.json b/src/locales/en-US.json index f045068..8d0522d 100644 --- a/src/locales/en-US.json +++ b/src/locales/en-US.json @@ -155,7 +155,7 @@ "description": "An unexpected error occurred during command execution.\nPlease ask an administrator to check the console output / logs for details." }, "message_will_be_deleted_in": "This message will be deleted in %d seconds", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "You do not have the permissions required to use this command:\n%s" }, diff --git a/src/locales/es-ES.json b/src/locales/es-ES.json index f045068..8d0522d 100644 --- a/src/locales/es-ES.json +++ b/src/locales/es-ES.json @@ -155,7 +155,7 @@ "description": "An unexpected error occurred during command execution.\nPlease ask an administrator to check the console output / logs for details." }, "message_will_be_deleted_in": "This message will be deleted in %d seconds", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "You do not have the permissions required to use this command:\n%s" }, diff --git a/src/locales/fr-FR.json b/src/locales/fr-FR.json index f045068..8d0522d 100644 --- a/src/locales/fr-FR.json +++ b/src/locales/fr-FR.json @@ -155,7 +155,7 @@ "description": "An unexpected error occurred during command execution.\nPlease ask an administrator to check the console output / logs for details." }, "message_will_be_deleted_in": "This message will be deleted in %d seconds", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "You do not have the permissions required to use this command:\n%s" }, diff --git a/src/locales/hi-IN.json b/src/locales/hi-IN.json index 44b4e0a..7147d39 100644 --- a/src/locales/hi-IN.json +++ b/src/locales/hi-IN.json @@ -155,7 +155,7 @@ "description": "An unexpected error occurred during command execution.\nPlease ask an administrator to check the console output / logs for details." }, "message_will_be_deleted_in": "This message will be deleted in %d seconds", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "You do not have the permissions required to use this command:\n%s" }, diff --git a/src/locales/id-ID.json b/src/locales/id-ID.json index 7dae64e..0f14b96 100644 --- a/src/locales/id-ID.json +++ b/src/locales/id-ID.json @@ -155,7 +155,7 @@ "description": "Eror tak terduga ketika mengeksekusi command.\nTolong tanya seorang adminstrator untuk memeriksa console atau log konsol untuk detail." }, "message_will_be_deleted_in": "Pesan ini akan dihapus dalam %d detik", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "Anda tidak memiliki izin yang diperlukan untuk menggunakan command ini:\n%s" }, diff --git a/src/locales/it-IT.json b/src/locales/it-IT.json index f045068..8d0522d 100644 --- a/src/locales/it-IT.json +++ b/src/locales/it-IT.json @@ -155,7 +155,7 @@ "description": "An unexpected error occurred during command execution.\nPlease ask an administrator to check the console output / logs for details." }, "message_will_be_deleted_in": "This message will be deleted in %d seconds", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "You do not have the permissions required to use this command:\n%s" }, diff --git a/src/locales/no-NO.json b/src/locales/no-NO.json index 8c96cad..fbeb7b3 100644 --- a/src/locales/no-NO.json +++ b/src/locales/no-NO.json @@ -155,7 +155,7 @@ "description": "Det oppstod en uventet feil under kommandoutførelsen.\nBe en administrator kontrollere konsollutdataene/-loggene for mer informasjon." }, "message_will_be_deleted_in": "Denne meldingen vil bli slettet om %d sekunder", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "Du har ikke tillatelsene som kreves for å bruke denne kommandoen:\n%s" }, diff --git a/src/locales/ru-RU.json b/src/locales/ru-RU.json index f045068..8d0522d 100644 --- a/src/locales/ru-RU.json +++ b/src/locales/ru-RU.json @@ -155,7 +155,7 @@ "description": "An unexpected error occurred during command execution.\nPlease ask an administrator to check the console output / logs for details." }, "message_will_be_deleted_in": "This message will be deleted in %d seconds", - "missing_perms": { + "missing_permissions": { "title": "❌", "description": "You do not have the permissions required to use this command:\n%s" }, diff --git a/src/modules/commands/manager.js b/src/modules/commands/manager.js index 2fdaf47..59936e4 100644 --- a/src/modules/commands/manager.js +++ b/src/modules/commands/manager.js @@ -61,6 +61,8 @@ module.exports = class CommandManager { * @param {Message} message - Command message */ async handle(message) { + if (message.author.bot) return; // ignore self and other bots + let settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); @@ -98,6 +100,56 @@ module.exports = class CommandManager { const cmd = this.commands.find(cmd => cmd.aliases.includes(cmd_name)); if (!cmd) return; + let bot_permissions = message.guild.me.permissionsIn(message.channel); + let required_bot_permissions = [ + 'ADD_REACTIONS', + 'ATTACH_FILES', + 'EMBED_LINKS', + 'MANAGE_CHANNELS', + 'MANAGE_MESSAGES', + 'READ_MESSAGE_HISTORY', + 'SEND_MESSAGES', + ]; + + if (!bot_permissions.has(required_bot_permissions)) { + let perms = required_bot_permissions.map(p => `\`${p}\``).join(', '); + if (bot_permissions.has(['EMBED_LINKS', 'SEND_MESSAGES'])) { + await message.channel.send( + new MessageEmbed() + .setColor('ORANGE') + .setTitle(i18n('bot.missing_permissions.title')) + .setDescription(i18n('bot.missing_permissions.description', perms)) + ); + } else if (bot_permissions.has('SEND_MESSAGES')) { + await message.channel.send('⚠️ ' + i18n('bot.missing_permissions.description', perms)); + } else if (bot_permissions.has('ADD_REACTIONS')) { + await message.react('⚠️'); + } else { + this.client.log.warn('Unable to respond to command due to missing permissions'); + } + return; + } + + const missing_permissions = cmd.permissions instanceof Array && !message.member.hasPermission(cmd.permissions); + if (missing_permissions) { + let perms = cmd.permissions.map(p => `\`${p}\``).join(', '); + return await message.channel.send( + new MessageEmbed() + .setColor(settings.error_colour) + .setTitle(i18n('missing_permissions.title')) + .setDescription(i18n('missing_permissions.description', perms)) + ); + } + + if (cmd.staff_only && await message.member.isStaff() === false) { + return await message.channel.send( + new MessageEmbed() + .setColor(settings.error_colour) + .setTitle(i18n('staff_only.title')) + .setDescription(i18n('staff_only.description')) + ); + } + let args = raw_args; if (cmd.process_args) { @@ -116,26 +168,6 @@ module.exports = class CommandManager { return await cmd.sendUsage(message.channel, cmd_name); } } - - const missing_perms = cmd.permissions instanceof Array && !message.member.hasPermission(cmd.permissions); - if (missing_perms) { - let perms = cmd.permissions.map(p => `\`${p}\``).join(', '); - return await message.channel.send( - new MessageEmbed() - .setColor(settings.error_colour) - .setTitle(i18n('missing_perms.title')) - .setDescription(i18n('missing_perms.description', perms)) - ); - } - - if (cmd.staff_only && await message.member.isStaff() === false) { - return await message.channel.send( - new MessageEmbed() - .setColor(settings.error_colour) - .setTitle(i18n('staff_only.title')) - .setDescription(i18n('staff_only.description')) - ); - } try { this.client.log.commands(`Executing "${cmd.name}" command (invoked by ${message.author.tag})`);