Files
commands
events
utils
.gitignore
config.json
index.js
package.json
README.md
Code
30 1,794
index.js
const { Client, GatewayIntentBits, Collection } = require('discord.js');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const logger = require('./utils/logger');
const { handleButton } = require('./utils/buttonHandler');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildModeration
]
});
client.commands = new Collection();
client.config = require('./config.json');
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
logger.info(`Loaded command: ${command.data.name}`);
} else {
logger.warn(`Command at ${filePath} is missing required "data" or "execute" property.`);
}
}
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
logger.info(`Loaded event: ${event.name}`);
}
client.on('interactionCreate', async interaction => {
if (interaction.isChatInputCommand()) {
const command = client.commands.get(interaction.commandName);
if (!command) {
logger.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
logger.info(`${interaction.user.tag} executed /${interaction.commandName}`);
} catch (error) {
logger.error(`Error executing ${interaction.commandName}:`, error);
const reply = { content: 'There was an error while executing this command!', ephemeral: true };
if (interaction.replied || interaction.deferred) {
await interaction.followUp(reply);
} else {
await interaction.reply(reply);
}
}
} else if (interaction.isButton() || interaction.isStringSelectMenu() || interaction.isModalSubmit()) {
try {
await handleButton(interaction);
} catch (error) {
logger.error('Error handling interaction:', error);
const reply = { content: 'There was an error processing your request!', ephemeral: true };
if (interaction.replied || interaction.deferred) {
await interaction.followUp(reply);
} else {
await interaction.reply(reply);
}
}
}
});
client.login(process.env.DISCORD_TOKEN);