crra/index.ts

89 lines
3.0 KiB
TypeScript
Raw Permalink Normal View History

2024-10-24 22:36:49 -04:00
import { Client, GatewayIntentBits, Partials, REST, Routes } from 'discord.js';
import { discordBotToken, discordClientID, MongoDbUrl } from './config.json';
import Collection from './util/Collection';
import DiscordInteractionCommand from './util/DiscordInteractionCommand';
import DiscordEvent from './util/DiscordEvent';
import * as DiscordInteractionCommandsIndex from './discord/commands';
import * as DiscordEventsIndex from './discord/events';
import mongoose from 'mongoose';
2024-03-19 17:41:20 -04:00
2024-10-24 22:36:49 -04:00
export const DiscordInteractionCommands: Collection<DiscordInteractionCommand> =
new Collection();
2024-03-19 17:41:20 -04:00
export const DiscordEvents: Collection<DiscordEvent> = new Collection();
// Instantiates a new Discord client
const discordClient = new Client({
2024-10-24 22:36:49 -04:00
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildIntegrations,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildInvites,
GatewayIntentBits.GuildModeration,
],
partials: [
Partials.GuildMember,
Partials.Message,
Partials.User,
Partials.Channel,
],
2024-03-19 17:41:20 -04:00
});
const discordREST = new REST().setToken(discordBotToken);
// const stripeClient = new Stripe(stripeToken, { typescript: true });
export async function main() {
2024-10-24 22:36:49 -04:00
// Connect to the databases
try {
//@ts-ignore
mongoose.connection.once('open', () => {
console.info('[Info - Database] Connected to MongoDB');
});
// TODO: Fetch the MongoDB URI from the config file
await mongoose.connect(MongoDbUrl, {});
} catch (error) {
console.error(`[Error - Database] Failed to connect to MongoDB: ${error}`);
process.exit(1);
}
// Load Discord interaction commands
for (const Command of Object.values(DiscordInteractionCommandsIndex)) {
const instance = new Command();
DiscordInteractionCommands.add(instance.name, instance);
console.info(
`[Info - Discord] Loaded interaction command: ${instance.name}`
);
}
// Load Discord events
for (const Event of Object.values(DiscordEventsIndex)) {
const instance = new Event(discordClient);
DiscordEvents.add(instance.name, instance);
discordClient.on(instance.name, instance.execute);
console.info(`[Info - Discord] Loaded event: ${instance.name}`);
}
await discordClient.login(discordBotToken);
2024-03-19 17:41:20 -04:00
2024-10-24 22:36:49 -04:00
try {
console.log(
`Started refreshing ${DiscordInteractionCommands.size} application (/) commands.`
);
const interactionCommandsData = [];
for (const command of DiscordInteractionCommands.values()) {
interactionCommandsData.push(command.builder.toJSON());
2024-03-19 17:41:20 -04:00
}
2024-10-24 22:36:49 -04:00
// The put method is used to fully refresh all commands in the guild with the current set
const data = await discordREST.put(
Routes.applicationCommands(discordClientID),
{ body: interactionCommandsData }
);
console.log(
`Successfully reloaded ${interactionCommandsData?.length} application (/) commands.`
);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
2024-03-19 17:41:20 -04:00
}
main();