HiddenPhox/src/index.js

164 lines
4.5 KiB
JavaScript

// ensure gateway/rest v9
const Constants = require("eris/lib/Constants");
const fixedConstants = Constants;
fixedConstants.GATEWAY_VERSION = 9;
fixedConstants.REST_VERSION = 9;
const constantsPath = require.resolve("eris/lib/Constants");
require.cache[constantsPath].exports = fixedConstants;
const Eris = require("eris");
const logger = require("./lib/logger.js");
const fs = require("fs");
const {resolve} = require("path");
const sqlite3 = require("sqlite3");
const config = require("../config.json");
const apikeys = require("../apikeys.json");
const Command = require("./lib/command.js");
const events = require("./lib/events.js");
const timer = require("./lib/timer.js");
const bot = new Eris(config.token, {
defaultImageFormat: "png",
defaultImageSize: 1024,
intents: Object.keys(Eris.Constants.Intents).filter(
(x) => x != "guildMembers" && x != "guildPresences"
),
});
const commands = new Eris.Collection();
const database = new sqlite3.Database(resolve(__dirname, "..", "database.db"));
function registerCommand(cmdObj) {
if (cmdObj instanceof Command) {
commands.set(cmdObj.name, cmdObj);
const aliases = cmdObj.getAliases();
logger.info(
"hf:cmd",
`Registered command '${cmdObj.name}'${
aliases.length > 0 ? ` (aliases: ${aliases.join(", ")})` : ""
}`
);
}
}
global.hf = {
bot,
config,
apikeys,
commands,
registerCommand,
events,
timer,
database,
};
const CommandDispatcher = require("./lib/commandDispatcher.js");
for (const file of fs.readdirSync(resolve(__dirname, "modules"))) {
require(resolve(__dirname, "modules", file));
logger.info("hf:modules", `Loaded module: '${file}'`);
}
bot.on("messageCreate", async (msg) => {
// fix DMs cause of gateway v8 changes
if (
!(msg.channel instanceof Eris.Channel) &&
msg.author.id != bot.user.id &&
!msg.guildID
) {
const newChannel = await bot.getDMChannel(msg.author.id);
if (msg.channel.id == newChannel.id) msg.channel = newChannel;
} else if (msg.guildID && !(msg.channel instanceof Eris.Channel)) {
const newChannel = bot.guilds.get(msg.guildID).channels.get(msg.channel.id);
if (newChannel) msg.channel = newChannel;
}
// if we still have no dm channel (threads cause this too)
if (!(msg.channel instanceof Eris.Channel)) return;
await CommandDispatcher(msg);
});
bot.on("messageUpdate", (msg) => {
const oneDay = Date.now() - 86400000;
if (msg.timestamp > oneDay && !msg.hasRan) {
CommandDispatcher(msg);
}
});
bot.on("ready", async () => {
logger.info("hf:main", "Connected to Discord.");
logger.info(
"hf:main",
`Logged in as: ${bot.user.username}#${bot.user.discriminator} (${bot.user.id})`
);
const channel = await bot.getDMChannel(config.owner_id);
if (channel) {
channel.createMessage("<:ms_tick:503341995348066313> Loaded HiddenPhox.");
}
});
bot.once("ready", async function () {
const guilds = bot.guilds.values();
async function cacheThreads(guild) {
if (!guild) return;
const threads = await bot.requestHandler
.request("GET", `/guilds/${guild.id}/threads/active`, true)
.then((x) => x.threads);
for (const thread of threads) {
guild.channels.set(thread.id, new Eris.TextChannel(thread, bot));
}
const next = guilds.next().value;
if (next) await cacheThreads(next);
}
await cacheThreads(guilds.next().value);
});
bot.on("unknown", (packet) => {
if (packet.t == "THREAD_CREATE") {
const guild = bot.guilds.get(packet.d.guild_id);
if (!guild) {
logger.warn(
`Got THREAD_CREATE from invalid guild??? id: ${packet.d.guild_id}`
);
} else {
guild.channels.set(packet.d.id, new Eris.TextChannel(packet.d, bot));
}
} else if (packet.t == "THREAD_DELETE") {
const guild = bot.guilds.get(packet.d.guild_id);
guild.channels.delete(packet.d.id);
}
});
bot.on("error", (err) => {
logger.error("hf:main", "Catching error: " + err);
});
bot.on("warn", (err) => {
logger.warn("hf:main", "Catching warn: " + err);
});
bot.on("shardDisconnect", (err, id) => {
logger.verbose("hf:shard", `Disconnecting from shard ${id}: ${err}`);
});
bot.on("shardResume", (id) => {
logger.verbose("hf:shard", "Resuming on shard " + id);
});
bot.on("shardPreReady", (id) => {
logger.verbose("hf:shard", `Shard ${id} getting ready`);
});
bot.on("shardReady", (id) => {
logger.verbose("hf:shard", `Shard ${id} ready`);
});
bot.on("unknown", (packet, id) => {
logger.verbose(
"hf:main",
`Shard ${id} caught unknown packet: ${JSON.stringify(packet)}`
);
});
bot.connect();