I’m trying to set up a welcome message for new members in my Discord server using a bot. I want the message to be an embed that shows up when someone joins but then goes away after a few seconds. Here’s what I’ve got so far:
const discord = require('discord.js');
const bot = new discord.Client();
bot.on('guildMemberAdd', newMember => {
const welcomeChannel = newMember.guild.channels.cache.get('WELCOME_CHANNEL_ID');
const welcomeEmbed = new discord.MessageEmbed()
.setColor('#3498db')
.setTitle(`Welcome, ${newMember.user.username}!`)
.setDescription(`You're our ${newMember.guild.memberCount}th member. Enjoy your stay!`)
.setThumbnail(newMember.user.displayAvatarURL());
welcomeChannel.send(welcomeEmbed).then(msg => {
setTimeout(() => msg.delete(), 5000);
});
});
bot.login('YOUR_BOT_TOKEN');
This code isn’t working quite right though. Can someone help me figure out what I’m doing wrong? I’m pretty new to Discord.js and I’m not sure if I’m handling the embed creation and deletion correctly. Any tips would be super helpful!
Your code is on the right track. One potential issue is that the bot might not have permission to delete messages in the welcome channel. Ensure the bot role has the ‘Manage Messages’ permission.
Also, consider increasing the timeout duration. Five seconds might be too quick for new members to read the message. Try extending it to 30 seconds or a minute:
setTimeout(() => msg.delete().catch(console.error), 30000);
This gives new members more time to see the welcome message while still keeping the channel clean. Remember to handle potential errors with a .catch() as suggested in another response.
hey there! ive done smth similar before. ur code looks good but try adding .catch(console.error) after msg.delete(). also make sure ur bot has perms to delete msgs in that channel. hope this helps! lmk if u need anything else 
I’ve implemented something similar in my server, and there are a few things to consider. First, make sure your bot has the necessary permissions, especially ‘Manage Messages’. Also, 5 seconds might be too quick - I found 20-30 seconds works better.
One thing to watch out for: if multiple people join at once, you might get rate limited. To avoid this, you could use a message collector instead of setTimeout:
welcomeChannel.send(welcomeEmbed).then(msg => {
const collector = msg.createReactionCollector(() => false, { time: 30000 });
collector.on('end', () => msg.delete().catch(console.error));
});
This approach is more robust and avoids potential timing issues. It’s served me well in my busier servers. Let me know if you need any clarification on implementing this!