If else statement based on user followage

Hey there, I’m trying to personalize my nightbot followage command a bit. I looked through the other “if else” topics on the forum but couldn’t find an answer that applied to my issue. Also, I know next to nothing about javascript so that might have something to do with it too.

Basically, I want to create an if else statement so that after nightbot displays a user’s follow time…

  • If it’s less than 5 months, it will say something like “Looks like you’re new here, thanks for the support!”
  • If it’s greater than 18 months, it will say “A real ride or die!”
  • And if it’s in between, it will say “Now that’s dedication!”

I’ve gone through a ton of code variations using info from this forum, w3 schools, and even chatGPT, but nothing has worked. Here’s what I have so far:

$(urlfetch https://2g.be/twitch/following.php?user=$(touser)&channel=$(channel)&format=mwdhms) $(eval var ft=$(urlfetch https://2g.be/twitch/following.php?user=$(touser)&channel=$(channel)&format=mwdhms); if ft.includes(“m”) fm=parseInt(ft.split(“m”)[0]); if (fm<5) {‘Looks like you’re new here, thanks for the support!’;} else if (fm>18) {‘A real ride or die!’;} else {‘Now that’s dedication!’})

I’m sure there’s a million things wrong with this code, but this is the best I could come up with after hours of research. Not even sure if this is possible, but any help would be greatly appreciated! Thank you :pray:

1 Like

Hey @ralbmcpalb!

We always appreciate people who try to write their code before asking for help! We volunteers, or even NightDev, aren’t required to write code for users as this is for advanced commands, and we expect people to know JS if they’re going to use that feature. However, I’m personally enjoying writing code for others (for now), and I’m happy to help, especially when the expectations are properly described, like you did.

Anyway, without further ado, here’s your command:

$(eval message = '$(urlfetch https://api.2g.be/twitch/followage/$(channel)/$(touser)?format=ymwd)'; monthAmount = '$(urlfetch https://api.2g.be/twitch/followage/$(channel)/$(touser)?format=monthsint)'; monthAmount = isNaN(monthAmount) ? -1 : parseInt(monthAmount); `${message}. | ${monthAmount === -1 ? '' : monthAmount < 5 ? 'Looks like you\'re new here, thanks for the support!' : monthAmount < 18 ? 'Now that\'s dedication!' : 'A real ride or die!'}`;)

We call the API twice, which I’m not fond of as it makes the command slower, but the second call asks for the monthsint format, which if you look at the first post describing this API in details, means the API will answer strictly with the amount of months the user has been following the channel.
Then we check if the amount of months is valid, because if the user is not following (or doesn’t exists), the API will respond with a message (imo is should respond with -1, but that’s because I’m a dev, from a user pov, a message is best).
And finally we display a different message depending on the monthAmount value, I used ternary operators (?:) instead of if/else but it’s basically the same thing (not quite, but pretty much). The message is composed with template literals.

2 Likes

Yesss, it works perfectly, you’re amazing!! Thank you so much for all your help, this is so cool! :pray:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.