Files
EllyDiscordBot/src/events/ready.ts

120 lines
5.4 KiB
TypeScript

/**
* Ready Event Handler
* Handles bot initialization when connected to Discord
*/
import { Events, REST, Routes } from 'discord.js';
import type { EllyClient } from '../client/EllyClient.ts';
import type { BotEvent } from '../types/index.ts';
export const readyEvent: BotEvent = {
name: Events.ClientReady,
once: true,
async execute(client: EllyClient): Promise<void> {
console.log('');
client.logger.info('═══════════════════════════════════════════════════════');
client.logger.info(' BOT CONNECTED ');
client.logger.info('═══════════════════════════════════════════════════════');
client.logger.info(`✓ Logged in as: ${client.user?.tag}`);
client.logger.info(` ├─ User ID: ${client.user?.id}`);
client.logger.info(` ├─ Guilds: ${client.guilds.cache.size}`);
client.logger.info(` └─ Users: ${client.users.cache.size} cached`);
// Register slash commands
await registerCommands(client);
console.log('');
client.logger.info('═══════════════════════════════════════════════════════');
client.logger.info(' ELLY IS NOW ONLINE ');
client.logger.info('═══════════════════════════════════════════════════════');
console.log('');
},
};
/**
* Register slash commands with Discord
*/
async function registerCommands(client: EllyClient): Promise<void> {
const token = Deno.env.get('DISCORD_TOKEN');
if (!token || !client.user) {
client.logger.error('✗ Cannot register commands: missing token or client user');
return;
}
const rest = new REST({ version: '10' }).setToken(token);
const commands = client.commands.map((cmd) => cmd.data.toJSON());
console.log('');
client.logger.info('───────────────────────────────────────────────────────');
client.logger.info(' SLASH COMMAND REGISTRATION ');
client.logger.info('───────────────────────────────────────────────────────');
try {
client.logger.info(`Preparing to sync ${commands.length} slash commands...`);
// List all commands being registered
const commandsByCategory: Record<string, string[]> = {};
for (const cmd of client.commands.values()) {
const category = cmd.data.name.includes('bedwars') || cmd.data.name.includes('skywars') || cmd.data.name === 'guild'
? 'Statistics'
: cmd.data.name.includes('marry') || cmd.data.name.includes('divorce') || cmd.data.name === 'relationship'
? 'Family'
: cmd.data.name === 'remind' || cmd.data.name === 'away'
? 'Utility'
: cmd.data.name === 'purge'
? 'Moderation'
: 'Developer';
if (!commandsByCategory[category]) {
commandsByCategory[category] = [];
}
commandsByCategory[category].push(`/${cmd.data.name}`);
}
for (const [category, cmds] of Object.entries(commandsByCategory)) {
client.logger.info(` 📁 ${category}: ${cmds.join(', ')}`);
}
// Register to specific guild for faster updates during development
if (client.config.guild.id) {
client.logger.info('');
client.logger.info(`Syncing to guild: ${client.config.guild.name}`);
client.logger.info(` ├─ Guild ID: ${client.config.guild.id}`);
client.logger.info(` ├─ Mode: Guild-specific (instant updates)`);
client.logger.info(` └─ Syncing...`);
const startTime = Date.now();
await rest.put(Routes.applicationGuildCommands(client.user.id, client.config.guild.id), {
body: commands,
});
const elapsed = Date.now() - startTime;
client.logger.info('');
client.logger.info(`✓ Successfully synced ${commands.length} commands to guild!`);
client.logger.info(` ├─ Time: ${elapsed}ms`);
client.logger.info(` ├─ Guild: ${client.config.guild.name}`);
client.logger.info(` └─ Commands are available immediately`);
} else {
client.logger.info('');
client.logger.info('Syncing globally (no guild ID configured)');
client.logger.info(' ├─ Mode: Global (may take up to 1 hour)');
client.logger.info(' └─ Syncing...');
const startTime = Date.now();
await rest.put(Routes.applicationCommands(client.user.id), {
body: commands,
});
const elapsed = Date.now() - startTime;
client.logger.info('');
client.logger.info(`✓ Successfully synced ${commands.length} commands globally!`);
client.logger.info(` ├─ Time: ${elapsed}ms`);
client.logger.info(` └─ Note: Global commands may take up to 1 hour to appear`);
}
} catch (error) {
client.logger.error('✗ Failed to register commands');
client.logger.error(` └─ Error: ${error instanceof Error ? error.message : String(error)}`);
}
}