summary refs log tree commit diff
path: root/index.js
blob: 57c109168c0a2683c995d5c492bc973f6963f826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * Copyright (C) 2024 Emilia Luminé <eqilia@national.shitposting.agency>
 * This file is a part of the Amygdala bot.
*/

const path = require('node:path');
const fs = require('node:fs');

const dotenv = require('dotenv');
const { Client, GatewayIntentBits, Partials } = require('discord.js');

globalThis.config = require('./config.json');
dotenv.config({ quiet: true });

const client = new Client({
	intents: [
		GatewayIntentBits.Guilds,
		GatewayIntentBits.GuildMessages,
		GatewayIntentBits.GuildMembers,
		GatewayIntentBits.MessageContent
	]
});
globalThis.client = client;

globalThis.reloadEvents = () => {
	client.removeAllListeners();

	const eventsDir = fs.readdirSync(config.directories.eventsDir).filter((fileName) => fileName.endsWith('.js'));

	for (let i = 0; i < eventsDir.length; i += 1) {
		const eventModulePath = path.join(process.cwd(), config.directories.eventsDir, eventsDir[i]);

		const eventModule = require(eventModulePath);

		if (typeof eventModule.name !== 'string' || typeof eventModule.execute !== 'function') {
			console.error(`${eventModulePath} is not a valid event module`);
			break;
		}

		console.log(`${eventModule.name}`);
		client.on(eventModule.name, eventModule.execute);
	}
}

process.on('unhandledRejection', async (a) => {
	console.error(a);
});

globalThis.reloadEvents();

client.login(process.env.DISCORD_BOT_TOKEN);