import discord
from discord.ext import commands, tasks
import datetime
import requests
import time
from bs4 import BeautifulSoup
client = discord.Client()
r = requests.get("https://www.worldometers.info/coronavirus/country/italy/")
s = BeautifulSoup(r.text, "html.parser")
data = s.find_all("div",class_ = "maincounter-number")
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@tasks.loop(seconds=50.0)
async def covid():
x = datetime.datetime.now()
d = x.strftime("%M")
if d == "21":
channel = bot.get_guild(guild).get_channel(channel)
await message.channel.send("casi di coronavirus in italia: \ncasi totali: " +
data[0].text.strip()
+ "\nmorti totali: " + data[1].text.strip()
+ "\nguariti totali: " + data[2].text.strip())
@covid.before_loop
async def before_printer(self):
print('waiting...')
await self.bot.wait_until_ready()
@covid.after_loop
async def post_loop(self):
if self.covid.failed():
import traceback
error = self.covid.get_task().exception()
traceback.print_exception(type(error), error, error.__traceback__)
client.run('token)
基本上,这段代码检查是否是指定的时间,是否发送带有意大利covid数据的消息,但是它不起作用并且不返回任何内容,我什至尝试添加错误处理程序(回溯)并什么都不做,我唯一的输出就是异步def on_ready()
所以我尝试了这个:
data = ''
@tasks.loop(seconds=50.0)
async def covid(ctx):
global data
x = datetime.datetime.now()
d = x.strftime("%M")
if d == "21":
data = "casi di coronavirus in italia: \ncasi totali: " +
data[0].text.strip() + "\nmorti totali: " + data[1].text.strip()
return data
@covid.before_loop
async def before_printer(self):
print('waiting...')
await self.bot.wait_until_ready()
@client.command()
async def get_covid(ctx):
global data
await ctx.send(data)
但是我没有任何输出,如果我在异步def covid之前添加@ client.command() 它给了我这个错误: 追溯(最近一次通话): 文件“ C:\ Users \ danie \ OneDrive \ Desktop \ test.py”,第26行,在 异步def covid(ctx): 装饰器中的文件“ C:\ Users \ danie \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ discord \ ext \ commands \ core.py”,行1162 结果=命令(* args,** kwargs)(func) 装饰器中的文件“ C:\ Users \ danie \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ discord \ ext \ commands \ core.py”,行1317 返回cls(func,name = name,** attrs) init 中的第210行,文件“ C:\ Users \ danie \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ discord \ ext \ commands \ core.py” 引发TypeError('回调必须是协程。') TypeError:回调必须是协程。 >>>
答案 0 :(得分:0)
您需要在函数和 @ bot.command()装饰器中添加一个上下文,后跟您的实例。
类似:
@client.command()
async def test(ctx):
await ctx.send('PASSED')
消息是事件,您需要添加适当的侦听器以使其起作用。除非需要文本数据,否则无需使用此事件。
在您的情况下,您是直接从COVID-19函数发送数据,该函数不执行任何操作,因为未定义消息。 它将是:
#STORE THE VALUE OF COVID INSIDE DATA VARIABLE.
data = ''
@tasks.loop(seconds=50.0)
async def covid():
global data
x = datetime.datetime.now()
d = x.strftime("%M")
if d == "21":
#Assign the value to DATA VARIABLE.
data = "casi di coronavirus in italia: \ncasi totali: " + data[0].text.strip() + "\nmorti totali: " + data[1].text.strip()
#Return the MODIFIED Data
return data
现在使用此功能发送数据。
@client.command()
async def get_covid(ctx):
global data
await ctx.send(data)
此外,您错误地定义了客户端。 client = discord.Client() 应该是:
# change '!' to any prefix you would like to use.
client = commands.Bot(command_prefix ='!')