'Get all Discord Members with DiscordJs

I try to get all members in a Discord server using the following code:

const memberList = await interaction.guild.members.fetch();
console.log(memberList)

If I run this I get a timeout.

I have heard that it is not optimal to fetch all members and it is more efficent to take them from the cache using the following code. However this respond only 2 people on the server to me.

interaction.guild.members.cache.forEach(member => console.log(member.user.username));

How can I get all members in the server? The plan is to check who have no role and kick them.

Thank you for your help in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source