-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.mjs
86 lines (68 loc) · 3 KB
/
bot.mjs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { Configuration, OpenAIApi } from 'openai';
import sqlite3 from 'sqlite3';
import { Client, Events, GatewayIntentBits, Collection } from 'discord.js';
import dotenv from 'dotenv';
dotenv.config({ path: './.env' });
//===============================================================================
//Open Connections / Make them available to other modules
//===============================================================================
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
export const openai = new OpenAIApi(configuration);
export const db = new sqlite3.Database('bot.db');
export const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
//GatewayIntentBits.MessageContent,
]
});
client.login(process.env.DISCORD_BOT_TOKEN);
client.commands = new Collection();
//===============================================================================
//Validate Connections
//===============================================================================
client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});
//===============================================================================
// Discord Specific Initializations
//===============================================================================
const commandsPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
commandFiles.forEach(async file => {
const filePath = path.join(commandsPath, file);
const fileURL = pathToFileURL(filePath);
try {
const command = await import(fileURL.href);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
} catch (error) {
console.error(`Error importing command ${file}:`, error);
}
});
//===============================================================================
// Event Handling
//===============================================================================
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.editReply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});