【问题标题】:Discord Bots are not recognized as Bots in Python codeDiscord Bot 在 Python 代码中不被识别为 Bot
【发布时间】:2021-12-27 00:46:59
【问题描述】:

我正在编写一个不和谐的机器人,功能 user.bot 不能正常工作

@client.event
async on_reaction_add(reaction, user):
     if not user.bot:
          await client.wait_until_ready()
     try:
          channel = client.get_channel(welcome_channel_id)
              try:
                  await channel.send(message)
                  await reaction.remove(user)
              except Exception as e
                  raise e
              except Exception as e
                  raise e

一切正常都接受 if 语句,我创建了一条带有机器人反应的消息,但机器人的反应被删除了。 我正在使用 Python 3.8。如果你需要任何额外的信息,请告诉我。

【问题讨论】:

  • reaction.remove() 听起来确实像是消除反应的东西,对吧?
  • 是的,但如果机器人做出反应,它仍然不应该这样做,这就是为什么会有if not user.bot:

标签: python discord.py python-3.8


【解决方案1】:

你的 try 块每次都会执行,在 if 语句被评估之后。

我假设它应该只在 if 语句评估为 true 时执行:

if not user.bot:
     await client.wait_until_ready()
     try:
          channel = client.get_channel(welcome_channel_id)
          await channel.send(message)
          await reaction.remove(user)
      except Exception as e
              raise e

但是 raise e 否定了使用 try/catch 块的全部意义——它没有任何效果。

上面的代码等价于

if not user.bot:
     await client.wait_until_ready()
     channel = client.get_channel(welcome_channel_id)
     await channel.send(message)
     await reaction.remove(user)

【讨论】:

  • 感谢您的快速帮助。它有效:)
  • 如果可行,请考虑接受答案。
  • 你在哪里禁食,我会尽快:)
  • 编辑了答案以删除多余的 try/catch 块。但是,try/catch 块没有任何作用,因为它只是重新引发每个异常 - 没有它它也能正常工作。
  • 在一个示例代码中,我是这样做的,所以我认为这样做更好,感谢您的纠正:)
猜你喜欢
  • 2023-01-22
  • 2018-05-31
  • 2018-08-15
  • 2022-07-12
  • 2020-03-24
  • 2020-08-15
  • 2021-11-04
  • 2019-01-13
  • 1970-01-01
相关资源
最近更新 更多