First time trying Nightbot !love command

Hello! I’ve been a long time lurker on here trying to learn how to code some nightbot commands. I made an account today to hopefully tweek and correct some of my mistakes. I made a nightbot “love” command, but somewhere it got mixed up or I forgot a part and cant seem to fix it.
Here is the code:

$(eval a=Math.ceil(Math.random()*100);`$(user) and $(touser) have ${a}% compatibility!`+(a<15?`Oof... Maybe one of you should just leave chat Kappa`:(a<30?`I mean... At least you both don't hate each other, right? LUL`:(a<45?`Oh ok! We're seeing some sparks now! Pog`:(a<60?`The start of something beautiful PillowYes`:(a<75?`Nice, I can feel the love in the air TwitchUnity`:(a<90?`Awh Yeah, it's about to get saucy in here! Kreygasm`:`You make a perfect couple <3 So jealous!`))))))

The code itself (when used with “!love”) posts a giant block of text. I also posted what the problem looks like with a picture. code
If anyone can help me or give me tips on how to fix it, it would be most appreciated.

EDIT: For reference (as to why my method was incorrect), and for anyone else learning like me, the code below was the one I used to make my first code. Learning from Emily helped significantly

$(eval a=Math.ceil(Math.random()*100);`There is a ${a}% chance of love between $(user) and $(touser). `+(a<25?`less than 25% response`:(a<50?`between 25% and 50% response`:(a<75?`between 50% and 70% response`:`between 75% and 100% response`))))

Hey @kawaiixnative!

It’s great to see some people interested in making commands for Nightbot!
Since it’s been a while you’ve been lurking, I assume you know the commands need to be written in JavaScript then.

When it comes to your code, I don’t know if I should fix it for you or if I should guide you towards the right answer since you’re trying to learn. So what I’ll do this time is fix it, using what seems to me the best way to write the code, and explain what I think you got mixed up, and my choice.

So here’s the fixed command:

$(eval r=Math.ceil(Math.random()*100); a=[`Oof... Maybe one of you should just leave chat Kappa`,`I mean... At least you both don't hate each other, right? LUL`,`30_TO_45_RESPONSE`,`Oh ok! We're seeing some sparks now! Pog`,`The start of something beautiful PillowYes`,`Nice, I can feel the love in the air TwitchUnity`,`Awh Yeah, it’s about to get saucy in here! Kreygasm`]; `$(user) and $(touser) have a ${r}% compatibility! ${a[Math.floor(r/15)]}`)

You’ll notice I specify a range is missing, that’s because you have a step of 15%, and 100/15=6.67, so that means you need 7 different responses. In your code, the 90 to 100% range isn’t covered, and by reading the options you already wrote, I determined that it was the 30 to 45% range that was missing, but you’re free to fix it however you want.

I notice you attempted to use the ternary operator, but you made a small mistake in your syntax, and there’s a simpler way to do what you want here, hence why I didn’t use it.
Here’s how a ternary operator works: condition?is truthy:is falsy
With that in mind, if you have multiple tests in a row, here’s how to do it:
condition?is truthy:new condition?is truthy:new condition?is truthy:if everything falsy
I advise starting with if/if else/else statements before using ternary operators.

Next, you used Math.ceil(Math.random()) to generate a random number, while valid, prefer Math.floor(Math.random()), here’s a good explanation why. In our case it’s fine so I kept it.

Okay, so let’s dig into what changed in my code.
First of all I put all the answers in an array, there would be a lot of ternary operators otherwise, which makes the code harder to read, and as I said earlier, there’s a simpler and clever way to do it.

The first item of an array has for index 0, so if we divide the random number r (which is between 0 included and 100 included) by 15 (since you go in 15% steps), and then put that number through Math.floor(), we’ll get a number ranging from 0 included to 6 included:



So now we can use that number to target the right index of the array: a[Math.floor(r/15)].

Hopefully that’s helpful and well-explained, if you need more explanation feel free to let me know.

Also, to make sure your code displays properly on the forums, either add four spaces before the code, or put the code between 3 backticks:

First option:

so⁣me te⁣xt


Second option:

so⁣me te⁣xt

Thank you so much for your well written explanation! I always come to this forum to try and learn more about coding commands. I also appreciate that you linked to more helpful explanations.

I’ll be honest, I am very bad at this snd have been self teaching myself while also taking tips I see from users on this forum. I want to apologize for not formatting the initial post correctly. In fact I tried over and over (by editing the post) to get the formatting right, but it’s around 3am here.

I used another person’s code as a guide on creating this one myself, but I’m super happy to see an easier and more efficient way of writing it.

Thank you Emily P!

1 Like

I’m self-taught myself too, you’ll see, it’ll get easier to write code and to find clever ways with time!
I can recommend W3Schools’ documentation to start, and MDN once you feel like you have a good level, usually you’ll start understanding MDN’s documentation at that point, it was confusing for me at first.

1 Like

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