You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pycord中使用斜杠命令清除消息?

Pycord 清除消息命令失效的解决方法

Pycord的Context对象并没有purge()方法,这个方法属于TextChannel类,所以你之前的代码无法生效。

修正后的代码如下:

@bot.command()
async def purge(ctx, amount: int):
    # 调用频道对象的purge方法执行清除
    await ctx.channel.purge(limit=amount + 1)  # +1是为了同时清除触发命令的那条消息
    await ctx.respond(f"已清除 {amount} 条消息。")

额外注意事项:

  • 机器人必须拥有管理消息的权限,否则无法执行清除操作
  • 如果不需要清除触发命令的消息,可去掉+1,仅保留limit=amount
  • Discord API限制:purge()无法清除超过14天的历史消息

内容的提问来源于stack exchange,提问作者ZenithGuy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 21:12:02