1
@bot.command()
async def purge(ctx, limit=100, member: discord.Member=None):
  await ctx.message.delete()
  msg = []
  for m in ctx.channel.history():
    if m.author == member:
      msg.append(m)
  await ctx.channel.delete_messages(msg)

heres my code, this is a purge command with discord.py. Im trying to make it so it purges the message of someone you mention. Any help?

2
  • This would be a great opportunity to use the channel.purge method
    – effprime
    Jan 19, 2021 at 23:21
  • 4
    Please do not vandalize your posts. Once you have submitted a post, you have licensed the content to the Stack Overflow community at large (under the CC BY-SA license). By Stack Exchange policy, any vandalism will be reverted. Jan 19, 2021 at 23:47

1 Answer 1

4

The reason this isn't working is because ctx.channel.history() is an async function, so you'll have to use the async keyword before the loop. Like this:

@bot.command()
async def purge(ctx, limit=100, member: discord.Member=None):
  await ctx.message.delete()
  msg = []
  async for m in ctx.channel.history():
    if m.author == member:
      msg.append(m)
  await ctx.channel.delete_messages(msg)

You can also just use the purge function with a lambda instead. If you were to go that route, your code would look like this:

@bot.command()
async def purge(ctx, limit=100, member: discord.Member=None):
  await ctx.message.delete()
  deleted = await ctx.channel.purge(limit=100, check=lambda msg: msg.author == member)
  await ctx.channel.send('Deleted {} message(s)'.format(len(deleted)))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.