Ad

Get Random Discord User

- 1 answer

Im creating a bot that taggs a random user, but im having some difficulties.

How can I get a random user? Im currently doing this:

let usersCount = message.guild.memberCount;
    let users = Array.from(message.guild.members);
    let randomNumber = Math.floor(Math.random() * usersCount);
    let randomUser = users[randomNumber];

    console.log(randomUser);

But its returning this, with the whole guild:

[ '208045530369097728', GuildMember {
guild:
 Guild {
   members: [Object],
   channels: [Object],
   roles: [Object],
   presences: [Object],
   deleted: false,
   available: true,
   id: '4481898438608814093132',
   name: 'bot test',
   icon: '21cddd6a700aaff9b9ccb0c2c1b8de5e',
   splash: null,
   region: 'brazil',
   memberCount: 71,
   large: false,
   features: [],
   applicationID: null,
   afkTimeout: 300,
   afkChannelID: '448206219711217674',
   systemChannelID: null,
   embedEnabled: undefined,
   verificationLevel: 0,
   explicitContentFilter: 0,
   mfaLevel: 0,
   joinedTimestamp: 1535857830448,
   defaultMessageNotifications: 'ALL',
   ownerID: '2013795851090283612',
   _rawVoiceStates: Collection {},
   emojis: [Object] },
user:
 User {
   id: '2180145503619097728',
   username: 'xxxx!',
   discriminator: '4198',
   avatar: 'a1fb0d1c273a18acc69fa9f796a8c7d7',
   bot: false,
   lastMessageID: null,
   lastMessage: null },
joinedTimestamp: 1526933713197,
_roles:
 [ '44811902750147915532',
   '45769217147885815476',
   '47113897343090168840' ],
serverDeaf: false,
serverMute: false,
selfMute: undefined,
selfDeaf: undefined,
voiceSessionID: undefined,
voiceChannelID: undefined,
speaking: false,
nickname: 'xxxx',
lastMessageID: null,
lastMessage: null,
deleted: false } ]

How can I get the user and tag him?

I already tried randomUser.user, returns undefined. I have no idea

Ad

Answer

Solved

 var user = message.guild.members.random();
 console.log(`${user.user}`);
Ad
source: stackoverflow.com
Ad