diff options
| -rw-r--r-- | bot.js | 61 | ||||
| -rw-r--r-- | commands/Fun/8ball.js (renamed from commands/8ball.js) | 1 | ||||
| -rw-r--r-- | commands/Fun/cureboredom.js (renamed from commands/cureboredom.js) | 1 | ||||
| -rw-r--r-- | commands/Fun/og151.js (renamed from commands/og151.js) | 1 | ||||
| -rw-r--r-- | commands/Fun/ship.js (renamed from commands/ship.js) | 1 | ||||
| -rw-r--r-- | commands/Fun/story.js (renamed from commands/story.js) | 1 | ||||
| -rw-r--r-- | commands/Getting Started/help.js | 31 | ||||
| -rw-r--r-- | commands/Information/contribute.js (renamed from commands/contribute.js) | 1 | ||||
| -rw-r--r-- | commands/Information/ping.js (renamed from commands/ping.js) | 1 | ||||
| -rw-r--r-- | commands/Moderation/ban.js (renamed from commands/ban.js) | 1 | ||||
| -rw-r--r-- | commands/Moderation/kick.js (renamed from commands/kick.js) | 1 | ||||
| -rw-r--r-- | commands/Moderation/purge.js (renamed from commands/purge.js) | 1 | ||||
| -rw-r--r-- | commands/Moderation/softban.js (renamed from commands/softban.js) | 1 | ||||
| -rw-r--r-- | commands/Moderation/warn.js (renamed from commands/warn.js) | 1 | ||||
| -rw-r--r-- | commands/Owners Only/poweroff.js (renamed from commands/poweroff.js) | 1 | ||||
| -rw-r--r-- | commands/Owners Only/say.js (renamed from commands/say.js) | 1 | ||||
| -rw-r--r-- | commands/Teams/join.js (renamed from commands/join.js) | 1 | ||||
| -rw-r--r-- | commands/Teams/leave.js (renamed from commands/leave.js) | 1 | ||||
| -rw-r--r-- | commands/Utility/suggest.js (renamed from commands/suggest.js) | 1 | ||||
| -rw-r--r-- | commands/Utility/uptime.js (renamed from commands/uptime.js) | 1 | ||||
| -rw-r--r-- | commands/help.js | 40 | ||||
| -rw-r--r-- | events/message.js | 29 |
22 files changed, 92 insertions, 87 deletions
@@ -5,27 +5,50 @@ const fs = require('fs'); bot.commands = new Discord.Collection(); bot.aliases = new Discord.Collection(); +bot.categories = new Discord.Collection(); -fs.readdir('./commands', (err, files) => { - if (err) console.error(err); - console.log(`Attempting to load a total of ${files.length} commands into the memory.`); - files.forEach(file => { - try { - const command = require(`./commands/${file}`); - console.log(`Attempting to load the command "${command.help.name}".`); - bot.commands.set(command.help.name, command); - command.conf.aliases.forEach(alias => { - bot.aliases.set(alias, command.help.name); - console.log(`Attempting to load "${alias}" as an alias for "${command.help.name}"`); - }); - } - catch (err) { - console.log('An error has occured trying to load a command. Here is the error.'); - console.log(err.stack); - } +cmdLoader(); + +async function cmdLoader() { + const categories = await fs.readdirSync('./commands'); + console.log(`Loading ${categories.length} categories(s) into memory\n`); + categories.forEach(x => { + loadGroup(x); }); - console.log('Command Loading complete!'); -}); +} +async function loadGroup(name) { + const files = await fs.readdirSync(`./commands/${name}`); + + console.log(`Loading the category '${name}' into memory with a total of ${files.length} command(s)`); + + bot.commands.set(name, new Map()); + bot.aliases.set(name, new Map()); + + const commands = []; + files.forEach(x => { + loadCmd(name, x); + commands.push(x.split('.')[0]); + }); + + bot.categories.set(name, commands); + console.log(`The category ${name} has been loaded.\n`); +} + +async function loadCmd(category, cmd) { + try { + console.log(`Loading the Command ${cmd.split('.')[0]}`); + const command = require(`./commands/${category}/${cmd}`); + bot.commands.get(category).set(command.help.name, command); + command.conf.aliases.forEach(alias => { + bot.aliases.get(category).set(alias, command.help.name); + }); + } + catch (err) { + console.log(`An error has occured trying to load the command '${cmd.split('.')[0]}'`); + console.log(err.stack); + } +} + fs.readdir('./events', (err, files) => { if (err) console.error(err); diff --git a/commands/8ball.js b/commands/Fun/8ball.js index f188270..3ebd1c3 100644 --- a/commands/8ball.js +++ b/commands/Fun/8ball.js @@ -44,5 +44,4 @@ exports.help = { name: '8ball', description: 'Ask the magic 8-ball something. It will answer back, and be as much of a smart-alac as it wants to.', usage: '<...question>', - category: 'Fun', }; diff --git a/commands/cureboredom.js b/commands/Fun/cureboredom.js index e011c80..9996645 100644 --- a/commands/cureboredom.js +++ b/commands/Fun/cureboredom.js @@ -20,5 +20,4 @@ exports.conf = { exports.help = { name: 'cureboredom', description: 'Finds you something to do.', - category: 'Fun', }; diff --git a/commands/og151.js b/commands/Fun/og151.js index fd7f3e5..bda9059 100644 --- a/commands/og151.js +++ b/commands/Fun/og151.js @@ -164,5 +164,4 @@ exports.conf = { exports.help = { name: 'og151', description: 'Randomly picks one of the generation 1 pokemon, and gives you its name.', - category: 'Fun', }; diff --git a/commands/ship.js b/commands/Fun/ship.js index 8282bf3..ed8cf69 100644 --- a/commands/ship.js +++ b/commands/Fun/ship.js @@ -10,5 +10,4 @@ exports.conf = { exports.help = { name: 'ship', description: 'Test the luck of your love life! Ships you with another user.', - category: 'Fun', }; diff --git a/commands/story.js b/commands/Fun/story.js index 85e535e..037b7cb 100644 --- a/commands/story.js +++ b/commands/Fun/story.js @@ -24,5 +24,4 @@ exports.conf = { exports.help = { name: 'story', description: 'Tells you a story.', - category: 'Fun', }; diff --git a/commands/Getting Started/help.js b/commands/Getting Started/help.js new file mode 100644 index 0000000..13ac206 --- /dev/null +++ b/commands/Getting Started/help.js @@ -0,0 +1,31 @@ +exports.run = (bot, msg) => { + const { RichEmbed } = require('discord.js'); + const embed = new RichEmbed(); + embed + .setColor (0x00ae86) + .setDescription('Notice: When using a command do not include "<" and ">".\n(Example: p:suggest Test)') + .setFooter('PokeBot Beta'); + + const categories = Array.from(bot.categories.keys()); + categories.forEach(x => { + let cat = ''; + const commands = bot.categories.get(x); + commands.forEach(cmd => { + const command = bot.commands.get(x).get(cmd); + const usage = command.help.usage ? `*${command.help.usage}* ` : ''; + cat += `**p:${command.help.name}** ${usage}| ${command.help.description} \n`; + }); + embed.addField(`${x} |`, cat); + }); + msg.channel.send({ embed }); +}; + +exports.conf = { + aliases: [], + guildOnly: true, +}; + +exports.help = { + name: 'help', + description: 'Displays this help message.', +}; diff --git a/commands/contribute.js b/commands/Information/contribute.js index b6a2d6f..6a578fe 100644 --- a/commands/contribute.js +++ b/commands/Information/contribute.js @@ -10,5 +10,4 @@ exports.conf = { exports.help = { name: 'contribute', description: 'Contributing to the bot.', - category: 'Information', }; diff --git a/commands/ping.js b/commands/Information/ping.js index d1d9204..f4e50e4 100644 --- a/commands/ping.js +++ b/commands/Information/ping.js @@ -10,5 +10,4 @@ exports.conf = { exports.help = { name: 'ping', description: 'Pings the bot and replies with the latency.', - category:'Information', }; diff --git a/commands/ban.js b/commands/Moderation/ban.js index 902b4fd..3582bc3 100644 --- a/commands/ban.js +++ b/commands/Moderation/ban.js @@ -37,5 +37,4 @@ exports.help = { name: 'ban', description: 'Ban a user from this server.', usage: '@user <...reason>', - category: 'Moderation', }; diff --git a/commands/kick.js b/commands/Moderation/kick.js index b015d01..040abb5 100644 --- a/commands/kick.js +++ b/commands/Moderation/kick.js @@ -20,5 +20,4 @@ exports.help = { name: 'kick', description: 'Kick a user out of the server.', usage: '@user <...reason>', - category: 'Moderation', }; diff --git a/commands/purge.js b/commands/Moderation/purge.js index f02cb0a..0540bb3 100644 --- a/commands/purge.js +++ b/commands/Moderation/purge.js @@ -27,5 +27,4 @@ exports.help = { name: 'purge', description: 'Get rid of messages quickly.', usage: '@user <messages>', - category:'Moderation', }; diff --git a/commands/softban.js b/commands/Moderation/softban.js index 3044090..f95e3b8 100644 --- a/commands/softban.js +++ b/commands/Moderation/softban.js @@ -37,5 +37,4 @@ exports.help = { name: 'softban', description: 'Kick the user and delete their messages.', usage: '@user <...reason>', - category: 'Moderation', }; diff --git a/commands/warn.js b/commands/Moderation/warn.js index 88305a0..75d586c 100644 --- a/commands/warn.js +++ b/commands/Moderation/warn.js @@ -29,5 +29,4 @@ exports.help = { name: 'warn', description: 'Logs a warning to the user.', usage : '@user <reason>', - category: 'Moderation', }; diff --git a/commands/poweroff.js b/commands/Owners Only/poweroff.js index ae2ec67..ddd0c56 100644 --- a/commands/poweroff.js +++ b/commands/Owners Only/poweroff.js @@ -17,5 +17,4 @@ exports.conf = { exports.help = { name: 'poweroff', description: 'Powers off the bot.', - category: 'Owners Only', }; diff --git a/commands/say.js b/commands/Owners Only/say.js index 1d57a06..0f8a6df 100644 --- a/commands/say.js +++ b/commands/Owners Only/say.js @@ -17,5 +17,4 @@ exports.help = { name: 'say', description: 'Control on what the bot says.', usage: '<...text>', - category: 'Owners Only', }; diff --git a/commands/join.js b/commands/Teams/join.js index dc3ba84..c9ba26e 100644 --- a/commands/join.js +++ b/commands/Teams/join.js @@ -57,5 +57,4 @@ exports.help = { name: 'join', description: 'Join one of the teams!', usage: '<mystic/valor/instinct>', - category: 'Teams', }; diff --git a/commands/leave.js b/commands/Teams/leave.js index 850d7ce..c22a1f2 100644 --- a/commands/leave.js +++ b/commands/Teams/leave.js @@ -24,5 +24,4 @@ exports.conf = { exports.help = { name: 'leave', description: 'Leave the team you currently are in.', - category: 'Teams', }; diff --git a/commands/suggest.js b/commands/Utility/suggest.js index 1332fde..831d222 100644 --- a/commands/suggest.js +++ b/commands/Utility/suggest.js @@ -21,5 +21,4 @@ exports.help = { name: 'suggest', description: 'Suggest a feature for the bot or the server.', usage: '<...suggestion>', - category: 'Utility', }; diff --git a/commands/uptime.js b/commands/Utility/uptime.js index 3c2f177..63bd6a4 100644 --- a/commands/uptime.js +++ b/commands/Utility/uptime.js @@ -21,5 +21,4 @@ exports.conf = { exports.help = { name: 'uptime', description: 'Get the uptime of the bot.', - category: 'Utility', }; diff --git a/commands/help.js b/commands/help.js deleted file mode 100644 index 6e97fcd..0000000 --- a/commands/help.js +++ /dev/null @@ -1,40 +0,0 @@ -exports.run = (bot, msg) => { - const { RichEmbed } = require('discord.js'); - const embed = new RichEmbed(); - embed - .setColor (0x00ae86) - .setDescription('Notice: When using a command do not include "<" and ">".\n(Example: p:suggest Test)') - .setFooter('PokeBot Beta'); - - const categories = []; - const commands = Array.from(bot.commands.keys()); - - commands.forEach(function(x) { - if (!categories.includes(bot.commands.get(x).help.category)) { - categories.push(bot.commands.get(x).help.category); - } - }); - - categories.forEach(function(x) { - let cat = ''; - commands.forEach(function(command) { - if (bot.commands.get(command).help.category == x) { - const usage = bot.commands.get(command).help.usage ? `*${bot.commands.get(command).help.usage}* ` : ''; - cat += `**p:${command}** ${usage}| ${bot.commands.get(command).help.description} \n`; - } - }); - embed.addField(`${x} |`, cat); - }); - msg.channel.send({ embed }); -}; - -exports.conf = { - aliases: [], - guildOnly: true, -}; - -exports.help = { - name: 'help', - description: 'Displays this help message.', - category: 'Getting Started', -}; diff --git a/events/message.js b/events/message.js index baf4983..7efd7ca 100644 --- a/events/message.js +++ b/events/message.js @@ -12,33 +12,42 @@ module.exports = (bot, msg) => { } }; - function parseCommand(bot, msg) { + let category; + + const prefix = 'p:'; if (msg.author.bot) return; - if (!msg.content.startsWith('p:')) return; - const args = msg.content.slice(2).trim().split(/ +/g); + if (!msg.content.startsWith(prefix)) return; + const args = msg.content.slice(prefix.length).trim().split(/ +/g); const command = args.shift(); - let cmd; - if (bot.commands.has(command)) { - cmd = bot.commands.get(command); - } else if (bot.aliases.has(command)) { - cmd = bot.commands.get(bot.aliases.get(command)); + Array.from(bot.categories.keys()).forEach(i => { + const cmds = bot.categories.get(i); + if (cmds.includes(command)) category = i; + }); + if (!category) return; + + if (bot.commands.get(category).has(command)) { + cmd = bot.commands.get(category).get(command); + } else if (bot.aliases.get(category).has(command)) { + cmd = bot.commands.get(category).get(bot.aliases.get(command)); } if (cmd) { if (cmd.conf.guildOnly == true) { if (!msg.channel.guild) { - return msg.channel.createMessage('This command can only be ran in a guild.'); + return msg.reply('This command can only be ran in a guild.'); } } try { + console.log(`${msg.author.tag} ran the command '${cmd.help.name}' in the guild '${msg.member.guild.name}'`); cmd.run(bot, msg, args); } catch (e) { - console.error('Error while running command' + e.stack); + console.error(e.stack); + msg.channel.send('There was an error trying to process your command. Don\'t worry because this issue is being looked into'); } } } |
