I’m working on a Discord bot using discord.js and trying to create a welcome message system. When someone joins my server, I want the bot to send a nice embed message to a specific channel.
However, I keep getting this syntax error and I can’t figure out what’s causing it:
/home/runner/discord-welcome-bot/bot.js:38
bot.login('hidden_token_here');
^^^^^
SyntaxError: Unexpected identifier
Here’s my code for the welcome feature:
bot.on('guildMemberAdd', member => {
const welcomeChannel = member.guild.channels.get('123456789012345678');
welcomeChannel.send({
embed: {
color: 0x00ff00,
author: {
name: bot.user.username,
icon_url: bot.user.avatarURL
},
title: "New Member Joined!",
description: "Welcome to our awesome server!",
timestamp: new Date(),
footer: {
icon_url: bot.user.avatarURL,
text: "Bot created by DevUser#1234"
}
}
});
bot.login('my_actual_token_was_here');
Can someone help me understand what’s wrong with this code? The error seems to be pointing to the login line but I’m not sure why.
The syntax error is happening because you have a missing closing brace and parenthesis for your event listener. Your bot.on('guildMemberAdd', member => { starts the function but never gets properly closed before you try to call bot.login(). After your welcomeChannel.send() block, you need to add }); to close the event listener function. Also worth noting that the embed property has been deprecated in newer versions of discord.js - you should use MessageEmbed or EmbedBuilder instead depending on your version. I ran into similar issues when I first started working with discord.js events and it took me a while to realize I was missing those closing brackets.
hey, i see the issue! u forgot to close the guildMemberAdd function properly. just put a }); after the send method, then ur bot.login can be outside that block. should fix that error pretty quick!
Looking at your code structure, the problem stems from improper function closure in your event handler. You opened the guildMemberAdd callback function but never closed it before attempting to execute bot.login(). The JavaScript parser encounters the login statement while still expecting the event handler to be completed, hence the unexpected identifier error. I experienced this exact issue when building my first moderation bot last year. Beyond fixing the missing }); after your send block, you should also consider updating your channel retrieval method since .get() has been replaced with .cache.get() in recent discord.js versions. Additionally, verify that your welcome channel ID is correct and that the bot has proper permissions to send messages there, as these silent failures can be frustrating to debug later.