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.
|
|
|
|
|
|
|
|
|
|
require('dotenv').config();
|
|
|
|
|
const fs = require('node:fs');
|
|
|
|
|
const path = require('node:path');
|
|
|
|
|
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
|
|
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
|
|
|
|
|
2025-02-06 20:08:37 -05:00
|
|
|
const logger = message => process.stdout.write(`${message}\n`);
|
|
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
|
commandFiles.map(async file => {
|
2025-02-02 14:10:51 -05:00
|
|
|
const filePath = path.join(commandsPath, file);
|
2025-02-06 20:08:37 -05:00
|
|
|
const command = await import(`file://${filePath}`);
|
2025-02-02 14:10:51 -05:00
|
|
|
if ('data' in command && 'execute' in command) {
|
2025-02-06 20:08:37 -05:00
|
|
|
client.commands.set(command.data.name, command);
|
2025-02-02 14:10:51 -05:00
|
|
|
} else {
|
2025-02-06 20:08:37 -05:00
|
|
|
logger(
|
|
|
|
|
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`,
|
|
|
|
|
);
|
2025-02-02 14:10:51 -05:00
|
|
|
}
|
2025-02-06 20:08:37 -05:00
|
|
|
}),
|
|
|
|
|
);
|
2025-02-02 14:10:51 -05:00
|
|
|
|
|
|
|
|
client.once(Events.ClientReady, () => {
|
2025-02-06 20:08:37 -05:00
|
|
|
console.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) {
|
|
|
|
|
console.error(`No command matching ${interaction.commandName} was found.`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-02-02 14:10:51 -05:00
|
|
|
|
2025-02-06 20:08:37 -05:00
|
|
|
try {
|
|
|
|
|
await command.execute(interaction);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
if (interaction.replied || interaction.deferred) {
|
|
|
|
|
await interaction.followUp({
|
|
|
|
|
content: 'There was an error while executing this command!',
|
|
|
|
|
ephemeral: true,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content: 'There was an error while executing this command!',
|
|
|
|
|
ephemeral: true,
|
|
|
|
|
});
|
2025-02-02 14:10:51 -05:00
|
|
|
}
|
2025-02-06 20:08:37 -05:00
|
|
|
}
|
2025-02-02 14:10:51 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
client.login(process.env.DISCORD_TOKEN);
|