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

114
Visualizações
Discord.js get user DM & log message

I want that my bot to log messages from a specific ID or if its not possible to log all DM messages and send to a discord server channel. If specific ID sends 'Hello!', the bot will send 'Hello!' to the specified channel id.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you have all required intents enabled, you can simply listen for messageCreate (message for DiscordJS v12 and older) and check for the channel type your message is coming from. For example:

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

// Initializing your client
const client = new Client({
  intents: [
    // Intent for catching direct messages
    Intents.DIRECT_MESSAGES,
    // Intents for interacting with guilds
    Intents.GUILDS,
    Intents.GUILD_MESSAGES
  ]
});

// Subscribe to the messages creation event
client.on('messageCreate', async (message) => {
  // Here you check for channel type
  // We only need direct messages here, so skip other messages
  if (message.channel.type !== 'DM')
    return;
  
  // Now we need guild where we need to log these messages
  // Note: It's better for you to fetch this guild once and store somewhere
  // and do not fetch it on every new received message
  const targetGuild = await client.guilds.fetch('YOUR GUILD ID');

  // Here you getting the channel from the list of the guild channels
  const targetLoggingChannel = await targetGuild.channels.fetch('LOGGING CHANNEL ID');
  
  // Sending content of the message to the target channel
  // You can also cover the message into embed with some additional
  // information about sender or time this message was sent
  await targetLoggingChannel.send(message.content);
});

// Authorizing
client.login('TOKEN HERE');

This is very minimal example of how to log messages from the DMs of the bot to some channel on any guild you want. You should also check for logging channel and guild existence to prevent errors. Also make sure that bot can send messages to the target channel.

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