Files
src
deploy-commands.js
index.js
package.json
Code
19 1,761
index.js
const { Client, GatewayIntentBits, Collection, Events, EmbedBuilder } = require('discord.js');
require('dotenv').config();
const path = require('path');
const fs = require('fs');
const db = require('./src/database');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'src', 'commands');
for (const file of fs.readdirSync(commandsPath).filter(f => f.endsWith('.js'))) {
const command = require(path.join(commandsPath, file));
if (command.data && command.execute) {
client.commands.set(command.data.name, command);
}
}
client.once(Events.ClientReady, () => {
console.log(`✅ Ready! Logged in as ${client.user.tag}`);
startLeaderboardUpdater(client);
});
client.on(Events.InteractionCreate, async interaction => {
if (interaction.isChatInputCommand()) {
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (err) {
console.error(`Error in /${interaction.commandName}:`, err);
const payload = { content: '❌ An error occurred while running this command.', ephemeral: true };
if (interaction.replied || interaction.deferred) {
await interaction.followUp(payload).catch(() => {});
} else {
await interaction.reply(payload).catch(() => {});
}
}
return;
}
if (interaction.isButton()) {
const prefix = interaction.customId.split('_')[0];
try {
if (prefix === 'bj') {
const { handleButton } = require('./src/commands/blackjack');
await handleButton(interaction);
} else if (prefix === 'mines') {
const { handleButton } = require('./src/commands/mines');
await handleButton(interaction);
}
} catch (err) {
console.error('Button handler error:', err);
await interaction.reply({ content: '❌ Something went wrong.', ephemeral: true }).catch(() => {});
}
}
});
function buildLeaderboardEmbed(guildId) {
const top = db.getTopUsers(guildId, 10);
const desc = top.length === 0
? '*No users have played yet.*'
: top.map((u, i) => {
const medal = i === 0 ? '🥇' : i === 1 ? '🥈' : i === 2 ? '🥉' : `**${i + 1}.**`;
return `${medal} <@${u.user_id}> — 🪙 **${u.balance.toLocaleString()}**`;
}).join('\n');
return new EmbedBuilder()
.setTitle('🏆 Server Leaderboard')
.setDescription(desc)
.setColor(0xFFD700)
.setFooter({ text: 'Auto-updates every minute' })
.setTimestamp();
}
async function startLeaderboardUpdater(client) {
const update = async () => {
const entries = db.getLeaderboardChannels();
for (const entry of entries) {
try {
const guild = await client.guilds.fetch(entry.guild_id).catch(() => null);
if (!guild) continue;
const channel = await guild.channels.fetch(entry.channel_id).catch(() => null);
if (!channel || !channel.isTextBased()) continue;
const embed = buildLeaderboardEmbed(entry.guild_id);
if (entry.message_id) {
const msg = await channel.messages.fetch(entry.message_id).catch(() => null);
if (msg) {
await msg.edit({ embeds: [embed] });
continue;
}
}
const newMsg = await channel.send({ embeds: [embed] });
db.updateLeaderboardMessage(entry.guild_id, newMsg.id);
} catch (err) {
console.error('Leaderboard update error:', err.message);
}
}
};
await update();
setInterval(update, 60_000);
}
client.login(process.env.DISCORD_TOKEN);