violet/src/index.ts
2022-08-27 13:22:42 -04:00

65 lines
1.7 KiB
TypeScript

import config from "./things/config";
import logger from "./things/logger";
import bot from "./things/bot";
import commands from "./commands/index";
import Eris, { Constants } from "eris";
bot.on("ready", () => {
const cmds = Object.values(commands).map((x) => {
return {
name: x.name,
description: x.description,
type: Constants.ApplicationCommandTypes.CHAT_INPUT,
options: x.options
};
});
if (process.env["NODE_ENV"] === "production") {
logger.info(`Loading ${cmds.length} commands in global mode...`);
bot.bulkEditCommands(cmds);
} else {
logger.info(`Loading ${cmds.length} commands in test mode...`);
for (const guild of config.testGuilds) {
logger.info(`Loading commands in guild ${guild}...`);
bot.bulkEditGuildCommands(guild, cmds);
}
}
logger.info(`All commands registered. Hello!`);
});
bot.on("interactionCreate", async (interaction) => {
logger.debug(`Handling interaction ${interaction.id}`);
if (interaction instanceof Eris.CommandInteraction) {
const commandName = interaction.data.name;
const command = commands[commandName];
if (command) {
try {
await command.command(interaction);
} catch (err) {
logger.error(
{ err },
`Error running command ${commandName} for interaction ${interaction.id}`
);
await interaction.createMessage({
content: ":warn: Something went wrong running this command.",
flags: Constants.MessageFlags.EPHEMERAL
});
}
} else {
logger.warn(`Unhandled command ${commandName}?`);
}
}
});
bot.on("error", (e) => {
logger.error("Error in bot", e);
});
bot.connect();