Files
dashboard
lavalink
src
.gitignore
package.json
README.md
Code
21 1,432
src/index.js
require('./config');
require('./command-compat');
const { Client, GatewayIntentBits, Partials, PermissionFlagsBits } = require('discord.js');
const { LavalinkManager } = require('lavalink-client');
const config = require('./config');
const { db } = require('./database');
const commandModule = require('./commands');
const extraCommands = require('./extra-commands');
const { handleInteraction, runGiveaway } = require('./interactions');
const { registerEvents } = require('./events');
const { createDashboardServer } = require('./dashboard-server');
const { logEmbed } = require('./services');
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildModeration, GatewayIntentBits.GuildInvites],
partials: [Partials.Channel, Partials.Message, Partials.GuildMember]
});
let music = null;
if (config.lavalink.host) {
music = new LavalinkManager({
nodes: [{ id: 'primary', host: config.lavalink.host, port: config.lavalink.port, authorization: config.lavalink.password, secure: config.lavalink.secure }],
sendToShard: (guildId, payload) => client.guilds.cache.get(guildId)?.shard.send(payload),
client: { id: config.clientId, username: 'Kodari Bot' }
});
client.on('raw', data => music.sendRawData(data));
music.on('nodeConnect', node => console.log(`Lavalink node connected: ${node.id}`));
music.on('nodeDisconnect', node => console.error(`Lavalink node disconnected: ${node.id}`));
music.on('trackError', (player, track, error) => console.error('Lavalink track error:', track?.info?.title, error));
}
const rest = new (require('@discordjs/rest').REST)({ version: '10' }).setToken(config.token);
async function registerCommands() {
const definitions = [...commandModule.commands, ...extraCommands.commands];
await rest.put(require('discord.js').Routes.applicationCommands(config.clientId), { body: definitions });
console.log(`Registered ${definitions.length} global slash commands.`);
}
const services = { client, music, registerCommands, handleCommand: null, handleInteraction: null };
services.handleCommand = async interaction => {
if (['botinfo', 'status', 'reload', 'maintenance'].includes(interaction.commandName) && (!config.ownerId || interaction.user.id !== config.ownerId)) return interaction.reply({ content: 'This command is restricted to the configured bot owner.', ephemeral: true });
if (['ticket', 'giveaway'].includes(interaction.commandName) && (!interaction.memberPermissions?.has(PermissionFlagsBits.ManageGuild))) return interaction.reply({ content: 'Manage Server permission is required.', ephemeral: true });
return commandModule.handleCommand(interaction, services);
};
services.handleInteraction = interaction => handleInteraction(interaction, services);
registerEvents(client, services);
client.once('clientReady', async () => {
try { await registerCommands(); } catch (error) { console.error('Slash command registration failed:', error); }
if (music) {
try { await music.init(client.user); } catch (error) { console.error('Lavalink initialization failed:', error); }
}
client.user.setPresence({ activities: [{ name: '/help', type: 0 }], status: 'online' });
});
const app = createDashboardServer(client, music, config);
const server = app.listen(config.serverPort, '0.0.0.0', () => console.log(`Dashboard/API listening on 0.0.0.0:${config.serverPort}`));
const giveawayTimer = setInterval(async () => {
const expired = db.prepare("SELECT * FROM giveaways WHERE status='active' AND ends_at<=?").all(Date.now());
for (const giveaway of expired) await runGiveaway(client, giveaway).catch(error => console.error('Giveaway completion failed:', error));
}, 15000);
giveawayTimer.unref();
const birthdayTimer = setInterval(async () => {
const now = new Date(), month = now.getMonth() + 1, day = now.getDate();
const rows = db.prepare('SELECT guild_id,user_id FROM birthdays WHERE month=? AND day=?').all(month, day);
for (const row of rows) {
const guild = client.guilds.cache.get(row.guild_id);
if (!guild) continue;
const channelId = require('./database').getSettings(row.guild_id).welcome.channelId;
const channel = channelId ? await guild.channels.fetch(channelId).catch(() => null) : null;
await channel?.send(`๐ Happy birthday, <@${row.user_id}>!`).catch(() => {});
}
}, 60 * 60 * 1000);
birthdayTimer.unref();
process.on('unhandledRejection', error => console.error('Unhandled promise rejection:', error));
process.on('uncaughtException', error => { console.error('Uncaught exception:', error); });
let shuttingDown = false;
async function shutdown(signal) {
if (shuttingDown) return;
shuttingDown = true;
console.log(`Received ${signal}; shutting down gracefully.`);
clearInterval(giveawayTimer); clearInterval(birthdayTimer);
server.close();
client.destroy();
db.close();
process.exit(0);
}
process.once('SIGINT', () => shutdown('SIGINT'));
process.once('SIGTERM', () => shutdown('SIGTERM'));
client.login(config.token).catch(error => { console.error('Discord login failed:', error); process.exit(1); });