2021-08-22 20:20:49 +00:00
|
|
|
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 =
|
2023-04-26 22:45:27 +00:00
|
|
|
function parseAsArgv(args) {
|
2022-11-30 01:15:41 +00:00
|
|
|
const optional = {};
|
|
|
|
|
2023-04-26 22:45:27 +00:00
|
|
|
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) {
|
2022-11-30 02:39:12 +00:00
|
|
|
optional[argName] = convertIfApplicable(arg.slice(equalsIndex + 1));
|
2022-11-30 01:15:41 +00:00
|
|
|
} else if (arg.charAt(0) === "-") {
|
|
|
|
if (arg.charAt(1) === "-") {
|
2022-11-30 02:39:12 +00:00
|
|
|
optional[argName] = true;
|
2022-11-30 01:15:41 +00:00
|
|
|
} else {
|
|
|
|
for (let b = 0; b < argName.length; b++) {
|
2022-11-30 02:39:12 +00:00
|
|
|
optional[argName.charAt(b)] = true;
|
2022-11-30 01:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-26 22:45:27 +00:00
|
|
|
// 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.";
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:01:33 +00:00
|
|
|
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);
|
2021-03-15 02:22:28 +00:00
|
|
|
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.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 02:22:28 +00:00
|
|
|
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(" ");
|
|
|
|
|
2022-04-02 00:10:10 +00:00
|
|
|
try {
|
2022-11-30 01:15:41 +00:00
|
|
|
const response = await runCommand(msg, cmd, line);
|
2022-04-02 00:10:10 +00:00
|
|
|
if (response != null) {
|
2022-11-30 01:15:41 +00:00
|
|
|
msg.hasRan = true;
|
2022-04-02 00:10:10 +00:00
|
|
|
if (response.file) {
|
2022-10-10 01:33:44 +00:00
|
|
|
const newFile = response.file;
|
2022-04-02 00:10:10 +00:00
|
|
|
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 ?? [];
|
2022-10-10 01:33:44 +00:00
|
|
|
files.push(newFile);
|
2023-01-25 19:36:42 +00:00
|
|
|
response.attachments = files;
|
2021-08-30 02:04:26 +00:00
|
|
|
}
|
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
|
|
|
}
|
2022-04-02 00:10:10 +00:00
|
|
|
if (response.embed) {
|
|
|
|
response.embeds = [...(response.embeds ?? []), response.embed];
|
2022-04-20 19:48:29 +00:00
|
|
|
delete response.embed;
|
2022-04-02 00:10:10 +00:00
|
|
|
}
|
|
|
|
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);
|
2022-04-02 00:10:10 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const outMessage = await msg.channel.createMessage(
|
|
|
|
Object.assign(
|
|
|
|
typeof response === "string" ? {content: response} : response,
|
|
|
|
{
|
|
|
|
allowedMentions: {
|
|
|
|
repliedUser: false,
|
|
|
|
},
|
|
|
|
messageReference: {
|
|
|
|
messageID: msg.id,
|
|
|
|
},
|
|
|
|
}
|
2022-10-10 01:32:03 +00:00
|
|
|
)
|
2022-04-02 00:10:10 +00:00
|
|
|
);
|
|
|
|
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);
|
2021-05-27 22:45:56 +00:00
|
|
|
}
|
2022-04-02 00:10:10 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-05-27 22:45:56 +00:00
|
|
|
msg.channel.createMessage({
|
2022-04-02 00:10:10 +00:00
|
|
|
content: `:warning: An error has occurred:\n\`\`\`${err}\`\`\``,
|
2021-05-27 21:18:51 +00:00
|
|
|
allowedMentions: {
|
|
|
|
repliedUser: false,
|
|
|
|
},
|
|
|
|
messageReference: {
|
|
|
|
messageID: msg.id,
|
|
|
|
},
|
2021-05-27 22:45:56 +00:00
|
|
|
});
|
2022-04-02 00:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-27 22:45:56 +00:00
|
|
|
}
|
2022-04-02 00:10:10 +00:00
|
|
|
} 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;
|