Skip to content

Commit

Permalink
refactor: suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NotSugden committed Dec 27, 2020
1 parent 3d9a495 commit 4633bda
Show file tree
Hide file tree
Showing 22 changed files with 80 additions and 104 deletions.
8 changes: 1 addition & 7 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ class Client extends BaseClient {
super(Object.assign({ _tokenType: 'Bot' }, options));

// Obtain shard details from environment or if present, worker threads
let data = process.env;
try {
// Test if worker threads module is present and used
data = require('worker_threads').workerData ?? data;
} catch {
// Do nothing
}
const data = require('worker_threads').workerData ?? process.env;

if (this.options.shards === DefaultOptions.shards) {
if ('SHARDS' in data) {
Expand Down
3 changes: 2 additions & 1 deletion src/client/voice/networking/VoiceUDPClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ class VoiceConnectionUDPClient extends EventEmitter {

const packet = parseLocalPacket(message);
if (packet.error) {
this.emit('debug', `[UDP] ERROR: ${packet.error}`).emit('error', packet.error);
this.emit('debug', `[UDP] ERROR: ${packet.error}`);
this.emit('error', packet.error);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/errors/DJSError.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function makeDiscordjsError(Base) {
constructor(key, ...args) {
super(message(key, args));
this[kCode] = key;
Error.captureStackTrace?.(this, DiscordjsError);
Error.captureStackTrace(this, DiscordjsError);
}

get name() {
Expand Down
5 changes: 2 additions & 3 deletions src/managers/GuildEmojiManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ class GuildEmojiManager extends BaseGuildEmojiManager {
if (!attachment) throw new TypeError('REQ_RESOURCE_TYPE');

const data = { image: attachment, name };
data.roles = roles?.reduce((array, role) => {
data.roles = roles?.map(role => {
const resolvedRole = this.guild.roles.resolve(role);
if (!resolvedRole) {
throw new TypeError('INVALID_TYPE', 'options.roles', 'Array or Collection of Roles or Snowflakes', true);
}
array.push(role);
return array;
return role.id;
});

const emoji = await this.client.api.guilds(this.guild.id).emojis.post({ data, reason });
Expand Down
5 changes: 4 additions & 1 deletion src/managers/GuildEmojiRoleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ class GuildEmojiRoleManager {
remove(roleOrRoles) {
if (roleOrRoles instanceof Collection) return this.remove(roleOrRoles.keyArray());
if (!Array.isArray(roleOrRoles)) return this.remove([roleOrRoles]);
roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolveID(r));
roleOrRoles = roleOrRoles.map(role => {
const resolvedRole = this.guild.roles.resolve(role);
return resolvedRole && resolvedRole.id;
});

if (roleOrRoles.includes(null)) {
return Promise.reject(new TypeError('INVALID_TYPE', 'roles', 'Array or Collection of Roles or Snowflakes', true));
Expand Down
2 changes: 1 addition & 1 deletion src/rest/DiscordAPIError.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class DiscordAPIError extends Error {
} else if (typeof v === 'string') {
messages.push(v);
} else {
return messages.concat(this.flattenErrors(v, newKey));
messages.push(...this.flattenErrors(v, newKey));
}
return messages;
}, []);
Expand Down
15 changes: 2 additions & 13 deletions src/sharding/ShardingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,7 @@ class ShardingManager extends EventEmitter {
* @returns {Promise<Shard[]>}
*/
broadcast(message) {
return Promise.all(
this.shards.reduce((promises, shard) => {
promises.push(shard.send(message));
return promises;
}),
[],
);
return Promise.all(this.shards.map(shard => shard.send(message)));
}

/**
Expand Down Expand Up @@ -266,12 +260,7 @@ class ShardingManager extends EventEmitter {
return Promise.reject(new Error('SHARDING_SHARD_NOT_FOUND', shard));
}

return Promise.all(
this.shards.reduce((promises, _shard) => {
promises.push(_shard[method](...args));
return promises;
}),
);
return Promise.all(this.shards.map(_shard => _shard[method](...args)));
}

/**
Expand Down
14 changes: 6 additions & 8 deletions src/structures/BaseGuildEmoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class BaseGuildEmoji extends Emoji {
*/
this.guild = guild;

this.requiresColons = null;
this.managed = null;
this.available = null;
this.requiresColons = this.managed = this.available = null;

/**
* Array of role ids this emoji is active for
Expand All @@ -33,33 +31,33 @@ class BaseGuildEmoji extends Emoji {
}

_patch(data) {
if (data.name) this.name = data.name;
if ('name' in data) this.name = data.name;

if (typeof data.require_colons !== 'undefined') {
if ('require_colons' in data) {
/**
* Whether or not this emoji requires colons surrounding it
* @type {?boolean}
*/
this.requiresColons = data.require_colons;
}

if (typeof data.managed !== 'undefined') {
if ('managed' in data) {
/**
* Whether this emoji is managed by an external service
* @type {?boolean}
*/
this.managed = data.managed;
}

if (typeof data.available !== 'undefined') {
if ('available' in data) {
/**
* Whether this emoji is available
* @type {?boolean}
*/
this.available = data.available;
}

if (data.roles) this._roles = data.roles;
if ('roles' in data) this._roles = data.roles;
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/structures/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class Channel extends Base {
*/
this.deleted = false;

if (data) this._patch(data);
}

_patch(data) {
/**
* The unique ID of the channel
* @type {Snowflake}
Expand Down Expand Up @@ -116,7 +112,7 @@ class Channel extends Base {
channel = new PartialGroupDMChannel(client, data);
}
} else {
guild = guild ?? client.guilds.cache.get(data.guild_id);
if (!guild) guild = client.guilds.cache.get(data.guild_id);
if (guild) {
switch (data.type) {
case ChannelTypes.TEXT: {
Expand Down
42 changes: 24 additions & 18 deletions src/structures/ClientApplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,47 @@ const Application = require('./interfaces/Application');
* @extends {Application}
*/
class ClientApplication extends Application {
constructor(client, data) {
super(client, data);

this.owner = null;
this.rpcOrigins = [];
}

_patch(data) {
super._patch(data);

/**
* The app's cover image
* @type {?string}
*/
this.cover = data.cover_image ?? null;

/**
* The app's RPC origins, if enabled
* @type {string[]}
* If this app's bot is public
* @type {boolean}
*/
this.rpcOrigins = data.rpc_origins ?? [];
this.botPublic = data.bot_public;

/**
* If this app's bot requires a code grant when using the OAuth2 flow
* @type {?boolean}
* @type {boolean}
*/
this.botRequireCodeGrant = data.bot_require_code_grant ?? null;
this.botRequireCodeGrant = data.bot_require_code_grant;

/**
* If this app's bot is public
* @type {?boolean}
* The app's cover image
* @type {?string}
*/
this.botPublic = data.bot_public ?? null;
this.cover = data.cover_image;

if ('rpc_origins' in data) {
/**
* The app's RPC origins, if enabled
* @type {string[]}
*/
this.rpcOrigins = data.rpc_origins;
}

/**
* The owner of this OAuth application
* @name ClientApplication#owner
* @type {?User|Team}
*/
if (data.team) this.owner = new Team(this.client, data.team);
else if (data.owner) this.owner = this.client.users.add(data.owner);
else this.owner = null;
this.owner = data.team ? new Team(this.client, data.team) : this.client.users.add(data.owner);
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/structures/ClientUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ class ClientUser extends Structures.get('User') {
_patch(data) {
super._patch(data);

if ('verified' in data) {
if ('mfa_enabled' in data) {
/**
* Whether or not this account has been verified
* If the bot's {@link ClientApplication#owner Owner} has MFA enabled on their account
* @type {boolean}
*/
this.verified = data.verified;
this.mfaEnabled = data.mfa_enabled;
}

if ('mfa_enabled' in data) {
if ('verified' in data) {
/**
* If the bot's {@link ClientApplication#owner Owner} has MFA enabled on their account
* @type {?boolean}
* Whether or not this account has been verified
* @type {boolean}
*/
this.mfaEnabled = data.mfa_enabled ?? null;
} else if (typeof this.mfaEnabled === 'undefined') {
this.mfaEnabled = null;
this.verified = data.verified;
}

if (data.token) this.client.token = data.token;
Expand Down
1 change: 1 addition & 0 deletions src/structures/DMChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DMChannel extends Channel {
super(client, data);
// Override the channel type so partials have a known type
this.type = 'dm';

/**
* A manager of the messages belonging to this channel
* @type {MessageManager}
Expand Down
5 changes: 2 additions & 3 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -933,13 +933,12 @@ class Guild extends Base {
if (!user) throw new TypeError('INVALID_TYPE', 'user', 'UserResolvable');
if (this.members.cache.has(user)) return this.members.cache.get(user);
options.access_token = options.accessToken;
options.roles = options.roles?.reduce((array, role) => {
options.roles = options.roles?.map(role => {
const resolvedRole = this.guild.roles.resolve(role);
if (!resolvedRole) {
throw new TypeError('INVALID_TYPE', 'options.roles', 'Array or Collection of Roles or Snowflakes', true);
}
array.push(role);
return array;
return resolvedRole.id;
}, []);

const data = await this.client.api.guilds(this.id).members(user).put({ data: options });
Expand Down
4 changes: 2 additions & 2 deletions src/structures/GuildChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ class GuildChannel extends Channel {
*/
get members() {
return this.guild.members.cache.reduce((members, member) => {
if (this.permissionsFor(member)?.has(Permissions.FLAGS.VIEW_CHANNEL)) {
if (this.permissionsFor(member).has(Permissions.FLAGS.VIEW_CHANNEL)) {
members.set(member.id, member);
}
return members;
}, []);
}, new Collection());
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/structures/GuildMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ class GuildMember extends Base {
* @readonly
*/
get displayColor() {
const role = this.roles.color;
return role?.color ?? 0;
return this.roles.color?.color ?? 0;
}

/**
Expand All @@ -181,8 +180,7 @@ class GuildMember extends Base {
* @readonly
*/
get displayHexColor() {
const role = this.roles.color;
return role?.hexColor ?? '#000000';
return this.roles.color?.hexColor ?? '#000000';
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/structures/Invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ class Invite extends Base {
get deletable() {
const { guild } = this;
if (!guild || !this.client.guilds.cache.has(guild.id)) return false;
if (!guild.me) throw new Error('GUILD_UNCACHED_ME');
const { me } = guild;
if (!me) throw new Error('GUILD_UNCACHED_ME');
return (
this.channel.permissionsFor(this.client.user)?.has(Permissions.FLAGS.MANAGE_CHANNELS) ||
guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD)
this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS) ||
me.permissions.has(Permissions.FLAGS.MANAGE_GUILD)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Message extends Base {
* @type {ReactionManager}
*/
this.reactions = new ReactionManager(this);
if (data.reactions?.length > 0) {
if (data.reactions) {
for (const reaction of data.reactions) {
this.reactions.add(reaction);
}
Expand Down
20 changes: 8 additions & 12 deletions src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,14 @@ class Webhook {
}

const { data, files } = await apiMessage.resolveFiles();
return this.client.api
.webhooks(this.id, this.token)
.post({
data,
files,
query: { wait: true },
auth: false,
})
.then(d => {
const channel = this.client.channels?.cache.get(d.channel_id);
return channel ? channel.messages.add(d, false) : d;
});
const d = await this.client.api.webhooks(this.id, this.token).post({
data,
files,
query: { wait: true },
auth: false,
});
const channel = this.client.channels?.cache.get(d.channel_id);
return channel ? channel.messages.add(d, false) : d;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/structures/interfaces/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ const AssetTypes = Object.keys(ClientApplicationAssetTypes);
class Application extends Base {
constructor(client, data) {
super(client);
this._patch(data);
}

_patch(data) {
/**
* The ID of the app
* @type {Snowflake}
*/
this.id = data.id;

this._patch(data);
}

_patch(data) {
/**
* The name of the app
* @type {string}
Expand Down

0 comments on commit 4633bda

Please sign in to comment.