0

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.

https://sourceb.in/wQxrROfX9A

  • 1
    Does this answer your question? [Find out if someone has a role](https://stackoverflow.com/questions/45317305/find-out-if-someone-has-a-role) – node_modules Jul 21 '21 at 22:14
  • yes and no. yes because you made me notice the r.name is trying to find the NAME of the role while i am feeding it the role ID. thanks for that. but also no because the way you call the member is slightly different with this function. i.e the newMember and oldMember parts – CarrymesenpaiUwU Jul 21 '21 at 22:21

1 Answers1

0
if (test1 > test2 || test1 === null){
    console.log('role change');
}

Inside that if condition you are checking if an object of type GuildMember is bigger than a boolean value. This will give you false every time.

Since test1 is never null the if statement result in an overall result of false.

In order to fix this, you could check if the oldMember's role cache has the role with the name of process.env.PAID_ROLE, too.

Example:

let test1 = oldMember.roles.cache.some(r => r.name === `${[process.env.PAID_ROLE]}`);

and then you should replace the if statement with that statement:

if (test1 !== test2) {
    console.log("role change");
} else {
    return;
}

(By the way, this statement is only triggered if the user either got process.env.PAID_ROLE removed or added)

Xandrrrr
  • 118
  • 1
  • 6
  • good idea on the !== part. much simpler. il try the part above in a second. – CarrymesenpaiUwU Jul 21 '21 at 22:19
  • ya i still get the "Cannot read property "id" of undefined. also tried to have it find the role name for sanity checking sake and that too says the same thing :( i also tried to change r.name to r.id to see if that would try to find an id instead of a name but it does not – CarrymesenpaiUwU Jul 21 '21 at 22:29
  • Can you tell me where exactly you get that error because you never used anything related to IDs – Xandrrrr Jul 21 '21 at 22:32
  • when i change let test1 = oldMember; to let test1 = oldMember.roles.cache.some(r => r.name === `${[process.env.PAID_ROLE]}`); i get the same error as before. if i keep it as let test1 = oldMember i get the error on the next line when i do the same thing. also like i said thats why i changed r.name to r.id cause i noticed i was looking for name instead of an id. but even with changing r.name to r.id i get the same error – CarrymesenpaiUwU Jul 21 '21 at 22:35
  • i broke it down to let test1 = oldMember.roles and it gives me the same member object as if i did let test1 = oldMember so i think its not fetching the roles at ALL is the problem – CarrymesenpaiUwU Jul 21 '21 at 22:36
  • I actually think the code you provided ain't the whole code. Because you say, you get an error called `Cannot read property "id" of undefined`, but in your provided code you never use `id` – Xandrrrr Jul 21 '21 at 22:39
  • thats the problem. il upload the whole index.js if that helps but thats why im so stuck. even when im not calling id it still talks about an id. its bizzare. – CarrymesenpaiUwU Jul 21 '21 at 22:40