python - 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'
任何帮助,将不胜感激 :)
解决方案
您正在尝试在机器人准备好之前使用通道。尝试在该行之前添加以下channel = bot.get_channel(766572517367873556)
行。
await bot.wait_until_ready()
除了 - 请仔细检查您的缩进。
推荐阅读
- php - 如何从复选框中获取选定的 ID 到选择选项中?
- php - 通过添加属性项在新 xml 中创建虚拟产品 - Woocommerce
- html - 如何在 github-pages 上的 mkdocs 生成网站中嵌入本地 pdf 文件?
- python - 如何为两个 QListWidgets 使用一个 Qscrollbar
- android-studio - 如何使用 Android Studio 在 Kotlin 中的 typealias 上抑制这个已弃用的警告?
- reactjs - 无法在用户数组中推送数据。承诺没有得到解决
- python - 我正在尝试应用多标签分类
- powershell-5.0 - 已卸载巧克力包,无法重新安装
- visual-studio - 使用 CMake 和 Visual Studio 自定义清理目标
- reactjs - React:在将外部数据存储在 Redux 后无法加载外部数据并将其传递给组件