Skip to content

Instantly share code, notes, and snippets.

@Apexal
Created March 24, 2016 18:10
Show Gist options
  • Save Apexal/ea15f5fee79f8068239e to your computer and use it in GitHub Desktop.
Save Apexal/ea15f5fee79f8068239e to your computer and use it in GitHub Desktop.
// IN main.js
var Discord = require("discord.js");
var bot = new Discord.Client();
// Store all the commands from commands.js in a variable
var commands = require("./commands");
// Prefix that all commands must start with
var commandPrefix = "?";
function handleCommand(message) {
// Easy access to message author
var sender = message.author;
// Easy access to text of the message
var content = message.content.trim();
// Split up the text of the message by " "
var parts = content.split(" ");
// Get rid of the first part (the command name)
var args = parts.slice(1, parts.length);
// Take the first part (the command name)
var command = parts[0].replace(commandPrefix, "");
console.log(command + " command by " + sender.username + ": '"+content+"'");
// See if a function for this command exists in the command object (from commands.js)
if(commands[command] !== undefined) {
// Run the command
commands[command](bot, message, args);
} else {
// The command doesn't exist!
bot.reply(message, "Command not recognized!");
}
}
bot.on("message", function(message) {
// Check if message is command
if(message.content.startsWith(commandPrefix)) {
handleCommand(message);
}else{
}
});
bot.login("username", "password");
// -----------------
// IN commands.js
var commands = {};
commands.echo = function(bot, message, args) {
bot.reply(message, args.join(" "));
}
commands.help = function(bot, message, args) {
var help = ["**Command List**"];
for(var com in commands) {
help.push("`-` "+com);
}
bot.sendMessage(message.channel, help.join("\n"));
}
module.exports = commands;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment