Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Webhook): add '(edit|delete)Message' methods #5223

Merged
merged 14 commits into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,34 @@ class Webhook {
return this;
}

/**
* Edits a message that was sent by this webhook.
* @param {MessageResolvable} message The message to edit
* @param {StringResolvable} [content] The new content for the message
* @param {Object} [options] The options to provide
* @param {MessageMentionOptions} [options.allowedMentions] Which mentions should be parsed from the message content
* @property {MessageEmbed[]|Object[]} [options.embeds] An array of embeds for the message
* @returns {Promise<Message|Object>} Returns the raw message data if the webhook was instantiated as a
* {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned
*/
async editMessage(message, content, options) {
const { data } = APIMessage.create(this, content, options).resolveData();
const d = await this.client.api
.webhooks(this.id, this.token)
.messages(typeof message === 'string' ? message : message.id)
.patch({ data });

const messageManager = this.client.channels?.cache.get(d.channel_id)?.messages;
if (!messageManager) return d;

const existing = messageManager.cache.get(d.id);
if (!existing) return messageManager.add(d);

const clone = existing._clone();
clone._patch(d);
return clone;
}

/**
* Deletes the webhook.
* @param {string} [reason] Reason for deleting this webhook
Expand All @@ -224,6 +252,19 @@ class Webhook {
delete(reason) {
return this.client.api.webhooks(this.id, this.token).delete({ reason });
}

/**
* Delete a message that was sent by this webhook.
* @param {MessageResolvable} message The message to delete
* @returns {Promise<void>}
*/
async deleteMessage(message) {
await this.client.api
.webhooks(this.id, this.token)
.messages(typeof message === 'string' ? message : message.id)
.delete();
}

/**
* The timestamp the webhook was created at
* @type {number}
Expand Down Expand Up @@ -262,7 +303,17 @@ class Webhook {
}

static applyToClass(structure) {
for (const prop of ['send', 'sendSlackMessage', 'edit', 'delete', 'createdTimestamp', 'createdAt', 'url']) {
for (const prop of [
'send',
'sendSlackMessage',
'edit',
'editMessage',
'delete',
'deleteMessage',
'createdTimestamp',
'createdAt',
'url',
]) {
Object.defineProperty(structure.prototype, prop, Object.getOwnPropertyDescriptor(Webhook.prototype, prop));
}
}
Expand Down
9 changes: 9 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2089,7 +2089,14 @@ declare module 'discord.js' {
readonly createdTimestamp: number;
readonly url: string;
delete(reason?: string): Promise<void>;
deleteMessage(message: MessageResolvable): Promise<void>;
edit(options: WebhookEditData): Promise<Webhook>;
editMessage(
message: MessageResolvable,
content: APIMessageContentResolvable,
options?: WebhookEditMessageOptions,
): Promise<Message | object>;
editMessage(message: MessageResolvable, options: WebhookEditMessageOptions): Promise<Message | object>;
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
send(
content: APIMessageContentResolvable | (WebhookMessageOptions & { split?: false }) | MessageAdditions,
): Promise<Message>;
Expand Down Expand Up @@ -3195,6 +3202,8 @@ declare module 'discord.js' {
reason?: string;
}

type WebhookEditMessageOptions = Pick<WebhookMessageOptions, 'embeds' | 'allowedMentions'>;
izexi marked this conversation as resolved.
Show resolved Hide resolved

interface WebhookMessageOptions {
username?: string;
avatarURL?: string;
Expand Down