Search code examples
javascriptnode.jsdiscorddiscord.js

Cannot read properties of undefined (reading 'send') mirano


can you help me , i have a probleme with my discord bot , i have a welcome message on a specific channel and all work , message are send but after the bot crash with this error :

enter image description here

`

client.on('guildMemberAdd', member => {
      console.log('User @' + member.user.tag + ' has joined the server!');

      const welcomepublic = new client.discord.EmbedBuilder()
        .setColor(client.config.mainColor)
        .setAuthor({
          name: client.config.botName,
          iconURL: client.user.avatarURL()
        })
        .setThumbnail(member.user.avatarURL())
        .setDescription(client.locales.other.joinPublicMessage.replace('USERNAME', '<@' + member.user.id + '>').replace('SERVERNAME', client.config.serverName))
        .setFooter({
          text: "Dev by Akaya",
          iconUrl: "https://i.imgur.com/PNK8rlZ.png"
        });
      member.guild.channels.cache.get(client.config.welcomeChannel).send({
        embeds: [welcomepublic]
      })
      var role = client.config.roleJoin
      member.roles.add(role);
    });

`

i dont understand because sometime the bot work and sometime the bot crash


Solution

  • Well it exactly tells you what the issue is, the member.guild.channels.cache.get(client.config.welcomeChannel) is undefined. I would suggest checking if the value exists before trying to use the send method, i.e.

    const welcomeChannel = member.guild.channels.cache.get(client.config.welcomeChannel);
    if (welcomeChannel) {
      welcomeChannel.send({
        embeds: [welcomepublic]
      })
    }