314 lines
8.5 KiB
TypeScript
314 lines
8.5 KiB
TypeScript
|
import { logger } from '@squeebot/core/lib/core';
|
||
|
import { IRepository } from '@squeebot/core/lib/plugin/repository';
|
||
|
import { ReadLine } from 'readline';
|
||
|
import { Squeebot } from './core';
|
||
|
|
||
|
export class SqueebotCLI {
|
||
|
constructor(private bot: Squeebot) {}
|
||
|
|
||
|
private checkUpdate(repo: IRepository): void {
|
||
|
this.bot.repositoryManager.checkForUpdates(repo).then((updatable) => {
|
||
|
if (updatable.length) {
|
||
|
logger.log('[%s] The following plugins can be updated:', repo.name,
|
||
|
updatable.map((u) => u.name).join(', '));
|
||
|
} else {
|
||
|
logger.log('[%s] All plugins are up-to-date!', repo.name);
|
||
|
}
|
||
|
}, (e) => logger.error(e.message));
|
||
|
}
|
||
|
|
||
|
private repositoryCommand(...args: any[]): void {
|
||
|
const help = 'repository add <url> | update <name> | remove <name>';
|
||
|
if (!args[0] || args[0] === 'help') {
|
||
|
logger.log(help);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
switch (args[0]) {
|
||
|
case 'a':
|
||
|
case 'i':
|
||
|
case 'add':
|
||
|
case 'install':
|
||
|
if (!args[1]) {
|
||
|
logger.error('URL is required');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.bot.repositoryManager.installRepository(args[1]).then((repo) => {
|
||
|
logger.log('Installed repository %s!', repo.name);
|
||
|
}, (e) => logger.error(e.message));
|
||
|
break;
|
||
|
case 'r':
|
||
|
case 'rem':
|
||
|
case 'remove':
|
||
|
case 'uninstall':
|
||
|
if (!args[1]) {
|
||
|
logger.error('Name is required');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.bot.repositoryManager.uninstallRepository(args[1]).then(() => {
|
||
|
logger.log('Installed repository %s.', args[1]);
|
||
|
}, (e) => logger.error(e.message));
|
||
|
break;
|
||
|
case 'u':
|
||
|
case 'upd':
|
||
|
case 'update':
|
||
|
if (!args[1]) {
|
||
|
const repos = this.bot.repositoryManager.getAll();
|
||
|
for (const repo of repos) {
|
||
|
this.checkUpdate(repo);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const repo = this.bot.repositoryManager.getRepoByName(args[1]);
|
||
|
if (!repo) {
|
||
|
logger.error('No such repository found.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.checkUpdate(repo);
|
||
|
break;
|
||
|
default:
|
||
|
logger.log(help);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private pluginCommand(...args: any[]): void {
|
||
|
const help = 'plugin install | update | uninstall | enable | disable | start | stop | list | running [<name>]';
|
||
|
if (!args[0] || args[0] === 'help' || (!args[1] && args[0] !== 'list' && args[0] !== 'running')) {
|
||
|
logger.log(help);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
switch (args[0]) {
|
||
|
case 'i':
|
||
|
case 'u':
|
||
|
case 'install':
|
||
|
case 'update':
|
||
|
this.bot.repositoryManager.installPlugin(args[1]).then((mf) => {
|
||
|
logger.log('Installed plugin %s version %s!', mf.name, mf.version);
|
||
|
}, (e) => console.error(e.message));
|
||
|
|
||
|
break;
|
||
|
case 'uninst':
|
||
|
case 'remove':
|
||
|
case 'uninstall':
|
||
|
this.bot.repositoryManager.uninstallPlugin(args[1]).then(() => {
|
||
|
logger.log('Uninstalled plugin %s.', args[1]);
|
||
|
}, (e) => console.error(e.message));
|
||
|
break;
|
||
|
case 'list':
|
||
|
logger.log('Installed plugins:',
|
||
|
this.bot.pluginManager.availablePlugins.map((mf) => mf.name).join(', '));
|
||
|
break;
|
||
|
case 'running':
|
||
|
logger.log('Currently running plugins:',
|
||
|
this.bot.pluginManager.getLoaded().map((p) => p.manifest.name).join(', '));
|
||
|
break;
|
||
|
case 's':
|
||
|
case 'run':
|
||
|
case 'load':
|
||
|
case 'start':
|
||
|
const plugin = this.bot.pluginManager.getAvailableByName(args[1]);
|
||
|
if (!plugin) {
|
||
|
logger.error('No such plugin is available. Maybe try installing it? plugin install', args[1]);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.bot.pluginManager.load(plugin).then((p) => {
|
||
|
logger.log('Started plugin "%s" successfully.', args[1]);
|
||
|
}, (e) => logger.error(e.stack));
|
||
|
|
||
|
break;
|
||
|
case 'stop':
|
||
|
case 'kill':
|
||
|
if (!this.bot.pluginManager.getAvailableByName(args[1])) {
|
||
|
logger.error('No such plugin is available.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
logger.log('Stopping plugin', args[1]);
|
||
|
|
||
|
this.bot.stream.emitTo(args[1], 'pluginUnload', args[1]);
|
||
|
break;
|
||
|
case 'enable':
|
||
|
if (!this.bot.pluginManager.getAvailableByName(args[1])) {
|
||
|
logger.error('No such plugin is available.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
logger.log('Enabling plugin', args[1]);
|
||
|
|
||
|
if (!this.bot.config.config.enabled) {
|
||
|
this.bot.config.config.enabled = [args[1]];
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (this.bot.config.config.enabled.indexOf(args[1]) === -1) {
|
||
|
this.bot.config.config.enabled.push(args[1]);
|
||
|
}
|
||
|
|
||
|
this.bot.config.save();
|
||
|
break;
|
||
|
case 'disable':
|
||
|
if (!this.bot.pluginManager.getAvailableByName(args[1])) {
|
||
|
logger.error('No such plugin is available.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
logger.log('Disabling plugin', args[1]);
|
||
|
|
||
|
if (!this.bot.config.config.enabled) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const indx = this.bot.config.config.enabled.indexOf(args[1]);
|
||
|
if (indx > -1) {
|
||
|
this.bot.config.config.enabled.splice(indx, 1);
|
||
|
}
|
||
|
|
||
|
this.bot.config.save();
|
||
|
break;
|
||
|
default:
|
||
|
logger.log(help);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private channelCommand(...args: any[]): void {
|
||
|
const help = 'channel new | del | addplugin | delplugin [<name>] [<plugin>]';
|
||
|
if (!args[0] || args[0] === 'help' || (!args[1] && args[0] !== 'list')) {
|
||
|
logger.log(help);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
switch(args[0]) {
|
||
|
case 'new':
|
||
|
if (this.bot.channelManager.getChannelByName(args[1])) {
|
||
|
logger.error('A channel by that name already exists!');
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
this.bot.channelManager.addChannel({
|
||
|
name: args[1],
|
||
|
plugins: [],
|
||
|
enabled: true,
|
||
|
});
|
||
|
|
||
|
logger.log('Channel added!');
|
||
|
break;
|
||
|
case 'del':
|
||
|
case 'delete':
|
||
|
case 'remove':
|
||
|
const chan = this.bot.channelManager.getChannelByName(args[1]);
|
||
|
if (!chan) {
|
||
|
logger.error('No such channel exists!');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.bot.channelManager.removeChannel(chan);
|
||
|
|
||
|
logger.log('Channel removed!');
|
||
|
break;
|
||
|
case 'addp':
|
||
|
case 'addplugin':
|
||
|
const chan1 = this.bot.channelManager.getChannelByName(args[1]);
|
||
|
if (!chan1) {
|
||
|
logger.error('No such channel exists!');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!args[2]) {
|
||
|
logger.error('A plugin name is required.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (chan1.plugins.indexOf(args[2]) !== -1) {
|
||
|
chan1.plugins.push(args[2]);
|
||
|
}
|
||
|
|
||
|
logger.log('Plugin added to channel!');
|
||
|
break;
|
||
|
case 'remp':
|
||
|
case 'delp':
|
||
|
case 'remplugin':
|
||
|
case 'delplugin':
|
||
|
const chan2 = this.bot.channelManager.getChannelByName(args[1]);
|
||
|
if (!chan2) {
|
||
|
logger.error('No such channel exists!');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!args[2]) {
|
||
|
logger.error('A plugin name is required.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const idx = chan2.plugins.indexOf(args[2]);
|
||
|
if (idx !== -1) {
|
||
|
chan2.plugins.splice(idx, 1);
|
||
|
}
|
||
|
|
||
|
logger.log('Plugin added to channel!');
|
||
|
break;
|
||
|
case 'enable':
|
||
|
const chan3 = this.bot.channelManager.getChannelByName(args[1]);
|
||
|
if (!chan3) {
|
||
|
logger.error('No such channel exists!');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
chan3.enabled = true;
|
||
|
logger.log('Channel enabled!');
|
||
|
break;
|
||
|
case 'disable':
|
||
|
const chan4 = this.bot.channelManager.getChannelByName(args[1]);
|
||
|
if (!chan4) {
|
||
|
logger.error('No such channel exists!');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
chan4.enabled = false;
|
||
|
logger.log('Channel disabled!');
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
this.bot.config.config.channels = this.bot.channelManager.getAll();
|
||
|
this.bot.config.save();
|
||
|
}
|
||
|
|
||
|
public attach(rl: ReadLine) {
|
||
|
rl.on('line', (line: string) => {
|
||
|
const split = line.split(' ');
|
||
|
|
||
|
if (!split[0]) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
switch(split[0]) {
|
||
|
case 'r':
|
||
|
case 'repo':
|
||
|
case 'repository':
|
||
|
this.repositoryCommand(...split.slice(1));
|
||
|
break;
|
||
|
case 'p':
|
||
|
case 'pl':
|
||
|
case 'plugin':
|
||
|
this.pluginCommand(...split.slice(1));
|
||
|
break;
|
||
|
case 'c':
|
||
|
case 'chan':
|
||
|
case 'channel':
|
||
|
this.channelCommand(...split.slice(1));
|
||
|
break;
|
||
|
case 'stop':
|
||
|
case 'exit':
|
||
|
case 'quit':
|
||
|
case 'shutdown':
|
||
|
this.bot.shutdown();
|
||
|
break;
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|