From fe5563e15857efa79d25a8bd0f5c4878b0b64c1b Mon Sep 17 00:00:00 2001 From: bdistin Date: Sun, 24 Feb 2019 02:37:10 -0600 Subject: [PATCH] refactor(User): avoid invoking dmChannel getter more than once per method (#3108) also refactors to async methods --- src/structures/User.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/structures/User.js b/src/structures/User.js index 24bce7937..f510ee137 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -204,22 +204,24 @@ class User extends Base { * Creates a DM channel between the client and the user. * @returns {Promise} */ - createDM() { - if (this.dmChannel) return Promise.resolve(this.dmChannel); - return this.client.api.users(this.client.user.id).channels.post({ data: { + async createDM() { + const { dmChannel } = this; + if (dmChannel) return dmChannel; + const data = await this.client.api.users(this.client.user.id).channels.post({ data: { recipient_id: this.id, - } }) - .then(data => this.client.actions.ChannelCreate.handle(data).channel); + } }); + return this.client.actions.ChannelCreate.handle(data).channel; } /** * Deletes a DM channel (if one exists) between the client and the user. Resolves with the channel if successful. * @returns {Promise} */ - deleteDM() { - if (!this.dmChannel) return Promise.reject(new Error('USER_NO_DMCHANNEL')); - return this.client.api.channels(this.dmChannel.id).delete() - .then(data => this.client.actions.ChannelDelete.handle(data).channel); + async deleteDM() { + const { dmChannel } = this; + if (!dmChannel) throw new Error('USER_NO_DMCHANNEL'); + const data = await this.client.api.channels(dmChannel.id).delete(); + return this.client.actions.ChannelDelete.handle(data).channel; } /**