Tengo un fragmento de código que se ejecuta automáticamente cada 4 horas para enviar un mensaje establecido a un canal de discordia específico en mi servidor de discordia, pero me gustaría que esta función se inicie con el comando y me gustaría que ese comando esté en un archivo separado de mi código de índice principal si alguien pudiera darme una plantilla para este código como un comando de barra, lo agradecería mucho, me ahorraría días, posiblemente semanas, de tediosa investigación y prueba y error. Necesito un código que esté actualizado con v13 de discord. .js y será compatible con mi código de archivo de índice principal que se establecerá debajo de la función específica que quiero lograr.
setInterval(() => { client.channels.cache.get("916642101989617684").send("!stats"); }, (1000 * 60) * 240); const fs = require('fs'); const { Client, Collection, Intents } = require('discord.js'); const { token } = require('./config.json'); const client = new Client({ intents: [Intents.FLAGS.GUILDS] }); client.commands = new Collection(); const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.data.name, command); } const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js')); for (const file of eventFiles) { const event = require(`./events/${file}`); if (event.once) { client.once(event.name, (...args) => event.execute(...args)); } else { client.on(event.name, (...args) => event.execute(...args)); } } client.once('ready', () => { console.log('Ready!'); }); setInterval(() => { client.channels.cache.get("916642101989617684").send("!stats"); }, (1000 * 60) * 240); client.on('interactionCreate', async interaction => { if (!interaction.isCommand()) return; const command = client.commands.get(interaction.commandName); if (!command) return; try { await command.execute(interaction); } catch (error) { console.error(error); return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } }); client.login(token);Simplemente póngalo en un evento de creación de interacción, pero asegúrese de que sea una vez para que no haga más intervalos constantemente.
client.once("interactionCreate", async (interaction) => { if (interaction.commandName === "register") { //or whatever name the command should be //the code you want to start here })