Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda