The author is checked the ability to manage messages before it can do pin and save commands.master
@@ -4,7 +4,7 @@ import messages from '../messages.json'; | |||||
import { transferPin } from '../utils/common'; | import { transferPin } from '../utils/common'; | ||||
const pin = async ( | const pin = async ( | ||||
message: Message, pinTextChannel: TextChannel, silent = false, | |||||
message: Message, mainTextChannel: TextChannel, pinTextChannel: TextChannel, silent = false, | |||||
): Promise<void> => { | ): Promise<void> => { | ||||
const isReply = message.type === 'REPLY'; | const isReply = message.type === 'REPLY'; | ||||
if (!isReply) { | if (!isReply) { | ||||
@@ -12,6 +12,18 @@ const pin = async ( | |||||
return; | 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(); | const reply = await message.fetchReference(); | ||||
await transferPin(reply, pinTextChannel, silent); | await transferPin(reply, pinTextChannel, silent); | ||||
await message.reply(messages.PIN_COMPLETE); | await message.reply(messages.PIN_COMPLETE); | ||||
@@ -6,6 +6,18 @@ import { transferPin } from '../utils/common'; | |||||
const save = async ( | const save = async ( | ||||
message: Message, mainTextChannel: TextChannel, pinTextChannel: TextChannel, silent = true, | message: Message, mainTextChannel: TextChannel, pinTextChannel: TextChannel, silent = true, | ||||
): Promise<void> => { | ): Promise<void> => { | ||||
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 pinnedMessages = await mainTextChannel.messages.fetchPinned(); | ||||
const pinnedMessagesCollection = Array.from(pinnedMessages.values()); | const pinnedMessagesCollection = Array.from(pinnedMessages.values()); | ||||
if (pinnedMessagesCollection.length <= 0) { | if (pinnedMessagesCollection.length <= 0) { | ||||
@@ -38,7 +38,7 @@ const listen = async (channelIDMap: Map<string, string>) => { | |||||
const reply = await message.fetchReference(); | const reply = await message.fetchReference(); | ||||
await transferPin(reply, pinTextChannel, false); | await transferPin(reply, pinTextChannel, false); | ||||
await reply.unpin(); | 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(); | await message.delete(); | ||||
return; | return; | ||||
} | } | ||||
@@ -54,7 +54,7 @@ const listen = async (channelIDMap: Map<string, string>) => { | |||||
switch (message.content) { | switch (message.content) { | ||||
case getCommandPattern(clientUser, 'pin'): | case getCommandPattern(clientUser, 'pin'): | ||||
await pin(message, pinTextChannel); | |||||
await pin(message, mainTextChannel, pinTextChannel); | |||||
return; | return; | ||||
case getCommandPattern(clientUser, 'save'): | case getCommandPattern(clientUser, 'save'): | ||||
await save(message, mainTextChannel, pinTextChannel); | await save(message, mainTextChannel, pinTextChannel); | ||||