I’m trying to make an unban command but I have no way of getting the banned users list. This is the code:
JavaScript
x
8
1
@bot.slash_command(guild_ids = GUILD_IDS, name='bans', description='Prints a list of bans for debugging')
2
@commands.has_permissions(ban_members = True)
3
async def bans(ctx):
4
await ctx.defer()
5
bans = ctx.guild.bans()
6
for entry in bans:
7
print(entry.user.name)
8
Error code:
JavaScript
1
2
1
Application Command raised an exception: TypeError: 'BanIterator' object is not iterable
2
I tried even copying the code from this man (https://youtu.be/KS1_3km5vC0) but it doesn’t work. This is the code:
JavaScript
1
8
1
@bot.slash_command(guild_ids = GUILD_IDS, name='bans', description='Prints a list of bans for debugging')
2
@commands.has_permissions(ban_members = True)
3
async def bans(ctx):
4
await ctx.defer()
5
bans = await ctx.guild.bans()
6
for entry in bans:
7
print(entry.user.name)
8
Error code:
JavaScript
1
2
1
TypeError: object BanIterator can't be used in 'await' expression
2
This is all the bot code:
JavaScript
1
32
32
1
import discord
2
from discord.ext import commands
3
import listeners
4
import moderation
5
from quick_csv import QuickCSV
6
7
# Bot config
8
intents = discord.Intents(members=True, presences=True, messages=True, guilds=True, bans=True)
9
bot = commands.Bot(command_prefix='€', description='TODO', intents=intents)
10
11
# Bot events
12
@bot.event
13
async def on_ready():
14
print(f'Logged in as {bot.user.name}#{bot.user.discriminator} tID:{bot.user.id}')
15
16
17
config = QuickCSV('config.cfg')
18
GUILD_IDS = [config.read('SERVER_ID')]
19
20
21
@bot.slash_command(guild_ids = GUILD_IDS, name='bans', description='Prints a list of bans for debugging')
22
@commands.has_permissions(ban_members = True)
23
async def bans(ctx):
24
await ctx.defer()
25
bans = await ctx.guild.bans()
26
for entry in bans:
27
print(entry.user.name)
28
29
30
# Run bot
31
bot.run(TOKEN)
32
Advertisement
Answer
as in Documentation , you need to do it in async for
JavaScript
1
3
1
async for entry in ctx.guild.bans:
2
print(entry.user.name)
3