【问题标题】:How to perform an action when anybody sends a message with discord.py?当有人使用 discord.py 发送消息时如何执行操作?
【发布时间】:2019-04-11 03:56:14
【问题描述】:

我想在某人发送消息时编辑其用户名以添加一个以显示他们发送了多少消息,我将如何在 discord.py 中执行此操作?

我正在设置一个机器人,通过在他们每次发送消息时编辑他们的用户名来 +1 号码。

    keyword = "thing"
    @bot.event
    async def on_message(message):
          message_text = message.content.strip().upper()
          if keyword in message_text:
          await ctx.send('. . .')

如果检测到关键字,我已经尝试过仅输出文本。

我希望得到 . . .但什么也没有发生,没有错误消息,什么都没有,只是什么都不做。

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: python discord.py discord.py-rewrite


    【解决方案1】:

    message_text全是大写,而"thing"是小写

    keyword = "thing"
    @bot.event
    async def on_message(message):
          message_text = message.content.strip().lower()
          if keyword in message_text:
              await message.channel.send('. . .')
    

    请注意,您不能执行ctx.send,因为您不在命令中,因此无权访问调用上下文对象。

    【讨论】:

    • 非常感谢!有没有办法让它在每次发送消息时做到这一点,而不管关键字是什么?
    • 当然,只需删除检查。 on_message 会为机器人可以“看到”的每条发送的消息调用。小心,因为这也包括机器人自己的消息,所以你可能需要if message.author == bot.user: return 检查
    猜你喜欢
    • 2021-01-26
    • 1970-01-01
    • 2021-02-24
    • 2021-03-11
    • 2020-12-02
    • 2022-12-01
    • 2019-09-19
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多