Im trying to change a variable in my database whenever I either give or take a role from someone (or if something like hyperlabs gives them a role). It notices when I give or take a role away but I can't seem to figure out how to properly word this to find the role ids.
This is the code I'm currently using.
///Check for if a guild owner gained or lost the paid role
bot.on('guildMemberUpdate', async (oldMember, newMember) => {
let test1 = oldMember;
console.log(test1);
let test2 = newMember.roles.cache.some(r => r.name === `${[process.env.PAID_ROLE]}`);
console.log(test2);
if (test1 > test2 || test1 === null){
console.log('role change');
} else {
return;
}
});
When console.log(test1)
goes through I get this in return. (The x's are me taking out ids. I dont get x's back.)
GuildMember {
guild: <ref *1> Guild {
members: GuildMemberManager {
cacheType: [class Collection extends Collection],
cache: [Collection [Map]],
guild: [Circular *1]
},
channels: GuildChannelManager {
cacheType: [class Collection extends Collection],
cache: Collection(0) [Map] {},
guild: [Circular *1]
},
roles: RoleManager {
cacheType: [class Collection extends Collection],
cache: Collection(0) [Map] {},
guild: [Circular *1]
},
presences: PresenceManager {
cacheType: [class Collection extends Collection],
cache: Collection(0) [Map] {}
},
voiceStates: VoiceStateManager {
cacheType: [class Collection extends Collection],
cache: Collection(0) [Map] {},
guild: [Circular *1]
},
deleted: false,
available: false,
id: 'xxxxxxxxxxxxxxxxx',
shardID: 0
},
joinedTimestamp: 1626625250120,
lastMessageID: null,
lastMessageChannelID: null,
premiumSinceTimestamp: 0,
deleted: false,
nickname: null,
_roles: [ '865042563555000350' ],
user: User {
id: 'xxxxxxxxxxxxxxxxxxx',
system: null,
locale: null,
flags: UserFlags { bitfield: 0 },
username: 'xxxxxxx',
bot: false,
discriminator: '9770',
avatar: '292a70982b25628322b90ffea55b0d57',
lastMessageID: null,
lastMessageChannelID: null
}
}
I also tried a method where I took the .roles.cache.size
of oldMember
and newMember
and wrote:
if cache size of old member is bigger than or smaller than new member then check the role and update accordingly
But that gave me an error:
Cannot read property "id" of undefined.
I had someone say that the reason this isn't working is because the roles section is not readable with how I'm calling it or something along those lines. I don't remember exactly what he said sadly.
EDIT: Sourcebin link to the entire index.js since this might be an issue outside of this block of code.