PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 11, 2022

[FIXED] how do i DM the bot which puts direct messages into a public channel without spamming messages

 July 11, 2022     bots, discord, discord.js, javascript, message     No comments   

Issue

so what i'm using here is a discord bot that should put down the same message (that i DMed it to) as the one in a public channel within the guild / discord server. but my problem here is that for some reason it spams 6 messages each second.

Down below i described pretty much everything i have done. i bet i'm like BABY steps away from correctly writing the code but i am struggling with it for alot longer that it should have taken.

i've been looking for lots of solutions and tried many variations that i came across and it just confused me more and more on the client.on('ready') or bot.on()

When I used client.on() i got a ReferenceError: msg is not defined error all the time. i couldn't find where the problem was. and when i used bot.on() it said ''ReferenceError: bot is not defined''. which i googled and some said you put down ''client.on()'' etc.

So i got frustrated and decided to ask someone here that can put down a code for me that i can't seem to fix myself idk. i just wasted too much time with it. i'll greatly appreciate for any helping hand out there! thanks in advance!

so anyway here's my code:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
    console.log('Message received: ' + msg.content);

    const channel = client.channels.cache.get('CHANNEL ID');
    if (msg.content) {
        channel.send(msg.content);
    }
});

client.login('BOT TOKEN');

Solution

Your bot is simply responding to itself. It replies to your message, then replies to the reply to your message, then replies to the reply to the reply to the reply to your message, etc. You can prevent this by checking if the message author is a bot with User.bot.

You should also check if the message was sent in a dm so that it doesn't trigger on every single random message.

client.on('message', (msg) => {
 if (msg.author.bot || msg.channel.type !== 'dm') return;
 console.log('Message received: ' + msg.content);

 const channel = client.channels.cache.get('CHANNEL ID');
 if (msg.content) {
  channel.send(msg.content);
 }
});


Answered By - Lioness100
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing