简体   繁体   中英

Discord.js guildMemberUpdate event

I'm trying to make a welcome message bot with Discord.js, and the welcome message will be sent through a webhook in a certain channel when a member has the access to it.

And this is what I have right now:

client.on("guildMemberUpdate", member => {

  const wc = new WebhookClient('webhook-id', 'webook-token');

  if (member.roles.cache.some(r => r.name === "test")) {
    wc.send(`Welcome <@${member.id}> !`);
  } else if (member.roles.cache.find(r => r.name === "Server Booster")) {
    wc.send(`Thank you for the boost <@${member.id}> !`);
  } else return;
});

I am having a problem with the guildMemberUpdate not emitting when the role is added, the console log shows it only triggers when a role is deleted.

Any insight would be greatly appreciated!

Thanks for reading

Problem solved with using oldMember and newMember instead of member

client.on("guildMemberUpdate", (oldMember, newMember) => {

  const wc = new WebhookClient("webhook-id', 'webook-token");

  if (oldMember.roles.cache.isze !== newMember.roles.cache.size) {
    if (!oldMember.roles.cache.has("role-id") && newMember.roles.cache.has("role-id")) {
        wc.send(`Welcome ${newMember} !`);
    }
    if (!oldMember.roles.cache.has("boost-role-id") && newMember.roles.cache.has("boost-role-id")) {
        wc.send(`Thank you for the Nitro Boost ${newMember} !`);
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM