Add checks for whether the user can delete a message

This commit is contained in:
Samuel Elliott 2018-04-25 23:02:47 +01:00
parent b0ae424adb
commit 02b313f2e6
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
1 changed files with 10 additions and 1 deletions

View File

@ -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.
*/