Use of $(user) in $(eval) causing command to only work for first user of the command

We’re trying to make a command similar to the 8ball example, except it takes the user’s username and computes a hash code to determine the index we pull the response from. This will create a consistent response for each user. The code works, but only for the first person to use the command. If I use the command first, the response will come back for me every time I use it. If another person tries to use it, no response (or error) is sent by Nightbot. If I delete and re-create the command and they use it first, it works for them, but not for me.

I suspect this is due to variable scoping issues, but I don’t know what kind of container these eval commands are run in and the lifecycle of variables used in an eval expression. Any information or pointers are greatly appreciated.

!addcom !team $(eval const responses = ["team 1", "team 2", "team 3", "team 4"];
let hash = 0;
let name = "$(user)";
for (i = 0; i < name.length; i++) {
    let char = name.charCodeAt(i);
    hash = ((hash<<5)-hash)+char;
    hash = hash & hash;
}
responses[Math.abs(hash % responses.length)];)

There’s no variable scoping issues, since every command use invokes a brand new sandbox for that command. I think you’re being bamboozled by command cooldowns, which prevent commands from being spammed. Moderators mostly bypass the cooldown, but users do not.

1 Like

Thanks for the quick reply! This is invaluable knowledge I think would do well to be included in the Nightbot documentation. It seems obvious, but eliminating one possible source of trouble could be helpful for others in the future.

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