I'm building a Discord bot in Java using JDA api. What my bot does is return a quote from a league of legends champion, and then the user must guess which champion says that by typing in the chat.
The problem I'm facing is that when a person calls the bot by the command on one server and also on another server or channel, the bot conflicts and the response changes to the last one that was generated, so the user who used the command first will not be able to get the answer right because it has changed.
To make it more clear, i will use a example:
Suppose I call a command that displays a math equation where the user must correctly answer it:
But someone else also uses the command on another server after i used the command:
The answer now is 5, because the last equation generated is the one that will counts.
What can i do to prevent this? To separate different processes to a single command?
I've tried to set a boolean to define if the command is active or not, but it happens the same.
Ex: someone used the command and a equation was generated, and then the boolean is set to true and then the same command can not be used. But this boolean is also preventing to someone else use the command in another server.
You could use a HashMap
for storing the relevant data for each interaction. I'd like to suggest using the user id as key and storing question, correct answer and maybe the number of tries in an object.
Whenever someone uses your command you can check the map for their user id. If they are already in, you could discontinue their last attempt and start a new one or have them finish their last attempt before starting a new one.
Once a riddle is completed you just delete the respective element of the map.
Each time an user writes a message you just need to check if their id is a key in your map. If it is, you can react according to the entry, if not, just ignore the message.