Intento crear un comando de "Configuración" con message.channel.createMessageCollector() en Discord.js v13. Quiero que el usuario escriba quit para cancelar/abortar el comando. Estoy intentando con este código usando un return dentro del colector que no lo detiene:
const filter = (m) => { return m.author.id === message.author.id; }; const collector = message.channel.createMessageCollector({ filter, max: 5, time: 1000 * 20, }); collector.on("collect", (collect) => { if (collect.content.toLowerCase() === "quit") return message.reply("Bye!"); // The return is doesn't work });¿Cómo puedo hacer que funcione?
Una simple declaración de return no detendrá al coleccionista. Simplemente hace que el resto del código dentro de esa función no se ejecute. Pero, si hay un nuevo mensaje entrante, la función de devolución de llamada se ejecuta nuevamente.
Sin embargo, puede usar el método Collector#stop() que detiene el recopilador y emite el evento end . También puede agregar un motivo por el cual finaliza el recopilador.
Echa un vistazo al código a continuación:
const filter = (m) => m.author.id === message.author.id; const collector = message.channel.createMessageCollector({ filter, max: 5, time: 1000 * 20, }); collector.on('collect', (collected) => { if (collected.content.toLowerCase() === 'quit') { // collector stops and emits the end event collector.stop('user cancelled'); // although the collector stopped this line is still executed return message.reply('Bye!'); } // this line only runs if the above if statement is false message.reply(`You said _"${collected.content}"_`); }); // listening for the end event collector.on('end', (collected, reason) => { // reason is the one you passed above with the stop() method message.reply(`I'm no longer collecting messages. Reason: ${reason}`); });