From fbe9bc499b3f678b6a6236b03b1c632d245ab12e Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Sun, 5 Jan 2020 18:34:00 +0100 Subject: [PATCH] feat(Webhook): add ability to change channel and specify reason to edit (#3587) * feat(Webhook): add ability to change channel and specify reason to edit * fix(RESTMethods): update channelID of the webhook too --- src/client/rest/RESTMethods.js | 20 ++++++++++++---- src/structures/Webhook.js | 43 +++++++++++++++++++++++++++------- typings/index.d.ts | 9 ++++++- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index ebb53423b..92d4ecb3a 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -801,13 +801,23 @@ class RESTMethods { .then(data => new Webhook(this.client, data)); } - editWebhook(webhook, name, avatar) { - return this.rest.makeRequest('patch', Endpoints.Webhook(webhook.id, webhook.token), false, { - name, - avatar, - }).then(data => { + editWebhook(webhook, options, reason) { + let endpoint; + let auth; + + // Changing the channel of a webhook or specifying a reason requires a bot token + if (options.channel_id || reason) { + endpoint = Endpoints.Webhook(webhook.id); + auth = true; + } else { + endpoint = Endpoints.Webhook(webhook.id, webhook.token); + auth = false; + } + + return this.rest.makeRequest('patch', endpoint, auth, options, undefined, reason).then(data => { webhook.name = data.name; webhook.avatar = data.avatar; + webhook.channelID = data.channel_id; return webhook; }); } diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index cf8f4f2f6..b970f5add 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -276,19 +276,46 @@ class Webhook extends EventEmitter { return this.client.rest.methods.sendSlackWebhookMessage(this, body); } + /** + * Options provided to edit a webhook. + * @property {string} [name] The new name for the webhook + * @property {BufferResolvable} [avatar] The new avatar for the webhook + * @property {ChannelResolvable} [channel] The new channel for the webhook + * @typedef {Object} WebhookEditOptions + */ + /** * Edit the webhook. - * @param {string} name The new name for the webhook - * @param {BufferResolvable} [avatar] The new avatar for the webhook + * @param {string|WebhookEditOptions} nameOrOptions The new name for the webhook **(deprecated, use options)** + * Alternatively options for the webhook, overriding the avatar parameter. + * @param {BufferResolvable|string} [avatarOrReason] The new avatar for the webhook **(deprecated, use options)** + * Alternatively a reason to edit, if using options as first parameter. * @returns {Promise} */ - edit(name = this.name, avatar) { - if (avatar) { - return this.client.resolver.resolveImage(avatar).then(data => - this.client.rest.methods.editWebhook(this, name, data) - ); + edit(nameOrOptions = this.name, avatarOrReason) { + if (typeof nameOrOptions !== 'object') { + process.emitWarning('Webhook#edit: Use options object instead of separate parameters.'); + nameOrOptions = { + name: nameOrOptions, + avatar: avatarOrReason, + }; + // Parameter was an avatar here; Clear the now reason parameter + avatarOrReason = undefined; } - return this.client.rest.methods.editWebhook(this, name); + + if (nameOrOptions.channel) { + nameOrOptions.channel_id = this.client.resolver.resolveChannelID(nameOrOptions.channel); + nameOrOptions.channel = undefined; + } + + if (nameOrOptions.avatar) { + return this.client.resolver.resolveImage(nameOrOptions.avatar).then(data => { + nameOrOptions.avatar = data; + return this.client.rest.methods.editWebhook(this, nameOrOptions, avatarOrReason); + }); + } + + return this.client.rest.methods.editWebhook(this, nameOrOptions, avatarOrReason); } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index 808d6c98c..b2536e72f 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1579,7 +1579,8 @@ declare module 'discord.js' { public owner: User | object; public token: string; public delete(reason?: string): Promise; - public edit(name: string, avatar: BufferResolvable): Promise; + public edit(name?: string, avatar?: BufferResolvable): Promise; + public edit(options?: WebhookEditOptions, reason?: string): Promise; public send(content?: StringResolvable, options?: WebhookMessageOptions | RichEmbed | Attachment): Promise; public send(options?: WebhookMessageOptions | RichEmbed | Attachment): Promise; public sendCode(lang: string, content: StringResolvable, options?: WebhookMessageOptions): Promise; @@ -2201,6 +2202,12 @@ declare module 'discord.js' { type VoiceStatus = number; + type WebhookEditOptions = { + name?: string; + avatar?: BufferResolvable; + channel?: ChannelResolvable; + }; + type WebhookMessageOptions = { username?: string; avatarURL?: string;