HiddenPhox/src/lib/commandDispatcher.js

133 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-03-15 01:41:40 +00:00
const logger = require("npmlog");
2021-08-12 02:41:28 +00:00
const {pastelize, getTopColor} = require("./lib/utils.js");
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;
}
async function runCommand(msg, cmd, line, args) {
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.";
}
2021-03-15 02:30:09 +00:00
if (cmdObj.guildOnly && !msg.channel.guild) {
return "This command can only be used in guilds.";
}
2021-03-15 01:41:40 +00:00
try {
const ret = cmdObj.callback(msg, line, ...args);
if (ret instanceof Promise) {
return await ret;
} else {
return ret;
}
2021-03-15 01:41:40 +00:00
} catch (err) {
logger.error("hf:cmd:" + cmd, err);
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(" ");
const args = parseArguments(line);
const response = await runCommand(msg, cmd, line, args);
if (response != null) {
2021-05-27 22:44:31 +00:00
let file;
if (response.file) {
file = response.file;
delete response.file;
}
2021-08-12 02:41:28 +00:00
if (response.embed) {
response.embed.color =
response.embed.color ||
getTopColor(msg, hf.bot.user.id, pastelize(hf.bot.user.id));
}
if (response.reaction) {
msg.addReaction(response.reaction);
} else {
msg.channel
.createMessage(
Object.assign(
typeof response === "string" ? {content: response} : response,
{
allowedMentions: {
repliedUser: false,
},
messageReference: {
messageID: msg.id,
},
}
),
file
)
.catch((e) => {
msg.channel.createMessage({
content: `:warning: An error has occurred:\n\`\`\`${e}\`\`\``,
2021-05-27 21:18:51 +00:00
allowedMentions: {
repliedUser: false,
},
messageReference: {
messageID: msg.id,
},
});
2021-05-27 21:18:51 +00:00
});
}
2021-06-22 04:43:05 +00:00
msg.hasRan = true;
2021-03-15 01:41:40 +00:00
}
}
}
module.exports = CommandDispatcher;