|
|
@ -103,18 +103,19 @@ exports.Servant = class {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Parses the message and executes the command callback for the found command entry in the commands dict
|
|
|
|
* Processes the command
|
|
|
|
* @param msg
|
|
|
|
* @param msg
|
|
|
|
|
|
|
|
* @param globResult
|
|
|
|
|
|
|
|
* @param content
|
|
|
|
* @returns {*}
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
parseCommand(msg) {
|
|
|
|
processCommand(msg, globResult, content, returnFunction) {
|
|
|
|
let globResult = parseGlobalCommand(msg);
|
|
|
|
|
|
|
|
logger.debug(`Global command result is ${globResult}`);
|
|
|
|
|
|
|
|
let content = msg.content;
|
|
|
|
|
|
|
|
let command = (content.match(/^.\w+/) || [])[0];
|
|
|
|
let command = (content.match(/^.\w+/) || [])[0];
|
|
|
|
if (!command || !this.commands[command]) return globResult;
|
|
|
|
if (!command || !this.commands[command])
|
|
|
|
|
|
|
|
return globResult;
|
|
|
|
let cmd = this.commands[command];
|
|
|
|
let cmd = this.commands[command];
|
|
|
|
if (!checkPermission(msg, cmd.role)) return 'No Permission';
|
|
|
|
if (!checkPermission(msg, cmd.role))
|
|
|
|
|
|
|
|
return 'No Permission';
|
|
|
|
logger.debug(`Permission <${cmd.role || 'all'}> granted for command ${command} for user <${msg.author.tag}>`);
|
|
|
|
logger.debug(`Permission <${cmd.role || 'all'}> granted for command ${command} for user <${msg.author.tag}>`);
|
|
|
|
let argvars = content.match(/(?<= )\S+/g) || [];
|
|
|
|
let argvars = content.match(/(?<= )\S+/g) || [];
|
|
|
|
let kwargs = {};
|
|
|
|
let kwargs = {};
|
|
|
@ -125,9 +126,7 @@ exports.Servant = class {
|
|
|
|
let argv = argvars.slice(nLength);
|
|
|
|
let argv = argvars.slice(nLength);
|
|
|
|
logger.debug(`Executing callback for command: ${command}, kwargs: ${kwargs}, argv: ${argv}`);
|
|
|
|
logger.debug(`Executing callback for command: ${command}, kwargs: ${kwargs}, argv: ${argv}`);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
let locResult = cmd.callback(msg, kwargs, argv);
|
|
|
|
let locResult = returnFunction? () => cmd.callback(msg, kwargs, argv) : cmd.callback(msg, kwargs, argv);
|
|
|
|
if (locResult instanceof Promise)
|
|
|
|
|
|
|
|
return locResult; // because Promise equals false in conditional
|
|
|
|
|
|
|
|
return locResult || globResult;
|
|
|
|
return locResult || globResult;
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err.message);
|
|
|
|
logger.error(err.message);
|
|
|
@ -135,6 +134,28 @@ exports.Servant = class {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Parses the message and executes the command callback for the found command entry in the commands dict
|
|
|
|
|
|
|
|
* @param msg
|
|
|
|
|
|
|
|
* @returns {*}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
parseCommand(msg) {
|
|
|
|
|
|
|
|
let globResult = parseGlobalCommand(msg);
|
|
|
|
|
|
|
|
logger.debug(`Global command result is ${globResult}`);
|
|
|
|
|
|
|
|
let content = msg.content;
|
|
|
|
|
|
|
|
let commands = content.split(';').map(x => x.replace(/^ +/, ''));
|
|
|
|
|
|
|
|
if (commands.length === 1) {
|
|
|
|
|
|
|
|
return this.processCommand(msg, globResult, content);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let answers = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < commands.length; i++)
|
|
|
|
|
|
|
|
answers.push(this.processCommand(msg, globResult[i], commands[i],
|
|
|
|
|
|
|
|
true)); // return function to avoid "race conditions"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return answers;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -158,7 +179,8 @@ exports.createGlobalCommand = function (command, call, args, description, role)
|
|
|
|
'args': args || [],
|
|
|
|
'args': args || [],
|
|
|
|
'description': description,
|
|
|
|
'description': description,
|
|
|
|
'callback': call,
|
|
|
|
'callback': call,
|
|
|
|
'role': role
|
|
|
|
'role': role,
|
|
|
|
|
|
|
|
'name': command
|
|
|
|
};
|
|
|
|
};
|
|
|
|
logger.debug(`Created global command: ${command}, args: ${args}`);
|
|
|
|
logger.debug(`Created global command: ${command}, args: ${args}`);
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -209,26 +231,48 @@ exports.init = function (prefix) {
|
|
|
|
}, ['command'], "Shows this help.");
|
|
|
|
}, ['command'], "Shows this help.");
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
function processCommand(cmd, msg, content, returnFunction) {
|
|
|
|
* Parses the message by calling the assigned function for the command with arguments
|
|
|
|
|
|
|
|
* @param msg
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function parseGlobalCommand(msg) {
|
|
|
|
|
|
|
|
let content = msg.content;
|
|
|
|
|
|
|
|
let command = (content.match(/^.\w+/) || [])[0];
|
|
|
|
|
|
|
|
if (!command || !globCommands[command]) return false;
|
|
|
|
|
|
|
|
let cmd = globCommands[command];
|
|
|
|
|
|
|
|
if (!checkPermission(msg, cmd.role)) return false;
|
|
|
|
|
|
|
|
logger.debug(`Permission <${cmd.role}> granted for command ${command} for user <${msg.author.tag}>`);
|
|
|
|
|
|
|
|
let argvars = content.match(/(?<= )\S+/g) || [];
|
|
|
|
let argvars = content.match(/(?<= )\S+/g) || [];
|
|
|
|
let kwargs = {};
|
|
|
|
let kwargs = {};
|
|
|
|
let nLength = Math.min(cmd.args.length, argvars.length);
|
|
|
|
let nLength = Math.min(cmd.args.length, argvars.length);
|
|
|
|
for (let i = 0; i < nLength; i++)
|
|
|
|
for (let i = 0; i < nLength; i++)
|
|
|
|
kwargs[cmd.args[i]] = argvars[i];
|
|
|
|
kwargs[cmd.args[i]] = argvars[i];
|
|
|
|
|
|
|
|
|
|
|
|
let argv = argvars.slice(nLength);
|
|
|
|
let argv = argvars.slice(nLength);
|
|
|
|
logger.debug(`Executing callback for command: ${command}, kwargs: ${JSON.stringify(kwargs)}, argv: ${argv}`);
|
|
|
|
logger.debug(`Executing callback for command: ${cmd.name}, kwargs: ${JSON.stringify(kwargs)}, argv: ${argv}`);
|
|
|
|
return cmd.callback(msg, kwargs, argv);
|
|
|
|
return returnFunction? () => cmd.callback(msg, kwargs, argv) : cmd.callback(msg, kwargs, argv);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Parses the message by calling the assigned function for the command with arguments
|
|
|
|
|
|
|
|
* @param msg
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function parseGlobalCommand(msg) {
|
|
|
|
|
|
|
|
let content = msg.content;
|
|
|
|
|
|
|
|
let commands = content.split(';').map(x => x.replace(/^ +/, ''));
|
|
|
|
|
|
|
|
if (commands.length === 1) {
|
|
|
|
|
|
|
|
let command = (content.match(/^.\w+/) || [])[0];
|
|
|
|
|
|
|
|
if (!command || !globCommands[command])
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let cmd = globCommands[command];
|
|
|
|
|
|
|
|
if (!checkPermission(msg, cmd.role))
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
logger.debug(`Permission <${cmd.role}> granted for command ${command} for user <${msg.author.tag}>`);
|
|
|
|
|
|
|
|
return processCommand(cmd, msg, content);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let answers = [];
|
|
|
|
|
|
|
|
for (let commandPart of commands) {
|
|
|
|
|
|
|
|
let command = (commandPart.match(/^.\w+/) || [])[0];
|
|
|
|
|
|
|
|
if (command && globCommands[command]) {
|
|
|
|
|
|
|
|
let cmd = globCommands[command];
|
|
|
|
|
|
|
|
if (checkPermission(msg, cmd.role)) {
|
|
|
|
|
|
|
|
logger.debug(`Permission <${cmd.role}> granted for command ${command} for user <${msg.author.tag}>`);
|
|
|
|
|
|
|
|
answers.push(processCommand(cmd, msg, commandPart,
|
|
|
|
|
|
|
|
true)); // return an function to avoid "race conditions"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return answers;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|