este código no está ejecutando la función deseada, que es poder iniciar un mensaje para repetir en un intervalo establecido. Al depurar no hay errores y el bot se conecta bien, pero el comando no se puede usar y no dice nada que sugiera cuál es el problema. Necesito la ayuda de un profesional para identificar la razón por la que este código no funciona y por qué el bot actúa como si ni siquiera existiera. Esta es la cantidad más corta de código para replicar los resultados. He omitido config.json porque tiene mi token, pero estoy seguro de que ese archivo está configurado correctamente de todos modos.
Archivo test.ts en la carpeta de comandos
const Discord = require("discord.js"); const source = require('../index'); module.exports.run = (Client, message, args) => { if (!args [0]) return message.reply(`Please specify if you are turning the command on or off!`); if (args[1]) return message.reply(`Please specify if you are turning the command on or off! [Too many Arguments!]`); switch (args[0]) { default: { message.reply('Invalid argument specified.') break; } case "on": { if (!source.timedCheck){ source.timedCheck =setInterval(() =>{ // Function for set interval console.log("Interval cycle run!" + (val++) + "times!"); valcheck(); }, 25 * 10000); message.reply('command started!'); } else { return message.reply(`command already running!`) } break; } case "off": { if (source.timedCheck){ message.reply(`user has turned off command!`); clearInterval(source.timedCheck); source.timedCheck = undefined; } else { return message.reply(`command already offline!`) } break; } } let valcheck = () => { if (source.val > 5){ clearInterval(source.timedCheck); source.timedCheck = undefined; message.channel.send(`command finished as scheduled!`); } }; }; module.exports.help = { name: "test", usage: "test <on/off>" };Archivo index.js
// Require the necessary discord.js classes const { Client, Intents } = require('discord.js'); const { token } = require('./config.json'); // Create a new client instance const client = new Client({ intents: [Intents.FLAGS.GUILDS] }); // When the client is ready, run this code (only once) client.once('ready', () => { console.log('Ready!'); }); // Export variables for commands module.exports.timedCheck = undefined; module.exports.val = 0; // Login to Discord with your client's token client.login(token);No le dijiste al bot que ejecutara este comando. En su archivo de índice, debe solicitar el archivo de comando, capturar el "mensaje" o los eventos "interactionCreate" y luego ejecutar su comando. Este código está incompleto y solo puedo guiarte en una dirección, ya que te pierdes muchas cosas en tu código.
Debería leer un tutorial: https://discordjs.guide/creating-your-bot/creating-commands.html#command-deployment-script
client.commands = new Discord.Collection(); const commandFiles = fs.readdirSync('./src/commands'); // require every command and add to commands collection for (const file of commandFiles) { const command = require(`./src/commands/${file}`); client.commands.set(command.name, command); } client.on('message', async message => { // extract your args from the message, something like: const args = message.content.slice(prefix.length).split(/ +/); try { command.execute(message, args, client); // provide more vars if you need them } catch (error) { // handle errors here }