0

EDIT I think my question can be summed up to discord js sometimes return the wrong user when fetching.

client.users.fetch(args[0]).then(user => message.reply(user.id));

whois 187548574697914368

277199585372274689

The bot return the wrong user. I tought it was link to some weird caching issue, but setting force parameter to true still return the wrong user.

client.users.fetch(args[0], false, true).then(...);

I don't understand where does this error come from. Is there a way to refresh the cache, so I can correctly fetch users ?

ORIGINAL POST

I'm facing a very weird issue with my bot. Sometimes, I don't understand after what but when some peoples use the bot, they can switch id. The bot start reading the wrong value from message.author.id, and interpret some messages as there were sent by someone else. When someone is "bugged", his id is always missread with another value (UserA.id will be always read as the id of user b) I don't know what is the link between User A and User B (user B's id can sometimes be fine, while user A is switched to user B)

client.on('message', message => {
    console.log("Message recieved : <#" + message.channel.id + "> <@" + message.author.id + "> : " + message.content);
    ...
});

Message recieved : #general @UserB : I'm user A

It seems that this function is where the bug come from (I'm not sure)

async execute(messageReaction, arg) {

    let users = await messageReaction.users.fetch();
    console.log("Looting gift : " + users.map(user => user.username + ' '));
    users.delete(messageReaction.message.client.user.id);

    Doing stuff ....

    await messageReaction.message.reactions.removeAll();
},

It seems that the bot at some point corrrupt cached data, and keep loading the user id from it (I don't know if it is possible) So I tried adding this at the end of the function :

    messageReaction.message.guild.members.fetch(users.map(user => user.id))
        .catch(err => console.log('Erreur while fetching members : ' + users.map(user => user.id) + ' : \n ' + err ));

    for(user of messageReaction.message.client.inventory.inventory.keys()) {
        messageReaction.message.client.users.fetch(user).catch(err => console.log('Erreur while fetching user : ' + users.id + ' : \n ' + err ));
    }

But the issue is still there, and I don't understand why. The only solution I found so far was to logout the bot and relaunching it. It is not very handy, and is there anything else I can do to either understand that error or remove it.

Portevent
  • 714
  • 3
  • 12
  • 34

2 Answers2

0

If you're having problems with .fetch(), try .cache.get(). For example, in your case, you would do something like await client.users.cache.get(args[0]).

Bea
  • 1
  • 2
0

Are you by chance storing the user.id as an integer in your table? I came across your post as I was having a similiar issue, and storing it as a string solved it for me.