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
pull/54/head
Trivernis 6 years ago
parent d61ae6332d
commit ab4e2de2cb

@ -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

@ -86,6 +86,7 @@ class Bot {
messageHandler: this.messageHandler,
config: config
});
await this.messageHandler.registerCommandModule(require('./lib/commands/MiscCommands').module, {});
this.registerEvents();
}

@ -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
});

@ -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.
Loading…
Cancel
Save