const Discord = require("discord.js");
const Client = new Discord.Client();
const CommandClient = require("./CommandClient");
CommandClient(Client, "!");
Client.commands.register("ping", (args, message) => {
message.reply("Pong!");
}, {
runIf: m => m.author.id === "1234567890"
});
Client.commands.register("eval", (args, message) => {
eval(args.join(" "));
}, {
runIf: m => {
if(m.author.id !== "1234567890"){
m.reply("You aren't allowed to use this command!");
return false;
}
return true;
},
showHelp: false
});
Last active
April 25, 2018 02:52
-
-
Save hcortezr/75ac0538130cc0a33e23627270f1b93f to your computer and use it in GitHub Desktop.
Discord Command Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* CommandClient | |
* have custom commands for your bot | |
* @author eDroid | |
*/ | |
const Discord = require("discord.js"); | |
class CommandClient { | |
constructor(client, prefix = "/"){ | |
this.client = client; | |
this.prefix = prefix; | |
this._commands = new Map(); | |
this._init(); | |
} | |
_init(){ | |
this.client.on("message", message => { | |
this._handle(message); | |
}); | |
} | |
_handle(message){ | |
if(!message.content.startsWith(this.prefix)) return; | |
let args = message.content.split(" "); | |
let cmd = args.shift().substr(1).toLowerCase(); | |
if(!this._commands.has(cmd)) return; | |
let command = this._commands.get(cmd); | |
if((typeof command.options.runIf === "function" && command.options.runIf(message)) || command.options.runIf === undefined) command.cb(args, message); | |
} | |
register(name, callable, opts = {}){ | |
if((typeof name !== "string") || (typeof callable !== "function")) return false; | |
if(typeof opts !== "object") opts = {}; | |
if(this._commands.has(name)) return false; | |
this._commands.set(name, {cb: callable, options: opts}); | |
return true; | |
} | |
unregister(name){ | |
return this._commands.delete(name); | |
} | |
setHelpCommand(options){ | |
return this.register("help", (args, msg) => { | |
msg.channel.startTyping(); | |
let embed = new Discord.RichEmbed(); | |
embed | |
.setColor(options.color ? options.color : "#000000") | |
.setTitle(`Help for ${options.name ? options.name : this.client.user.username}:`) | |
.setDescription(options.description ? options.description : "[arg] = optional, <arg> = required") | |
.setFooter(options.footer ? (options.footer.text ? options.footer.text : this.client.user.username) : this.client.user.username, options.footer ? (options.footer.icon ? options.footer.icon : this.client.user.avatarURL) : this.client.user.avatarURL); | |
let fields = []; | |
new Map([...this._command.entries()].sort()).forEach((command, name) => { | |
if(command.options.showHelp === false) return; | |
fields.push({ | |
name: `${this.prefix}${name}`, | |
value: [ | |
command.options.description ? command.options.description : "No Description", | |
command.options.usage ? `**Usage:** ${command.options.usage.replace(/{cmd}/g, `${this.prefix}${name}`)}` : "No Usage", | |
command.options.example ? `**Example:** ${command.options.example.replace(/{cmd}/g, `${this.prefix}${name}`)}` : "No Example" | |
].join("\n") | |
}); | |
}); | |
if(fields.length === 0){ | |
fields.push({ | |
name: "Whoops!", | |
value: "No commands found!" | |
}); | |
} | |
embed.fields = fields; | |
msg.channel.send(embed).then(m => m.channel.stopTyping()).catch(console.error); | |
}, { | |
showHelp: false | |
}); | |
} | |
} | |
module.exports = function(client, prefix){ | |
client.commands = new CommandClient(client, prefix); | |
return client; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* CommandClient | |
* have custom commands for your bot | |
* @author eDroid | |
*/ | |
const Discord = require("discord.js"); | |
class CommandClient { | |
constructor(client, prefix = "/"){ | |
this.client = client; | |
this.prefix = prefix; | |
this._commands = new Map(); | |
this._init(); | |
} | |
_init(){ | |
this.client.on("message", message => { | |
this._handle(message); | |
}); | |
} | |
_handle(message){ | |
if(!message.content.startsWith(this.prefix)) return; | |
let args = message.content.split(" "); | |
let cmd = args.shift().substr(this.prefix.length).toLowerCase(); | |
if(!this._commands.has(cmd)) return; | |
let command = this._commands.get(cmd); | |
if((typeof command.options.runIf === "function" && command.options.runIf(message)) || command.options.runIf === undefined) command.run(args, message); | |
} | |
register(name, callable, opts = {}){ | |
if((typeof name !== "string") || (typeof callable !== "function")) return false; | |
if(typeof opts !== "object") opts = {}; | |
if(this._commands.has(name)) return false; | |
this._commands.set(name, {name: name, run: callable, options: opts}); | |
return true; | |
} | |
unregister(name){ | |
return this._commands.delete(name); | |
} | |
hasCommand(name){ | |
return this._commands.has(name); | |
} | |
getCommand(name){ | |
return this._commands.get(name); | |
} | |
getCategories(){ | |
let categories = []; | |
this._commands.forEach(command => typeof command.options.category === "string" && categories.indexOf(command.options.category) === -1 ? categories.push(command.options.category) : null); | |
return categories; | |
} | |
getUncategorizedCommands(){ | |
return Array.from(this._commands.values()).filter(command => typeof command.options.category === "undefined").sort((x, y) => x.name < y.name ? -1 : x.name > y.name ? 1 : 0); | |
} | |
getCommandsByCategory(category){ | |
return Array.from(this._commands.values()).filter(command => typeof command.options.category === "string" && command.options.category === category).sort((x, y) => x.name < y.name ? -1 : x.name > y.name ? 1 : 0); | |
} | |
setHelpCommand(options){ | |
return this.register("help", (args, msg) => { | |
if(args.length === 0){ | |
msg.channel.startTyping(); | |
let embed = new Discord.RichEmbed(); | |
embed | |
.setColor(options.color ? options.color : "#000000") | |
.setTitle(`Help for ${options.name ? options.name : this.client.user.username}:`) | |
.setDescription(options.description ? options.description : `Run \`${this.prefix}help [command]\` for help on a certain command.`) | |
.addBlankField() | |
.setFooter(options.footer ? (options.footer.text ? options.footer.text : this.client.user.username) : this.client.user.username, options.footer ? (options.footer.icon ? options.footer.icon : this.client.user.avatarURL) : this.client.user.avatarURL); | |
this.getCategories().forEach(category => { | |
embed.addField(category, this.getCommandsByCategory(category).filter(command => command.options.showHelp !== false).map(command => this.prefix + command.name).join("\n"), true); | |
}); | |
if(this.getUncategorizedCommands().length > 0) embed.addField("Uncategorized", this.getUncategorizedCommands().filter(command => command.options.showHelp !== false).map(command => this.prefix + command.name).join("\n"), true); | |
msg.channel.send(embed).then(m => m.channel.stopTyping()).catch(e => { | |
console.error(e); | |
msg.reply("An error occured while sending embed!"); | |
}); | |
}else if(args.length > 0){ | |
let cmd = args[0].toLowerCase(); | |
if(this.hasCommand(cmd)){ | |
let command = this.getCommand(cmd); | |
if(command.options.showHelp === false){ | |
msg.reply("No help found for that command."); | |
return; | |
} | |
let embed = new Discord.RichEmbed(); | |
embed | |
.setColor(options.color ? options.color : "#000000") | |
.setTitle(`Help for ${this.prefix + cmd}:`) | |
.setDescription([ | |
"[arg] = optional, <arg> = required", | |
"", | |
command.options.description ? command.options.description : "No Description", | |
command.options.usage ? `**Usage:** ${command.options.usage.replace(/{cmd}/g, `${this.prefix}${name}`)}` : "No Usage", | |
command.options.example ? `**Example:** ${command.options.example.replace(/{cmd}/g, `${this.prefix}${name}`)}` : "No Example" | |
].join("\n")); | |
msg.channel.send(embed).then(m => m.channel.stopTyping()).catch(e => { | |
console.error(e); | |
msg.reply("An error occured while sending embed!"); | |
}); | |
}else{ | |
msg.reply("Command not found!"); | |
} | |
} | |
}, { | |
showHelp: false | |
}); | |
} | |
} | |
module.exports = function(client, prefix){ | |
client.commands = new CommandClient(client, prefix); | |
return client; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment