Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
Discord.js v13 return in message collector doesn't stop collecting

I try to create a "Setup" command with message.channel.createMessageCollector() in Discord.js v13. I want the user to type quit to cancel/abort the command. I am trying with this code using a return inside the collector doesn't stop it:

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
});

How can I make it work?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

A simple return statement won't stop the collector. It just makes the rest of the code inside that function not to run. But, if there is a new incoming message, the callback function gets executed again.

However, you can use the Collector#stop() method that stops the collector and emits the end event. You can also add a reason the collector is ending.

Check out the code below:

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}`);
});

enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda