From 9e73ddcae04b43b54c96e2f01767eae7b64e404c Mon Sep 17 00:00:00 2001 From: Mikael-R Date: Sat, 10 Oct 2020 14:50:22 -0300 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20separate=20permissions?= =?UTF-8?q?=20for=20client=20and=20user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/general/help/command.ts | 22 ++++++++++++++----- .../general/request-permission/command.ts | 10 ++++----- src/commands/moderator/clear/command.ts | 6 ++++- src/commands/moderator/kick/command.ts | 6 ++++- src/commands/music/join/command.ts | 5 ++++- src/commands/music/play/command.ts | 5 ++++- src/index.ts | 2 +- src/tools/onCallCommand.ts | 15 ++++++++----- src/types.ts | 7 ++++-- 9 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/commands/general/help/command.ts b/src/commands/general/help/command.ts index a7c8bdf..f3cddbc 100644 --- a/src/commands/general/help/command.ts +++ b/src/commands/general/help/command.ts @@ -55,11 +55,23 @@ class Help implements Command { Command.permissions && fields.push({ name: 'Permissions', - value: Command.permissions - .join(', ') - .split('_') - .join(' ') - .toLowerCase(), + value: `${ + Command.permissions?.client + ? `Client: ${Command.permissions.client + .join(', ') + .split('_') + .join(' ') + .toLowerCase()}\n\n` + : '' + }${ + Command.permissions?.user + ? `User: ${Command.permissions.user + .join(', ') + .split('_') + .join(' ') + .toLowerCase()}` + : '' + }`, }) fields.push({ name: 'Usage', diff --git a/src/commands/general/request-permission/command.ts b/src/commands/general/request-permission/command.ts index f402718..2d91bb4 100644 --- a/src/commands/general/request-permission/command.ts +++ b/src/commands/general/request-permission/command.ts @@ -60,7 +60,7 @@ class RequestPermission implements Command { const userNeedPermissions = needPermissions({ member: message.guild.members.resolve(message.author.id), - permissions: Command.permissions || [], + permissions: Command.permissions?.user || [], }) const invalidCallCommandDescription = ( @@ -70,8 +70,8 @@ class RequestPermission implements Command { Command, messageArgs, permissions: { + client: message.guild.me.permissions.toArray(), user: message.guild.me.permissions.toArray(), - bot: message.guild.me.permissions.toArray(), }, }) )?.description @@ -79,9 +79,9 @@ class RequestPermission implements Command { ?.slice(0, -1) switch (true) { - case !Command.permissions?.length: + case !Command.permissions?.user: return [ - `:red_circle: Not is required permission to run: \`\`${requestRun}\`\``, + `:red_circle: Users not need permission to run: \`\`${requestRun}\`\``, ] case !userNeedPermissions.length: @@ -112,7 +112,7 @@ class RequestPermission implements Command { const filter: CustomCollectorFilter = (reaction, user) => { const havePermission = !needPermissions({ member: reaction.message.guild.members.resolve(user.id), - permissions: Command?.permissions || [], + permissions: Command?.permissions?.user || [], }).length const isValidEmojiReaction = ['👍', '👎'].includes(reaction.emoji.name) diff --git a/src/commands/moderator/clear/command.ts b/src/commands/moderator/clear/command.ts index 4a601ff..299e327 100644 --- a/src/commands/moderator/clear/command.ts +++ b/src/commands/moderator/clear/command.ts @@ -10,7 +10,11 @@ class Clear implements Command { static commandName = 'clear' static aliases = ['c', 'cls'] static description = 'Delete previous messages' - static permissions: PermissionString[] = ['MANAGE_MESSAGES'] + static permissions = { + client: ['MANAGE_MESSAGES'] as PermissionString[], + user: ['MANAGE_MESSAGES'] as PermissionString[], + } + static minArguments = 1 static usage = 'clear [limit]' static example = 'clear 7' diff --git a/src/commands/moderator/kick/command.ts b/src/commands/moderator/kick/command.ts index 4d68df9..2df378e 100644 --- a/src/commands/moderator/kick/command.ts +++ b/src/commands/moderator/kick/command.ts @@ -19,7 +19,11 @@ class Kick implements Command { static aliases = ['k', 'kck'] static description = 'Kick user from server' static minArguments = 1 - static permissions: PermissionString[] = ['KICK_MEMBERS'] + static permissions = { + user: ['KICK_MEMBERS'] as PermissionString[], + client: ['KICK_MEMBERS'] as PermissionString[], + } + static usage = 'kick [mention, id]' static example = 'kick 736626386009194676' diff --git a/src/commands/music/join/command.ts b/src/commands/music/join/command.ts index 98ec13f..9cb58df 100644 --- a/src/commands/music/join/command.ts +++ b/src/commands/music/join/command.ts @@ -19,7 +19,10 @@ class Join implements Command { static aliases = ['jn', 'connect'] static description = 'Connect in voice channel' static minArguments = 0 - static permissions: PermissionString[] = ['CONNECT'] + static permissions = { + client: ['CONNECT'] as PermissionString[], + } + static usage = 'join' validator() { diff --git a/src/commands/music/play/command.ts b/src/commands/music/play/command.ts index d90201f..7b25f59 100644 --- a/src/commands/music/play/command.ts +++ b/src/commands/music/play/command.ts @@ -39,7 +39,10 @@ class Play implements Command { static aliases = ['p', 'pl'] static description = 'Plays audio from YouTube videos on voice channels' static minArguments = 1 - static permissions: PermissionString[] = ['SPEAK'] + static permissions = { + client: ['SPEAK'] as PermissionString[], + } + static usage = 'play [name, url, id]' static example = 'play Sub Urban - Cradles' diff --git a/src/index.ts b/src/index.ts index bbe0a65..d40f866 100644 --- a/src/index.ts +++ b/src/index.ts @@ -60,10 +60,10 @@ client.on('message', async message => { Command, messageArgs, permissions: { + client: message.guild.me.permissions.toArray(), user: message.guild.members .resolve(message.author.id) .permissions.toArray(), - bot: message.guild.me.permissions.toArray(), }, }) diff --git a/src/tools/onCallCommand.ts b/src/tools/onCallCommand.ts index c3ff6cc..622bb8e 100644 --- a/src/tools/onCallCommand.ts +++ b/src/tools/onCallCommand.ts @@ -19,10 +19,15 @@ const invalidCall: InvalidCall = async ({ }) => { const description: string[] = [] - const commandPermissions = Command.permissions || [] const needPermissions = { - user: commandPermissions.filter(perm => !permissions.user.includes(perm)), - bot: commandPermissions.filter(perm => !permissions.bot.includes(perm)), + client: + Command.permissions?.client?.filter( + perm => !permissions.client.includes(perm) + ) || [], + user: + Command.permissions?.user?.filter( + perm => !permissions.user.includes(perm) + ) || [], } const commandInitialized = new Command({ message, embed, messageArgs }) @@ -35,9 +40,9 @@ const invalidCall: InvalidCall = async ({ description.push(`:red_circle: Need arguments: \`\`${Command.usage}\`\``) break - case !!needPermissions.bot.length: + case !!needPermissions.client.length: description.push( - `:red_circle: I need permissions: \`\`${needPermissions.bot.join( + `:red_circle: I need permissions: \`\`${needPermissions.client.join( ', ' )}\`\`` ) diff --git a/src/types.ts b/src/types.ts index 0099568..b7e22a1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,7 +5,10 @@ export interface CommandClass { commandName: string aliases: string[] description: string - permissions?: PermissionString[] + permissions?: { + client?: PermissionString[] + user?: PermissionString[] + } minArguments: number usage: string example?: string @@ -43,8 +46,8 @@ export interface InvalidCall { Command: CommandClass messageArgs: string[] permissions: { + client: PermissionString[] user: PermissionString[] - bot: PermissionString[] } }): Promise | MessageEmbed }