Files
commands
handlers
utils
bot.js
package.json
README.md
Code
17 896
bot.js
require('dotenv').config();
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { refreshLeaderboard, refreshVoiceStats } = require('./utils/statistics');
if (!process.env.DISCORD_TOKEN) {
console.error('DISCORD_TOKEN is missing. Copy .env.example to .env and add your bot token.');
process.exit(1);
}
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
for (const file of fs.readdirSync(commandsPath).filter((entry) => entry.endsWith('.js'))) {
const command = require(path.join(commandsPath, file));
client.commands.set(command.data.name, command);
}
client.once(Events.ClientReady, async (readyClient) => {
try {
await readyClient.application.commands.set(
[...client.commands.values()].map((command) => command.data.toJSON())
);
console.log(`Logged in as ${readyClient.user.tag}`);
console.log('KODARI_READY');
for (const guild of readyClient.guilds.cache.values()) {
await refreshLeaderboard(client, guild.id).catch((error) => console.error('Leaderboard refresh failed:', error));
await refreshVoiceStats(client, guild.id).catch((error) => console.error('Voice stats refresh failed:', error));
}
} catch (error) {
console.error('Failed to register application commands:', error);
}
});
client.on(Events.InteractionCreate, async (interaction) => {
try {
if (interaction.isChatInputCommand()) {
const command = client.commands.get(interaction.commandName);
if (command) await command.execute(interaction, client);
return;
}
if (interaction.isButton() && interaction.customId === 'review:submit') {
const { showReviewModal } = require('./handlers/reviewFlow');
await showReviewModal(interaction);
return;
}
if (interaction.isModalSubmit() && interaction.customId === 'review:submit-modal') {
const { handleReviewSubmission } = require('./handlers/reviewFlow');
await handleReviewSubmission(interaction, client);
}
} catch (error) {
console.error('Interaction failed:', error);
const response = { content: 'Something went wrong while handling that interaction. Please try again later.', ephemeral: true };
if (interaction.deferred || interaction.replied) {
await interaction.followUp(response).catch(() => {});
} else {
await interaction.reply(response).catch(() => {});
}
}
});
client.on(Events.Error, (error) => console.error('Discord client error:', error));
process.on('unhandledRejection', (error) => console.error('Unhandled promise rejection:', error));
process.on('uncaughtException', (error) => {
console.error('Uncaught exception:', error);
process.exit(1);
});
setInterval(async () => {
for (const guild of client.guilds.cache.values()) {
await refreshVoiceStats(client, guild.id).catch((error) => console.error('Voice stats refresh failed:', error));
}
}, 10 * 60 * 1000).unref();
client.login(process.env.DISCORD_TOKEN);