当前位置: 首页 > 知识库问答 >
问题:

message.channel.send不发信息[discord.py]

沃念
2023-03-14
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)

但是我没有得到任何输出,如果我添加@client。在async def covid之前的command()它给了我这个错误:回溯(最后一次调用):文件“C:\Users\danie\OneDrive\Desktop\test.py”,在async def covid(ctx)中的第26行:文件“C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py”,第1162行,在decorator result=command(*args,**kwargs)(func)中文件“C:\Users\danie\AppData\Local\Programs\Python38\lib\site packages\discord\ext\commands\core.py”,第1317行,在decorator return cls(func,name=name,**attrs)文件“C:\Users\danie\AppData\Local\Programs\Python38\lib\site-packages\discord\ext\commands\core.py”,第210行,在init raise-raise-TypeError中(“回调必须是一个协程”)TypeError:回调必须是协同程序。

共有1个答案

苗康平
2023-03-14

您需要在函数和@bot中添加上下文。command()decorator后跟实例。

同样:

@client.command()
async def test(ctx):
    await ctx.send('PASSED')

消息是一个事件,您需要添加一个合适的侦听器才能使其工作。除非需要文本数据,否则不需要使用此事件。

对于2019冠状病毒疾病,你直接从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()它应该是:

# change '!' to any prefix you would like to use.
client = commands.Bot(command_prefix ='!')
 类似资料:
  • 在我的storm拓扑(有2个喷口和1个bolt)中,其中一个kafka喷口使用者的偏移量正在前进,但MSG没有通过kafka喷口发送到bolt。我可以在storm ui中看到,对于那个特定的喷口,发出和传送的消息是0。所以,我的问题是为什么消费者在前进,我可以看到消费者从zookeeper客户端的抵消逐渐增加。

  • 若要使用此功能,需使用PS Vita注册PlayStation®Network。 若要发送信息,需要对方的在线ID。在线ID是于注册PlayStation®Network时创建的。发送信息前,请事先询问对方的在线ID。 发送信息 1. 轻触想要发送信息的群。 显示群内收发的信息列表画面。 2. 在输入栏输入信息并轻触[发送]。 附加文件 可附加(照片)内保存的图像文件。轻触(添加附件),并轻触要附

  • 通过(群信息)的应用程序,可发送地点信息。 1. 轻触想发送的旗帜或(当前位置)。 会显示信息栏。 2. 轻触(信息)。 3. 轻触(选项)>[发送群信息]。 请遵循画面指示正确操作。

  • 试图从153减去消息中发送的数字 忽略异常在on_messageTraceback(最近的调用最后):文件"/opt/viralenvs/python3/lib/python3.8/site-包/不和谐/client.py",第343行,在_run_event等待coro(*args,**kwargs)文件"main.py",第13行,在on_message等待message.channel.se

  • 所以我用discord编写了一个discord机器人。js的一个特性是,当新用户加入服务器时,bot会发送一条消息。我现在不做嵌入,我以后可以自己做。 这是我的主要专为此功能制作的文件: 在我的文件中,我刚刚导入了该文件,并添加了。 但由于某些原因,我的代码没有出现在欢迎频道。。。任何帮助都将不胜感激。

  • #include <stdio.h> #include <signal.h> void handler(int sig); void handler(int sig) { signal(sig, handler); printf("Receive signal: %d\n", sig); } int main(void) { signal(SI