discordjs guildMemberUpdate changes to members object not yet progressed into guild.members collection

My discord bot, using discords, is using a module that allows users to pick one or more roles from a message with buttons. Those roles are then added (or removed if clicked again) from that guild member. The message is also updated to show the amount of members for each role in the role picker. This data is retrieved from discord itself. This all works fine.

However, when an admin manually adds or removes a role with the guild member, the messages that contain role pickers also need to update their counts. Here I run into an issue.

The main .js file listens to the guildMemberUpdate event and then calls the role picker module to handle the event.

client.on('guildMemberUpdate', (oldMember, newMember) => {
  rolepicker.onGuildMemberUpdate(client, newMember);
});

This works fine. So I receive the event and it is send to the role picker module.

onGuildMemberUpdate: async function(client, member) {
    logger.log({module: logmodule, level: 'debug', message: 'onGuildMemberUpdate()'});
    const sqlSelectItems = `SELECT rolepickerId FROM rolepicker WHERE guildId = "${member.guild.id}" AND messageId IS NOT NULL`;
    const items = await mysqlLib.query(sqlSelectItems).catch(err => {
      logger.log({module: logmodule, level: 'warn', message: 'DB Error in retrieving role pickers in onGuildMemberUpdate: ' + err, stack: err.stack});
    });

    if ( items != null && items.length > 0 ) {
      for ( i = 0 ; i < items.length ; i++ ) {
        const rolepickerId = items[i].rolepickerId;
        const guildid = member.guild.id;
        await rolepicker.updateRolepicker(client, guildid, rolepickerId);
      }
    }
  },

In the updateRolepicker function the message is retrieved and updated. Before actually counting the amount of members of each role, I do call:

await guilds.members.fetch();

to make sure that all data of all guilds members is retrieved.

However, it seems that the updated member data during the guildMemberUpdate event is not yet fully progressed through the discord system, as it role changes are not in the guilds.members collection even after the fetch() call.

I did found a workaround using:

setTimeout(function() { rolepicker.updateRolepicker(client, guildid, rolepickerId)}, 1000);

This delays the actual update of the role picker messages. When using this it does get the updated data in the guild.members collections.

However, using a setTimeout delay is not a clean solution as who knows how long it might take each time for the data to be actually fully updated on discord.

I hope someone can help me find a better solution.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật