index.ts

1import { Collection, Client, GatewayIntentBits } from 'discord.js'
2import { Bot } from '@/types/bot'
3import { config as dotenv } from 'dotenv'
4import { loadCommands, loadEvents, commands, events } from '@/utils/collection'
5import { join } from 'node:path'
6import Logger from './classes/logger'
7import { BlueskyService } from '@/services/bluesky'
8import { startServer } from './server'
9import { ReminderService } from './services/reminderService'
10import { GiveawayService } from '@/services/giveawayService'
11
12dotenv() // Load environment variables first
13const token = process.env.DISCORD_BOT_TOKEN
14
15const client = new Client({
16	intents: [
17		GatewayIntentBits.Guilds,
18		GatewayIntentBits.GuildMembers,
19		GatewayIntentBits.GuildVoiceStates,
20		GatewayIntentBits.MessageContent,
21		GatewayIntentBits.GuildEmojisAndStickers,
22		GatewayIntentBits.GuildMessages,
23		GatewayIntentBits.GuildMessageReactions,
24		GatewayIntentBits.DirectMessages,
25		GatewayIntentBits.GuildVoiceStates,
26	],
27	allowedMentions: {
28		parse: ['everyone', 'roles', 'users'],
29		repliedUser: true,
30	},
31}) as Bot<Client>
32
33// Initialize the commands Collection
34client.commands = new Collection()
35
36// Load commands first
37loadCommands(join(__dirname, 'commands'))
38// Set the commands to the client
39client.commands = commands
40
41// Then load events
42loadEvents(join(__dirname, 'events'), client)
43
44const blueskyService = new BlueskyService(client)
45
46const reminderService = new ReminderService(client)
47
48const giveawayService = new GiveawayService(client)
49giveawayService.start()
50
51// Add error handling for login
52client
53	.login(token)
54	.then(() => {
55		// Initialize Bluesky service after successful login
56		// blueskyService.init().catch(error => {
57		//     Logger.log('error', `Failed to initialize Bluesky service: ${error.message}`, 'Client');
58		// });
59
60		// Start reminder service after successful login
61		reminderService.start()
62	})
63	.catch((error) => {
64		Logger.log('error', `Failed to login: ${error.message}`, 'Client')
65		process.exit(1)
66	})
67
68// Add some basic error handlers
69client.on('error', (error) => {
70	Logger.log('error', `Client error: ${error.message}`, 'Client')
71})
72
73process.on('unhandledRejection', (error) => {
74	Logger.log('error', `Unhandled rejection: ${error}`, 'Process')
75})
76
77// Add cleanup for Bluesky service
78process.on('SIGINT', () => {
79	Logger.log('info', 'Shutting down...', 'Process')
80	// blueskyService.stop();
81	process.exit(0)
82})
83
84// Add cleanup for reminder service
85process.on('SIGINT', () => {
86	Logger.log('info', 'Shutting down...', 'Process')
87	reminderService.stop()
88	process.exit(0)
89})
90
91// Start the HTTP server
92startServer()
93