message.channel.send 没有发送消息 [discord.py]
message.channel.send doesn't send message [discord.py]
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 数据的消息,但它不起作用,也没有 return 我试过的任何东西,甚至添加了一个错误处理程序(回溯)并且不执行任何操作我唯一的输出来自 async 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)
但我没有得到任何输出,如果我在 async 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)(函数)
装饰器中的文件“C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py”,第 1317 行
return cls(函数,名称=名称,**属性)
文件“C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site- packages\discord\ext\commands\core.py”,第 210 行,在 init 中
引发类型错误('Callback must be a coroutine.')
类型错误:回调必须是协程。
>>>
您需要在函数中添加上下文和 @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)
另外,您错误地定义了客户端。 客户端=discord.Client()
它应该是:
# change '!' to any prefix you would like to use.
client = commands.Bot(command_prefix ='!')
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 数据的消息,但它不起作用,也没有 return 我试过的任何东西,甚至添加了一个错误处理程序(回溯)并且不执行任何操作我唯一的输出来自 async 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)
但我没有得到任何输出,如果我在 async 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)(函数) 装饰器中的文件“C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py”,第 1317 行 return cls(函数,名称=名称,**属性) 文件“C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site- packages\discord\ext\commands\core.py”,第 210 行,在 init 中 引发类型错误('Callback must be a coroutine.') 类型错误:回调必须是协程。 >>>
您需要在函数中添加上下文和 @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)
另外,您错误地定义了客户端。 客户端=discord.Client() 它应该是:
# change '!' to any prefix you would like to use.
client = commands.Bot(command_prefix ='!')