Why doesn't my bot always properly set/update a GuildMember's roles?
Since I'm developing an RPG bot and a lot of logic also requires guild members to have certain roles.
I've had situations where, for example, a guild member needed to receive 1 specific guild role and lose 1 guild role but they would only receive the mentioned role. When I would reran the same exact code, it would execute properly.
It's like on discord's side the role simply doesn't get added (or removed). Is it possible that this has something to do with caching?
I've been investigating this issue for quite some time and the only reason I could think of why this is happening is because I'm passing around the guildMember object through my code and perhaps the cache is stale and it doesn't properly update?
I couldn't even word that properly but I've come to a dead end. This happens randomly and it has been happening for a long time with different parts of my code.
5 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!not all members are cached
you have to fetch the member
Yes, I'm aware of that.
And I do have the guild member
without any code i cant really help you any further
Either cached or fetched, I have the guarantee that I have the guild member but when it comes to setting their roles, that's the issue
Yes, kind of
I can't really come up with an example because it's basically adding and removing roles
And even using the
set
method sometimes
But that's it
What if I have an array of role IDs that I want removed from a guildmember if they have them?
Would awaiting each remove
call be a solution to this?
It would be slower because it would await each update but woudl it fix the issue tho
Would you ever await
the resolve of adding/removing roles from a guild member in my case?
I'm not sure what would my best approach be if race conditions are causing this. I have a lot of places where roles are constantly checked, added and removed.
In my example, the 1 role being added and the other role being removed are not the same roles
I have 2 roles, one I want to add and one I want removed and I get random cases where one of those operations just doesn't happen
Yes
Sorry I was digging up some older code that I knew for a fact didnt work 100% of the time
I know that this is far from an okay way of writing this but it will illustrate my example
Basically, I have a guarantee that a guildMember should have 1 role from each array
And I had scenarios where they would get 3 instead of 4
Thanks for the tips on js. I have the guarantee I mentioned because createdCharacter.sicc.strength, intelligence, charisma and cunning are all set before to a range from 0-5
But that's all besides the point. Let's say I have this
Every once in a while, my guild member is missing one random role that is present in that array
There are other places in my code that change roles, yes, but there definitely isn't a place in my code that changes THESE specific roles
Okay so now we're getting somewhere
So if in my code in place A guildRole a
is being added while in place B the guildRole b
is being removed with add
and remove
methods respectively, at the same time, it can cause an issue?
No set
method is being used for example
I've definitely had those scenarios in the audit logs before
I've also had scenarios where the audit log clearly shows a role being removed from the guild member yet the role is still on them
Is that due to the same reasons?