PixelOrangeDev
PixelOrangeDev

Reputation: 63

Why doesn't my discord bot know its @Mention?

I'm working on a discord bot (This is just the AI module, there's another one connecting to the same bot for commands and stuff), but it doesnt know its own @Mention.

const token = process.env.PixelBot_token;
const keep_alive = require('./keep_alive.js');
const sendReply = require('./sendReply.js');
const Discord = require('discord.js');
const client = new Discord.Client();

// Set the client user's presence
client.on('ready', () => {
  var prefix = client.user.tag;
  console.log('PixelBot AI Loaded!');
  console.log(prefix);
});

client.on('message', message => {

  if (message.author.bot) return;
  var prefix = + client.user.tag;
  console.log(prefix);

  if (message.content.substring(0, prefix.length) === prefix) {

    //useful variables
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    if (command === "help") {
      console.info("Help Message Sent!\n");
    };

  };

});

client.login(token);

The above code prints out:

PixelBot AI Loaded!
PixelBot#9188

But does nothing when I send @PixelBot help. However, if I change the prefix to a string, eg: "pb", it works. It obviously knows it in "client.on('ready')", as it gets printed out. Any ideas?

EDIT: Here is the code with @Discord Expert's suggestion in:

const token = process.env.PixelBot_token;
const keep_alive = require('./keep_alive.js');
const sendReply = require('./sendReply.js');
const Discord = require('discord.js');
const client = new Discord.Client();

// Set the client user's presence
client.on('ready', () => {
  console.log('PixelBot AI Loaded!');
});

client.on('message', message => {

  if (message.author.bot) return;

  if (message.mentions.users.first() === client.user) {

    //useful variables
    const command = message.content.slice(message.mentions.users.first().length).trim().split(/ +/g);
    const identifier = command.shift().toLowerCase();
    console.log('identifier = "' + identifier + '"');
    console.log('command = "' + command +'"');
    console.log('message content = "' + message.content + '"');

    if (command === "help") {
      console.info("Help Message Sent!\n");
    };

  };

});

client.login(token);

This prints out:

PixelBot AI Loaded!
identifier = "<@569971848322744320>"
command = "help"
message content = "<@569971848322744320> help"

So it knows that command === "help", so why does it not execute the if statement?

Upvotes: 1

Views: 340

Answers (2)

PixelOrangeDev
PixelOrangeDev

Reputation: 63

Ok, I'm answering my own post here because I found the problem. For some reason, using === instead of == stops it from working. I don't know why.

Upvotes: 0

Discord expert
Discord expert

Reputation: 178

when people ping you on discord, an ID follows as with everything on discord, So in reality is:

<@USERID>

I recommend exploring message.mentions.users.first() and comparing it to your client.user

Alternatively message.mentions.users.first().id and client.user.id

Best regards Discord Expert

Upvotes: 1

Related Questions