简体   繁体   中英

Issues With Welcome Message | Discord.js

I have a welcome message bot which sends a welcome message every time someone joins but sometimes, it throws an error in the console saying:

    member.guild.channels.cache.find(ch => ch.name === "・𝘪𝘮𝘴𝘨").send(exampleEmbed).catch(e => co    nsole.error(`Welcome Error in ${message.guild.name}`) | message.channel.send("__**ERROR:**__ ** Someone joined the server but I was 
le to catch it.**"));
                                                                   ^

TypeError: Cannot read property 'send' of undefined

Code:

client.on('guildMemberAdd', member => {


    const exampleEmbed = new Discord.MessageEmbed()
        .setColor('RANDOM')
        .setTitle(`Welcome to lovell <$, **${member.user.username}**`)
        .setImage('https://images-ext-1.discordapp.net/external/Gy-micqoX7qrlivnytbmFgfTbxPx_CSvgChFLE3qC7g/https/media.discordapp.net/attachments/755425128635957348/758525058934767617/HaxzUWa46sK5CXKzIR.gif')
        .setFooter('Boost Us');

    member.guild.channels.cache.find(ch => ch.name === "・𝘪𝘮𝘴𝘨").send(exampleEmbed).catch(e => console.error(`Welcome Error in ${message.guild.name}`) | message.channel.send("__**ERROR:**__ ** Someone joined the server but I was unable to catch it.**"));
})

How would I fix this?

There is a much easier way to do what you are trying to do. Copy the ID of that channel and then get the channel in your code like this:

bot.on('guildMemberAdd', (member) => {
    const channel = member.guild.channels.cache.get('channel-id');
    channel.send('Welcome');
});

My guess is that if the channel is not found, that error occurs. The channel that wasn't found will return undefined. This code from the DiscordJS website will check if the channel exists and if it doesn't, it won't run the rest of the function.

Maybe try this?:

client.on('guildMemberAdd', member => {
    
    let welChannel = member.guild.channels.cache.find(ch => ch.name === "・𝘪𝘮𝘴𝘨")

    const exampleEmbed = new Discord.MessageEmbed()
        .setColor('RANDOM')
        .setTitle(`Welcome to lovell <$, **${member.user.username}**`)
        .setImage('https://images-ext-1.discordapp.net/external/Gy-micqoX7qrlivnytbmFgfTbxPx_CSvgChFLE3qC7g/https/media.discordapp.net/attachments/755425128635957348/758525058934767617/HaxzUWa46sK5CXKzIR.gif')
        .setFooter('Boost Us');

    welChannel.send(exampleEmbed).catch(e => console.error(`Welcome Error in ${message.guild.name}`) | message.channel.send("__**ERROR:**__ ** Someone joined the server but I was unable to catch it.**"));
})

If this does not work, it might be that the bot cannot find the・𝘪𝘮𝘴𝘨 channel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM