Search code examples
javascriptdiscord.jsbots

Script of welcome / goodbye to discord js bot Error


He is giving me this script of welcome/goodbye to Discord error and he has already tried many things if someone helps me I would appreciate it very much, Thanks

module.exports = (client) => {
    const channelIdA = '718596514305277972'
    
    client.on('guildMemberAdd', (member) => {
       console.log("Someone joined the server")
       
         const messageA = `message`

          const channel = (channelIdA)
          channel.send(messageA)
       })
   }
   
   module.exports = (client) => {
    const channelIdB = '890891192995303424'
    
    client.on('guildMemberRemove', (member) => {
       console.log("Someone left the server")
       
         const messageB = `message`
          
          const channel = (channelIdB)
          channel.send(messageB)
    })
}

Solution

  • You are attempting to send a message to a channel by calling the .send() method. However, you are calling the method on a string. The send() method only exists on text based channels. To send a message to a specific channel, replace your message sending code with this

    client.on("guildMemberAdd", members => {
     client.channels.cache.get("REPLACE WITH CHANNEL ID").send("message")
    });
    
    client.on("guildMemberRemove", members => {
     client.channels.cache.get("REPLACE WITH OTHER CHANNEL ID").send(" other message")
    });
    

    If the above does not work, try this: (works without cache)

    client.on("guildMemberAdd", async (member) => {
     const channel = await client.channels.fetch("REPLACE WITH CHANNEL ID")
     channel.send(`${member.user.username}, welcome`)
    });
    
    client.on("guildMemberRemove", async (member) => {
     const channel = await client.channels.fetch("REPLACE WITH OTHER CHANNEL ID")
     channel.send(`${member.user.username} has left`)
    });