【问题标题】:Discord.py How to make a bot send message to a specific channelDiscord.py 如何让机器人向特定频道发送消息
【发布时间】:2020-10-22 07:16:54
【问题描述】:

所以我想创建一个始终运行的命令,直到发生变化,当发生变化时,它会向我指定的频道发送消息但是,我不断收到错误消息。

import requests
from bs4 import BeautifulSoup

url = "url"
s = BeautifulSoup(requests.get(url)._content, "lxml")
canonical = s.find('link', {'rel': 'canonical'})
result = canonical['href']

    async def test():
      if result == url:
        channel = bot.get_channel(766572517367873556)
        await channel.send("checking.....\n"+ url + "\n(1) result Found")
        await asyncio.sleep(10)


bot.loop.create_task(test())

这是错误:

File "C:path\bot.py", line 91, in patch
    await channel.send("checking.....\n"+ url + "\n(1) result Found")
AttributeError: 'NoneType' object has no attribute 'send'

任何帮助将不胜感激:)

【问题讨论】:

  • 你的 discord.py 版本是多少?
  • bot 是在哪里定义的?
  • 我已经定义了机器人。这是我应该提到的代码的一半。我的 discord.py 版本是 1.5.1

标签: python discord discord.py


【解决方案1】:

您正在尝试在机器人准备好之前使用频道。尝试在channel = bot.get_channel(766572517367873556) 行之前添加以下行。

await bot.wait_until_ready()

除此之外 - 请仔细检查您的缩进。

【讨论】:

  • 是的,这行得通。谢谢,我的缩进偏离了我在这里粘贴的方式。非常感谢
猜你喜欢
  • 2020-07-23
  • 2021-06-19
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 2022-01-03
  • 2021-10-04
  • 2020-12-03
  • 2023-01-17
相关资源
最近更新 更多