B. Hanel
B. Hanel

Reputation: 43

Discord Bot replying to itself, doesn't recognize message.author.bot

so I have a bot that I'm trying to make respond to "Dan" with "We love Dan!" for a server I'm in:

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
    colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var mybot = new Discord.Client({
    token: auth.token,
    autorun: true
});
mybot.on('ready', function (evt) {
    logger.info('Connected');
    logger.info('Logged in as: ');
    logger.info(mybot.username + ' - (' + mybot.id + ')');
});
mybot.on('message', function (user, userID, channelID, message, evt) {
    if (message.author.bot) return;
    if (message.includes("Dan")) {
            mybot.sendMessage({
                to: channelID,
                message: 'We love Dan!',
            });
     }
});

I've looked up how to get a bot to not respond to itself, and many suggested that line of code

if (message.author.bot) return;

but when I put that in, I'm getting this error in the command prompt:

TypeError: Cannot read property 'bot' of undefined

It seems from threads like discord.js bot replies to itself that the solution works for people, but it's not for me.

What am I doing differently?

Thanks!

Upvotes: 1

Views: 3652

Answers (2)

Kai
Kai

Reputation: 9288

The error is telling you that message.author is undefined, therefore bot is not a valid property. You are attempting to use a solution from another discord library.

Instead, try checking if the user or user ID matches the bot's and then return.

Upvotes: 2

ContextCue
ContextCue

Reputation: 323

Hmmmmm.... At first glance looks like a scoping issue. Not sure what exactly you are building with, you may want to read MCV. Need to clean out the little bot I built and post it on github, doing that tomorrow I'll post it then. Was experimenting with short term memory array. But with a quick look and a test got this working:

const Discord = require("discord.js");
const client = new Discord.Client();
...
client.on("message", async message => {
  if (message.content.includes("Dan")){
    // right that is needed to kill the loop ->
    if(message.author.bot) return; 
    return message.reply("We love you Dan!");
    // could also use -> message.channel.send("We love you Dan!")
  } 
}

client in my case is bot in yours I believe and it will reply on the async or return so you will just need to call the in-built prototype method that exists as part of method. There are a bunch of others as well in the API Docs.

Upvotes: 1

Related Questions