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 aroundpull/54/head
parent
d61ae6332d
commit
ab4e2de2cb
@ -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…
Reference in New Issue