mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
feat(Webhook): add '(edit|delete)Message' methods (#5223)
This commit is contained in:
@@ -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
|
||||
@@ -92,6 +93,15 @@ 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 {FileOptions[]|string[]} [files] See {@link WebhookMessageOptions#files}
|
||||
* @property {MessageMentionOptions} [allowedMentions] See {@link WebhookMessageOptions#allowedMentions}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sends a message with this webhook.
|
||||
* @param {StringResolvable|APIMessage} [content=''] The content to send
|
||||
@@ -216,6 +226,34 @@ class Webhook {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits a message that was sent by this webhook.
|
||||
* @param {MessageResolvable} message The message to edit
|
||||
* @param {StringResolvable|APIMessage} [content] The new content for the message
|
||||
* @param {WebhookEditMessageOptions|MessageEmbed|MessageEmbed[]} [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, files } = await (
|
||||
content.resolveData?.() ?? APIMessage.create(this, content, options).resolveData()
|
||||
).resolveFiles();
|
||||
const d = await this.client.api
|
||||
.webhooks(this.id, this.token)
|
||||
.messages(typeof message === 'string' ? message : message.id)
|
||||
.patch({ data, files });
|
||||
|
||||
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
|
||||
@@ -224,6 +262,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}
|
||||
@@ -262,7 +313,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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user