From 02b313f2e6ade9f03964047756c15ab063a78f71 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Wed, 25 Apr 2018 23:02:47 +0100 Subject: [PATCH] Add checks for whether the user can delete a message --- client/src/structs/discord/message.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/client/src/structs/discord/message.js b/client/src/structs/discord/message.js index abe78eb8..41eaaaf2 100644 --- a/client/src/structs/discord/message.js +++ b/client/src/structs/discord/message.js @@ -9,7 +9,7 @@ */ import { DiscordApi, DiscordApiModules as Modules } from 'modules'; -import { List } from 'structs'; +import { List, InsufficientPermissions } from 'structs'; import { Channel } from './channel'; import { User } from './user'; @@ -148,9 +148,18 @@ export class Message { * TODO: how do we know if the message was deleted successfully? */ delete() { + if (!this.isDeletable) throw new Error(`Message type ${this.type} is not deletable.`); + if (this.author === DiscordApi.currentUser) {} + else if (this.channel.assertPermissions) this.channel.assertPermissions('MANAGE_MESSAGES', Modules.DiscordPermissions.MANAGE_MESSAGES); + else if (!this.channel.owner === DiscordApi.currentUser) throw new InsufficientPermissions('MANAGE_MESSAGES'); + Modules.MessageActions.deleteMessage(this.channelId, this.id); } + isDeletable() { + return this.type === 'DEFAULT' || this.type === 'CHANNEL_PINNED_MESSAGE' || this.type === 'GUILD_MEMBER_JOIN'; + } + /** * Jumps to the message. */