Home > Blockchain >  d.py number of bans in a guild
d.py number of bans in a guild

Time:12-03

So I tried using embed.add_field(name="Ban Count", value=f"{len(await ctx.guild.bans())} Bans",inline=False) but I get this error object async_generator can't be used in 'await' expression How do I display the amount of bans?

CodePudding user response:

You must first convert the bans into a list, then get the length of the list:

bans_list = [entry async for entry in ctx.guild.bans()]
number_of_bans = len(bans_list)

# output number of bans, etc...

CodePudding user response:

It looks like you are trying to use the await keyword with a generator object, which is not supported in Python. In your code, the await keyword is being used with the ctx.guild.bans() method, which returns an async_generator object. This object is a generator that produces the list of bans for the guild asynchronously, and it cannot be used directly with the await keyword.

To fix this error, you can use the async for loop syntax to iterate over the async_generator object returned by the ctx.guild.bans() method. This will allow you to access the list of bans asynchronously, without using the await keyword. Here is an example of how you can do this:

# Get the async_generator object for the guild bans
bans = ctx.guild.bans()

# Use an async for loop to iterate over the bans and count the number of bans
ban_count = 0
async for ban in bans:
  ban_count  = 1

# Use the ban count to set the value of the "Ban Count" field in the embed
embed.add_field(name="Ban Count", value=f"{ban_count} Bans", 
inline=False)

In this example, we use the async for loop to iterate over

  • Related