2

So I made an unban command in discord.py for my bot

Code:

@client.command()
@commands.has_permissions(ban_members=True)
async def unban(ctx, *, member):
    banned_users=await ctx.guild.bans()
    member_name, member_discriminator = member.split('#')

    for ban_entry in banned_users:
        user = ban_entry.user
    
    if (user.name, user.discriminator) == (member_name, member_discriminator):
        await ctx.guild.unban(user)
        await ctx.send(f'**Unbanned** {user.mention}')
        return

When I try to use it i get this error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: object async_generator can't be used in 'await' expression

How exactly do I fix this?

  • There is either *indentation problem* or *useless loop which save just last item* problem. – Olvin Roght Sep 05 '22 at 11:49
  • but how do I fix it? – NamelesKeed Sep 05 '22 at 11:56
  • instead of awaiting `ctx.guild.bans()`, use `async for` instead of `for` and indent the if condition and its body properly (should be aligned with `user = ban_entry.user` – 3nws Sep 05 '22 at 12:00
  • Does this answer your question? [How to use asynchronous generator in Python 3.6?](https://stackoverflow.com/questions/52429988/how-to-use-asynchronous-generator-in-python-3-6) – TheFungusAmongUs Sep 05 '22 at 17:49

1 Answers1

0

ctx.guild.bans() is now deprecated in discord.py 2.0. As per the discord.py API Reference, you should now use:

async for entry in ctx.guild.bans():
    user = entry.user
    # ...

The above code iterates through every banned user.

Or, to flatten all the banned users into a list:

banned_users = [entry.user async for entry in ctx.guild.bans()]

Hope this helped. By the way, if you are building a Discord bot, it would be useful to read the discord.py API Reference. It contains a lot of useful information about the basics of discord.py.

J Muzhen
  • 339
  • 1
  • 10