From 772caab56edc08e8a0ef2588626af77ef09c82fd Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 23 Feb 2018 20:09:34 -0500 Subject: [PATCH] Auto stash before merge of "master" and "origin/master" --- bot.js | 77 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/bot.js b/bot.js index ac661c7..c30b615 100644 --- a/bot.js +++ b/bot.js @@ -1,6 +1,33 @@ const Discord = require('discord.js'); const bot = new Discord.Client(); const config = require('./config.json'); +const fs = require('fs'); + + +bot.commands = new Discord.Collection(); +bot.aliases = 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); + } + }); + console.log('Command Loading complete!'); +}); + bot.on('ready', () => { console.log('PokeBot has finished loading.'); @@ -25,37 +52,29 @@ function parseCommand(msg) { if (msg.author.bot) return; if (!msg.content.startsWith('p:')) return; - const args = msg.content.slice(2).split(' '); + const args = msg.content.slice(2).trim().split(/ +/g); const command = args.shift(); - switch (command) - { - case 'help': - msg.channel.send( - new Discord.RichEmbed() - .setColor (0x00ae86) - .setTitle('PokeBot Command List') - .setDescription('These are the commands you can use. My prefix is `p:`') - .addField('Core', 'help\nping', true) - .addField('Utility', 'suggest', true) - .setFooter('PokeBot Beta') - ); - break; - case 'ping': - msg.channel.send(':ping_pong: Pong! ' + Math.floor(bot.ping) + 'ms.'); - break; - case 'suggest': - bot.channels.get('416726932927938570').send( - new Discord.RichEmbed() - .setColor (0x00ae86) - .setAuthor(msg.author.tag, msg.author.displayavatarURL) - .setDescription('This is a suggestion from a community member for something relating to the server. Please rate it based on your opinion, and a staff member will decide what to do with the suggestion.') - .addField('Suggestion Contents', args.join(' ')) - ).then(message => { - message.react('\u2705'); - message.react('\u274E'); - }); - break; + 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)); + } + + if (cmd) { + if (cmd.conf.guildOnly == true) { + if (!msg.channel.guild) { + return msg.channel.createMessage('This command can only be ran in a guild.'); + } + } + try { + cmd.run(bot, msg, args); + } + catch (e) { + console.error('Error while running command' + e.stack); + } } } bot.login(config.token);