From ab4e2de2cbd8e6c7f560bfd5da38da2aa5a72145 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Sun, 3 Mar 2019 16:50:30 +0100 Subject: [PATCH] Added Misc CommandModule - added `~choose` to choose from options - added `~delay` for an asynchronous delay in command sequences - added `~say` for output in command sequences or just trolling around --- CHANGELOG.md | 11 ++-- bot.js | 1 + lib/commands/MiscCommands/index.js | 82 +++++++++++++++++++++++++ lib/commands/MiscCommands/template.yaml | 29 +++++++++ 4 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 lib/commands/MiscCommands/index.js create mode 100644 lib/commands/MiscCommands/template.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 24ff2f7..3c1704e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] -### Changes -- changed template Files to name `template.yaml` -- changed loading template file form CommandModule property `templateFile` to loading the `template.yaml` file from the `_templateDir` property (still supporting loading form templateFile) -- added *METADATA* property to `template.yaml` files that is used as an anchor for shared command metadata (like `category`) +### Changed +- template Files to name `template.yaml` +- loading template file form CommandModule property `templateFile` to loading the `template.yaml` file from the `_templateDir` property (still supporting loading form templateFile) ### Added -- added `.template` to commands as a template for a command module with help comments +- `.template` to commands as a template for a command module with help comments +- *METADATA* property to `template.yaml` files that is used as an anchor for shared command metadata (like `category`) +- CommandModule misc with command that are not really fitting into any other module ### Removed diff --git a/bot.js b/bot.js index c2fe03c..155ec78 100644 --- a/bot.js +++ b/bot.js @@ -86,6 +86,7 @@ class Bot { messageHandler: this.messageHandler, config: config }); + await this.messageHandler.registerCommandModule(require('./lib/commands/MiscCommands').module, {}); this.registerEvents(); } diff --git a/lib/commands/MiscCommands/index.js b/lib/commands/MiscCommands/index.js new file mode 100644 index 0000000..8cfc852 --- /dev/null +++ b/lib/commands/MiscCommands/index.js @@ -0,0 +1,82 @@ +/* template index.js. Doesn't implement actual commands */ +const cmdLib = require('../../CommandLib'); + +/** + * Several commands that are that special that they can't be included in any other module. + */ + +/** + * Async delay + * @param seconds {Number} + */ +function delay(seconds) { + return new Promise((resolve) => { + setTimeout(resolve, seconds * 1000); + }); +} + +class TemplateCommandModule extends cmdLib.CommandModule { + + constructor() { + super(cmdLib.CommandScopes.Global); + this._templateDir = __dirname; + } + + /** + * Defines and registers commands to the commandHandler. + * @param commandHandler {CommandHandler} + */ + async register(commandHandler) { + await this._loadTemplate(); + + let sayCommand = new cmdLib.Command( + this.template.say, + new cmdLib.Answer((m, k, s) => { + return s.replace(/^"|"$/g, ''); + }) + ); + + let delayCommand = new cmdLib.Command( + this.template.delay, + new cmdLib.Answer(async (m, k) => { + this._logger.silly(`Delaying for ${k.seconds} seconds`); + await delay(k.seconds); + }) + ); + + let chooseCommand = new cmdLib.Command( + this.template.choose, + new cmdLib.Answer(async (m, k, s) => { + let options = s.split(',').map(x => { + if (x) { + let strippedValue = x.replace(/^\s+|\s+$/, ''); + if (strippedValue.length === 0) + return null; + else + return strippedValue; + } else { + return null; + } + }).filter(x => x); + if (options.length === 0) { + return this.template.choose.response.no_options; + } else { + this._logger.silly(`Choosing from ${options.join(', ')}`); + let item = options[Math.floor(Math.random() * options.length)]; + return `I've chosen ${item.replace(/^"|"$|^\s+|\s+$/g, '')}`; + } + }) + ); + + /* Register commands to handler */ + commandHandler + .registerCommand(sayCommand) + .registerCommand(delayCommand) + .registerCommand(chooseCommand); + } +} + + +Object.assign(exports, { + module: TemplateCommandModule +}); diff --git a/lib/commands/MiscCommands/template.yaml b/lib/commands/MiscCommands/template.yaml new file mode 100644 index 0000000..189ccad --- /dev/null +++ b/lib/commands/MiscCommands/template.yaml @@ -0,0 +1,29 @@ +METADATA: &METADATA + category: Misc + permission: all + +say: + <<: *METADATA + name: say + usage: say [...message] + description: > + The bot says what you defined in the message argument + +delay: + <<: *METADATA + name: delay + usage: delay + args: + - seconds + description: > + Set a delay in seconds. Useful for command sequences. + +choose: + <<: *METADATA + name: choose + usage: choose [opt-1], [opt-2], ..., [opt-n] + description: > + Chooses randomly from one of the options + response: + no_options: > + You need to define options for me to choose from.