Pinging a random non-bot user in Discord.py

Issue

Code:

import random
user = random.choice(message.guild.members)
await message.channel.send('Hello ' + user.mention)

My question is how do I ping a random, non-bot user in Discord.py?

Any help on this will be greatly appreciated, thanks!

Solution

You can check if a user is a bot with Member.bot which returns a bool

Use a list comprehension to sort the members in the guild that are not bots(user accounts) and then use random.choice accordingly.

import random

humans = [m for m in message.guild.members if not m.bot]
random_member = random.choice(humans)

await message.channel.send(f"Hello {random_member.mention}")

Answered By – Yeti

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published