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 8 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
62 changes: 60 additions & 2 deletions src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ class Webhook {
* @property {string} [username=this.name] Username override for the message
* @property {string} [avatarURL] Avatar URL override for the message
* @property {boolean} [tts=false] Whether or not the message should be spoken aloud
* @property {StringResolvable} [content] The content for the message
* @property {string} [nonce=''] The nonce for the message
* @property {Object[]} [embeds] An array of embeds for the message
* @property {MessageEmbed[]|Object[]} [embeds] An array of embeds for the message
* @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content
* (see [here](https://discord.com/developers/docs/resources/channel#embed-object) for more details)
* @property {FileOptions[]|string[]} [files] Files to send with the message
Expand All @@ -92,6 +93,14 @@ class Webhook {
* it exceeds the character limit. If an object is provided, these are the options for splitting the message.
*/

/**
* Options that can be passed into editMessage.
* @typedef {Object} WebhookEditMessageOptions
* @property {MessageEmbed[]|Object[]} [embeds] See {@link WebhookMessageOptions#embeds}
* @property {StringResolvable} [content] See {@link WebhookMessageOptions#content}
* @property {MessageMentionOptions} [allowedMentions] See {@link WebhookMessageOptions#allowedMentions}
*/

/**
* Sends a message with this webhook.
* @param {StringResolvable|APIMessage} [content=''] The content to send
Expand Down Expand Up @@ -216,6 +225,32 @@ 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 {WebhookEditMessageOptions} [options] The options to provide
* @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 +259,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 +310,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