How to remove role from user in guild discordjs V12?

Tags: , , ,



I have an economy system on my discord server and players can buy VIP for 30 days. Data are saved in firestore cloud database. The bot checks every 4 hours if someone has over his VIP and it should remove it but it doesn’t work.

Console says: Cannot read property 'roles' of undefined.

Here is the part of the code that does not work:

let server = bot.guilds.cache.get("472822894649540608");
let player = server.members.cache.get('452773419105255435');
player.roles.remove('476112578280685568');

Thanks for the help

Answer

You shouldn’t assume the member is in the cache. In this case, it wasn’t. Instead, always fetch() the member:

let server = bot.guilds.cache.get("472822894649540608");
let player = await server.members.fetch('452773419105255435');
player.roles.remove('476112578280685568');

P.S.: I think in v11 that used to work, but not anymore in v12.



Source: stackoverflow