Reputation: 6806
I'm trying to build a channel where users can update a bot message via command. The channel would work like this: if X sends + Item
the bot adds "Item" to an array linked to the user, that later is written on a file. To build the message, the bot reads the file and writes the elements of the array related to the user. How can I make that the array is someway linked to the user, like through the id, so that I can then read it?
Thanks for the help
Upvotes: 0
Views: 2195
Reputation: 6806
I finally ended up using a JSON object file, and reading the properties using list[id], since that id is a string. I set up the file in this way:
{"id1" : ["element1", "element2"], "id2": ["element3"]}
In this way, I can read the data stored in the file and write the modified values by simply writing the object with the fs module.
Upvotes: 0
Reputation: 4071
In case you did not knew, you can get the sender's ID via message.author.id
, where message
is Message
object.
You could find some node API around to help you with that.
Otherwise, you simply create a dictionary, with the user ID as the key value. You can check it out here or here.
(Either one of these would work. This can also help: clickme!, or you can google around for more.)
Storing into a textfile and maintaining the data is much trickier. If you can find some API that simplifies the process for you, then thats good.
But if alternative, you can store your data to a textfile somehow like strings are stored in a program. (Check out data-structures on how strings are stored and read.)
Basically, lets say you have 2 users with 2 element each. Your data in textfile is going to look like this:
user1-ID
element1
element2
NULL-VALUE
user2-ID
element1
element2
NULL-VALUE
And basically whenever you want to read from the textfile, just tell your program to search for the respective UserID, and read down the elements until it reaches a NULL-VALUE
. And once it reaches there, your program stops reading it.
(Of course you can use something else other than NULL-VALUE
, that was an example. I suggest making your elements wrapped around brackets. As some user might enter NULL-VALUE
in the command arguement, which would cause some trouble. )
Upvotes: 1