discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.12k stars 3.95k forks source link

GuildMember#displayColor and GuildMember#displayHexColor return inaccurate values when the user is not cached #5339

Closed ImRodry closed 3 years ago

ImRodry commented 3 years ago

Please describe the problem you are having in as much detail as possible: When you try to access a member's displayColor or displayHexColor when the user is not properly cached it returns weird values

Include a reproducible code sample here, if possible:

console.log(member.displayColor)
//#7289da (this is always the same value no matter what color the role has)
console.log(member.displayHexColor)
//##7289da (this is always the same value no matter what color the role has and it's not misspelt either)
member.fetch()
console.log(member.displayColor)
//43520 (example color - dark green)
console.log(member.displayHexColor)
//#00aa00 (example color - dark green)

Also keep in mind that fetching the member doesn't always work

Further details:

Relevant client options:

tipakA commented 3 years ago

I'm having hard time understanding from where did you get a "not properly cached" GuildMember object, considering you claim you don't have any partials or intents enabled. Could you elaborate on that?

ImRodry commented 3 years ago

I'm having hard time understanding from where did you get a "not properly cached" GuildMember object, considering you claim you don't have any partials or intents enabled. Could you elaborate on that?

Well what I’m referring to is one of those users whose name doesn’t show up when you tag them and instead it just shows <@!id> (and yes the member was on the server I tested this on). The member object was obtained through guild.members.cache.get(id). Hope this helps. To make it easier to get one of these, try getting a list of all the members in a guild with a certain role, one of those will most likely not be cached.

advaith1 commented 3 years ago

all members in guild.members.cache are cached (as the code suggests); if they weren't cached then guild.members.cache.get(id) would just return undefined

ImRodry commented 3 years ago

Well then I’m not sure what the issue is. The truth is that the exact same code didn’t work when the user’s name wasn’t showing on the mention and then it did when the user finally loaded. Could’ve been coincidental but I believe the behavior described should still be considered a bug

advaith1 commented 3 years ago

well I checked the code and it seems impossible that this was caused by discord.js so it's probably caused by your code or a different library you're using

also, your client's cache has nothing to do with your bot's cache

ImRodry commented 3 years ago

I also checked the code before submitting and it did seem unlikely but the truth is that it’s gotta be discord.js causing this. All my code is in that code example I had absolutely nothing else running before.

And yes I know client and bot cache are different and that’s why I said that could’ve been a coincidence. It was just an easier way of explaining how to obtain this result. Have you tried the steps I suggested?

xiBread commented 3 years ago

It's possible that the role color of the unresolved mention is returning blurple, in which case, Discord.js can't do anything about it because it's an issue with client-side caching.

tipakA commented 3 years ago

Just finished testing what you said; With 2 bot setup i picked few random IDs that were fetched on one bot but not on the other, and then manually mentioned uncached members. Bot cached 2 out of 3 mentioned members through mentions themselves, but still reported correct color for them.

On the other hand, i am unable to not fetch members with "actual" roles that have colors, no matter their status, so only thing i could test was if the color was 0 in both cases (fetched "normally" and through mention)

Discord.js does have that specific color code in the codebase (it's BLURPLE afterall), but it does not use it anywhere on its own.

ImRodry commented 3 years ago

It's possible that the role color of the unresolved mention is returning blurple, in which case, Discord.js can't do anything about it because it's an issue with client-side caching.

But even then it shouldn’t return a hex value with displayColor and a value with ## with displayHexColor.

Just finished testing what you said; With 2 bot setup i picked few random IDs that were fetched on one bot but not on the other, and then manually mentioned uncached members. Bot cached 2 out of 3 mentioned members through mentions themselves, but still reported correct color for them.

On the other hand, i am unable to not fetch members with "actual" roles that have colors, no matter their status, so only thing i could test was if the color was 0 in both cases (fetched "normally" and through mention)

Discord.js does have that specific color code in the codebase (it's BLURPLE afterall), but it does not use it anywhere on its own.

Were the users you tested displaying like I described (id instead of displayname)? If so did you try fetching all members of the guild so you could get that member object? Like I said in the issue, fetching the user individually doesn’t always fix the issue so you could still come across it. If you want I can also send screenshots of the bug happening to me.

tipakA commented 3 years ago

(id instead of displayname) If so did you try fetching all members of the guild so you could get that member object?

Again; What you see in your desktop client has absolutely nothing to do with what your bot has cached or not. Those are completely 2 irrelevant from each other things.

If you want I can also send screenshots of the bug happening to me.

Sure, since no one i spoke to seems to have any clue what would cause that

ImRodry commented 3 years ago

Again; What you see in your desktop client has absolutely nothing to do with what your bot has cached or not. Those are completely 2 irrelevant from each other things.

Again, I know that but so far every member that displayed like that had this bug happen to them

Here’s an example (I grabbed a list of members, picked one that wasn’t displaying correctly and used eval to see their displayColor and displayHexColor A87A4338-07E0-4931-8CDB-D0DFAF5A6435

almostSouji commented 3 years ago

'##7289da' should never ever happen. There is no reason why executing this code twice would result in a different outcome (added # symbol). This alone points to something going majorly wrong here. Seeing that no one (including me) can reproduce that in any way, shape, or form my guess would be that you either do something in another piece of code that executes (for example assignment instead of comparison *) or use a modified version of the library.


* Edit: looking into it again:

https://github.com/discordjs/discord.js/blob/8a7abc9f06d44bf693e35a615bb6ba2c3eb1d6e7/src/structures/Role.js#L110-L112

If you somehow manage to overwrite <Role>.color with "#7289da" - for example if (role.color = "#7289da") that'd explain both described behaviors, which I assume is happening somewhere in your code.

ImRodry commented 3 years ago

That command literally only evaluates whatever piece of code I give it with absolutely zero modifications to it. I already said previously that this behavior is inconsistent and therefore "running this code twice" doesn't always fix it (like I mentioned in the issue). If you wanna take a look at my code, here's the command I was running: https://github.com/Hypixel-Translators/hypixel-translators-bot/blob/master/commands/Admin/eval.js and here's the line where the client is defined: https://github.com/Hypixel-Translators/hypixel-translators-bot/blob/master/index.js#L3 I'm not modifying the role color anywhere in my code. The only place where that hex value appears is on my config to define the blurple variable which is not used anywhere else in this case. This is clearly a bug with your library and I don't understand why you're trying to blame it on me after all the proof I've given you.

castdrian commented 3 years ago

"I'm not modifying the role color anywhere" mhm, sure.Screenshot_20210220-130258_GitHub.jpg

ImRodry commented 3 years ago

"I'm not modifying the role color anywhere" mhm, sure.Screenshot_20210220-130258_GitHub.jpg

Are you kidding me? That's a completely different file from the one I sent. My bot only runs the file corresponding to the command I run so in this case it only ran the function in "eval.js" and not "members.js" which is where you got that from. Also that would still not explain the behaviour because "blurple" is #7289da and NOT ##7289da. Is that enough already?

advaith1 commented 3 years ago

the file doesn't matter; you're modifying the cached role object, which is accessed when you get the member from cache as you are doing in the eval

ImRodry commented 3 years ago

the file doesn't matter; you're modifying the cached role object, which is accessed when you get the member from cache as you are doing in the eval

Okay you're right, thanks for actually explaining it. I didn't think that would influence other commands but seems to do so I changed the behavior so now it modifies a variable instead of the role object. Thanks for your help once again @advaith1