Search code examples
discorddiscord.js

Bot sends a embed when joins a server


Im trying to make my bot send a embed in the server that it joins (E.G a thank you for inviting and what the bot can do).This is the error that I am getting.


const channel = message.guild.channels.cache.find(channel => channel.type === 'text' &&  client.user.me.permissions.has("send_messages"))
                                  ^

TypeError: Cannot read properties of undefined (reading 'channels')

this is the code that i am using in the index.js file

client.on('guildCreate', message => {

    const channel = message.guild.channels.cache.find(channel => channel.type === 'text' &&  client.user.me.permissions.has("send_messages"))

    const embed = new MessageEmbed()
    .setColor('GREEN')
    .setTitle('Thank You')
    .setDescription("Thank you for inviting Rynwin to your server. To get the command list please do /help.\n \n The default Bot prefix is ```<```")        
    .addFields([
        {
            name: "Our Support Server",
            value: "If you require assistance or would like to get daily bot updates please join our support discord [here] ()"
        },
        
        {
        name: "Commands",
        value: "Try some of these commands; \n\n**/prefix <prefix>** - Sets the server prefix\n\n**/help** -  Get the command list or information on a command \n\n**/botinfo** - Shows info on the bot"
        },



    channel.send({ embeds: [embed] })

    ])

Solution

  • There are a few things wrong on the first 2 lines. Firstly, guildCreate gives a Guild instance, not a message. Secondly, you aren't checking the correct channel type or permission. Here is the fixed code:

    client.on('guildCreate', guild => {
    
        const channel = guild.channels.cache.find(channel => channel.type === 'GUILD_TEXT' &&  guild.me.permissions.has("SEND_MESSAGES"))
    // GUILD_TEXT channel type, guild.me for client guild member, and uppercase permission
    // rest of code...
    }