Skip to content

Commit

Permalink
Merge pull request #6 from ansonfoong/feat/message-delete
Browse files Browse the repository at this point in the history
message.delete() method implemented, can take in MessageDeleteOptions as a parameter.
  • Loading branch information
stuyy authored May 21, 2020
2 parents 1a79a3e + 7d9abf3 commit a9637d9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/client/rest/RestAPIHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});

Expand Down
14 changes: 14 additions & 0 deletions src/models/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<Message> {
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);
});
}
}
5 changes: 5 additions & 0 deletions src/typedefs/MessageOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export interface MessageEmbed {
title: string;
description?: string;
}

export interface MessageDeleteOptions {
timeout?: number;
reason?: string;
}

0 comments on commit a9637d9

Please sign in to comment.