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

284
Vistas
DiscordJS (Node) Sending PM to user and await reply

I'm trying to get my bot to send a user a private message when a user types a command, and then wait for a reply to the private message.

Expected flow:

  • Jim types: !auth
  • bot sends Jim a private message
  • Jim replies to the message with blablabla
  • Bot sees the blablabla reply and outputs to terminal => console.log( replyResponse );

I have tried numerous examples I have found from here and in the docs but none of them works.

message.author.send('test').then(function () {
  message.channel
    .awaitMessages((response) => message.content, {
      max: 1,
      time: 300000000,
      errors: ['time'],
    })
    .then((collected) => {
      message.author.send(`you replied: ${collected.first().content}`);
    })
    .catch(function () {
      return;
    });
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

message.author.send('test') returns the sent message so you should use that returned message to set up a message collector. At the moment, you're using message.channel.awaitMessages and that waits for messages in the channel where the user sent the !auth message, not the one where the bot sent a response.

Check out the working code below. It uses async-await, so make sure that the parent function is an async function. I used createMessageCollector instead of awaitMessages as I think it's more readable.

try {
  // wait for the message to be sent
  let sent = await message.author.send('test');
  // set up a collector in the DM channel
  let collector = sent.channel.createMessageCollector({
    max: 1,
    time: 30 * 1000,
  });

  // fires when a message is collected
  collector.on('collect', (collected) => {
    collected.reply(`✅ You replied: _"${collected.content}"_`);
  });

  // fires when we stopped collecting messages
  collector.on('end', (collected, reason) => {
    sent.reply(`I'm no longer collecting messages. Reason: ${reason}`);
  });
} catch {
  // send a message when the user doesn't accept DMs
  message.reply(`❌ It seems I can't send you a private message...`);
}

enter image description here

Make sure, you don't forget to enable the DIRECT_MESSAGES intents. For example:

const { Client, Intents } = require('discord.js');

const client = new Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.FLAGS.DIRECT_MESSAGES,
  ],
});
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