Begging for coding help because I'm a shitlord

Begging for coding help because I'm a shitlord

This code is in my node.js Discord bot and no matter what happens in the if statement "a" always winds up resetting to what it was before (in this case "true", but when I don't set it beforehand it becomes undefined).

It's a simple issue and I'm definitely a retard but please help

Forgot to mention that I already fixed the missing semicolon but didn't take a new screenshot

If(a==true){
a=false;
}else{
A=true;
}

That makes an empty value default to true.

>no matter what happens in the if statement "a" always winds up resetting to what it was before
I'm no expert with node, but I believe it's because you're declaring two separate vars with the same name. It works fine the first time if you don't comment out the first line:

var a = true;

because then you say a equals not a, meaning you flip it's true value to false.

By not declaring it in the first line, var a has absolutely no value, and no datatype, hence undefined. So, you cannot say undefined equals not undefined.

>you're declaring two separate vars with the same name
Also, to expand on that bit a little, you have to think about scope. The first var a is of a higher scope than the second one, and is thus available in the entire IF block. The second var a was declared inside the IF block, and thus overrides the first var a. Also, the second var a would not be available outside of the IF block.

I'm having a bit of trouble grasping what you're saying. I added another line that prints the datatype to the console and it tells me that var a is boolean

Oh, I'm starting to see the issue now. So is an if statement not the right way to go about this? What would an alternative be?

Thank you for your help by the way

no need for semicolums anymomre on javascript
use a = !a instead of var a = !a

Not entirely sure what you're trying to do, but try this instead:

var a = true;
console.log("Before: " + a);
if(message.content.startsWith(".toggleA"))
{
a = !a;
}
console.log ("After: " + a);
return a;

Also, this: . Semicolons are not required in Javascript, but it's a good habit to use them anyway as programming languages like C/C++/C#/Java will require them.

Having the console.log statements outside of any conditional makes them run at wacky times it seems. That code didn't work and gave the same console output except during any command

>Having the console.log statements outside of any conditional makes them run at wacky times it seems.
Yes, they'll run regardless of the condition in the IF block. Can you give us your entire method? Do you have other IF conditions that you need to check for? If so, then those logs can go back inside the IF block, and possibly the return. I'm not sure if you're returning different bools depending on the IF block you're hitting.

Sure, this is the whole main function. The setup function just sets up a custom console logger and authentication stuff so I left it out.

The .a? command I made for debugging. The if statement at the bottom is sending the message "b" anytime a specific user sends a message, but only if var a is set to true. However it always gets set to true because of this issue I'm having.

Not sure if OP is troll or first time ever programming

I know it's something ultra simple but I usually code in Lua and I've never had this issue

Okay, at first glance, looks like all you need to do is get rid of that second var keyword. Also, this looks like an anonymous function, so I have no idea what the return is doing. I don't think you need that, as it's not really going anywhere.

As for your .a? condition, a will always be true, and a boolean because you won't be hitting any of the code inside the previous IF block (which is what flips it to false).

Your last IF block (a = true) will always happen because you're setting a inside the condition ().
= is set.
== is compare.
=== means something in Javascript, but I don't remember what.

I'm thinking first time.

What happens if you change the line

var a = !a

to

a = !a

instead?

You declared a variable fucking twice, even lua doesn't like that.

It is always true, because your method with toggleA is never called

Fuck, haven't seen his if(true) condition, nice eyes

He has to rename the a in the method. It doesn't follow paradigms else and causes fuckery in the future.

Thank you!

Any luck OP? Pretty much all you need to do is remove the extra var, remove the return, and make a=true a==true.

I moved the var a = true to the setup function, removed the return, removed the second var keyword, and most importantly fixed the last IF block. It seems to work fine now. Thank you very much and I'll be sure to read more into node.js documentation because the fairly basic tutorials I went through didn't cover this kind of issue.


Thanks again everyone.

=== compares value and type

>'1' == 1
>'1' === 1

first is true second is false

yeah it's called "strict equality" iirc

Wat? The issue that =true always means true and ==true is a compare oparator. What were you working with? Haskell?

fixed it

Never used node.js. Is regex really the way to go with this? Seems overly complicated. But I honestly dunno.

unless you're trolling.

=>

It keeps your code cleaner. So instead of writing a ton of code and "startsWith", you can just use a regex, get the command name and call a function tha handles that command.

This is probably the best way to go about making it, your code is a lot more concise and less prone to weird programming errors that crop up. I'm going to continue to improve in node.js until I'm closer to the level where I can write things like that, I'm still learning and writing code that's closer to how my brain sees the task at hand rather than how the computer can best deal with it.

Yeah, but can't you just have a function call to what you type?

>someone types: toggleA
>finds and calls function getToggleA
>someone types: jlkshdfljksdf
>tries to find getJlkshdfljksdf and throws an error?

Never done command line stuff. Is regex really the best way to do this?

Hmm, not sure what op wants to do, but your regex always delivers true with a dot. Meaning someone writing a sentence like
.ass also spawn the hello message, so you cant differencitate between different bot requests

In the code I posted, all commands are in the "handlers" object.
And then the message handler calls "hasOwnProperty" and checks whether or not the handlers object has a key with the name of the command. If it does, it calls the method.
There is no "jlkshdfljksdf" key, so nothing happens.


Also here is a different version of the code without the "isQuestion" flag. Only used that in my first example because the command was ".a?".

Yeah, I get what it's doing. And I like what you wrote.

I'm just wondering if it's worth firing up the regex engine every time someone types something is the way to go.

Like, does it help prevent some kind of injection that I don't know about?

For your idea to work, you need a handler method for every response. Would use key value pairs and change the regex a little bit to allow spelling errors. So one handler method and only specific commands have a own handler

Regex is pretty cheap and fast. Usually the way to go. Mainly to keep the code clean.
You can pretty much run thousands of regex without a noticeable impact. It's just for a discord bot after all. Those don't have that many messages.

You would need a handler per command - not per response.

My own bot for instance has a ".google" command.
That handler just sends a request to google with whatever the "line" variable is filled. I also coded several games for my bot in the same fashion.

>var
is that PHP or something?

The scope of var in js is the whole function. If you want block scoped variables use let.

No. var is javascript. It means to create a variable in scope (it exists within curly braces) as opposed to making it global.

I don't think php uses var.

>You would need a handler per command - not per response.
sorry, meant per command. Still the problem exists: most messages will be static or changed once every other hour and only some will need a logic check behind them. Then it is computationally and programatically better to use key:value, maybe as hash map

I thought it was let and const

Maybe in node.js. I've never used it.

>The scope of var in js is the whole function. If you want block scoped variables use let.
Ah, interesting. I've done some Angular 4, but no pure JS, so there's a lot I still don't know. I'm a C# guy, myself. Thanks for the info.

see, this shit is why I don't want anything to do with programming.