'discord.js bot to tag random user in server [duplicate]

I want to include a '-random' command that will tag random member of a server. I have found the following code:

if(command === 'random'){
              
        let randomUser = message.guild.members.cache.random().user;
        message.channel.send('<@'+randomUser+'>');
    }

It works but it will only tag me, the bot itself, or someone else if that person recently wrote in the channel. How do I fix this?



Solution 1:[1]

To get all the members in a guild, you would have to enable a privileged intent before client.login(token): https://discordjs.guide/popular-topics/intents.html#privileged-intents

Make sure you enable the intent in your Discord Developer bot dashboard too: https://discord.com/developers/applications

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Skk