mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
refactor(User): avoid invoking dmChannel getter more than once per method (#3108)
also refactors to async methods
This commit is contained in:
@@ -204,22 +204,24 @@ class User extends Base {
|
|||||||
* Creates a DM channel between the client and the user.
|
* Creates a DM channel between the client and the user.
|
||||||
* @returns {Promise<DMChannel>}
|
* @returns {Promise<DMChannel>}
|
||||||
*/
|
*/
|
||||||
createDM() {
|
async createDM() {
|
||||||
if (this.dmChannel) return Promise.resolve(this.dmChannel);
|
const { dmChannel } = this;
|
||||||
return this.client.api.users(this.client.user.id).channels.post({ data: {
|
if (dmChannel) return dmChannel;
|
||||||
|
const data = await this.client.api.users(this.client.user.id).channels.post({ data: {
|
||||||
recipient_id: this.id,
|
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.
|
* Deletes a DM channel (if one exists) between the client and the user. Resolves with the channel if successful.
|
||||||
* @returns {Promise<DMChannel>}
|
* @returns {Promise<DMChannel>}
|
||||||
*/
|
*/
|
||||||
deleteDM() {
|
async deleteDM() {
|
||||||
if (!this.dmChannel) return Promise.reject(new Error('USER_NO_DMCHANNEL'));
|
const { dmChannel } = this;
|
||||||
return this.client.api.channels(this.dmChannel.id).delete()
|
if (!dmChannel) throw new Error('USER_NO_DMCHANNEL');
|
||||||
.then(data => this.client.actions.ChannelDelete.handle(data).channel);
|
const data = await this.client.api.channels(dmChannel.id).delete();
|
||||||
|
return this.client.actions.ChannelDelete.handle(data).channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user