const fs = require('node:fs'); const path = require('node:path'); const { Client, Collection, Events, GatewayIntentBits, WebhookClient } = require('discord.js'); const { token, winterToken } = require('./config.json'); const deployCommands = require('./deploy-commands.js'); const winterDeployCommands = require('./deploy-commands-winter.js'); const { startAPI } = require('./api/handler.js'); // Correct import const clientBot1 = new Client({ intents: [GatewayIntentBits.Guilds] }); const clientBot2 = new Client({ intents: [GatewayIntentBits.Guilds] }); clientBot1.commands = new Collection(); clientBot1.cooldowns = new Collection(); clientBot2.commands = new Collection(); clientBot2.cooldowns = new Collection(); const commandFolders = ['commands', 'winter-commands']; async function loadCommands(client, folders, botType) { for (const folderName of folders) { let commandsPath = path.join(__dirname, folderName); console.log(`[LAUNCH] Loading commands for ${botType} from: ${commandsPath}`); // Log on launch if (fs.existsSync(commandsPath)) { 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); console.log(`[LAUNCH] Detected command '${command.data.name}' for ${botType}`); // Log detected command } else { console.log(`[WARNING] The command at ${filePath} for ${botType} is missing a required "data" or "execute" property.`); } } } else { console.warn(`[WARNING] Command folder "${commandsPath}" not found for ${botType}.`); } } } loadCommands(clientBot1, ['commands'], 'pepe'); loadCommands(clientBot2, ['winter-commands'], 'winter'); 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) { clientBot1.once(event.name, (...args) => event.execute(...args)); clientBot2.once(event.name, (...args) => event.execute(...args)); } else { clientBot1.on(event.name, (...args) => event.execute(...args)); clientBot2.on(event.name, (...args) => event.execute(...args)); } } async function handleInteraction(interaction, client) { if (!interaction.isChatInputCommand()) return; const commandName = interaction.commandName; const command = client.commands.get(commandName); if (command) { console.log(`[RUN] Detected command '${commandName}' run by ${interaction.user.tag} on bot ${client === clientBot1 ? 'pepe' : 'winter'}`); // Log when command is run } if (!command) { console.error(`No command matching ${commandName} was found for bot ${client === clientBot1 ? 'pepe' : 'winter'}.`); return; } const { cooldowns } = client; if (!cooldowns.has(command.data.name)) { cooldowns.set(command.data.name, new Collection()); } const now = Date.now(); const timestamps = cooldowns.get(command.data.name); const defaultCooldownDuration = 3; const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000; if (timestamps.has(interaction.user.id)) { const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount; if (now < expirationTime) { const expiredTimestamp = Math.round(expirationTime / 1000); return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\` on bot ${client === clientBot1 ? 'pepe' : 'winter'}. You can use it again .`, ephemeral: true }); } } timestamps.set(interaction.user.id, now); setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount); try { await command.execute(interaction); } catch (error) { console.error(`Error executing command ${commandName} for bot ${client === clientBot1 ? 'pepe' : 'winter'}:`, error); if (interaction.replied || interaction.deferred) { await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); } else { await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } } } clientBot1.on(Events.InteractionCreate, async interaction => { await handleInteraction(interaction, clientBot1); }); clientBot2.on(Events.InteractionCreate, async interaction => { await handleInteraction(interaction, clientBot2); }); clientBot1.login(token); clientBot2.login(winterToken); deployCommands(); winterDeployCommands(); startAPI(); // Corrected function call