I’m developing a Discord bot with discord.js, but I’m encountering a ReferenceError that states ‘message is not defined.’ Since I’m new to programming with JavaScript, I’m unsure about what could be causing this problem.
const Discord = require('discord.js');
const bot = new Discord.Client();
let commandPrefix = '!';
bot.on('ready', () => {
bot.user.setStatus('online');
console.log('Bot is up and running!');
});
bot.on('ready', guild => {
bot.user.setActivity('!help | available on ' + bot.guilds.size + ' servers');
});
bot.on('message', incomingMessage => {
if(incomingMessage.author.bot) return;
if(incomingMessage.channel.type === 'dm') return;
let messageParts = incomingMessage.content.split(' ');
let command = messageParts[0];
let args = messageParts.slice(1);
if(!command.startsWith(commandPrefix)) return;
if(command === `${commandPrefix}userinfo`) {
let userEmbed = new Discord.RichEmbed()
.setAuthor(incomingMessage.author.username)
.setColor('#3498db')
.setThumbnail(incomingMessage.author.avatarURL)
.addField('Name', `${incomingMessage.author.username}#${incomingMessage.author.discriminator}`)
.addField('ID', incomingMessage.author.id);
incomingMessage.reply('Check your user information in your DMs!');
incomingMessage.channel.send({embed: userEmbed});
}
});
if(command === `${commandPrefix}help`) {
let helpEmbed = new Discord.RichEmbed()
.addField('!help', 'shows you this message')
.setTitle('Help Menu')
.setColor('#3498db')
.addField('!userinfo', 'provides user information')
.addField('!serverinfo', 'gives details about the server');
incomingMessage.reply('Here are the commands I can execute');
incomingMessage.channel.send({embed: helpEmbed});
}
function handleCommand() {
messageParts = incomingMessage.content.split(' ');
let command = messageParts[0];
if(command === `${commandPrefix}serverinfo`) {
let serverEmbed = new Discord.RichEmbed()
.setAuthor(incomingMessage.author.username)
.setColor('#3498db')
.addField('Server Name', incomingMessage.guild.name)
.addField('Owner', incomingMessage.guild.owner.user)
.addField('Server ID', incomingMessage.guild.id)
.addField('Online Members', `${incomingMessage.guild.members.filter(member => member.presence.status !== 'offline').size} / ${incomingMessage.guild.memberCount}`);
incomingMessage.channel.send({embed: serverEmbed});
}
}
bot.login('your_token_here');
I have been trying to improve my JavaScript skills with tutorials, but I keep making simple errors related to variable scope and syntax. The reference error mentions the message variable, and I’m struggling to find out why it’s not recognized. Any assistance would be greatly appreciated!