Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting random user from ctx.guild.members (discordpy)

I dont get whats wrong with this.. how could i fix it?

for users in ctx.guild.members:
        winner = random.choice(users)

    await channel.send(f"Congratulations! {winner} won {prize}!")

Traceback:

AttributeError: 'NoneType' object has no attribute 'id'
Ignoring exception in command giveaway:
Traceback (most recent call last):
  File "C:\Users\Zsombor\Desktop\Dexton\Cogs\giveaway.py", line 115, in giveaway
    winner = random.choice(users)
TypeError: object of type 'Member' has no len()

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: object of type 'Member' has no len()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The random.choice() takes a random item out of a list. So if you execute it on a single object, you get that error. In your case, a for loop is not necessary, just do

winner = random.choice(ctx.guild.members)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading