HiddenPhox/src/modules/bot.js

220 lines
5.9 KiB
JavaScript
Raw Normal View History

2021-06-01 18:59:59 +00:00
const Command = require("../lib/command.js");
const CATEGORY = "bot";
const logger = require("../lib/logger.js");
2021-07-23 22:44:09 +00:00
const child_process = require("child_process");
2021-07-06 00:57:28 +00:00
const {inspect} = require("util");
2021-07-23 22:44:09 +00:00
const {resolve} = require("path");
2021-07-06 00:57:28 +00:00
const {hastebin} = require("../lib/utils.js");
2021-06-01 18:59:59 +00:00
2022-05-07 22:48:48 +00:00
const guildSettings = require("../lib/guildSettings.js");
2021-07-23 22:44:09 +00:00
function spawn(args) {
const shell =
2022-11-30 01:15:41 +00:00
process.env.SHELL || (process.platform == "win32" ? "powershell" : "sh");
2021-07-23 22:44:09 +00:00
const newArgs = [];
if (shell.match(/powershell/i) && process.platform == "win32") {
newArgs.push("-NoLogo", "-Command");
} else {
newArgs.push("-c");
}
newArgs.push(args);
const proc = child_process.spawn(shell, newArgs, {
cwd: resolve(__dirname, "..", ".."),
});
return {
pid: proc.pid,
stdout: {
on: (event, handler) =>
proc.stdout.on(event, (data) => {
handler(data.toString("utf8"));
2021-07-23 22:44:09 +00:00
}),
},
stderr: {
on: (event, handler) =>
proc.stderr.on(event, (data) => {
handler(data.toString("utf8"));
2021-07-23 22:44:09 +00:00
}),
},
on: (event, handler) => proc.on(event, handler),
2021-07-23 22:44:09 +00:00
};
}
2021-06-01 18:59:59 +00:00
const reload = new Command("reload");
reload.ownerOnly = true;
reload.category = CATEGORY;
reload.helpText = "Reloads a module.";
reload.callback = function (msg, line) {
try {
require.resolve(`./${line}.js`);
} catch (err) {
if (err.code == "MODULE_NOT_FOUND") {
return "Module not found.";
} else {
return `:warning: An error occurred: \`\`\`\n${err}\`\`\``;
}
}
try {
logger.info("hf:modules", `Reloading module: '${line}'`);
2021-06-01 18:59:59 +00:00
delete require.cache[require.resolve(`./${line}.js`)];
require(`./${line}.js`);
return {reaction: "\uD83D\uDC4C"};
} catch (err) {
return `:warning: An error occurred: \`\`\`\n${err}\`\`\``;
}
};
hf.registerCommand(reload);
const restart = new Command("restart");
restart.ownerOnly = true;
restart.category = CATEGORY;
restart.helpText = "Restarts the bot.";
restart.callback = function () {
setTimeout(process.exit, 500);
return {reaction: "\uD83D\uDD04"};
};
hf.registerCommand(restart);
2021-07-06 00:57:28 +00:00
const _eval = new Command("eval");
_eval.elevatedOnly = true;
_eval.category = CATEGORY;
_eval.helpText = "Evalueates Javascript";
_eval.callback = async function (msg, line) {
let errored = false;
let out;
try {
out = eval(line);
if (out && out instanceof Promise) out = await out;
2021-07-06 00:57:28 +00:00
} catch (err) {
out = err.toString();
2021-07-06 00:57:28 +00:00
errored = true;
}
out = errored ? out : inspect(out, {depth: 0});
2021-07-06 00:57:28 +00:00
const token = hf.config.token;
out = out.replace(
new RegExp(token.replace(/\./g, "\\."), "g"),
"lol no key 4 u"
);
if (errored) {
return ":warning: Output (errored):\n```js\n" + out + "\n```";
} else {
if (out.toString().length > 1980) {
2022-04-02 02:07:15 +00:00
const haste = await hastebin(out.toString());
return `\u2705 Output too long to send in a message: ${haste}`;
2021-07-06 00:57:28 +00:00
} else {
return "\u2705 Output:\n```js\n" + out + "\n```";
}
}
};
hf.registerCommand(_eval);
2021-07-23 22:44:09 +00:00
const exec = new Command("exec");
exec.elevatedOnly = true;
exec.category = CATEGORY;
exec.helpText = "Executes a command";
exec.callback = async function (msg, line) {
const proc = spawn(line);
2022-11-30 01:15:41 +00:00
let out = `\x1b[1mSpawned ${proc.pid}: \`${line}'\x1b[0m\n`;
2021-07-23 22:44:09 +00:00
proc.stdout.on("data", (data) => {
2021-07-23 23:17:07 +00:00
out += data;
2021-07-23 22:44:09 +00:00
});
proc.stderr.on("data", (data) => {
2021-07-23 23:17:07 +00:00
out += data;
2021-07-23 22:44:09 +00:00
});
proc.on("close", async (code) => {
out += `\n\x1b[0m\x1b[1m====\x1b[0m\n\x1b[1m${
code != 0 ? "\x1b[31m" : ""
}Exited with ${code}\x1b[0m`;
2021-07-23 22:44:09 +00:00
if (out.length > 1980) {
2022-04-02 02:07:15 +00:00
const haste = await hastebin(out);
2021-07-23 22:44:09 +00:00
msg.channel.createMessage({
2022-04-02 02:07:15 +00:00
content: `Output too long to send in a message: ${haste}`,
2021-07-23 22:44:09 +00:00
allowedMentions: {
repliedUser: false,
},
messageReference: {
messageID: msg.id,
},
});
} else {
msg.channel.createMessage({
2022-11-30 01:15:41 +00:00
content: `\`\`\`ansi\n${out}\`\`\``,
2021-07-23 22:44:09 +00:00
allowedMentions: {
repliedUser: false,
},
messageReference: {
messageID: msg.id,
},
});
}
});
};
hf.registerCommand(exec);
2022-05-07 22:48:48 +00:00
const settings = new Command("settings");
settings.category = CATEGORY;
settings.helpText = "Manage guild specific bot settings";
2022-11-30 01:15:41 +00:00
settings.callback = async function (msg, line, [cmd, key, value]) {
2022-05-07 22:48:48 +00:00
if (!msg.guildID) "This command only works in guilds.";
switch (cmd) {
case "get":
case "set":
return "TODO";
case "flags": {
const flags = await guildSettings.getFlags(msg.guildID);
return `\`\`\`\n${Object.keys(flags)
.map((key) => `${key} = ${flags[key]}`)
.join("\n")}\n\`\`\``;
}
case "enable": {
if (
2023-01-24 21:33:17 +00:00
!msg.channel.permissionsOf(msg.author.id).has("manageGuild") &&
2022-05-07 22:48:48 +00:00
!hf.config.elevated.includes(msg.author.id)
)
return "You do not have `Manage Server` permissions";
if (!guildSettings.flags[key]) return "Unknown key.";
2022-05-07 23:59:02 +00:00
const enabled = await guildSettings.hasFlag(msg.guildID, key);
2022-05-07 22:54:23 +00:00
if (enabled === true) return "Already enabled.";
2022-05-07 22:48:48 +00:00
await guildSettings.enableFlag(msg.guildID, key);
return {reaction: "\uD83D\uDC4C"};
}
case "disable": {
if (
2023-01-24 21:33:17 +00:00
!msg.channel.permissionsOf(msg.author.id).has("manageGuild") &&
2022-05-07 22:48:48 +00:00
!hf.config.elevated.includes(msg.author.id)
)
return "You do not have `Manage Server` permissions";
if (!guildSettings.flags[key]) return "Unknown key.";
2022-05-07 23:59:02 +00:00
const enabled = await guildSettings.hasFlag(msg.guildID, key);
2022-05-07 22:54:23 +00:00
if (enabled === false) return "Already disabled.";
2022-05-07 22:48:48 +00:00
await guildSettings.disableFlag(msg.guildID, key);
return {reaction: "\uD83D\uDC4C"};
}
default:
return `__**Settings Subcommands**__
\u2022 \`get <key>\` - List all current values or a specific value
\u2022 \`set [key] [value]\` - Set a value
\u2022 \`flags\` - List flags
\u2022 \`enable [key]\` - Enable a flag
\u2022 \`disable [key]\` - Disable a flag`;
}
};
hf.registerCommand(settings);