2020-08-12 17:08:37 -04:00
|
|
|
const { CommandManager, defaultParameterTypes, TypeConversionError, IParameter, ICommandConfig } = require("knub-command-manager");
|
|
|
|
const Eris = require("eris");
|
|
|
|
const config = require("./cfg");
|
|
|
|
const utils = require("./utils");
|
|
|
|
const threads = require("./data/threads");
|
|
|
|
const Thread = require("./data/Thread");
|
2019-06-16 15:27:30 -04:00
|
|
|
|
2020-08-16 16:26:04 -04:00
|
|
|
/**
|
|
|
|
* @callback CommandFn
|
|
|
|
* @param {Eris.Message} msg
|
|
|
|
* @param {object} args
|
|
|
|
*/
|
|
|
|
|
2020-10-21 17:17:14 -04:00
|
|
|
/**
|
|
|
|
* @callback InboxServerCommandHandler
|
|
|
|
* @param {Eris.Message} msg
|
|
|
|
* @param {object} args
|
|
|
|
* @param {Thread} [thread]
|
|
|
|
*/
|
|
|
|
|
2020-08-16 16:26:04 -04:00
|
|
|
/**
|
|
|
|
* @callback InboxThreadCommandHandler
|
|
|
|
* @param {Eris.Message} msg
|
|
|
|
* @param {object} args
|
|
|
|
* @param {Thread} thread
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback AddGlobalCommandFn
|
|
|
|
* @param {string} trigger
|
|
|
|
* @param {string} parameters
|
|
|
|
* @param {CommandFn} handler
|
|
|
|
* @param {ICommandConfig} commandConfig
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback AddInboxServerCommandFn
|
|
|
|
* @param {string} trigger
|
|
|
|
* @param {string} parameters
|
2020-10-21 17:17:14 -04:00
|
|
|
* @param {InboxServerCommandHandler} handler
|
2020-08-16 16:26:04 -04:00
|
|
|
* @param {ICommandConfig} commandConfig
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback AddInboxThreadCommandFn
|
|
|
|
* Add a command that can only be invoked in a thread on the inbox server
|
|
|
|
*
|
|
|
|
* @param {string} trigger
|
|
|
|
* @param {string} parameters
|
|
|
|
* @param {InboxThreadCommandHandler} handler
|
|
|
|
* @param {ICommandConfig} commandConfig
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback AddAliasFn
|
|
|
|
* @param {string} originalCmd
|
|
|
|
* @param {string} alias
|
|
|
|
*/
|
|
|
|
|
2019-06-16 15:27:30 -04:00
|
|
|
module.exports = {
|
|
|
|
createCommandManager(bot) {
|
|
|
|
const manager = new CommandManager({
|
|
|
|
prefix: config.prefix,
|
|
|
|
types: Object.assign({}, defaultParameterTypes, {
|
|
|
|
userId(value) {
|
|
|
|
const userId = utils.getUserMention(value);
|
|
|
|
if (! userId) throw new TypeConversionError();
|
|
|
|
return userId;
|
|
|
|
},
|
|
|
|
|
|
|
|
delay(value) {
|
|
|
|
const ms = utils.convertDelayStringToMS(value);
|
|
|
|
if (ms === null) throw new TypeConversionError();
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
const handlers = {};
|
|
|
|
const aliasMap = new Map();
|
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
bot.on("messageCreate", async msg => {
|
2019-06-16 15:27:30 -04:00
|
|
|
if (msg.author.bot) return;
|
|
|
|
if (msg.author.id === bot.user.id) return;
|
|
|
|
if (! msg.content) return;
|
|
|
|
|
|
|
|
const matchedCommand = await manager.findMatchingCommand(msg.content, { msg });
|
|
|
|
if (matchedCommand === null) return;
|
|
|
|
if (matchedCommand.error !== undefined) {
|
|
|
|
utils.postError(msg.channel, matchedCommand.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const allArgs = {};
|
|
|
|
for (const [name, arg] of Object.entries(matchedCommand.args)) {
|
|
|
|
allArgs[name] = arg.value;
|
|
|
|
}
|
|
|
|
for (const [name, opt] of Object.entries(matchedCommand.opts)) {
|
|
|
|
allArgs[name] = opt.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers[matchedCommand.id](msg, allArgs);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a command that can be invoked anywhere
|
2020-08-16 16:26:04 -04:00
|
|
|
* @type {AddGlobalCommandFn}
|
2019-06-16 15:27:30 -04:00
|
|
|
*/
|
|
|
|
const addGlobalCommand = (trigger, parameters, handler, commandConfig = {}) => {
|
|
|
|
let aliases = aliasMap.has(trigger) ? [...aliasMap.get(trigger)] : [];
|
|
|
|
if (commandConfig.aliases) aliases.push(...commandConfig.aliases);
|
|
|
|
|
|
|
|
const cmd = manager.add(trigger, parameters, { ...commandConfig, aliases });
|
|
|
|
handlers[cmd.id] = handler;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a command that can only be invoked on the inbox server
|
2020-08-16 16:26:04 -04:00
|
|
|
* @type {AddInboxServerCommandFn}
|
2019-06-16 15:27:30 -04:00
|
|
|
*/
|
|
|
|
const addInboxServerCommand = (trigger, parameters, handler, commandConfig = {}) => {
|
|
|
|
const aliases = aliasMap.has(trigger) ? [...aliasMap.get(trigger)] : [];
|
|
|
|
if (commandConfig.aliases) aliases.push(...commandConfig.aliases);
|
|
|
|
|
|
|
|
const cmd = manager.add(trigger, parameters, {
|
|
|
|
...commandConfig,
|
|
|
|
aliases,
|
|
|
|
preFilters: [
|
|
|
|
(_, context) => {
|
|
|
|
if (! utils.messageIsOnInboxServer(context.msg)) return false;
|
|
|
|
if (! utils.isStaff(context.msg.member)) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
handlers[cmd.id] = async (msg, args) => {
|
|
|
|
const thread = await threads.findOpenThreadByChannelId(msg.channel.id);
|
|
|
|
handler(msg, args, thread);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a command that can only be invoked in a thread on the inbox server
|
2020-08-16 16:26:04 -04:00
|
|
|
* @type {AddInboxThreadCommandFn}
|
2019-06-16 15:27:30 -04:00
|
|
|
*/
|
|
|
|
const addInboxThreadCommand = (trigger, parameters, handler, commandConfig = {}) => {
|
|
|
|
const aliases = aliasMap.has(trigger) ? [...aliasMap.get(trigger)] : [];
|
|
|
|
if (commandConfig.aliases) aliases.push(...commandConfig.aliases);
|
|
|
|
|
|
|
|
let thread;
|
|
|
|
const cmd = manager.add(trigger, parameters, {
|
|
|
|
...commandConfig,
|
|
|
|
aliases,
|
|
|
|
preFilters: [
|
|
|
|
async (_, context) => {
|
|
|
|
if (! utils.messageIsOnInboxServer(context.msg)) return false;
|
|
|
|
if (! utils.isStaff(context.msg.member)) return false;
|
|
|
|
thread = await threads.findOpenThreadByChannelId(context.msg.channel.id);
|
|
|
|
if (! thread) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
handlers[cmd.id] = async (msg, args) => {
|
|
|
|
handler(msg, args, thread);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-08-16 16:26:04 -04:00
|
|
|
/**
|
|
|
|
* @type {AddAliasFn}
|
|
|
|
*/
|
2019-06-16 15:27:30 -04:00
|
|
|
const addAlias = (originalCmd, alias) => {
|
|
|
|
if (! aliasMap.has(originalCmd)) {
|
|
|
|
aliasMap.set(originalCmd, new Set());
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasMap.get(originalCmd).add(alias);
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
manager,
|
|
|
|
addGlobalCommand,
|
|
|
|
addInboxServerCommand,
|
|
|
|
addInboxThreadCommand,
|
|
|
|
addAlias,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|