2024-01-03 22:16:50 -08:00
|
|
|
|
2024-01-09 22:50:22 -06:00
|
|
|
import { actionsList } from './commands/actions.js';
|
|
|
|
import { queryList } from './commands/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);
|
|
|
|
if (commandMatch) {
|
|
|
|
const commandName = "!"+commandMatch[1];
|
|
|
|
if (commandList.some((command) => command.name === commandName))
|
|
|
|
return commandName;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-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))
|
|
|
|
return `Command ${command.name} was given incorrect number of arguments`;
|
|
|
|
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-01-11 18:04:59 -06:00
|
|
|
Do not use codeblocks. Only use one command in each response, trailing commands and comments will be ignored. Use these commands frequently in your responses!\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';
|
|
|
|
}
|