I am creating a bot for my discord and I have been trying for 3 days to integrate the delete option and I encounter the following problem:
in delete
messages = await ctx.channel.history(limit=number_of_messages + 1).flatten()
AttributeError: 'async_generator' object has no attribute 'flatten'
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'async_generator' object has no attribute 'flatten'
Code:
@bot.command(name='del')
async def delete(ctx: commands.Context, number_of_messages: int):
messages = await ctx.channel.history(limit=number_of_messages + 1).flatten()
for each_message in messages:
await each_message.delete()
python version : 3.10.11
In discord.py 2.0 the .flatten()
method was removed since the introduction of async list comprehensions.
@bot.command(name='del')
async def delete(ctx: commands.Context, number_of_messages: int):
messages = [message async for message in ctx.channel.history(number_of_messages + 1)]
for each_message in messages:
await each_message.delete()
This is a bad solution, though. Use the channel.purge()
method since it bulk deletes messages:
@bot.command(name='del')
async def delete(ctx: commands.Context, number_of_messages: int):
await ctx.channel.purge(limit=number_of_messages + 1)