leveling system with discord.js - node.js

So, I want to make a leveling/xp system for my discord bot (like mee6 or tatsumaki) but the only way I know how to do this is by using mSQL. Is there a way to do this just using discord.js or is there an eazier way to do this?
I'm sorry for this question being so general but i can't find an answer anywhere, thanks

You could, though using a DB will help more in the future.
Using a Database will probably be the only solution unless you want to write files uselessly or want the levels to be cleared upon restart. From my experience, a database will just work best if you want to store anything like this. Also when using a Database you can use other tables to save more information (Command statistics, etc.) without a problem.
I've been there myself, though once you get over not wanting to use a database and setting one up you'll wonder how you lived without it.

I'm using a point system on my bot. I'm saving it on a JSON file, it's pretty easy to do with node.
You can scan all the users every time you launch the bot for new users and initialize them in your file.
The downside is that you can erase all of the file if you parse it when you boot the bot and you get an error.
I'm considering switching to a DB instead.

Related

Storing Temporary Variables in NodeJS

I've just started trying to use NodeJS and socket.io to create a simple multiplayer online game (similar idea to online chess). I apologise if the answer to my question is really obvious because I have tried googling around, but I think I am missing some key bit of understanding.
Basically, I need to store a few things on the server while the application is running. For example:
I need to store which socket connections are hosts, and which are players.
I need to store the current state of each game (e.g. in the case of chess, where the pieces are and whose turn it is)
It would also be nice to be able to store all the socket.io "rooms".
Feel free to answer the question at this point, information below is for extra reference.
There are a few things that I have tried or seen online:
When I google something with "persistence", I get results based on saving to a database or something, I don't think this is what I want.
I have tried just adding variables at the top of the NodeJS file, like I would with global variables in an ordinary JS file. This seems to work, but just feels wrong to me, if someone could explain how this works it would be great.
I have also seen things called session variables, I think this might be what I want.
I have seen applications that do this by just passing the information back and forward between to client and server, but I would prefer that the client couldn't just edit the information to "hack" to game.
Any help or explanation appreciated.
Nothing wrong with saving to a database. If your server crashes and restarts a few seconds later, you don't really want everyone's data to just be obliterated. I think you're thinking about it in the way that databases are always long-term and slow. But really, there are DB technologies great for this type of thing, and oft used with socket.io.
The one I'd probably opt for is Redis, which is super fast and stores data in-memory. This means that it's not constantly writing to disk, and it's a bit of a halfway house between having full persistent storage like with MySQL, and the slightly dodgy method of just keeping it in Node memory via variables.
When reddit created "Place", that massive multiplayer drawing with a tonne of concurrent users, they used Redis and Cassandra together. You can read a bit about it here.

Write/Read Small File

I want to save the "state" of my application each time it is changed, and load it each time the application is booted up.
The "state" will be a simple object with a handful of variables in it, the idea is to JSON.stringify it to a file, and JSON.parse it when needed.
From what I understand, this cannot be done using Node's fs, since files on Heroku are not permanent.
I cannot use S3 either, because it's not free (free plan only lasts a year), and this is a hobby project of mine - I am not willing to pay for it.
Another recurring suggestion, is to use some sort of a database, but I think that is a waste of time, since I will only be dealing with one very small file.
Essentially, my question is, how can I achieve something that is closest to this?:
WRITE("filename.txt",JSON.stringify(x));
x=JSON.parse(READ("filename.txt"));
(P.S: I've read somewhere, can't seem to remember where, that Heroku gives free 100MB (Which would be way more than enough). What is that? Does it have anything to do with my code?)
I can think of a few ways to do this for free. They all pretty much boil down to "What free service allows me to read/write arbitrary file content, and access via an API?"…
Do you use or already pay for Dropbox (or something similar?). If so you could you the Dropbox API for Node.js to save/load your application state.
You could use the Github Gist API and just update the same Gist over and over.
Otherwise, you mentioned databases. Sure, a database would be overkill tech-wise, but given your constraints (and the fact that you can get a small db for free on Heroku), and how much overhead implementing one of the aforementioned APIs would be, it might be the best option.
Hope this helps.

Is there a mechanism to sign a node application?

I wrote a node application which will be embedded in the client. I would like to protect my code so the client will not be able to modify it (license check for example).
I already read (and try) this one but obfuscation (and minification) can be reversed and I can't move critical code to an external service (the node application may not have external internet connections).
Is there a (native) mechanism to sign a node application? Something which will prevent the app to run if the code is changed?
There is not a native mechanism to sign JavaScript code. This has been attempted in the past but scrapped quickly.
Checkout this question and its answers on how difficult this is. User Mason Wheeler sums it up well:
It's been tried. Many, many times. There's an entire sub-industry dedicated to attempting to use encryption to keep users from accessing a program as they see fit. And it never works.
If you want to attempt this anyways, I recommend a commercial product like jscrambler.com for trying to 'defend' your code with encryption and self-defense techniques. There are so many drawbacks to doing this yourself that it isn't worth it.
Also - keep in mind that for every change you make to your code (minify, obfuscate, encryption, etc.), you increase the possibility that your code will not function properly. One more reason to avoid it and/or use an commercial/supported product.

How to make chat app with maintain the chat history in Mobile php+nodeJS+socketIO+AppGyver

I'm thinking to do Chat application in AppGyver. For now I have chat app on web with NodeJS and SocketIO, messages are store in MySQL. I was thinking how to keep on phone messages history like whatsapp.
The first thing that came to mind is that i store messages in SQLite on phone. But now bothering me the following problem, how to refresh the changes that were made while the user was not online? Perhaps to make a new table in MySQL which would contain changes made while the user is offline.
For keeping the changes involves information about the deleted conversations, deleted or added messages.
The second thing that came to mind is that all the data which get from the server, to keep as json files. However, it is difficult to carry out the changes that have made while the user was not online. At every refreshment need to open all files, then find the appropriate value and make the change.
Which of these methods would you choose, or you may have a better solution?
I wonder if someone has done something similar and whether there might be a better solutions than mine?
Best regards.

Creating session mechanism with core nodejs

I am trying to create a complete session managment in nodejs for logins, chat sessions etc.
I googled a lot and every solution that i got was with some framework/module. I don't want to use any module/framework. I would rather like to build my own solution for this:
So this is the plan:
I will set a session cookie on the client machine (yet to figure out how)
For each cookie, i will be maintaining a unique id in the database instead of files as is the case with php (i am using mongodb)
When a user opens the application, a cookie will be set, a entry will be made in database and corresponding information from the db will be fetched.
I am yet to lay a concrete plan for this. I wanted to know whether doing it this way is a good idea? i read somewhere....'Real men don't use any framework. They make everything on their own' :P
Please correct me if i am on a wrong direction. M just starting with these things....
I'm not aware of any node.js frameworks that are closed-source. Just pick one that seems to do what you want to do, download it, and study the source code to see how the developer implemented it. Then come up with your (perceived) improvement on how they did it. You'll probably find that implementing session management involves a whole bunch of nitpicky details that were never obvious to you.
Ignore all the above advice if this is a school assignment where you're not allowed to look at related code. If that's the case, I pity you because you have an incompetent teacher.

Resources