From 7d9abf300f94b0c1349fd9d5e4dd9b2b72a45c15 Mon Sep 17 00:00:00 2001 From: Anson Foong Date: Thu, 21 May 2020 18:26:45 -0400 Subject: [PATCH] message.delete() method implemented, can take in MessageDeleteOptions --- src/client/rest/RestAPIHandler.ts | 9 +++++++++ src/index.ts | 2 +- src/models/Message.ts | 14 ++++++++++++++ src/typedefs/MessageOptions.ts | 5 +++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/client/rest/RestAPIHandler.ts b/src/client/rest/RestAPIHandler.ts index 8db2ddb..2ca7197 100644 --- a/src/client/rest/RestAPIHandler.ts +++ b/src/client/rest/RestAPIHandler.ts @@ -81,6 +81,15 @@ export default class RestAPIHandler { return response.json(); } + async deleteMessage(channelId: string, messageId: string) { + return fetch( + `${Constants.API}/${ENDPOINTS.CHANNELS}/${channelId}/${ENDPOINTS.MESSAGES}/${messageId}`, + { + method: 'DELETE', + headers + } + ); + } set token(token: string) { this._token = token; headers.Authorization = `Bot ${this._token}`; diff --git a/src/index.ts b/src/index.ts index 0abe010..09e76ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,7 +16,7 @@ client.on("message", async (message: Message) => { console.log(message.channel.messages.size); if (message.content === "?hello") { const msg = await message.channel.send("hello"); - console.log(msg); + msg.delete(); } }); diff --git a/src/models/Message.ts b/src/models/Message.ts index c1a6f05..078c2b9 100644 --- a/src/models/Message.ts +++ b/src/models/Message.ts @@ -2,6 +2,7 @@ import Guild from "./Guild.ts"; import User from "./User.ts"; import { TextChannel } from "./channels/TextChannel.ts"; import GuildMember from './GuildMember.ts'; +import { MessageDeleteOptions } from '../typedefs/MessageOptions.ts'; export default class Message { constructor( @@ -41,4 +42,17 @@ export default class Message { public get pinned(): boolean { return this._pinned; } public get type(): number { return this._type; } public get content(): string { return this._content; } + + public delete(options?: MessageDeleteOptions): Promise { + return new Promise((resolve, reject) => { + setTimeout(async () => { + try { + await this.channel.client.rest.deleteMessage(this.channel.id, this.id); + resolve(this); + } catch (err) { + reject(err); + } + }, options?.timeout || 0); + }); + } } diff --git a/src/typedefs/MessageOptions.ts b/src/typedefs/MessageOptions.ts index 6ecca11..85c2abf 100644 --- a/src/typedefs/MessageOptions.ts +++ b/src/typedefs/MessageOptions.ts @@ -8,3 +8,8 @@ export interface MessageEmbed { title: string; description?: string; } + +export interface MessageDeleteOptions { + timeout?: number; + reason?: string; +} \ No newline at end of file