Skip to content Skip to sidebar Skip to footer

Sending Messages Across Channels With Discord.js, Yields 'undefined' Error

I'm looking to create a 'say' command that allows me to directly message through the bot to a specific (or any, perhaps) channel. For beginner, and general testing purposes, my goa

Solution 1:

First of all, I just have to outline a few things that no longer work in the Discord.JS library (Thanks for the heads-up Ty Q.!)

First of all, .sendMessage() has been deprecated, and will more than likely not work at all anymore / soon. The working equivalent of this is .send() - which is simpler, and is an easy change to make.

Second: .defaultChannel is unfortunately no longer a thing. We will have to be more specific when we want to send a message.

I'm not great at talking about these kinds of issues, so if anyone else wants to pick it up, please do.


Alright, let's let your command be set out like this:a~speak [ChannelID] <Message>

Let's say we use this command: a~speak 12345689 I has a bucket

What we want to do is find out whether we can find the channel with the ID, and if we can, send the message to it.

To do that, I think we should see if we can use the client.channels.find("id", ID) function.


Let's Start by Setting Up Our Command

if(commandIs("speak", message)){
    args = args.shift();
    /* ["a~speak", "CHANNELID", "I", "has", "a", "bucket"]
    BECOMES
    ["123456789", "I", "has", "a", "bucket"]
    args [0]      [1]   [2]   [3]     [4]*/


    let chan = client.channels.find("id", args[0]); // Find the channel ID "123456789"
    if(chan) { // Check if that channel exists
        chan.send(args.shift().join(" "));
    } else {
        message.channel.send(args.join(" "));
    }
}

Let's Follow That Command

If we type in a~speak 12345689 I has a bucket, our bot will look for a channel that it has access to with the id "123456789".


If your bot found the channel with the id "123456789":

chan.send(args.shift().join(" "));

We defined earlier that chan = args[0] - which would be "123456789", and we don't want to send that in the message, so we use args.shift() - this removes the first argument from the rest, leaving us with ["I", "has", "a", "bucket"].

After that, we use .join(" ") - this connects ["I", "has", "a", "bucket"] with spaces to form I has a bucket.

We then simply send that message to the channel we defined, with chan.send().


If your bot did not find the channel with the id "12456789"

message.channel.send(args.join(" "));

This one's a bit different in a few ways, first of all, we forget about the chan variable, because we couldn't find that channel. If we know this, we can deduce that the user may have instead said something like a~speak I has a bucket - without the ID altogether.

Knowing that, we (unlike before), don't want to args.shift() - we would remove the "I" bit! Instead, we just want to args.join(" ") to once again join the words together into a sentence, then send it away with message.channel.send() - which just sends it in the server it got the message from.


If you need any more help, just comment with your Discord name, and I'll be happy to give you a little push in the right direction.

Sources of Information:

Some good places to look for help on these kinds of topics (Discord.JS), are the Official Discord.JS Server and AnIdiotsGuide. Both of which will be able to solve your problems.


Post a Comment for "Sending Messages Across Channels With Discord.js, Yields 'undefined' Error"