Search code examples
javascriptdiscorddiscord.js

Trying to setup a new welcome.js


const Discord = require('discord.js')
const bot = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MEMBERS"]})
const config = require('./botconfig.json')
const prefix = '...';
const welcome = require("./welcome");
const Welcome = require("discord-welcome");
const client = new Discord.Client({
    intents: [
      Discord.Intents.FLAGS.GUILDS,
      Discord.Intents.FLAGS.GUILD_MEMBERS,
    ],
  });

bot.on('ready', async () => {
    console.log(`${bot.user.username} is online and in ${bot.guilds.cache.size} server!`)
}) 

// START OF WELCOME BOT
bot.on('guildMemberAdd', async member => {
    client.channels.cache.get('ChannelHere')
    let embed = new Discord.MessageEmbed()
    .setTitle('TESTING EMBED')
    .setDescription('TITLE LAUNCHED...')
    .setColor("#ffd8e3")
    channel.send(`please welcome ${member} to the server!`, embed)
    //client.channels.get("775086826154229840").send(`please welcome ${member} to the server!`, embed)
    //msg.channel.send(`please welcome ${member} to the server!`, embed)
});
// END OF WELCOME BOT



bot.login(config.token)

Whenever a user joins, it failes. It worked on the last server but not now? Any help or indication that something is wrong would be much appreciated. I am new to this so sorry if bad


Solution

  • So there are a couple things wrong here. First you have 2 “clients” defined

    // first client
    const bot = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MEMBERS"]})
    // second client 
    const client = new Discord.Client({
        intents: [
          Discord.Intents.FLAGS.GUILDS,
          Discord.Intents.FLAGS.GUILD_MEMBERS,
        ],
      });
    

    Then your code reads as follow

    bot.on(‘guildMemberAdd’, async member => {
    // above line states that the first client will do something when a member joins
    
    client.channels.cache.get('ChannelHere')
    //above line states that the second client will retrieve the channel
    …
    
    channel.send(`please welcome ${member} to the server!`, embed)
    //above line has the first client doing something (sending a message) however the first client didn’t retrieve the channel so channel.anything won’t work.
    client.channels.cache.get("775086826154229840").send(`please welcome ${member} to the server!`, embed)
    //this line would technically work except client isn’t logged in, bot is and you needed the .cache part
    msg.channel.send(`please welcome ${member} to the server!`, embed)
    // msg is not defined probably just need to delete msg
    });
    }
    

    Try this and it should work

    const Discord = require('discord.js')
    
    // if using discord v12 pick only one bot
    const bot = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MEMBERS"]})
    
    // if using discord v13 pick only one bot
    const bot = new Discord.Client({
    intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_MESSAGES]
    })
    
    const config = require('./botconfig.json')
    const prefix = '...';
    const welcome = require("./welcome");
    const Welcome = require("discord-welcome");
    
    bot.on('ready', async () => {
        console.log(`${bot.user.username} is online and in ${bot.guilds.cache.size} server!`)
    }) 
    
    // START OF WELCOME BOT
    bot.on('guildMemberAdd', async member => {
        const channel = bot.channels.cache.get('ChannelHere')
        let embed = new Discord.MessageEmbed()
        .setTitle('TESTING EMBED')
        .setDescription('TITLE LAUNCHED...')
        .setColor("#ffd8e3")
       
        // if using discord v12
        channel.send(`please welcome ${member} to the server!`, embed)
        
        // if using discord v13
        channel.send({
            content: `please welcome ${member} to the server!`,
            embeds: [embed]
        })
    });
    // END OF WELCOME BOT
    
    
    
    bot.login(config.token)