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
d.js toolkit
d.js toolkit16mo ago
- 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!
treble/luna
treble/luna16mo ago
not all members are cached you have to fetch the member
Torrino
TorrinoOP16mo ago
Yes, I'm aware of that. And I do have the guild member
treble/luna
treble/luna16mo ago
without any code i cant really help you any further
Torrino
TorrinoOP16mo ago
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
var strRoles = ['399186047256363008', '399186312856469506', '399186497456308225', '399186503940571147', '399186515542016011', '399186520717787147'];
var intRoles = ['399216062450892811', '399216066145812481', '399216068951801889', '399216071086964736', '399216073171271694', '399216076086575105'];
var charRoles = ['3399216095367528450', '399216101038489610', '399216102963412992', '399216103965851650', '399216105668739083', '399216107040538625'];
var cunRoles = ['399186525214081025', '399187023388344331', '399187027184189452', '399187032087461888', '399187036424110080', '399187040341852171'];

for(let i = 0; i < strRoles.length; i++) {
if(createdCharacter.sicc.strength == i) {
const str = strRoles[i];
permRoles.push(str)
break;
}
}
for(let i = 0; i < intRoles.length; i++) {
if(createdCharacter.sicc.intelligence == i) {
const int = intRoles[i];
permRoles.push(int)
break;
}
}
for(let i = 0; i < charRoles.length; i++) {
if(createdCharacter.sicc.charisma == i) {
const char = charRoles[i];
permRoles.push(char)
break;
}
}
for(let i = 0; i < cunRoles.length; i++) {
if(createdCharacter.sicc.cunning == i) {
const cun = cunRoles[i];
permRoles.push(cun)
break;
}
}

// some other irrelevant code

guildMember.roles.set(permRoles);
var strRoles = ['399186047256363008', '399186312856469506', '399186497456308225', '399186503940571147', '399186515542016011', '399186520717787147'];
var intRoles = ['399216062450892811', '399216066145812481', '399216068951801889', '399216071086964736', '399216073171271694', '399216076086575105'];
var charRoles = ['3399216095367528450', '399216101038489610', '399216102963412992', '399216103965851650', '399216105668739083', '399216107040538625'];
var cunRoles = ['399186525214081025', '399187023388344331', '399187027184189452', '399187032087461888', '399187036424110080', '399187040341852171'];

for(let i = 0; i < strRoles.length; i++) {
if(createdCharacter.sicc.strength == i) {
const str = strRoles[i];
permRoles.push(str)
break;
}
}
for(let i = 0; i < intRoles.length; i++) {
if(createdCharacter.sicc.intelligence == i) {
const int = intRoles[i];
permRoles.push(int)
break;
}
}
for(let i = 0; i < charRoles.length; i++) {
if(createdCharacter.sicc.charisma == i) {
const char = charRoles[i];
permRoles.push(char)
break;
}
}
for(let i = 0; i < cunRoles.length; i++) {
if(createdCharacter.sicc.cunning == i) {
const cun = cunRoles[i];
permRoles.push(cun)
break;
}
}

// some other irrelevant code

guildMember.roles.set(permRoles);
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
const roles: string[] = ['399186047256363008', '399186312856469506', '399186497456308225', '399186503940571147', '399186515542016011', '399186520717787147'];

guildMember.roles.set(permRoles);
const roles: string[] = ['399186047256363008', '399186312856469506', '399186497456308225', '399186503940571147', '399186515542016011', '399186520717787147'];

guildMember.roles.set(permRoles);
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?
Want results from more Discord servers?
Add your server