Search code examples
javascriptdiscord.jscommando

How to fix "Null" output for AFK Channel


I'm having a problem where when I launch my bot and run this command, it says "undefined" or "null" as the output. How do I fix this?

I've tried using the same method as I did for all the other options.

if (serv.verified == false) {
    var veri = "No.";
} 

Where it checks for a boolean, string, or number. It just doesn't seem to work. It sets my variable at null or undefined.

if (serv.afkChannel == `null`) {
    var afk = "No AFK VC.";
}

if (serv.verified == true) {
    var veri = "Yes.";
} 

if (serv.verified == false) {
    var veri = "No.";
} 

        var myInfo2 = new discord.RichEmbed()
        .setAuthor(`${serv.name}'s guild info`)
        .addField(`AFK Channel`,`${afk}`,true)
        .addField(`AFK Timeout`,`${serv.afkTimeout}s`,true)
        .addField(`Channels`,`${serv.channels.size}`,true)
        .addField(`Creation of Guild`,`${serv.createdAt}`,true)
        .addField(`Default Notification`, defn,true)

Expected Result : It will say No AFK VC Actual Result : It says undefined or null.


Solution

  • First, you want to check if there IS a channel. Then, you want to set the variable afk as No AFK Channel. And last, if there is an afk channel it would say said AFK Channel.

    if (!serv.afkChannel)
        var afk = `No AFK Channel`
        else var afk = `${serv.afkChannel}`
    

    The "!" basically makes the following code opposite. The next code would give off a the variable afk is already defined error, but it's basically a "OR". If there is no AFK channel then, OR If there is an AFK channel, then.

    All you need now is to find out how to get rid of the # if you want to. Enjoy!