mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 01:23:31 +01:00
refactor(*): use async functions (#6210)
This commit is contained in:
@@ -65,10 +65,8 @@ class GuildEmojiManager extends BaseGuildEmojiManager {
|
||||
}
|
||||
}
|
||||
|
||||
return this.client.api
|
||||
.guilds(this.guild.id)
|
||||
.emojis.post({ data, reason })
|
||||
.then(emoji => this.client.actions.GuildEmojiCreate.handle(this.guild, emoji).emoji);
|
||||
const emoji = await this.client.api.guilds(this.guild.id).emojis.post({ data, reason });
|
||||
return this.client.actions.GuildEmojiCreate.handle(this.guild, emoji).emoji;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -195,45 +195,42 @@ class GuildManager extends CachedManager {
|
||||
}
|
||||
if (systemChannelFlags) systemChannelFlags = SystemChannelFlags.resolve(systemChannelFlags);
|
||||
|
||||
return new Promise((resolve, reject) =>
|
||||
this.client.api.guilds
|
||||
.post({
|
||||
data: {
|
||||
name,
|
||||
icon,
|
||||
verification_level: verificationLevel,
|
||||
default_message_notifications: defaultMessageNotifications,
|
||||
explicit_content_filter: explicitContentFilter,
|
||||
roles,
|
||||
channels,
|
||||
afk_channel_id: afkChannelId,
|
||||
afk_timeout: afkTimeout,
|
||||
system_channel_id: systemChannelId,
|
||||
system_channel_flags: systemChannelFlags,
|
||||
},
|
||||
})
|
||||
.then(data => {
|
||||
if (this.client.guilds.cache.has(data.id)) return resolve(this.client.guilds.cache.get(data.id));
|
||||
const data = await this.client.api.guilds.post({
|
||||
data: {
|
||||
name,
|
||||
icon,
|
||||
verification_level: verificationLevel,
|
||||
default_message_notifications: defaultMessageNotifications,
|
||||
explicit_content_filter: explicitContentFilter,
|
||||
roles,
|
||||
channels,
|
||||
afk_channel_id: afkChannelId,
|
||||
afk_timeout: afkTimeout,
|
||||
system_channel_id: systemChannelId,
|
||||
system_channel_flags: systemChannelFlags,
|
||||
},
|
||||
});
|
||||
|
||||
const handleGuild = guild => {
|
||||
if (guild.id === data.id) {
|
||||
clearTimeout(timeout);
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
this.client.decrementMaxListeners();
|
||||
resolve(guild);
|
||||
}
|
||||
};
|
||||
this.client.incrementMaxListeners();
|
||||
this.client.on(Events.GUILD_CREATE, handleGuild);
|
||||
if (this.client.guilds.cache.has(data.id)) return this.client.guilds.cache.get(data.id);
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
this.client.decrementMaxListeners();
|
||||
resolve(this.client.guilds._add(data));
|
||||
}, 10000).unref();
|
||||
return undefined;
|
||||
}, reject),
|
||||
);
|
||||
return new Promise(resolve => {
|
||||
const handleGuild = guild => {
|
||||
if (guild.id === data.id) {
|
||||
clearTimeout(timeout);
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
this.client.decrementMaxListeners();
|
||||
resolve(guild);
|
||||
}
|
||||
};
|
||||
this.client.incrementMaxListeners();
|
||||
this.client.on(Events.GUILD_CREATE, handleGuild);
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
this.client.decrementMaxListeners();
|
||||
resolve(this.client.guilds._add(data));
|
||||
}, 10000).unref();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -277,8 +277,8 @@ class GuildMemberManager extends CachedManager {
|
||||
* .then(pruned => console.log(`I just pruned ${pruned} people!`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
prune({ days = 7, dry = false, count: compute_prune_count = true, roles = [], reason } = {}) {
|
||||
if (typeof days !== 'number') return Promise.reject(new TypeError('PRUNE_DAYS_TYPE'));
|
||||
async prune({ days = 7, dry = false, count: compute_prune_count = true, roles = [], reason } = {}) {
|
||||
if (typeof days !== 'number') throw new TypeError('PRUNE_DAYS_TYPE');
|
||||
|
||||
const query = { days };
|
||||
const resolvedRoles = [];
|
||||
@@ -286,7 +286,7 @@ class GuildMemberManager extends CachedManager {
|
||||
for (const role of roles) {
|
||||
const resolvedRole = this.guild.roles.resolveId(role);
|
||||
if (!resolvedRole) {
|
||||
return Promise.reject(new TypeError('INVALID_ELEMENT', 'Array', 'options.roles', role));
|
||||
throw new TypeError('INVALID_ELEMENT', 'Array', 'options.roles', role);
|
||||
}
|
||||
resolvedRoles.push(resolvedRole);
|
||||
}
|
||||
@@ -297,16 +297,11 @@ class GuildMemberManager extends CachedManager {
|
||||
|
||||
const endpoint = this.client.api.guilds(this.guild.id).prune;
|
||||
|
||||
if (dry) {
|
||||
return endpoint.get({ query, reason }).then(data => data.pruned);
|
||||
}
|
||||
const { pruned } = await (dry
|
||||
? endpoint.get({ query, reason })
|
||||
: endpoint.post({ data: { ...query, compute_prune_count }, reason }));
|
||||
|
||||
return endpoint
|
||||
.post({
|
||||
data: { ...query, compute_prune_count },
|
||||
reason,
|
||||
})
|
||||
.then(data => data.pruned);
|
||||
return pruned;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -366,17 +361,14 @@ class GuildMemberManager extends CachedManager {
|
||||
return this.guild.bans.remove(user, reason);
|
||||
}
|
||||
|
||||
_fetchSingle({ user, cache, force = false }) {
|
||||
async _fetchSingle({ user, cache, force = false }) {
|
||||
if (!force) {
|
||||
const existing = this.cache.get(user);
|
||||
if (existing && !existing.partial) return Promise.resolve(existing);
|
||||
if (existing && !existing.partial) return existing;
|
||||
}
|
||||
|
||||
return this.client.api
|
||||
.guilds(this.guild.id)
|
||||
.members(user)
|
||||
.get()
|
||||
.then(data => this._add(data, cache));
|
||||
const data = await this.client.api.guilds(this.guild.id).members(user).get();
|
||||
return this._add(data, cache);
|
||||
}
|
||||
|
||||
_fetchMany({
|
||||
|
||||
@@ -57,10 +57,10 @@ class GuildStickerManager extends CachedManager {
|
||||
|
||||
const data = { name, tags, description: description ?? '' };
|
||||
|
||||
return this.client.api
|
||||
const sticker = await this.client.api
|
||||
.guilds(this.guild.id)
|
||||
.stickers.post({ data, files: [file], reason, dontUsePayloadJSON: true })
|
||||
.then(sticker => this.client.actions.GuildStickerCreate.handle(this.guild, sticker).sticker);
|
||||
.stickers.post({ data, files: [file], reason, dontUsePayloadJSON: true });
|
||||
return this.client.actions.GuildStickerCreate.handle(this.guild, sticker).sticker;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -80,12 +80,11 @@ class MessageManager extends CachedManager {
|
||||
* .then(messages => console.log(`Received ${messages.size} messages`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
fetchPinned(cache = true) {
|
||||
return this.client.api.channels[this.channel.id].pins.get().then(data => {
|
||||
const messages = new Collection();
|
||||
for (const message of data) messages.set(message.id, this._add(message, cache));
|
||||
return messages;
|
||||
});
|
||||
async fetchPinned(cache = true) {
|
||||
const data = await this.client.api.channels[this.channel.id].pins.get();
|
||||
const messages = new Collection();
|
||||
for (const message of data) messages.set(message.id, this._add(message, cache));
|
||||
return messages;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,7 +54,9 @@ class PermissionOverwriteManager extends CachedManager {
|
||||
*/
|
||||
set(overwrites, reason) {
|
||||
if (!Array.isArray(overwrites) && !(overwrites instanceof Collection)) {
|
||||
throw new TypeError('INVALID_TYPE', 'overwrites', 'Array or Collection of Permission Overwrites', true);
|
||||
return Promise.reject(
|
||||
new TypeError('INVALID_TYPE', 'overwrites', 'Array or Collection of Permission Overwrites', true),
|
||||
);
|
||||
}
|
||||
return this.channel.edit({ permissionOverwrites: overwrites, reason });
|
||||
}
|
||||
@@ -81,7 +83,7 @@ class PermissionOverwriteManager extends CachedManager {
|
||||
let { type, reason } = overwriteOptions;
|
||||
if (typeof type !== 'number') {
|
||||
userOrRole = this.channel.guild.roles.resolve(userOrRole) ?? this.client.users.resolve(userOrRole);
|
||||
if (!userOrRole) return Promise.reject(new TypeError('INVALID_TYPE', 'parameter', 'User nor a Role'));
|
||||
if (!userOrRole) throw new TypeError('INVALID_TYPE', 'parameter', 'User nor a Role');
|
||||
type = userOrRole instanceof Role ? OverwriteTypes.role : OverwriteTypes.member;
|
||||
}
|
||||
|
||||
|
||||
@@ -57,12 +57,9 @@ class ReactionManager extends CachedManager {
|
||||
* Removes all reactions from a message.
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
removeAll() {
|
||||
return this.client.api
|
||||
.channels(this.message.channel.id)
|
||||
.messages(this.message.id)
|
||||
.reactions.delete()
|
||||
.then(() => this.message);
|
||||
async removeAll() {
|
||||
await this.client.api.channels(this.message.channel.id).messages(this.message.id).reactions.delete();
|
||||
return this.message;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,15 +57,14 @@ class ReactionUserManager extends CachedManager {
|
||||
* @param {UserResolvable} [user=this.client.user] The user to remove the reaction of
|
||||
* @returns {Promise<MessageReaction>}
|
||||
*/
|
||||
remove(user = this.client.user) {
|
||||
async remove(user = this.client.user) {
|
||||
const userId = this.client.users.resolveId(user);
|
||||
if (!userId) return Promise.reject(new Error('REACTION_RESOLVE_USER'));
|
||||
if (!userId) throw new Error('REACTION_RESOLVE_USER');
|
||||
const message = this.reaction.message;
|
||||
return this.client.api.channels[message.channel.id].messages[message.id].reactions[this.reaction.emoji.identifier][
|
||||
await this.client.api.channels[message.channel.id].messages[message.id].reactions[this.reaction.emoji.identifier][
|
||||
userId === this.client.user.id ? '@me' : userId
|
||||
]
|
||||
.delete()
|
||||
.then(() => this.reaction);
|
||||
].delete();
|
||||
return this.reaction;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -118,31 +118,27 @@ class RoleManager extends CachedManager {
|
||||
* .then(console.log)
|
||||
* .catch(console.error);
|
||||
*/
|
||||
create(options = {}) {
|
||||
async create(options = {}) {
|
||||
let { name, color, hoist, permissions, position, mentionable, reason } = options;
|
||||
if (color) color = resolveColor(color);
|
||||
if (typeof permissions !== 'undefined') permissions = new Permissions(permissions);
|
||||
|
||||
return this.client.api
|
||||
.guilds(this.guild.id)
|
||||
.roles.post({
|
||||
data: {
|
||||
name,
|
||||
color,
|
||||
hoist,
|
||||
permissions,
|
||||
mentionable,
|
||||
},
|
||||
reason,
|
||||
})
|
||||
.then(r => {
|
||||
const { role } = this.client.actions.GuildRoleCreate.handle({
|
||||
guild_id: this.guild.id,
|
||||
role: r,
|
||||
});
|
||||
if (position) return role.setPosition(position, reason);
|
||||
return role;
|
||||
});
|
||||
const data = await this.client.api.guilds(this.guild.id).roles.post({
|
||||
data: {
|
||||
name,
|
||||
color,
|
||||
hoist,
|
||||
permissions,
|
||||
mentionable,
|
||||
},
|
||||
reason,
|
||||
});
|
||||
const { role } = this.client.actions.GuildRoleCreate.handle({
|
||||
guild_id: this.guild.id,
|
||||
role: data,
|
||||
});
|
||||
if (position) return role.setPosition(position, reason);
|
||||
return role;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -74,13 +74,11 @@ class ThreadMemberManager extends CachedManager {
|
||||
* @param {string} [reason] The reason for adding this member
|
||||
* @returns {Promise<Snowflake>}
|
||||
*/
|
||||
add(member, reason) {
|
||||
async add(member, reason) {
|
||||
const id = member === '@me' ? member : this.client.users.resolveId(member);
|
||||
if (!id) return Promise.reject(new TypeError('INVALID_TYPE', 'member', 'UserResolvable'));
|
||||
return this.client.api
|
||||
.channels(this.thread.id, 'thread-members', id)
|
||||
.put({ reason })
|
||||
.then(() => id);
|
||||
if (!id) throw new TypeError('INVALID_TYPE', 'member', 'UserResolvable');
|
||||
await this.client.api.channels(this.thread.id, 'thread-members', id).put({ reason });
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,11 +87,9 @@ class ThreadMemberManager extends CachedManager {
|
||||
* @param {string} [reason] The reason for removing this member from the thread
|
||||
* @returns {Promise<Snowflake>}
|
||||
*/
|
||||
remove(id, reason) {
|
||||
return this.client.api
|
||||
.channels(this.thread.id, 'thread-members', id)
|
||||
.delete({ reason })
|
||||
.then(() => id);
|
||||
async remove(id, reason) {
|
||||
await this.client.api.channels(this.thread.id, 'thread-members', id).delete({ reason });
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user