HiddenPhox/src/lib/commandDispatcher.js

235 lines
6.2 KiB
JavaScript
Raw Normal View History

const logger = require("./logger.js");
2021-08-12 02:42:13 +00:00
const {pastelize, getTopColor} = require("./utils.js");
2021-03-15 01:41:40 +00:00
2022-11-30 02:37:56 +00:00
function convertIfApplicable(val) {
2022-11-30 01:15:41 +00:00
if (isNaN(val)) {
if (val.toString().toLowerCase() === "true") {
return true;
} else if (val.toString().toLowerCase() === "false") {
return false;
} else {
return val;
}
} else {
return Number(val);
}
}
function removeStartHyphens(val) {
2022-12-10 22:31:36 +00:00
return val.replace(/^--?/g, "");
2022-11-30 01:15:41 +00:00
}
// taken from ethanent/gar
// modified to make - arguments only be bools unless =
function parseAsArgv(args) {
2022-11-30 01:15:41 +00:00
const optional = {};
for (let i = 0; i < args.length; i++) {
const arg = args.shift();
2022-12-10 22:32:55 +00:00
const equalsIndex = arg.charAt(0) === "-" ? arg.indexOf("=") : -1;
2022-11-30 01:15:41 +00:00
const argName =
2022-12-10 22:32:55 +00:00
equalsIndex === -1
2022-11-30 01:15:41 +00:00
? removeStartHyphens(arg)
: removeStartHyphens(arg.slice(0, equalsIndex));
2022-12-10 22:32:55 +00:00
if (equalsIndex !== -1) {
optional[argName] = convertIfApplicable(arg.slice(equalsIndex + 1));
2022-11-30 01:15:41 +00:00
} else if (arg.charAt(0) === "-") {
if (arg.charAt(1) === "-") {
optional[argName] = true;
2022-11-30 01:15:41 +00:00
} else {
for (let b = 0; b < argName.length; b++) {
optional[argName.charAt(b)] = true;
2022-11-30 01:15:41 +00:00
}
}
} else {
// all optional args must be at the start of the argument string
args.splice(0, 0, arg);
break;
2022-11-30 01:15:41 +00:00
}
}
return {
optional,
args,
};
}
2021-03-15 01:41:40 +00:00
function parseArguments(str) {
return str.match(/\\?.|^$/g).reduce(
(p, c) => {
if (c === '"') {
p.quote ^= 1;
} else if (!p.quote && c === " ") {
p.a.push("");
} else {
p.a[p.a.length - 1] += c.replace(/\\(.)/, "$1");
}
return p;
},
{a: [""]}
).a;
}
2022-11-30 01:15:41 +00:00
async function runCommand(msg, cmd, line) {
2021-03-15 01:41:40 +00:00
let cmdObj = hf.commands.get(cmd);
if (!cmdObj) {
for (const c of hf.commands.values()) {
if (c.hasAlias(cmd)) {
cmdObj = c;
break;
}
}
}
if (!cmdObj) return null;
if (cmdObj.ownerOnly && msg.author.id != hf.config.owner_id) {
return "No\n\nSent from my iPhone.";
}
if (cmdObj.elevatedOnly && !hf.config.elevated.includes(msg.author.id)) {
return "No\n\nSent from my iPhone.";
}
if (cmdObj.guildOnly && !msg.guildID) {
2021-03-15 02:30:09 +00:00
return "This command can only be used in guilds.";
}
2021-03-15 01:41:40 +00:00
try {
2022-11-30 01:15:41 +00:00
const args = parseArguments(line);
const argv = parseAsArgv(args);
const ret = cmdObj.callback(msg, line, argv.args, argv.optional);
if (ret instanceof Promise) {
return await ret;
} else {
return ret;
}
2021-03-15 01:41:40 +00:00
} catch (err) {
2023-01-27 05:54:33 +00:00
logger.error("hf:cmd:" + cmd, err + "\n" + err.stack);
2021-03-15 01:41:40 +00:00
return ":warning: An internal error occurred.";
}
}
async function CommandDispatcher(msg) {
2021-03-15 01:41:40 +00:00
let str = msg.content;
let inCommand = false;
const prefix1 = hf.config.prefix;
const prefix2 = `<@${hf.bot.user.id}> `;
const prefix3 = `<@!${hf.bot.user.id}> `;
if (str.startsWith(prefix1)) {
str = str.substring(prefix1.length);
inCommand = true;
} else if (str.startsWith(prefix2)) {
str = str.substring(prefix2.length);
inCommand = true;
} else if (str.startsWith(prefix3)) {
str = str.substring(prefix3.length);
inCommand = true;
}
if (inCommand) {
let line = str.split(" ");
let [cmd] = line.splice(0, 1);
cmd = cmd.toLowerCase();
line = line.join(" ");
try {
2022-11-30 01:15:41 +00:00
const response = await runCommand(msg, cmd, line);
if (response != null) {
2022-11-30 01:15:41 +00:00
msg.hasRan = true;
if (response.file) {
const newFile = response.file;
delete response.file;
2023-01-25 19:36:42 +00:00
if (newFile.contents) {
newFile.file = newFile.contents;
delete newFile.contents;
}
if (newFile.name) {
newFile.filename = newFile.name;
delete newFile.name;
}
const files = response.attachments ?? [];
files.push(newFile);
2023-01-25 19:36:42 +00:00
response.attachments = files;
}
2023-01-25 19:29:55 +00:00
if (response.files) {
response.attachments = response.files;
delete response.files;
2023-01-25 19:36:42 +00:00
for (const attachment of response.attachments) {
if (attachment.contents) {
attachment.file = attachment.contents;
delete attachment.contents;
}
if (attachment.name) {
attachment.filename = attachment.name;
delete attachment.name;
}
}
2023-01-25 19:29:55 +00:00
}
if (response.embed) {
response.embeds = [...(response.embeds ?? []), response.embed];
delete response.embed;
}
if (response.embeds) {
for (const embed of response.embeds) {
embed.color =
embed.color ||
getTopColor(msg, hf.bot.user.id, pastelize(hf.bot.user.id));
}
}
if (response.reaction) {
2023-01-22 05:02:53 +00:00
msg.addReaction(response.reaction);
} else {
try {
const outMessage = await msg.channel.createMessage(
Object.assign(
typeof response === "string" ? {content: response} : response,
{
allowedMentions: {
repliedUser: false,
},
messageReference: {
messageID: msg.id,
},
}
)
);
if (response.addReactions) {
for (const index in response.addReactions) {
const reaction = response.addReactions[index];
2023-01-22 05:02:53 +00:00
await outMessage.addReaction(reaction);
}
}
} catch (err) {
msg.channel.createMessage({
content: `:warning: An error has occurred:\n\`\`\`${err}\`\`\``,
2021-05-27 21:18:51 +00:00
allowedMentions: {
repliedUser: false,
},
messageReference: {
messageID: msg.id,
},
});
}
}
}
} catch (err) {
msg.channel.createMessage({
content: `:warning: An error has occurred:\n\`\`\`${err}\`\`\``,
allowedMentions: {
repliedUser: false,
},
messageReference: {
messageID: msg.id,
},
});
2021-03-15 01:41:40 +00:00
}
}
}
module.exports = CommandDispatcher;