From 694f78cd439f946df87e88c886ee9fd0a028f6eb Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Wed, 6 Sep 2017 23:11:40 +0200 Subject: [PATCH] fix(WebhookClient): use applyToClass instead of multiple inheritance (#1896) --- src/client/WebhookClient.js | 8 +++++--- src/structures/Webhook.js | 36 +++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/client/WebhookClient.js b/src/client/WebhookClient.js index edb1a0c13..bc413cef6 100644 --- a/src/client/WebhookClient.js +++ b/src/client/WebhookClient.js @@ -14,14 +14,16 @@ class WebhookClient extends BaseClient { * @example * // Create a new webhook and send a message * const hook = new Discord.WebhookClient('1234', 'abcdef'); - * hook.sendMessage('This will send a message').catch(console.error); + * hook.send('This will send a message').catch(console.error); */ constructor(id, token, options) { super(options); - Webhook.call(this, null, id, token); + Object.defineProperty(this, 'client', { value: this }); + this.id = id; + this.token = token; } } -Object.assign(WebhookClient.prototype, Object.create(Webhook.prototype)); +Webhook.applyToClass(WebhookClient); module.exports = WebhookClient; diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index 8d96e1ed6..51e0dea3f 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -8,21 +8,15 @@ const MessageEmbed = require('./MessageEmbed'); * Represents a webhook. */ class Webhook { - constructor(client, dataOrID, token) { - if (client) { - /** - * The client that instantiated the webhook - * @name Webhook#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: client }); - if (dataOrID) this._patch(dataOrID); - } else { - this.id = dataOrID; - this.token = token; - Object.defineProperty(this, 'client', { value: this }); - } + constructor(client, data) { + /** + * The client that instantiated the webhook + * @name Webhook#client + * @type {Client} + * @readonly + */ + Object.defineProperty(this, 'client', { value: client }); + if (data) this._patch(data); } _patch(data) { @@ -274,6 +268,18 @@ class Webhook { delete(reason) { return this.client.api.webhooks(this.id, this.token).delete({ reason }); } + + static applyToClass(structure) { + for (const prop of [ + 'send', + 'sendSlackMessage', + 'edit', + 'delete', + ]) { + Object.defineProperty(structure.prototype, prop, + Object.getOwnPropertyDescriptor(Webhook.prototype, prop)); + } + } } module.exports = Webhook;