ReferenceError: message is not defined

Im trying to make some commands but i keep getting this error
ReferenceError: message is not defined

The code that i put in is
onst Discord = require (‘discord.js’);

const client = new Discord.Client();

client.once(‘ready’, () => {

console.log ('JorisAndCoStudiosBot is now Online!')

});

const args = message.content.slice(prefix.length).split(/ +/);

const command = args.shift().toLowerCase();

if(command === ‘ping’){

message.channel.send('pong!');

};

Can someone help?

Hi, you need to wrap your message code in a message event handler like this:

client.on("message", message => {
const args = message.content.slice(prefix.length).split(/ +/);

const command = args.shift().toLowerCase();

if(command === ‘ping’){

message.channel.send('pong!');
};
});
2 Likes

it keeps saying invalid character

Hi. It looks like you are new to the Programming.
I recommend you to Learn first: >

did you ever fix this command? i have a code format you could use for this command if not

This user is probably not active anymore here.

sad but true, most people just get on the forums to ask and then get off.

Like the guy who said :>

i have problem with my code : (my discord id is :Yoda:4079)
error code:TypeError: Cannot read property ‘users’ of undefined

const Discord = require(‘discord.js’);

const config = require(’./config.json’);

const fs = require(‘fs’);

const { userInfo } = require(‘os’);

const client = new Discord.Client();

client.once(‘ready’, () => {

console.log('The bot is online!');

client.user.setActivity("With Light Saver");

})

client.on(‘message’, message => {

if (!message.content.startsWith(config.prefix) || message.author.bot) return;



const args = message.content.slice(config.prefix.length).split(/ +/);

const command = args.shift();

var number = -1; 

var folder = -1; 

if (config.folder.includes(command)) {

    folder = config.folder.indexOf(command);

    fs.readdir('./' + config.folder[folder], (err, files) => {

        number = files.length;

        if (!args.length) {

            imageNumber = Math.floor(Math.random() * number) + 1;

        } else {

            imageNumber = args[0];

            if (imageNumber > number) { 

                return message.channel.send("Possible arguments: " + config.prefix + command + " [1-" + number + "]."); 

            }

        }

        message.channel.send ({files: ["./" + config.folder[folder] + "\\" + imageNumber + ".png"]} )

    });

} else if (command === "random") {

    folder = Math.floor(Math.random() * config.folder.length);

    fs.readdir('./' + config.folder[folder], (err, files) => {

        number = files.length;

        imageNumber = Math.floor(Math.random() * number) + 1;

        message.channel.send ({files: ["./" + config.folder[folder] + "\\" + imageNumber + ".png"]} )

    });

}

});

client.on(‘message’, msg=>{

if(msg.content === "!hello"){

   msg.reply('HELLO FRIEND!');

}   

})

client.on(‘message’, msg=>{

if(msg.content === "!info"){

    msg.reply('2.0!');

}

})

client.on(‘message’, msg=>{

if(msg.content === "!bday"){

    msg.reply('Happy Birthday my Friend 🥳');

}

})

client.on(‘message’, msg=>{

if(msg.content === "!hi"){

   msg.reply('hi nice to meet you :)');

}   

})

client.on(‘message’, msg =>{

if(msg.content === "!youtube"){

   msg.reply('https://www.youtube.com/channel/UC-gxQ1O2J4TtW8PLHlBv0Ng?view_as=subscriber');

}

})

client.on(‘message’, msg =>{

if(msg.content === "!yt"){

   msg.reply('https://www.youtube.com/channel/UC-gxQ1O2J4TtW8PLHlBv0Ng?view_as=subscriber');

}

})

client.on(‘message’, msg =>{

if(msg.content === "!8ball"){

   msg.reply('I can see you going to have good day ;)');

}

})

client.on(‘message’, message =>{

if(!message.content.startsWith() || message.author.bot) return;

const args = message.content.slice(prefix.length).split(/ +/);

const command = args.shift().toLowerCase();

if(command === 'clear'){

    client.commands.get('clear').execute(message, args);

} else if(command === 'kick'){

    client.commands.get('kick').execute(message, args);

} else if(command === 'ban'){

    client.commands.get('ban').execute(message, args);

}

});

client.on(‘message’, function(message){

if ('message'.mentions.users.first() --- bot.user) {

    message.channel.send('My prefix is \'!\'')

}

})

//clear

client.on(‘message’, message => {

let args = message.content.substring(prefix.length).split(" ");



switch(args[0]){

    case 'clear':

    message.delete();

}

})

client.login(config.token);

user is not defined

You should add some Checks to COLLECTOR First before Calling first().

Like:

//If FromUser is undefined
if (!message.fromUser) return msg.reply('Please mention someone.')
// THIS IS ONLY SOME EXAMPLE

The problem is here:

client.on('message', function(message){
  if ('message'.mentions.users.first() --- bot.user) {

The part inside the if condition should be message.mentions, remove the quotes around it. The string 'message' doesn’t have a property called mentions.

Also I think you need to change the --- to ===:

client.on('message', function(message){
  if (message.mentions.users.first() === bot.user) {

Good luck

2 Likes