From 43e65e6949d0b4a7969f2fa5e9675a905b481f37 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Sun, 10 Feb 2019 14:27:39 +0100 Subject: [PATCH] Changed commands - changed global command syntax to templates - added ~about and ~bug --- bot.js | 46 +++++++++++++-------- commands/globalcommands.json | 78 +++++++++++++++++++++++++++++++++++- lib/cmd.js | 29 ++++++++------ 3 files changed, 122 insertions(+), 31 deletions(-) diff --git a/bot.js b/bot.js index 371d57f..370fb3d 100644 --- a/bot.js +++ b/bot.js @@ -8,6 +8,7 @@ const Discord = require("discord.js"), args = require('args-parser')(process.argv), waterfall = require('promise-waterfall'), sqliteAsync = require('./lib/sqliteAsync'), + globcommands = require('./commands/globalcommands.json'), authToken = args.token || config.api.botToken, prefix = args.prefix || config.prefix || '~', gamepresence = args.game || config.presence; @@ -161,21 +162,21 @@ class Bot { */ registerCommands() { // useless test command - cmd.createGlobalCommand(prefix + 'say', (msg, argv, args) => { + cmd.createGlobalCommand(prefix, globcommands.utils.say, (msg, argv, args) => { return args.join(' '); - }, [], "Repeats what you say"); + }); // adds a presence that will be saved in the presence file and added to the rotation - cmd.createGlobalCommand(prefix + 'addpresence', async (msg, argv, args) => { + cmd.createGlobalCommand(prefix, globcommands.utils.addpresence, async (msg, argv, args) => { let p = args.join(' '); this.presences.push(p); await this.maindb.run('INSERT INTO presences (text) VALUES (?)', [p]); return `Added Presence \`${p}\``; - }, [], "Adds a presence to the rotation.", 'owner'); + }); // shuts down the bot after destroying the client - cmd.createGlobalCommand(prefix + 'shutdown', async (msg) => { + cmd.createGlobalCommand(prefix, globcommands.utils.shutdown, async (msg) => { try { await msg.reply('Shutting down...'); logger.debug('Destroying client...'); @@ -198,10 +199,10 @@ class Bot { logger.error(err.message); logger.debug(err.stack); } - }, [], "Shuts the bot down.", 'owner'); + }); // forces a presence rotation - cmd.createGlobalCommand(prefix + 'rotate', () => { + cmd.createGlobalCommand(prefix, globcommands.utils.rotate, () => { try { this.client.clearInterval(this.rotator); this.rotatePresence(); @@ -209,15 +210,15 @@ class Bot { } catch (error) { logger.warn(error.message); } - }, [], 'Force presence rotation', 'owner'); + }); // ping command that returns the ping attribute of the client - cmd.createGlobalCommand(prefix + 'ping', () => { + cmd.createGlobalCommand(prefix, globcommands.info.ping, () => { return `Current average ping: \`${this.client.ping} ms\``; - }, [], 'Returns the current average ping', 'owner'); + }); // returns the time the bot is running - cmd.createGlobalCommand(prefix + 'uptime', () => { + cmd.createGlobalCommand(prefix , globcommands.info.uptime, () => { let uptime = utils.getSplitDuration(this.client.uptime); return new Discord.RichEmbed().setDescription(` **${uptime.days}** days @@ -226,14 +227,14 @@ class Bot { **${uptime.seconds}** seconds **${uptime.milliseconds}** milliseconds `).setTitle('Uptime'); - }, [], 'Returns the uptime of the bot', 'owner'); + }); // returns the numbe of guilds, the bot has joined - cmd.createGlobalCommand(prefix + 'guilds', () => { + cmd.createGlobalCommand(prefix, globcommands.info.guilds, () => { return `Number of guilds: \`${this.client.guilds.size}\``; - }, [], 'Returns the number of guilds the bot has joined', 'owner'); + }); - cmd.createGlobalCommand(prefix + 'createUser', (msg, argv) => { + cmd.createGlobalCommand(prefix, globcommands.utils.createUser, (msg, argv) => { return new Promise((resolve, reject) => { if (msg.guild) { resolve("It's not save here! Try again via PM."); @@ -249,7 +250,20 @@ class Bot { }).catch((err) => reject(err.message)); } }); - }, ['username', 'password', 'scope'], 'Generates a token for a username and returns it.', 'owner'); + }); + + cmd.createGlobalCommand(prefix, globcommands.info.about, () => { + return new Discord.RichEmbed() + .setTitle('About') + .setDescription(globcommands.info.about.response.about_creator) + .addField('Icon', globcommands.info.about.response.about_icon); + }); + + cmd.createGlobalCommand(prefix, globcommands.utils.bugreport, () => { + return new Discord.RichEmbed() + .setTitle('Where to report a bug?') + .setDescription(globcommands.utils.bugreport.response.bug_report); + }); } /** diff --git a/commands/globalcommands.json b/commands/globalcommands.json index e544b7d..7ee7886 100644 --- a/commands/globalcommands.json +++ b/commands/globalcommands.json @@ -3,11 +3,85 @@ "help": { "name": "help", "permission": "all", - "description": "Shows this help command", + "description": "Shows this help command.", "category": "Utility", "args": [ "command" ] + }, + "say": { + "name": "say", + "permission": "all", + "description": "Says something. ~say [String].", + "category": "Utility" + }, + "addpresence": { + "name": "addpresence", + "permission": "owner", + "description": "Adds a presence to presences.", + "category": "Utility" + }, + "shutdown": { + "name": "shutdown", + "description": "Shuts the bot down.", + "permission": "owner", + "category": "Utility" + }, + "rotate": { + "name": "rotate", + "description": "Forces a presence rotation", + "permission": "owner", + "category": "Utility" + }, + "createUser": { + "name": "createUser", + "permission": "owner", + "description": "Creates a new user for the webinterface.", + "category": "Utility", + "args": [ + "username", + "password", + "scope" + ] + }, + "bugreport": { + "name": "bug", + "permission": "all", + "description": "Get info about how to report a bug", + "category": "Utility", + "response": { + "bug_report": "Please report your bugs [here](https://github.com/Trivernis/discordbot.js/issues)" + } + } + }, + "info": { + "about": { + "name": "about", + "permission": "all", + "description": "Shows information about this bot.", + "category": "Info", + "response": { + "about_icon": "This icon war created by [blackrose14344](https://www.deviantart.com/blackrose14344). \n [Original](https://www.deviantart.com/blackrose14344/art/2B-Chi-B-685771489)", + "about_creator": "This bot was created by Trivernis. More about this bot [here](https://github.com/Trivernis/discordbot.js)." + } + }, + "ping": { + "name": "ping", + "permission": "owner", + "description": "Answers with the current average ping of the bot.", + "category": "Info" + }, + "uptime": { + "name": "uptime", + "permission": "owner", + "description": "Answers with the current uptime of the bot.", + "category": "Info" + }, + "guilds": { + "name": "guilds", + "permission": "owner", + "description": "Answers with the number of guilds the bot has joined", + "category": "Info" } } -} \ No newline at end of file +} diff --git a/lib/cmd.js b/lib/cmd.js index 697834e..997e5e4 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -183,21 +183,24 @@ exports.setLogger = function (newLogger) { /** * Creates a global command that can be executed in every channel. - * @param command + * @param prefix + * @param template * @param call - * @param args - * @param description - * @param role */ -exports.createGlobalCommand = function (command, call, args, description, role) { - globCommands[command] = { - 'args': args || [], - 'description': description, +exports.createGlobalCommand = function (prefix, template, call) { + if (!template.name) { + logger.debug(`Name of command template is null or undef. Failed to create command.`); + return; + } + globCommands[prefix + template.name] = { + 'args': template.args || [], + 'description': template.description, 'callback': call, - 'role': role, - 'name': command + 'role': template.permission, + 'name': template.name, + 'category': template.category || 'Other' }; - logger.debug(`Created global command: ${command}, args: ${args}`); + logger.debug(`Created global command: ${prefix + template.name}, args: ${template.args}`); }; @@ -215,7 +218,7 @@ exports.parseMessage = function (msg) { */ exports.init = function (prefix) { logger.verbose("Created help command"); - this.createGlobalCommand(((prefix || config.prefix) + 'help'), (msg, kwargs) => { + this.createGlobalCommand((prefix || config.prefix), gcmdTempl.utils.help, (msg, kwargs) => { if (kwargs.command) { let cmd = kwargs.command; if (cmd.charAt(0) !== prefix) @@ -244,7 +247,7 @@ exports.init = function (prefix) { helpEmbed.setDescription(description); return helpEmbed; } - }, ['command'], "Shows this help."); + }); }; function processCommand(cmd, msg, content, returnFunction) {