TrollCraft 1002
TrollCraft 1002

Reputation: 11

Discord.js messege is not defined

I am trying to make my first bot but for some reason I am getting this error. I am very new to this so please don't be toxic <3

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

client.once('ready', () => {
    console.log('Ready!');
});

client.login('token');
//reading
client.on('message', message => {
    console.log(message.content);
});
//youtube channel command
if (message.content === '!yt'&&'!youtube') {
    message.channel.send('yt channel');
}
//--------------------

ERROR

ReferenceError: message is not defined
    at Object.<anonymous> (C:\Users\NoVirusesAllowed\Desktop\Discord Bot\index.js:14:1)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)

    at internal/main/run_main_module.js:17:47 

Upvotes: 0

Views: 44

Answers (1)

Green-Avocado
Green-Avocado

Reputation: 891

message is not part of the function where it's defined. Because the variable is defined locally, other functions won't be able to access it.

You likely want to move the if statement inside the client.on() block, as this will execute on every message.

With your current code, the if statement attempts to execute once at the start of the script, and doesn't know what message its referring to.

EDIT: I've also modified the condition in the if statement. Before, the condition would succeed if the message was '!yt' and '!youtube' was none zero. You likely want the condition to succeed if either the message is equal to either '!yt' or '!youtube', which can be done with an OR operator ||.

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

client.once('ready', () => {
    console.log('Ready!');
});

client.login('token');
//reading
client.on('message', message => {
    console.log(message.content);

    //youtube channel command
    if (message.content == '!yt' || message.content == '!youtube') {
        message.channel.send('yt channel');
    }
});
//--------------------

Upvotes: 3

Related Questions