0
votes

I am trying to make a welcoming message to whoever joins my discord server with this bot, but nothing happens when someone joins. I am getting an error: ReferenceError: channelname is not defined at C:\Users\Vir\Desktop\DiscBot\index.js:13:71 at Map.find (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\util\Collection.js:506:11) at Client. (C:\Users\Vir\Desktop\DiscBot\index.js:13:43) at Client.emit (events.js:223:5) at Guild._addMember (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\structures\Guild.js:1298:19) at GuildMemberAddHandler.handle (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\client\websocket\packets\handlers\GuildMemberAdd.js:12:13) at WebSocketPacketManager.handle (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65) at WebSocketConnection.onPacket (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35) at WebSocketConnection.onMessage (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17) at WebSocket.onMessage (C:\Users\Vir\Desktop\DiscBot\node_modules\ws\lib\event-target.js:120:16) PS C:\Users\Vir\Desktop\DiscBot> But I have no clue what that means. I've tried searching it up and found nothing. The code is

const Discord = require('discord.js');
const bot = new Discord.Client();

const token = "NzEzMTcwNjc4NjYwMjAyNTA2.XscOxQ.0YxwpbBEITN0DIwGFwYIdRxCOu0";

const PREFIX = ";";

bot.on('ready', () =>{
    console.log('This bot is online!');
})

bot.on('guildMemberAdd', member =>{
    const channel = member.guild.channels.find(channel => channelname === "welcome");
    if(!channel) return;

    channel.send('Welcome, ${member}, make sure to read the rules and verfiy.') 
});


bot.on('message', message=>{

    let args = message.content.substring(PREFIX.length).split(" ")

    switch(args[0]){
        case 'Version':
            message.reply('Version 1.0.0');
        break;
        case 'Commands':
            message.reply(';Version ;Commands');
        break;


    }
})

bot.login(token);
3

3 Answers

1
votes

This is the code that i use for this

      client.on("guildMemberAdd", (member) => {

    const channel = member.guild.channels.cache.get('CHANNEL_ID'); 
    channel.send(`**Hey ${member.user}, welcome to the server!\nMake sure to read the rules in <#CHANNEL_ID>**`); 
});

0
votes

They have changed it, so you'll need to use cache now. So in this case:

bot.on('guildMemberAdd', member =>{
    const channel = member.guild.channels.cache.find(channel => channel.name === "welcome");
    if(!channel) return;

    channel.send('Welcome, ${member}, make sure to read the rules and verfiy.') 
});

Hope this helps!

0
votes

The error suggests that channelname is not defined. I believe you should be using channel.name instead, just a simple typo.

Following OP's comment, I checked the docs and you must access the cache property to get a list of channels, like so:

bot.on('guildMemberAdd', member => {
    const channel = member.guild.channels.cache.find(channel => channel.name === "welcome");

    if(!channel) {
        return;
    }

    channel.send(`Welcome, ${member}, make sure to read the rules and verfiy.`) 
});