message.channel.send不发送消息[discord.py]

2024-10-03 15:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

基本上,这段代码检查它是否是一个指定的时间,如果是,它会发送一条带有意大利新冠病毒数据的消息,但它不起作用,也不会返回任何我尝试过的东西,甚至添加了一个错误处理程序(回溯),也没有做任何事情。我唯一的输出是来自_ready()上的异步def

所以我试了一下:

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新冠病毒(ctx): 文件“C:\Users\danie\AppData\Local\Programs\Python\38\lib\site-packages\discord\ext\commands\core.py”,第1162行,在decorator中 结果=命令(*args,**kwargs)(func) 文件“C:\Users\danie\AppData\Local\Programs\Python\38\lib\site packages\discord\ext\commands\core.py”,第1317行,在decorator中 返回cls(func,name=name,**attrs) 文件“C:\Users\danie\AppData\Local\Programs\Python\38\lib\site-packages\discord\ext\commands\core.py”,第210行,在init raise TypeError('回调必须是协同程序') TypeError:回调必须是协同程序。 &燃气轮机&燃气轮机&燃气轮机


Tags: textimportselfclientloopdatagetdatetime
1条回答
网友
1楼 · 发布于 2024-10-03 15:29:36

您需要在函数中添加上下文,并在实例后面添加@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 ='!')

相关问题 更多 >