Skip to content

Commit

Permalink
Code cleanup & formatting (#488)
Browse files Browse the repository at this point in the history
Spread syntax, string interpolation, and some other styling standardization
  • Loading branch information
apacheli authored and abalabahaha committed May 29, 2019
1 parent ae2fac1 commit 3f35fb8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
25 changes: 9 additions & 16 deletions lib/Client.js
Expand Up @@ -28,14 +28,14 @@ let EventEmitter;
try {
EventEmitter = require("eventemitter3");
} catch(err) {
EventEmitter = require("events").EventEmitter;
EventEmitter = require("events");
}
let Erlpack;
try {
Erlpack = require("erlpack");
} catch(err) { // eslint-disable no-empty
}
const sleep = (ms) => new Promise((res) => setTimeout(() => res(), ms));
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));

/**
* Represents the main Eris client
Expand Down Expand Up @@ -113,29 +113,25 @@ class Client extends EventEmitter {
restMode: false,
seedVoiceConnections: false,
ws: {},
agent: null
agent: null,
...options
};
if(typeof options === "object") {
for(const property of Object.keys(options)) {
this.options[property] = options[property];
}
}
if(this.options.lastShardID === undefined && this.options.maxShards !== "auto") {
this.options.lastShardID = this.options.maxShards - 1;
}
if(typeof window !== "undefined") {
this.options.compress = false; // zlib does not like Blobs, Pako is not here
}
if(!Constants.ImageFormats.includes(this.options.defaultImageFormat.toLowerCase())) {
throw new TypeError("Invalid default image format: " + this.options.defaultImageFormat);
throw new TypeError(`Invalid default image format: ${this.options.defaultImageFormat}`);
}
const defaultImageSize = this.options.defaultImageSize;
if(
defaultImageSize < Constants.ImageSizeBoundaries.MINIMUM ||
defaultImageSize > Constants.ImageSizeBoundaries.MAXIMUM ||
(defaultImageSize & (defaultImageSize - 1))
) {
throw new TypeError("Invalid default image size: " + defaultImageSize);
throw new TypeError(`Invalid default image size: ${defaultImageSize}`);
}
// Set HTTP Agent on Websockets if not already set
if (this.options.agent && !(this.options.ws && this.options.ws.agent)) {
Expand All @@ -148,7 +144,7 @@ class Client extends EventEmitter {
this.requestHandler = new RequestHandler(this);

this.ready = false;
this.bot = this.options.restMode && this.token ? this.token.startsWith("Bot ") : true;
this.bot = this.options.restMode && token ? token.startsWith("Bot ") : true;
this.startTime = 0;
this.lastConnect = 0;
this.channelGuildMap = {};
Expand Down Expand Up @@ -193,7 +189,7 @@ class Client extends EventEmitter {
if(!data.url.endsWith("/")) {
data.url += "/";
}
this.gatewayURL = data.url + "?v=" + Constants.GATEWAY_VERSION + "&encoding=" + (Erlpack ? "etf" : "json");
this.gatewayURL = `${data.url}?v=${Constants.GATEWAY_VERSION}&encoding=${Erlpack ? "etf" : "json"}`;

if(this.options.compress) {
this.gatewayURL += "&compress=zlib-stream";
Expand Down Expand Up @@ -287,10 +283,7 @@ class Client extends EventEmitter {
* @arg {String} channelID The ID of the voice channel
*/
leaveVoiceChannel(channelID) {
if(!channelID) {
return;
}
if(!this.channelGuildMap[channelID]) {
if(!channelID || !this.channelGuildMap[channelID]) {
return;
}
this.closeVoiceConnection(this.channelGuildMap[channelID]);
Expand Down
30 changes: 13 additions & 17 deletions lib/command/CommandClient.js
Expand Up @@ -36,13 +36,9 @@ class CommandClient extends Client {
name: null,
owner: "an unknown user",
prefix: "@mention ",
defaultCommandOptions: {}
defaultCommandOptions: {},
...commandOptions
};
if(typeof commandOptions === "object") {
for(const property of Object.keys(commandOptions)) {
this.commandOptions[property] = commandOptions[property];
}
}
this.guildPrefixes = {};
this.commands = {};
this.commandAliases = {};
Expand All @@ -54,15 +50,15 @@ class CommandClient extends Client {
this.commandOptions.name = `**${this.user.username}**`;
}
if(Array.isArray(this.commandOptions.prefix)) {
for(let i = 0; i < this.commandOptions.prefix.length; i++) {
for(let i = 0; i < this.commandOptions.prefix.length; ++i) {
this.commandOptions.prefix[i] = this.commandOptions.prefix[i].replace(/@mention/g, this.user.mention);
}
} else {
this.commandOptions.prefix = this.commandOptions.prefix.replace(/@mention/g, this.user.mention);
}
for(const key of Object.keys(this.guildPrefixes)) {
if(Array.isArray(this.guildPrefixes[key])) {
for(let i = 0; i < this.guildPrefixes[key].length; i++) {
for(let i = 0; i < this.guildPrefixes[key].length; ++i) {
this.guildPrefixes[key][i] = this.guildPrefixes[key][i].replace(/@mention/g, this.user.mention);
}
} else {
Expand Down Expand Up @@ -90,7 +86,7 @@ class CommandClient extends Client {
if(!cur) {
return "Command not found";
}
label += " " + cur.label;
label += ` ${cur.label}`;
}
result += `**${msg.prefix}${label}** ${cur.usage}\n${cur.fullDescription}`;
if(cur.aliases.length > 0) {
Expand Down Expand Up @@ -261,7 +257,7 @@ class CommandClient extends Client {
if(!this.preReady) {
this.guildPrefixes[guildID] = prefix;
} else if(Array.isArray(prefix)) {
for(let i = 0; i < prefix.length; i++) {
for(let i = 0; i < prefix.length; ++i) {
prefix[i] = prefix[i].replace(/@mention/g, this.user.mention);
}
this.guildPrefixes[guildID] = prefix;
Expand All @@ -280,7 +276,7 @@ class CommandClient extends Client {
} else if(Array.isArray(prefixes)) {
return prefixes.find((prefix) => msg.content.replace(/<@!/g, "<@").startsWith(prefix));
}
throw new Error("Unsupported prefix format | " + prefixes);
throw new Error(`Unsupported prefix format | ${prefixes}`);
}

/**
Expand All @@ -291,7 +287,7 @@ class CommandClient extends Client {
registerCommandAlias(alias, label) {
let caseInsensitiveLabel = false;
if(!this.commands[label] && !(this.commands[(label = label.toLowerCase())] && (caseInsensitiveLabel = this.commands[label.toLowerCase()].caseInsensitive))) {
throw new Error("No command registered for " + label);
throw new Error(`No command registered for ${label}`);
}
alias = caseInsensitiveLabel === true ? alias.toLowerCase() : alias;
if(this.commandAliases[alias]) {
Expand Down Expand Up @@ -363,7 +359,7 @@ class CommandClient extends Client {
}
let lowercaseCommand = label.toLowerCase();
if(this.commands[label] || (this.commands[lowercaseCommand] && this.commands[lowercaseCommand].caseInsensitive)) {
throw new Error("You have already registered a command for " + label);
throw new Error(`You have already registered a command for ${label}`);
}
// Aliases are not deleted when deleting commands
let command = this.commandAliases[label]; // Just to make the following if statement less messy
Expand All @@ -380,7 +376,7 @@ class CommandClient extends Client {
}
label = options.caseInsensitive === true ? label.toLowerCase() : label;
if(this.commands[label]) {
throw new Error("You have already registered a command for " + label);
throw new Error(`You have already registered a command for ${label}`);
}
command = this.commandAliases[label];
if(this.commands[command]) {
Expand All @@ -390,7 +386,7 @@ class CommandClient extends Client {
options.aliases.forEach((alias) => {
lowercaseCommand = alias.toLowerCase();
if(this.commands[alias] || (this.commands[lowercaseCommand] && this.commands[lowercaseCommand].caseInsensitive)) {
throw new Error("You have already registered a command for alias " + alias);
throw new Error(`You have already registered a command for alias ${alias}`);
}
command = this.commandAliases[alias];
lowercaseCommand = this.commandAliases[alias.toLowerCase()];
Expand All @@ -399,7 +395,7 @@ class CommandClient extends Client {
}
alias = options.caseInsensitive === true ? alias.toLowerCase() : alias;
if(this.commands[alias]) {
throw new Error("You have already registered a command for alias " + alias);
throw new Error(`You have already registered a command for alias ${alias}`);
}
command = this.commandAliases[alias];
if(this.commands[command]) {
Expand Down Expand Up @@ -429,7 +425,7 @@ class CommandClient extends Client {
unwatchMessage(id, channelID) {
delete this.activeMessages[id];
if(channelID) {
this.removeMessageReactions(channelID, id).catch(function(){});
this.removeMessageReactions(channelID, id).catch(() => {});
}
}
}
Expand Down

0 comments on commit 3f35fb8

Please sign in to comment.