You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.8 KiB
JavaScript
111 lines
3.8 KiB
JavaScript
6 years ago
|
const cmdLib = require('../../lib/CommandLib');
|
||
6 years ago
|
|
||
|
/**
|
||
|
* Utility commands are all commands that allow the user to control the behaviour of the
|
||
|
* bot. Utility commands for example are allowed to:
|
||
|
* - manipulate the main database
|
||
|
* - manipulate the bot's presences
|
||
|
* - manipulate the process (e.g. shutdown)
|
||
|
*/
|
||
|
|
||
|
class UtilityCommandModule extends cmdLib.CommandModule {
|
||
|
|
||
|
/**
|
||
|
* @param opts {Object} properties:
|
||
|
* bot - the instance of the bot.
|
||
|
* config - the config object
|
||
|
*/
|
||
|
constructor(opts) {
|
||
|
super(cmdLib.CommandScopes.User);
|
||
6 years ago
|
this._templateDir = __dirname;
|
||
6 years ago
|
this._bot = opts.bot;
|
||
|
this._config = opts.config;
|
||
6 years ago
|
}
|
||
|
|
||
|
async register(commandHandler) {
|
||
|
await this._loadTemplate();
|
||
|
|
||
|
let addPresence = new cmdLib.Command(
|
||
|
this.template.add_presence,
|
||
|
new cmdLib.Answer(async (m, k, s) => {
|
||
6 years ago
|
this._bot.presences.push(s);
|
||
|
await this._bot.maindb.run('INSERT INTO presences (text) VALUES (?)', [s]);
|
||
6 years ago
|
return `Added Presence \`${s}\``;
|
||
|
})
|
||
|
);
|
||
|
|
||
|
let rotatePresence = new cmdLib.Command(
|
||
|
this.template.rotate_presence,
|
||
|
new cmdLib.Answer(() => {
|
||
|
try {
|
||
6 years ago
|
this._bot.client.clearInterval(this._bot.rotator);
|
||
|
this._bot.rotatePresence();
|
||
|
this._bot.rotator = this._bot.client.setInterval(() => this._bot.rotatePresence(),
|
||
|
this._config.presence_duration);
|
||
6 years ago
|
} catch (error) {
|
||
6 years ago
|
this._logger.warn(error.message);
|
||
6 years ago
|
}
|
||
|
})
|
||
|
);
|
||
|
|
||
|
let shutdown = new cmdLib.Command(
|
||
|
this.template.shutdown,
|
||
|
new cmdLib.Answer(async (m) => {
|
||
|
try {
|
||
|
await m.reply('Shutting down...');
|
||
6 years ago
|
this._logger.debug('Destroying client...');
|
||
|
await this._bot.client.destroy();
|
||
6 years ago
|
} catch (err) {
|
||
6 years ago
|
this._logger.error(err.message);
|
||
|
this._logger.debug(err.stack);
|
||
6 years ago
|
}
|
||
|
try {
|
||
6 years ago
|
this._logger.debug('Exiting server...');
|
||
|
await this._bot.webServer.stop();
|
||
6 years ago
|
} catch (err) {
|
||
6 years ago
|
this._logger.error(err.message);
|
||
|
this._logger.debug(err.stack);
|
||
6 years ago
|
}
|
||
|
try {
|
||
6 years ago
|
this._logger.debug(`Exiting Process...`);
|
||
6 years ago
|
process.exit(0);
|
||
|
} catch (err) {
|
||
6 years ago
|
this._logger.error(err.message);
|
||
|
this._logger.debug(err.stack);
|
||
6 years ago
|
}
|
||
|
})
|
||
|
);
|
||
|
|
||
|
let createUser = new cmdLib.Command(
|
||
|
this.template.create_user,
|
||
|
new cmdLib.Answer(async (m, k) => {
|
||
|
if (k.username &&k.password && k.scope) {
|
||
6 years ago
|
this._logger.debug(`Creating user entry for ${k.username}`);
|
||
|
let token = await this._bot.webServer.createUser(
|
||
6 years ago
|
k.username, k.password, k.scope, false);
|
||
|
return `${k.username}'s token is ${token}`;
|
||
|
}
|
||
|
})
|
||
|
);
|
||
|
|
||
|
let bugReport = new cmdLib.Command(
|
||
|
this.template.bugreport,
|
||
|
new cmdLib.Answer(() => {
|
||
|
return new cmdLib.ExtendedRichEmbed(this.template.bugreport.response.title)
|
||
|
.setDescription(this.template.bugreport.response.bug_report);
|
||
|
})
|
||
|
);
|
||
|
|
||
|
// register commands
|
||
6 years ago
|
commandHandler.registerCommand(addPresence)
|
||
|
.registerCommand(rotatePresence)
|
||
|
.registerCommand(shutdown)
|
||
|
.registerCommand(createUser)
|
||
|
.registerCommand(bugReport);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
Object.assign(exports, {
|
||
|
'module': UtilityCommandModule
|
||
|
});
|