2025-02-02 14:10:51 -05:00
|
|
|
// bot.js - Discord bot for moderation and utilities
|
|
|
|
|
// Copyright (C) 2025 Luis Bauza
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
2025-02-19 12:01:09 -05:00
|
|
|
import 'dotenv/config';
|
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
|
import { dirname } from 'path';
|
|
|
|
|
import fs from 'node:fs';
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
import { Client, Collection, Events, GatewayIntentBits } from 'discord.js';
|
|
|
|
|
import Logger from './logger.js';
|
|
|
|
|
import { loadCommands } from './utils/commandLoader.js';
|
|
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
|
const __dirname = dirname(__filename);
|
2025-02-09 14:58:31 -05:00
|
|
|
|
|
|
|
|
const logger = new Logger('bot');
|
2025-02-02 14:10:51 -05:00
|
|
|
|
|
|
|
|
const client = new Client({
|
2025-02-06 20:08:37 -05:00
|
|
|
intents: [
|
|
|
|
|
GatewayIntentBits.Guilds,
|
|
|
|
|
GatewayIntentBits.GuildMessages,
|
|
|
|
|
GatewayIntentBits.MessageContent,
|
|
|
|
|
],
|
2025-02-02 14:10:51 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
client.commands = new Collection();
|
|
|
|
|
const commandsPath = path.join(__dirname, 'commands');
|
|
|
|
|
|
2025-02-19 12:01:09 -05:00
|
|
|
const commands = await loadCommands(commandsPath, logger);
|
|
|
|
|
commands.forEach(command => {
|
|
|
|
|
client.commands.set(command.data.name, command);
|
|
|
|
|
});
|
2025-02-02 14:10:51 -05:00
|
|
|
|
|
|
|
|
client.once(Events.ClientReady, () => {
|
2025-02-09 14:58:31 -05:00
|
|
|
logger.log(`Ready! Logged in as ${client.user.tag}`);
|
2025-02-02 14:10:51 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
client.on(Events.InteractionCreate, async interaction => {
|
2025-02-06 20:08:37 -05:00
|
|
|
if (!interaction.isChatInputCommand()) return;
|
2025-02-02 14:10:51 -05:00
|
|
|
|
2025-02-06 20:08:37 -05:00
|
|
|
const command = client.commands.get(interaction.commandName);
|
2025-02-02 14:10:51 -05:00
|
|
|
|
2025-02-06 20:08:37 -05:00
|
|
|
if (!command) {
|
2025-02-09 14:58:31 -05:00
|
|
|
logger.error(`No command matching ${interaction.commandName} was found.`);
|
2025-02-06 20:08:37 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2025-02-02 14:10:51 -05:00
|
|
|
|
2025-02-06 20:08:37 -05:00
|
|
|
try {
|
|
|
|
|
await command.execute(interaction);
|
|
|
|
|
} catch (error) {
|
2025-03-25 11:17:26 -04:00
|
|
|
// Log detailed error context
|
|
|
|
|
logger.error(
|
|
|
|
|
`Command "${interaction.commandName}" failed for user ${interaction.user.tag}:`,
|
|
|
|
|
error,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Prepare error response
|
|
|
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
|
|
|
const errorMessage = isProduction
|
|
|
|
|
? `❌ Command failed. Please try again later.`
|
|
|
|
|
: `❌ Command failed: ${error.message}\n\n${error.stack}`;
|
|
|
|
|
|
|
|
|
|
const responseMethod = interaction.replied || interaction.deferred ? 'followUp' : 'reply';
|
|
|
|
|
|
|
|
|
|
await interaction[responseMethod]({
|
|
|
|
|
content: errorMessage,
|
|
|
|
|
ephemeral: true,
|
|
|
|
|
});
|
2025-02-06 20:08:37 -05:00
|
|
|
}
|
2025-02-02 14:10:51 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
client.login(process.env.DISCORD_TOKEN);
|