Skip to content

Commit

Permalink
Doc/style/code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
apacheli authored and abalabahaha committed Mar 7, 2019
1 parent 0fc778d commit e0a2fc0
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 52 deletions.
2 changes: 1 addition & 1 deletion lib/Client.js
Expand Up @@ -76,7 +76,7 @@ class Client extends EventEmitter {
* @arg {Number} [options.largeThreshold=250] The maximum number of offline users per guild during initial guild data transmission
* @arg {Number} [options.latencyThreshold=30000] The average request latency at which Eris will start emitting latency errors
* @arg {Number} [options.lastShardID=options.maxShards - 1] The ID of the last shard to run for this client
* @arg {Number|String} [options.maxShards=1] The total number of shards you want to run. If "auto" Eris will use Discord's recommended shard count.
* @arg {Number | String} [options.maxShards=1] The total number of shards you want to run. If "auto" Eris will use Discord's recommended shard count.
* @arg {Number} [options.messageLimit=100] The maximum size of a channel message cache
* @arg {Boolean} [options.opusOnly=false] Whether to suppress the node-opus not found error or not
* @arg {Number} [options.ratelimiterOffset=0] A number of milliseconds to offset the ratelimit timing calculations by
Expand Down
28 changes: 15 additions & 13 deletions lib/command/Command.js
Expand Up @@ -38,7 +38,7 @@ class Command {
* @arg {Function} [options.hooks.postCommand] A function that is executed after a response has been posted, and the command has finished processing. The function is passed the command message, arguments, and the response message (if applicable) as parameters.
* @arg {Function | String} [options.invalidUsageMessage] A string or a function that returns a string to show when a command was improperly used. The function is passed the Message object as a parameter.
* @arg {Function | String} [options.permissionMessage] A string or a function that returns a string to show when the user doesn't have permissions to use the command. The function is passed the Message object as a parameter.
* @arg {Array<{emoji: String, type: String, response: Function | String | Array<Function | String>}>} [options.reactionButtons] An array of objects specifying reaction buttons
* @arg {Array<{emoji: String, type: String, response: (Function | String | Array<Function | String>)}>} [options.reactionButtons] An array of objects specifying reaction buttons
* `emoji` specifies the button emoji. Custom emojis should be in format `emojiName:emojiID`
* `type` specifies the type of the reaction button, either "edit" or "cancel"
* `response` specifies the content to edit the message to when the reaction button is pressed. This accepts the same arguments as the `generator` parameter of this function, but with an extra userID parameter for generator functions (`function(msg, args, userID)`) describing the user that made the reaction
Expand Down Expand Up @@ -83,9 +83,15 @@ class Command {
this.dmOnly = !!options.dmOnly;
this.cooldown = options.cooldown || 0;
this.cooldownExclusions = options.cooldownExclusions || {};
if(!this.cooldownExclusions.userIDs) this.cooldownExclusions.userIDs = [];
if(!this.cooldownExclusions.guildIDs) this.cooldownExclusions.guildIDs = [];
if(!this.cooldownExclusions.channelIDs) this.cooldownExclusions.channelIDs = [];
if(!this.cooldownExclusions.userIDs) {
this.cooldownExclusions.userIDs = [];
}
if(!this.cooldownExclusions.guildIDs) {
this.cooldownExclusions.guildIDs = [];
}
if(!this.cooldownExclusions.channelIDs) {
this.cooldownExclusions.channelIDs = [];
}
this.restartCooldown = !!options.restartCooldown;
this.cooldownReturns = options.cooldownReturns || 0;
this.cooldownMessage = options.cooldownMessage || false;
Expand Down Expand Up @@ -114,7 +120,7 @@ class Command {
} else if(button.type === "cancel") {
return button;
} else {
throw new Error("Invalid reaction button response generator (index " + index + ")");
throw new Error(`Invalid reaction button response generator (index ${index})`);
}
}) : null;
this.reactionButtonTimeout = options.reactionButtonTimeout || 60000;
Expand All @@ -137,7 +143,7 @@ class Command {
} else if(typeof item === "function") {
return item;
} else {
throw new Error("Invalid command response generator (index " + index + ")");
throw new Error(`Invalid command response generator (index ${index})`);
}
});
this.execute = () => this.responses[Math.floor(Math.random() * this.responses.length)]();
Expand All @@ -154,18 +160,14 @@ class Command {
}

get fullLabel() {
if(this.parentCommand) {
return this.parentCommand.fullLabel + " " + this.label;
} else {
return this.label;
}
return `${this.parentCommand ? this.parentCommand.fullLabel + " " : ""}${this.label}`;
}

permissionCheck(msg) {
let req = false;
if(this.requirements.custom && typeof this.requirements.custom === "function") {
req = true;
if(this.requirements.custom(msg)){
if(this.requirements.custom(msg)) {
return true;
}
}
Expand Down Expand Up @@ -431,7 +433,7 @@ class Command {
* @arg {Function} [options.hooks.postCommand] A function that is executed after a response has been posted, and the command has finished processing. The function is passed the command message, arguments, and the response message (if applicable) as parameters.
* @arg {Function | String} [options.invalidUsageMessage] A string or a function that returns a string to show when a command was improperly used
* @arg {Function | String} [options.permissionMessage] A string or a function that returns a string to show when the user doesn't have permissions to use the command
* @arg {Array<{emoji: String, type: String, response: Function | String | Array<Function | String>}>} [options.reactionButtons] An array of objects specifying reaction buttons
* @arg {Array<{emoji: String, type: String, response: (Function | String | Array<Function | String>)}>} [options.reactionButtons] An array of objects specifying reaction buttons
* `emoji` specifies the button emoji. Custom emojis should be in format `emojiName:emojiID`
* `type` specifies the type of the reaction button, either "edit" or "cancel"
* `response` specifies the content to edit the message to when the reaction button is pressed. This accepts the same arguments as the `generator` parameter of this function, but with an extra userID parameter for generator functions (`function(msg, args, userID)`) describing the user that made the reaction
Expand Down
47 changes: 21 additions & 26 deletions lib/command/CommandClient.js
Expand Up @@ -23,7 +23,7 @@ class CommandClient extends Client {
* @arg {Boolean} [commandOptions.ignoreSelf=true] Whether to ignore the bot's own account or not
* @arg {String} [commandOptions.name="<Bot username>"] The bot name to show in the default help command
* @arg {String} [commandOptions.owner="an unknown user"] The owner to show in the default help command
* @arg {String|Array} [commandOptions.prefix="@mention "] The bot prefix. Can be either an array of prefixes or a single prefix. "@mention" will be automatically replaced with the bot's actual mention
* @arg {String | Array<String>} [commandOptions.prefix="@mention "] The bot prefix. Can be either an array of prefixes or a single prefix. "@mention" will be automatically replaced with the bot's actual mention
* @arg {Object} [commandOptions.defaultCommandOptions={}] Default command options. This object takes the same options as a normal Command
*/
constructor(token, options, commandOptions) {
Expand Down Expand Up @@ -53,19 +53,16 @@ class CommandClient extends Client {
if(!this.commandOptions.name) {
this.commandOptions.name = `**${this.user.username}**`;
}
if(Array.isArray(this.commandOptions.prefix)){
for(const i in this.commandOptions.prefix){
if(Array.isArray(this.commandOptions.prefix)) {
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 in this.guildPrefixes) {
if(!this.guildPrefixes.hasOwnProperty(key)) {
continue;
}
if(Array.isArray(this.guildPrefixes[key])){
for(const i in this.guildPrefixes[key]){
for(const key of Object.keys(this.guildPrefixes)) {
if(Array.isArray(this.guildPrefixes[key])) {
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 @@ -96,12 +93,13 @@ class CommandClient extends Client {
label += " " + cur.label;
}
result += `**${msg.prefix}${label}** ${cur.usage}\n${cur.fullDescription}`;
if(Object.keys(cur.aliases).length > 0) {
if(cur.aliases.length > 0) {
result += `\n\n**Aliases:** ${cur.aliases.join(", ")}`;
}
if(Object.keys(cur.subcommands).length > 0) {
const subcommands = Object.keys(cur.subcommands);
if(subcommands.length > 0) {
result += "\n\n**Subcommands:**";
for(const subLabel in cur.subcommands) {
for(const subLabel of subcommands) {
if(cur.subcommands.hasOwnProperty(subLabel) && cur.subcommands[subLabel].permissionCheck(msg)) {
result += `\n **${subLabel}** - ${cur.subcommands[subLabel].description}`;
}
Expand All @@ -112,8 +110,7 @@ class CommandClient extends Client {
if(this.commandOptions.owner) {
result += `by ${this.commandOptions.owner}\n`;
}
result += "\n";
result += "**Commands:**\n";
result += "\n**Commands:**\n";
for(const label in this.commands) {
if(this.commands.hasOwnProperty(label) && this.commands[label] && this.commands[label].permissionCheck(msg) && !this.commands[label].hidden) {
result += ` **${msg.prefix}${label}** - ${this.commands[label].description}\n`;
Expand Down Expand Up @@ -258,26 +255,24 @@ class CommandClient extends Client {
/**
* Register a prefix override for a specific guild
* @arg {String} guildID The ID of the guild to override prefixes for
* @arg {String|Array} prefix The bot prefix. Can be either an array of prefixes or a single prefix. "@mention" will be automatically replaced with the bot's actual mention
* @arg {String | Array} prefix The bot prefix. Can be either an array of prefixes or a single prefix. "@mention" will be automatically replaced with the bot's actual mention
*/
registerGuildPrefix(guildID, prefix) {
if(!this.preReady){
if(!this.preReady) {
this.guildPrefixes[guildID] = prefix;
} else {
if(Array.isArray(prefix)){
for(const i in prefix){
prefix[i] = prefix[i].replace(/@mention/g, this.user.mention);
}
this.guildPrefixes[guildID] = prefix;
} else {
this.guildPrefixes[guildID] = prefix.replace(/@mention/g, this.user.mention);
} else if(Array.isArray(prefix)) {
for(let i = 0; i < prefix.length; i++) {
prefix[i] = prefix[i].replace(/@mention/g, this.user.mention);
}
this.guildPrefixes[guildID] = prefix;
} else {
this.guildPrefixes[guildID] = prefix.replace(/@mention/g, this.user.mention);
}
}

checkPrefix(msg) {
let prefixes = this.commandOptions.prefix;
if(msg.channel.guild !== undefined && this.guildPrefixes[msg.channel.guild.id] !== undefined){
if(msg.channel.guild !== undefined && this.guildPrefixes[msg.channel.guild.id] !== undefined) {
prefixes = this.guildPrefixes[msg.channel.guild.id];
}
if(typeof prefixes === "string") {
Expand Down Expand Up @@ -338,7 +333,7 @@ class CommandClient extends Client {
* @arg {Function} [options.hooks.postCommand] A function that is executed after a response has been posted, and the command has finished processing. The function is passed the command message, arguments, and the response message (if applicable) as parameters.
* @arg {Function | String} [options.invalidUsageMessage] A string or a function that returns a string to show when a command was improperly used
* @arg {Function | String} [options.permissionMessage] A string or a function that returns a string to show when the user doesn't have permissions to use the command
* @arg {Array<{emoji: String, type: String, response: Function | String | Array<Function | String>}>} [options.reactionButtons] An array of objects specifying reaction buttons
* @arg {Array<{emoji: String, type: String, response: (Function | String | Array<Function | String>)}>} [options.reactionButtons] An array of objects specifying reaction buttons
* `emoji` specifies the button emoji. Custom emojis should be in format `emojiName:emojiID`
* `type` specifies the type of the reaction button, either "edit" or "cancel"
* `response` specifies the content to edit the message to when the reaction button is pressed. This accepts the same arguments as the `generator` parameter of this function, but with an extra userID parameter for generator functions (`function(msg, args, userID)`) describing the user that made the reaction
Expand Down
9 changes: 4 additions & 5 deletions lib/structures/CategoryChannel.js
@@ -1,6 +1,5 @@
"use strict";

const Channel = require("./Channel");
const Collection = require("../util/Collection");
const GuildChannel = require("./GuildChannel");

Expand All @@ -16,18 +15,18 @@ const GuildChannel = require("./GuildChannel");
* @prop {Number} position The position of the channel
* @prop {Boolean} nsfw Whether the channel is an NSFW channel or not
* @prop {Collection<PermissionOverwrite>} permissionOverwrites Collection of PermissionOverwrites in this channel
* @prop {Collection<Channel>?} channels A collection of guild channels that are part of this category
* @prop {Collection<GuildChannel>?} channels A collection of guild channels that are part of this category
*/
class CategoryChannel extends GuildChannel {
constructor(data, guild) {
super(data, guild);
this.channels = new Collection(Channel);
this.channels = new Collection(GuildChannel);
if(guild) {
guild.channels.forEach((channel) => {
for(const channel of guild.channels.values()) {
if(channel.parentID === this.id) {
this.channels.add(channel);
}
});
}
}

this.update(data);
Expand Down
6 changes: 3 additions & 3 deletions lib/structures/Guild.js
Expand Up @@ -3,9 +3,9 @@
const Base = require("./Base");
const CategoryChannel = require("./CategoryChannel");
const CDN_URL = require("../rest/Endpoints").CDN_URL;
const Channel = require("./Channel");
const Constants = require("../Constants");
const Collection = require("../util/Collection");
const GuildChannel = require("./GuildChannel");
const Member = require("./Member");
const Role = require("./Role");
const TextChannel = require("./TextChannel");
Expand All @@ -30,7 +30,7 @@ const VoiceChannel = require("./VoiceChannel");
* @prop {Boolean} unavailable Whether the guild is unavailable or not
* @prop {Boolean} large Whether the guild is "large" by "some Discord standard"
* @prop {Number} maxPresences The maximum number of people that can be online in a guild at once (returned from REST API only)
* @prop {Collection<Channel>} channels Collection of Channels in the guild
* @prop {Collection<GuildChannel>} channels Collection of Channels in the guild
* @prop {Collection<Member>} members Collection of Members in the guild
* @prop {Number} memberCount Number of members in the guild
* @prop {Collection<Role>} roles Collection of Roles in the guild
Expand All @@ -46,7 +46,7 @@ class Guild extends Base {
this.shard = client.shards.get(client.guildShardMap[this.id]);
this.unavailable = !!data.unavailable;
this.joinedAt = Date.parse(data.joined_at);
this.channels = new Collection(Channel);
this.channels = new Collection(GuildChannel);
this.members = new Collection(Member);
this.memberCount = data.member_count;
this.roles = new Collection(Role);
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/Message.js
Expand Up @@ -115,7 +115,7 @@ class Message extends Base {
this.mentionEveryone = !!data.mention_everyone;

this.mentions = data.mentions.map((mention) => {
let user = client.users.add(mention, client);
const user = client.users.add(mention, client);
if(mention.member && this.channel.guild) {
mention.member.id = mention.id;
this.channel.guild.members.update(mention.member, this.channel.guild);
Expand Down
6 changes: 3 additions & 3 deletions lib/voice/VoiceConnection.js
Expand Up @@ -636,14 +636,14 @@ class VoiceConnection extends EventEmitter {
return;
}
}
let hasExtension = !!(msg[0] & 0b10000);
let cc = msg[0] & 0b1111;
const hasExtension = !!(msg[0] & 0b10000);
const cc = msg[0] & 0b1111;
if(cc > 0) {
data = data.slice(cc * 4);
}
// Not a RFC5285 One Byte Header Extension (not negotiated)
if(hasExtension) { // RFC3550 5.3.1: RTP Header Extension
let l = data[2] << 8 | data[3];
const l = data[2] << 8 | data[3];
let index = 4 + l * 4;
while(data[index] == 0) {
++index;
Expand Down

0 comments on commit e0a2fc0

Please sign in to comment.