Skip to content

Commit

Permalink
fix(WebhookClient): use applyToClass instead of multiple inheritance (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC authored and iCrawl committed Sep 6, 2017
1 parent 2305311 commit 694f78c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/client/WebhookClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
36 changes: 21 additions & 15 deletions src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

0 comments on commit 694f78c

Please sign in to comment.