我得到了一个属性错误的删除\u消息,我应该如何解决这个问题?

2024-10-02 10:23:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个bot命令,删除某个通道中的最后200条消息。你必须有一个特定的角色才能成功地执行命令

我收到一个AttributeError,即“Bot”没有“delete\u message”属性。我该怎么解决这个问题

@client.command(pass_context = True)
@commands.has_role("watch announcement purger")
async def clear(ctx):
     channel = client.get_channel(535156631760273428)
     clearLimit = 200
     await client.delete_message(ctx.message)
     async for x in channel.history(limit = clearLimit):
          await client.delete_message(x)

Tags: 命令client消息角色messageasyncbotchannel
3条回答

我只是使用以下代码:

@commands.has_permissions(manage_messages=True)
async def clear(ctx,limit:int):
    await ctx.channel.purge(limit=limit+1)
    await ctx.send(f"Cleared {limit} messages", delete_after=5) 

我认为这可能有助于解决这个问题

您使用的是discord.py v0.16版本的语法,该版本不再受支持。
the guide for migrating to v1

使用^{}^{}

@client.command()
@commands.has_role("watch announcement purger")
async def clear(ctx):
     channel = client.get_channel(535156631760273428)
     await ctx.message.delete()
     await channel.purge(limit=200)

相关问题 更多 >

    热门问题