HiddenPhox/src/index.js

244 lines
7.4 KiB
JavaScript
Raw Normal View History

2024-07-23 22:10:17 +00:00
const {Client, Collection, Channel, Permission} = require("@projectdysnomia/dysnomia");
2024-05-16 20:43:51 +00:00
const fs = require("node:fs");
const {resolve} = require("node:path");
2021-07-23 23:21:50 +00:00
const sqlite3 = require("sqlite3");
2023-09-25 20:00:36 +00:00
const {instead, before} = require("spitroast");
2021-03-15 01:41:40 +00:00
2024-08-18 20:40:33 +00:00
const config = require("#root/config.json");
const apikeys = require("#root/apikeys.json");
2024-05-17 20:33:01 +00:00
2024-08-18 20:40:33 +00:00
const logger = require("#lib/logger.js");
const events = require("#lib/events.js");
const timer = require("#lib/timer.js");
const Command = require("#lib/command.js");
const InteractionCommand = require("#lib/interactionCommand.js");
2021-03-15 01:41:40 +00:00
2024-08-18 20:40:33 +00:00
const {APIEndpoints, Intents, ApplicationCommandTypes, GatewayOPCodes} = require("#util/dconstants.js");
2024-07-23 22:10:17 +00:00
const bot = new Client(config.token, {
2021-03-15 01:41:40 +00:00
defaultImageFormat: "png",
defaultImageSize: 1024,
gateway: {
2024-07-23 22:10:17 +00:00
intents: Object.values(Intents),
},
2024-05-16 20:43:51 +00:00
restMode: true,
2021-03-15 01:41:40 +00:00
});
2024-07-23 22:10:17 +00:00
const commands = new Collection();
const interactionCommands = new Collection();
2021-03-15 01:41:40 +00:00
2021-07-24 00:39:34 +00:00
const database = new sqlite3.Database(resolve(__dirname, "..", "database.db"));
2021-03-15 01:41:40 +00:00
function registerCommand(cmdObj) {
if (cmdObj instanceof Command) {
commands.set(cmdObj.name, cmdObj);
const aliases = cmdObj.getAliases();
logger.info(
"hf:cmd",
2024-07-23 22:10:17 +00:00
`Registered command '${cmdObj.name}'${aliases.length > 0 ? ` (aliases: ${aliases.join(", ")})` : ""}`
2021-03-15 01:41:40 +00:00
);
2024-05-17 20:33:01 +00:00
} else if (cmdObj instanceof InteractionCommand) {
interactionCommands.set(cmdObj.name, cmdObj);
logger.info("hf:cmd", `Registered interaction command '${cmdObj.name}'`);
2021-03-15 01:41:40 +00:00
}
}
global.hf = {
bot,
config,
2021-06-11 03:54:38 +00:00
apikeys,
2021-03-15 01:41:40 +00:00
commands,
2024-05-17 20:33:01 +00:00
interactionCommands,
2021-03-15 01:41:40 +00:00
registerCommand,
2021-05-17 22:35:18 +00:00
events,
timer,
2021-07-24 00:39:34 +00:00
database,
2021-03-15 01:41:40 +00:00
};
2024-08-18 20:40:33 +00:00
const {formatUsername} = require("#util/misc.js");
const CommandDispatcher = require("#lib/commandDispatcher.js");
const {InteractionDispatcher} = require("#lib/interactionDispatcher.js");
const {hasFlag} = require("#lib/guildSettings.js");
2024-05-17 20:36:52 +00:00
2024-08-18 20:40:33 +00:00
for (const file of fs.readdirSync(resolve(__dirname, "modules"), {withFileTypes: true})) {
if (file.isDirectory()) continue;
2024-07-23 22:10:17 +00:00
try {
2024-08-18 20:40:33 +00:00
require(`#modules/${file.name}`);
logger.info("hf:modules", `Loaded module: "${file.name}"`);
2024-07-23 22:10:17 +00:00
} catch (err) {
2024-08-18 20:40:33 +00:00
logger.error("hf:modules", `Failed to load "${file.name}": ${err}`);
2024-07-23 22:10:17 +00:00
}
2021-03-15 01:41:40 +00:00
}
bot.on("messageCreate", async (msg) => {
2024-05-16 20:43:51 +00:00
try {
// fix DMs cause of gateway v8 changes
2024-07-23 22:10:17 +00:00
if (!(msg.channel instanceof Channel) && msg.author.id != bot.user.id && !msg.guildID) {
2024-05-16 20:43:51 +00:00
const newChannel = await bot.getDMChannel(msg.author.id);
if (msg.channel.id == newChannel.id) msg.channel = newChannel;
}
2023-01-24 05:50:55 +00:00
2024-07-23 22:10:17 +00:00
if (!(msg.channel instanceof Channel)) return;
if (msg.author.bot && msg.guildID) {
const canBot = await hasFlag(msg.guildID, "replyToBots");
if (!canBot) return;
}
2023-01-24 05:50:55 +00:00
2024-05-16 20:43:51 +00:00
await CommandDispatcher(msg);
} catch (err) {
const stack = (err?.stack ?? err.message).split("\n");
const error = stack.shift();
2024-07-23 22:10:17 +00:00
logger.error("hf:main", `Failed to dispatch command: ${error}\n\t${stack.join("\n\t")}`);
2024-05-16 20:43:51 +00:00
}
});
2024-05-17 20:33:01 +00:00
bot.on("messageUpdate", async (msg, oldMsg) => {
2024-05-16 20:43:51 +00:00
try {
const oneDay = Date.now() - 86400000;
2024-07-23 22:10:17 +00:00
if (msg.timestamp > oneDay && !msg.hasRan && oldMsg && oldMsg.content !== msg.content) {
2024-05-17 20:33:01 +00:00
await CommandDispatcher(msg);
2024-05-16 20:43:51 +00:00
}
} catch (err) {
const stack = (err?.stack ?? err.message).split("\n");
const error = stack.shift();
2024-07-23 22:10:17 +00:00
logger.error("hf:main", `Failed to dispatch command update: ${error}\n\t${stack.join("\n\t")}`);
2024-05-16 20:43:51 +00:00
}
});
bot.on("messageReactionAdd", async (msg, reaction, reactor) => {
2024-05-17 20:39:14 +00:00
if (msg?.author?.id !== bot.user.id) return;
2024-05-16 20:43:51 +00:00
if (reaction.name !== "\u274c") return;
try {
let channel = msg.channel;
2024-07-23 22:10:17 +00:00
if (!(channel instanceof Channel)) {
2024-05-16 20:43:51 +00:00
const newChannel = hf.bot.getChannel(channel.id);
if (newChannel) {
channel = newChannel;
} else {
channel = await hf.bot.getRESTChannel(channel.id);
}
}
if (!msg.messageReference) {
msg = await channel.getMessage(msg.id);
}
if (!msg.messageReference) return;
const ref = await channel.getMessage(msg.messageReference.messageID);
if (!ref) return;
if (ref.author.id !== reactor.id) return;
await msg.delete("Command sender requested output deletion.");
} catch (err) {
const stack = (err?.stack ?? err.message).split("\n");
const error = stack.shift();
2024-07-23 22:10:17 +00:00
logger.error("hf:main", `Failed to self-delete message: ${error}\n\t${stack.join("\n\t")}`);
2021-03-15 01:41:40 +00:00
}
});
2024-05-17 20:33:01 +00:00
bot.on("interactionCreate", async (interaction) => {
try {
await InteractionDispatcher(interaction);
} catch (err) {
const stack = (err?.stack ?? err.message).split("\n");
const error = stack.shift();
2024-07-23 22:10:17 +00:00
logger.error("hf:main", `Failed to dispatch interaction command: ${error}\n\t${stack.join("\n\t")}`);
2024-05-17 20:33:01 +00:00
}
});
2021-03-15 01:41:40 +00:00
bot.once("ready", async () => {
2021-03-15 01:41:40 +00:00
logger.info("hf:main", "Connected to Discord.");
2024-07-23 22:10:17 +00:00
logger.info("hf:main", `Logged in as: ${formatUsername(bot.user)} (${bot.user.id})`);
2021-03-15 01:41:40 +00:00
2023-01-22 04:52:07 +00:00
const channel = await bot.getDMChannel(config.owner_id);
2021-03-15 01:41:40 +00:00
if (channel) {
2022-10-09 18:15:37 +00:00
channel.createMessage({
content: "<:ms_tick:503341995348066313> Loaded HiddenPhox.",
});
2021-03-15 01:41:40 +00:00
}
bot.on("ready", () => {
logger.info("hf:main", "Reconnected to Discord.");
});
2024-05-17 20:33:01 +00:00
2024-08-13 19:34:31 +00:00
if (!config.no_commands) {
const commands = [];
for (const command of interactionCommands.values()) {
const options = [];
for (const option of Object.values(command.options)) {
const newOption = Object.assign({}, option);
delete newOption.default;
options.push(newOption);
}
2024-08-13 19:34:31 +00:00
// stupid hack
const send = options.shift();
options.push(send);
const formattedCommand = {
name: command.name,
type: command.type,
description: command.helpText,
options: options,
integration_types: [0],
contexts: [0],
};
if (!command.guildOnly) {
formattedCommand.integration_types.push(1);
formattedCommand.contexts.push(1, 2);
}
2024-08-13 19:34:31 +00:00
if (command.type === ApplicationCommandTypes.CHAT_INPUT)
formattedCommand.name = formattedCommand.name.toLowerCase();
2024-05-17 20:33:01 +00:00
2024-08-13 19:34:31 +00:00
if (command.permissions !== undefined) {
formattedCommand.default_member_permissions =
command.permissions instanceof Permission ? String(command.permissions.allow) : String(command.permissions);
}
commands.push(formattedCommand);
2024-05-17 20:54:37 +00:00
}
2024-08-13 19:34:31 +00:00
await bot.requestHandler.request("PUT", APIEndpoints.COMMANDS(bot.application.id), true, commands);
2024-05-17 20:33:01 +00:00
}
2021-03-15 01:41:40 +00:00
});
2021-06-01 18:59:15 +00:00
bot.on("error", (err) => {
logger.error("hf:main", "Catching error: " + err);
2021-07-21 16:33:36 +00:00
});
bot.on("warn", (err) => {
logger.warn("hf:main", "Catching warn: " + err);
2021-07-21 16:33:36 +00:00
});
bot.on("shardDisconnect", (err, id) => {
logger.verbose("hf:shard", `Disconnecting from shard ${id}: ${err}`);
2021-07-21 16:33:36 +00:00
});
bot.on("shardResume", (id) => {
logger.verbose("hf:shard", "Resuming on shard " + id);
2021-07-21 16:33:36 +00:00
});
bot.on("shardPreReady", (id) => {
logger.verbose("hf:shard", `Shard ${id} getting ready`);
2021-07-21 16:33:36 +00:00
});
bot.on("shardReady", (id) => {
logger.verbose("hf:shard", `Shard ${id} ready`);
2021-07-21 16:33:36 +00:00
});
bot.on("unknown", (packet, id) => {
2024-07-23 22:10:17 +00:00
logger.verbose("hf:main", `Shard ${id} caught unknown packet: ${JSON.stringify(packet)}`);
2021-06-01 18:59:15 +00:00
});
2023-09-25 20:00:36 +00:00
instead("spawn", bot.shards, function (args, orig) {
const ret = orig.apply(this, args);
const shard = this.get(args[0]);
if (shard) {
2023-09-25 20:12:14 +00:00
before("sendWS", shard.__proto__, function ([op, _data]) {
2024-07-23 22:10:17 +00:00
if (op === GatewayOPCodes.IDENTIFY) {
2023-09-25 20:00:36 +00:00
_data.properties.browser = "Discord Embedded";
delete _data.properties.device;
}
});
}
return ret;
});
2021-03-15 01:41:40 +00:00
bot.connect();