【发布时间】:2019-11-23 05:52:38
【问题描述】:
我正在尝试从 URL 中抓取 HTML 并让我的不和谐机器人显示结果。朋友提供了代码并进行了测试。但是,我相信他使用的是旧版本的 discord.py。他目前不可用。我已经搜索了所需的适当更改,但没有找到针对我的问题的特定更改。
我尝试将 client.send_message 更改为 channel.send - 我收到另一个错误:AttributeError: 'NoneType' object has no attribute 'send'
import discord
import requests
from bs4 import BeautifulSoup
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
channel = client.get_channel('CHANNEL ID')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!dfx2'):
website_url = requests.get('http://novaworld.cc/dfx2lobby.php?lob=pub').text
soup = BeautifulSoup(website_url, 'html.parser')
table = soup.find('table')
test = table.select_one('tr:contains("!GET SOME")')
text = test.get_text()
print(text)
await channel.send(message.channel, content = text)
client.run('TOKEN')
我收到错误 AttributeError: 'NoneType' object has no attribute 'send' after changed client.send_message
【问题讨论】:
-
您的频道分配发生在客户端启动之前,因此它始终为无。将分配移到
on_message函数中。
标签: python python-3.x discord discord.py