【问题标题】:DISCORD PY The command for deleting author messages isn't executingDISCORD PY 删除作者信息的命令未执行
【发布时间】:2021-01-17 15:36:45
【问题描述】:

我希望它在输入后自动删除作者的消息 (*answer84),这是我的代码:

@client.command()
async def on_message(message):
    await client.process_commands(message)
    if message.content.startswith('*answer84'):
        await ctx.message.delete()

由于某种原因,它只是不删除消息,(没有回溯错误消息)任何帮助将不胜感激,谢谢!

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

您的代码有一些错误:

事件

您需要使用 event 装饰器而不是 command() 装饰器。这是“注册要监听的事件的装饰器。”。阅读更多关于事件参考here

删除消息

由于on_message事件只接受message作为参数,所以没有ctx这样的东西。删除ctx,就可以了!

代码

@client.event #changed decorator to event
async def on_message(message):
    if message.content.startswith('*answer84'):
        await message.delete() #removed ctx

    await client.process_commands(message)

编辑

client.process_commands 移至活动底部,这是一个很好的做法。

【讨论】:

  • 我认为最好在if 条件之后调用process_commands()
猜你喜欢
  • 2018-08-27
  • 2019-09-21
  • 2019-06-16
  • 1970-01-01
  • 2021-06-28
  • 2020-11-21
  • 2021-05-31
  • 2021-06-02
  • 1970-01-01
相关资源
最近更新 更多