2024-01-25 13:25:36 -08:00
|
|
|
import { actionsList } from './actions.js';
|
|
|
|
import { queryList } from './queries.js';
|
2024-01-03 22:16:50 -08:00
|
|
|
|
|
|
|
|
2024-01-09 22:50:22 -06:00
|
|
|
const commandList = queryList.concat(actionsList);
|
2024-01-03 22:16:50 -08:00
|
|
|
const commandMap = {};
|
|
|
|
for (let command of commandList) {
|
|
|
|
commandMap[command.name] = command;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCommand(name) {
|
|
|
|
return commandMap[name];
|
|
|
|
}
|
|
|
|
|
2024-01-11 15:59:52 -06:00
|
|
|
const commandRegex = /!(\w+)(?:\(((?:[^)(]+|'[^']*'|"[^"]*")*)\))?/
|
|
|
|
const argRegex = /(?:"[^"]*"|'[^']*'|[^,])+/g;
|
|
|
|
|
2024-01-03 22:16:50 -08:00
|
|
|
export function containsCommand(message) {
|
2024-01-11 15:59:52 -06:00
|
|
|
const commandMatch = message.match(commandRegex);
|
2024-01-14 14:33:25 -06:00
|
|
|
if (commandMatch)
|
2024-01-15 11:04:50 -06:00
|
|
|
return "!" + commandMatch[1];
|
2024-01-11 15:59:52 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-01-14 14:33:25 -06:00
|
|
|
export function commandExists(commandName) {
|
|
|
|
if (!commandName.startsWith("!"))
|
|
|
|
commandName = "!" + commandName;
|
|
|
|
return commandMap[commandName] !== undefined;
|
|
|
|
}
|
|
|
|
|
2024-01-11 18:10:51 -06:00
|
|
|
// todo: handle arrays?
|
2024-01-11 15:59:52 -06:00
|
|
|
function parseCommandMessage(message) {
|
|
|
|
const commandMatch = message.match(commandRegex);
|
|
|
|
if (commandMatch) {
|
|
|
|
const commandName = "!"+commandMatch[1];
|
|
|
|
if (!commandMatch[2])
|
|
|
|
return { commandName, args: [] };
|
|
|
|
let args = commandMatch[2].match(argRegex);
|
|
|
|
if (args) {
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
|
|
args[i] = args[i].trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
|
|
let arg = args[i];
|
|
|
|
if ((arg.startsWith('"') && arg.endsWith('"')) || (arg.startsWith("'") && arg.endsWith("'"))) {
|
|
|
|
args[i] = arg.substring(1, arg.length-1);
|
|
|
|
} else if (!isNaN(arg)) {
|
|
|
|
args[i] = Number(arg);
|
2024-01-11 18:10:51 -06:00
|
|
|
} else if (arg === 'true' || arg === 'false') {
|
|
|
|
args[i] = arg === 'true';
|
2024-01-11 15:59:52 -06:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 22:16:50 -08:00
|
|
|
}
|
2024-01-11 15:59:52 -06:00
|
|
|
else
|
|
|
|
args = [];
|
|
|
|
|
|
|
|
return { commandName, args };
|
2024-01-03 22:16:50 -08:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-02-25 14:13:32 -06:00
|
|
|
export function truncCommandMessage(message) {
|
|
|
|
const commandMatch = message.match(commandRegex);
|
|
|
|
if (commandMatch) {
|
|
|
|
return message.substring(0, commandMatch.index + commandMatch[0].length);
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2024-01-11 15:59:52 -06:00
|
|
|
function numParams(command) {
|
|
|
|
if (!command.params)
|
|
|
|
return 0;
|
|
|
|
return Object.keys(command.params).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function executeCommand(agent, message) {
|
|
|
|
let parsed = parseCommandMessage(message);
|
|
|
|
if (parsed) {
|
|
|
|
const command = getCommand(parsed.commandName);
|
|
|
|
let numArgs = 0;
|
|
|
|
if (parsed.args) {
|
|
|
|
numArgs = parsed.args.length;
|
|
|
|
}
|
|
|
|
console.log('parsed command:', parsed);
|
|
|
|
if (numArgs !== numParams(command))
|
2024-01-11 18:10:51 -06:00
|
|
|
return `Command ${command.name} was given ${numArgs} args, but requires ${numParams(command)} args.`;
|
2024-01-11 15:59:52 -06:00
|
|
|
else
|
|
|
|
return await command.perform(agent, ...parsed.args);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return `Command is incorrectly formatted`;
|
|
|
|
}
|
|
|
|
|
2024-01-03 22:16:50 -08:00
|
|
|
export function getCommandDocs() {
|
2024-01-11 15:59:52 -06:00
|
|
|
let docs = `\n*COMMAND DOCS\n You can use the following commands to perform actions and get information about the world.
|
|
|
|
Use the commands with the syntax: !commandName or !commandName("arg1", 1.2, ...) if the command takes arguments.\n
|
2024-04-05 16:58:08 -05:00
|
|
|
Do not use codeblocks. Only use one command in each response, trailing commands and comments will be ignored.\n`;
|
2024-01-03 22:16:50 -08:00
|
|
|
for (let command of commandList) {
|
|
|
|
docs += command.name + ': ' + command.description + '\n';
|
2024-01-11 15:59:52 -06:00
|
|
|
if (command.params) {
|
|
|
|
docs += 'Params:\n';
|
|
|
|
for (let param in command.params) {
|
|
|
|
docs += param + ': ' + command.params[param] + '\n';
|
|
|
|
}
|
|
|
|
}
|
2024-01-03 22:16:50 -08:00
|
|
|
}
|
|
|
|
return docs + '*\n';
|
2024-01-25 13:25:36 -08:00
|
|
|
}
|