refactor: new node features (#5132)

Co-authored-by: Antonio Román <kyradiscord@gmail.com>
This commit is contained in:
Sugden
2021-06-30 21:40:33 +01:00
committed by GitHub
parent f108746c15
commit 1e8f01253e
68 changed files with 305 additions and 360 deletions

View File

@@ -41,7 +41,7 @@ class ApplicationCommandManager extends BaseManager {
*/
commandPath({ id, guildID } = {}) {
let path = this.client.api.applications(this.client.application.id);
if (this.guild || guildID) path = path.guilds(this.guild?.id ?? guildID);
if (this.guild ?? guildID) path = path.guilds(this.guild?.id ?? guildID);
return id ? path.commands(id) : path.commands;
}
@@ -84,9 +84,7 @@ class ApplicationCommandManager extends BaseManager {
async fetch(id, { guildID, cache = true, force = false } = {}) {
if (typeof id === 'object') {
({ guildID, cache = true, force = false } = id);
id = undefined;
}
if (id) {
} else if (id) {
if (!force) {
const existing = this.cache.get(id);
if (existing) return existing;

View File

@@ -67,7 +67,7 @@ class BaseGuildEmojiManager extends BaseManager {
if (emoji instanceof ReactionEmoji) return emoji.identifier;
if (typeof emoji === 'string') {
const res = parseEmoji(emoji);
if (res && res.name.length) {
if (res?.name.length) {
emoji = `${res.animated ? 'a:' : ''}${res.name}${res.id ? `:${res.id}` : ''}`;
}
if (!emoji.includes('%')) return encodeURIComponent(emoji);

View File

@@ -17,7 +17,7 @@ class BaseManager {
* @private
* @readonly
*/
Object.defineProperty(this, 'holds', { value: Structures.get(holds.name) || holds });
Object.defineProperty(this, 'holds', { value: Structures.get(holds.name) ?? holds });
/**
* The client that instantiated this Manager
@@ -42,12 +42,12 @@ class BaseManager {
}
add(data, cache = true, { id, extras = [] } = {}) {
const existing = this.cache.get(id || data.id);
if (existing && existing._patch && cache) existing._patch(data);
const existing = this.cache.get(id ?? data.id);
if (cache) existing?._patch(data);
if (existing) return existing;
const entry = this.holds ? new this.holds(this.client, data, ...extras) : data;
if (cache) this.cache.set(id || entry.id, entry);
if (cache) this.cache.set(id ?? entry.id, entry);
return entry;
}
@@ -58,7 +58,7 @@ class BaseManager {
*/
resolve(idOrInstance) {
if (idOrInstance instanceof this.holds) return idOrInstance;
if (typeof idOrInstance === 'string') return this.cache.get(idOrInstance) || null;
if (typeof idOrInstance === 'string') return this.cache.get(idOrInstance) ?? null;
return null;
}

View File

@@ -22,10 +22,10 @@ class ChannelManager extends BaseManager {
add(data, guild, cache = true) {
const existing = this.cache.get(data.id);
if (existing) {
if (existing._patch && cache) existing._patch(data);
if (guild) guild.channels?.add(existing);
if (ThreadChannelTypes.includes(existing.type) && typeof existing.parent?.threads !== 'undefined') {
existing.parent.threads.add(existing);
if (cache) existing._patch(data);
guild?.channels?.add(existing);
if (ThreadChannelTypes.includes(existing.type)) {
existing.parent?.threads?.add(existing);
}
return existing;
}

View File

@@ -115,9 +115,10 @@ class GuildChannelManager extends BaseManager {
* ],
* })
*/
async create(name, options = {}) {
let { type, topic, nsfw, bitrate, userLimit, parent, permissionOverwrites, position, rateLimitPerUser, reason } =
options;
async create(
name,
{ type, topic, nsfw, bitrate, userLimit, parent, permissionOverwrites, position, rateLimitPerUser, reason } = {},
) {
if (parent) parent = this.client.channels.resolveID(parent);
if (permissionOverwrites) {
permissionOverwrites = permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));

View File

@@ -269,7 +269,7 @@ class GuildMemberManager extends BaseManager {
* @example
* // Kick a user by ID (or with a user/guild member object)
* guild.members.kick('84484653687267328')
* .then(user => console.log(`Kicked ${user.username || user.id || user} from ${guild.name}`))
* .then(user => console.log(`Kicked ${user.username ?? user.id ?? user} from ${guild.name}`))
* .catch(console.error);
*/
async kick(user, reason) {
@@ -356,7 +356,7 @@ class GuildMemberManager extends BaseManager {
},
});
const fetchedMembers = new Collection();
const option = query || limit || presences || user_ids;
const option = Boolean(query || limit || presences || user_ids);
let i = 0;
const handler = (members, _, chunk) => {
timeout.refresh();

View File

@@ -68,7 +68,7 @@ class GuildMemberRoleManager {
* @readonly
*/
get premiumSubscriberRole() {
return this.cache.find(role => role.tags && role.tags.premiumSubscriberRole) || null;
return this.cache.find(role => role.tags?.premiumSubscriberRole) ?? null;
}
/**
@@ -79,7 +79,7 @@ class GuildMemberRoleManager {
*/
get botRole() {
if (!this.member.user.bot) return null;
return this.cache.find(role => role.tags && role.tags.botID === this.member.user.id) || null;
return this.cache.find(role => role.tags?.botID === this.member.user.id) ?? null;
}
/**

View File

@@ -150,7 +150,7 @@ class MessageManager extends BaseManager {
if (!message) throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable');
const data = await this.client.api.channels(this.channel.id).messages(message).crosspost.post();
return this.cache.get(data.id) || this.add(data);
return this.cache.get(data.id) ?? this.add(data);
}
/**

View File

@@ -40,7 +40,7 @@ class PresenceManager extends BaseManager {
const presenceResolvable = super.resolve(presence);
if (presenceResolvable) return presenceResolvable;
const UserResolvable = this.client.users.resolveID(presence);
return super.resolve(UserResolvable) || null;
return super.resolve(UserResolvable);
}
/**

View File

@@ -19,7 +19,7 @@ class ReactionManager extends BaseManager {
}
add(data, cache) {
return super.add(data, cache, { id: data.emoji.id || data.emoji.name, extras: [this.message] });
return super.add(data, cache, { id: data.emoji.id ?? data.emoji.name, extras: [this.message] });
}
/**