From fdd5b4f3feab8f188dc983694c2b361042467930 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Mon, 9 Aug 2021 15:01:11 +0800 Subject: [PATCH] Check permissions The author is checked the ability to manage messages before it can do pin and save commands. --- src/commands/pin.ts | 14 +++++++++++++- src/commands/save.ts | 12 ++++++++++++ src/index.ts | 4 ++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/commands/pin.ts b/src/commands/pin.ts index 9c0169e..7878cb1 100644 --- a/src/commands/pin.ts +++ b/src/commands/pin.ts @@ -4,7 +4,7 @@ import messages from '../messages.json'; import { transferPin } from '../utils/common'; const pin = async ( - message: Message, pinTextChannel: TextChannel, silent = false, + message: Message, mainTextChannel: TextChannel, pinTextChannel: TextChannel, silent = false, ): Promise => { const isReply = message.type === 'REPLY'; if (!isReply) { @@ -12,6 +12,18 @@ const pin = async ( return; } + const authorMember = await message.guild?.members.fetch(message.author); + if (!authorMember) { + await message.reply(messages.INVALID_PERMISSIONS); + return; + } + + const authorCanPin = authorMember.permissionsIn(mainTextChannel).has('MANAGE_MESSAGES'); + if (!authorCanPin) { + await message.reply(messages.INVALID_PERMISSIONS); + return; + } + const reply = await message.fetchReference(); await transferPin(reply, pinTextChannel, silent); await message.reply(messages.PIN_COMPLETE); diff --git a/src/commands/save.ts b/src/commands/save.ts index f36629e..c31ee60 100644 --- a/src/commands/save.ts +++ b/src/commands/save.ts @@ -6,6 +6,18 @@ import { transferPin } from '../utils/common'; const save = async ( message: Message, mainTextChannel: TextChannel, pinTextChannel: TextChannel, silent = true, ): Promise => { + const authorMember = await message.guild?.members.fetch(message.author); + if (!authorMember) { + await message.reply(messages.INVALID_PERMISSIONS); + return; + } + + const authorCanPin = authorMember.permissionsIn(mainTextChannel).has('MANAGE_MESSAGES'); + if (!authorCanPin) { + await message.reply(messages.INVALID_PERMISSIONS); + return; + } + const pinnedMessages = await mainTextChannel.messages.fetchPinned(); const pinnedMessagesCollection = Array.from(pinnedMessages.values()); if (pinnedMessagesCollection.length <= 0) { diff --git a/src/index.ts b/src/index.ts index 67fb222..e94e69b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,7 +38,7 @@ const listen = async (channelIDMap: Map) => { const reply = await message.fetchReference(); await transferPin(reply, pinTextChannel, false); await reply.unpin(); - await mainTextChannel.send(`${message.author.toString()} ${messages.PIN_COMPLETE as string}`); + await mainTextChannel.send(`${message.author.toString()} ${messages.PIN_COMPLETE}`); await message.delete(); return; } @@ -54,7 +54,7 @@ const listen = async (channelIDMap: Map) => { switch (message.content) { case getCommandPattern(clientUser, 'pin'): - await pin(message, pinTextChannel); + await pin(message, mainTextChannel, pinTextChannel); return; case getCommandPattern(clientUser, 'save'): await save(message, mainTextChannel, pinTextChannel);