0

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.

SomePerson
  • 1,171
  • 4
  • 16
  • 45
  • note to past self: use `var veri; serv.verified ? ... : ...`, that's some bad code. p.s. this errored because I checked serv.afkChannel against a string, not `null`, but `'null'` – SomePerson Aug 28 '22 at 03:07

2 Answers2

0

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!

SomePerson
  • 1,171
  • 4
  • 16
  • 45
0

Nothing will ever match the literal string 'null' other than... well, 'null'. Here's just a brief example:

console.log(null == 'null');       // The identity operator (==) even does type
console.log(undefined == 'null');  // conversions and these aren't the same.

console.log(null == null);         // These are both true, because null is not a string.
console.log(undefined == null);    // <-- Watch out: this is only true using the twins.

console.log('null' == 'null');     // The only match is the string compared to itself.

Guild.afkChannel will be undefined if the channel isn't defined. Nothing should ever return a string 'null'. That being said, when you compare serv.afkChannel to `null`, the boolean will never be true. Since you don't have an else in the chain, afk won't get to be defined.


When declaring the afk variable, you can use the logical OR operator (||) to replace your if/else statements and prevent your issue:

var afk = serv.afkChannel || 'No AFK Channel';

If serv.afkChannel is falsy, the variable will be declared as 'No AFK Channel' instead.

slothiful
  • 5,548
  • 3
  • 12
  • 36