Skip to content
Advertisement

discord.py – BanIterator object is not iterable

I’m trying to make an unban command but I have no way of getting the banned users list. This is the code:

@bot.slash_command(guild_ids = GUILD_IDS, name='bans', description='Prints a list of bans for debugging')
@commands.has_permissions(ban_members = True)
async def bans(ctx):
    await ctx.defer()
    bans = ctx.guild.bans()
    for entry in bans:
        print(entry.user.name)

Error code:

Application Command raised an exception: TypeError: 'BanIterator' object is not iterable

I tried even copying the code from this man (https://youtu.be/KS1_3km5vC0) but it doesn’t work. This is the code:

@bot.slash_command(guild_ids = GUILD_IDS, name='bans', description='Prints a list of bans for debugging')
@commands.has_permissions(ban_members = True)
async def bans(ctx):
    await ctx.defer()
    bans = await ctx.guild.bans()
    for entry in bans:
        print(entry.user.name)

Error code:

TypeError: object BanIterator can't be used in 'await' expression

This is all the bot code:

import discord
from discord.ext import commands
import listeners
import moderation
from quick_csv import QuickCSV

# Bot config
intents = discord.Intents(members=True, presences=True, messages=True, guilds=True, bans=True)
bot = commands.Bot(command_prefix='€', description='TODO', intents=intents)

# Bot events
@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}#{bot.user.discriminator} tID:{bot.user.id}')


config = QuickCSV('config.cfg')
GUILD_IDS = [config.read('SERVER_ID')]


@bot.slash_command(guild_ids = GUILD_IDS, name='bans', description='Prints a list of bans for debugging')
@commands.has_permissions(ban_members = True)
async def bans(ctx):
    await ctx.defer()
    bans = await ctx.guild.bans()
    for entry in bans:
        print(entry.user.name)


# Run bot
bot.run(TOKEN)

Advertisement

Answer

as in Documentation , you need to do it in async for

async for entry in ctx.guild.bans:
    print(entry.user.name)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement