Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
Discordbot read DM messages and log them

I want my bot to read DM messages only and send them to a discord channel, but my code spam the message infinitely, spamming 5 times and then it pauses for a few seconds then spam again, also the bot don't read only DM messages and reads also guild messages so if I send anything in the guild it spams the content.

What do I want? If someone sends 'Hello!' (message content) to the bot in DM, the bot needs to send 'Hello!' (message content) to the specified channel (logs channel).

const Discord = require("discord.js");  
const client = new Discord.Client({ intents:['DIRECT_MESSAGES'],
partials: ['MESSAGE'] });


client.on('ready', () =>{
    console.log('Bot Online!'); //bot successfully logged in

});


  client.on('message', async (message) => {
    await client.channels.cache.get('CHANNEL ID').send(message.content);
    console.log(message.content); //log messages
    return;
});
// Authorizing
client.login('LOGIN');
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The thing is that when the bot sends the message to the log channel. It also triggers the message event again. To avoid this simply add

if(message.author.bot) return;

You code would look like

  client.on('message', async (message) => {
    if(message.author.bot) return;
    await client.channels.cache.get('CHANNEL ID').send(message.content);
    console.log(message.content); //log messages
    return;
});

If you only want to get direct messages

You can use

if(message.guild) return;
  client.on('message', async (message) => {
    if(message.guild) return;
    await client.channels.cache.get('CHANNEL ID').send(message.content);
    console.log(message.content); //log messages
    return;
});
about 4 years ago · Juan Pablo Isaza Report

0

Checking if the message comes from a guild should stop this. Return early if message.guild is truthy (in DM message.guild is null which is a falsey value)

if (message.guild) return;

You would put this at the top of the event callback so that nothing runs if it's in a guild

about 4 years ago · Juan Pablo Isaza Report

0

For Discord.js v13 use the CHANNEL partial as well, i hope that works

Btw

if (message.channel.type != "DM") return;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!