首页 > 解决方案 > Discord.py 如何让机器人向特定频道发送消息

问题描述

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

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'

任何帮助,将不胜感激 :)

标签: pythondiscorddiscord.py

解决方案


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

await bot.wait_until_ready()

除了 - 请仔细检查您的缩进。


推荐阅读