I wanted to have a check on whether a user has a role or not, with a simple
let author = message.author.id
let guild = await client.guilds.fetch("guild")
let member = await guild.members.fetch(author)
member.roles.cache.has("role")
However, sometimes it returns false
even though I just gave that member a role. And it still returns true
after I remove their role. It'll work again only after I reset the bot.
Is this a bug or do I need to cache the member as a whole again? If so, how would I go about doing this to make it quick?
CodePudding user response:
Seems like a cache issue you're facing...
I've not really developed using discord api however reading their documentation I get the impression that you should be using either:
await guild.members.fetch({ user: author/id, force: true });
The force flag specifies not to use cache in the query - more here. Obviously with disabling cache, you might experience performance degradation.
Additionally, the documentation specifies message.member
object which might be doing this already for you, saving you a query or two.