Cant get my random welcome messages to work in discord

I am trying to build my own discord bot which is pretty good fun, but having difficulty building my own little array to set up multiple welcome messages, so when someone joins, they will get one of those messages at random.

Here is my current code:

module.exports = ('guildMemberAdd', (member) => {
    const channel = member.guild.channels.cache.find(ch => ch.name === 'bot-testing');

    if (!channel) return;

    var messages = [
        "Welcome to Take My Sword '${member}', test 1",
        "Welcome to Take My Sword '${member}', test 2",
        "Welcome to Take My Sword '${member}', test 3",
        "Welcome to Take My Sword '${member}', test 4",
        "Welcome to Take My Sword '${member}', test 5"
    ];

    var messages = Math.floor(Math.random() * messages.length);
    
    channel.send(messageArray[rando]); 
});

And this is the error message, I am receiving:

/home/discord-bot/events/member/guildMemberAdd.js:2
        const channel = member.guild.channels.cache.find(ch => ch.name === 'bot-testing');
                                     ^

TypeError: Cannot read property 'channels' of undefined
    at module.exports (/home/discord-bot/events/member/guildMemberAdd.js:2:31)
    at Client.emit (events.js:315:20)
    at Object.module.exports [as GUILD_MEMBER_ADD] (/home/discord-bot/node_modules/discord.js/src/client/websocket/handlers/GUILD_MEMBER_ADD.js:16:14)
    at WebSocketManager.handlePacket (/home/discord-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/discord-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/discord-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/discord-bot/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (/home/discord-bot/node_modules/ws/lib/websocket.js:825:20)
    at Receiver.emit (events.js:315:20)

My friend who has been helping me said to try change the 1st line, (member) to (client, member) and when I do, I just get this error message instead:

/home/discord-bot/events/member/guildMemberAdd.js:16
        channel.send(messageArray[rando]);
                     ^

ReferenceError: messageArray is not defined
    at module.exports (/home/discord-bot/events/member/guildMemberAdd.js:16:15)
    at Client.emit (events.js:315:20)
    at Object.module.exports [as GUILD_MEMBER_ADD] (/home/discord-bot/node_modules/discord.js/src/client/websocket/handlers/GUILD_MEMBER_ADD.js:16:14)
    at WebSocketManager.handlePacket (/home/discord-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/discord-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/discord-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/discord-bot/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (/home/discord-bot/node_modules/ws/lib/websocket.js:825:20)
    at Receiver.emit (events.js:315:20)
[nodemon] app crashed - waiting for file changes before starting...

Understandably the array is probably coded wrong altogether, going off the errors but unfortunately I don’t have advanced knowlege to be able to fix it on my own so that is why I decided to ask here!

The bot can write/post to that room etc. that I am trying to do the test to so I know its not permissions, and I did have a single welcome message working perfectly fine, I just want to try an Array with multiple welcome messages, and on join they get a random one of how ever many I decide to make.

Any help would be really appreciated, even if its just a link to guide to better understand building arrays.

Kind Regards.

James.

3 thoughts on “Cant get my random welcome messages to work in discord”

  1. Your first error was on line 2 as you can see in the first line:

    /home/discord-bot/events/member/guildMemberAdd.js:2
    

    You already fixed that one so don’t worry about that now.

    Your second error is in line 16 and mentions that a variable called messageArray is not defined. Looking at your code that is true. You are declaring two times the variable called messages, so start by renaming the second one to rando. That way you will have a variable with all the messages and one with the random number to select the message.

    Once you have that fix the line 16 to use the correct variable names, that will be messages and rando.

    It should look like this:

    var messages = [
        "Welcome to Take My Sword '${member}', test 1",
        "Welcome to Take My Sword '${member}', test 2",
        "Welcome to Take My Sword '${member}', test 3",
        "Welcome to Take My Sword '${member}', test 4",
        "Welcome to Take My Sword '${member}', test 5"
    ];
    
    var rando = Math.floor(Math.random() * messages.length);
    
    channel.send(messages[rando]); 
    
    Reply

Leave a Comment