python 在create_task()中获取消息信息,而不使用async函数

vc9ivgsu  于 2023-08-02  发布在  Python
关注(0)|答案(1)|浏览(140)

所以,我试图发送一个不和谐的消息,而不使用异步功能,它的工作,但有一个问题,我不能得到有关信息的消息(如消息ID)。
下面是我的代码:

import thread, discord
channel = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] #assume this is channel id

@tasks.loop(seconds=10)
async def notification():
    def template(latest_data):
        embed = discord.Embed(title="Ring Ring!", description=latest_data["des"], colour=discord.Colour.green())
        return embed

    def send(none,divided_channels,page):
        for channel_id in divided_channels:
            message_info = client.loop.create_task(channel.send(embed=page))
            # i want to get the message id i sent in in message_info

    def newnotification(page):
        split = 5
        looping = 0
        while looping < len(channel):
            divided_channels = channel[looping:(split+looping)]
            looping += split
            var_name = f"variable{looping}"
            globals()[var_name] = threading.Thread(target=send,args=(divided_channels,divided_channels,page))
            globals()[var_name].start()

global oldest_data, channel
latest_data = file("notification.json")
if (oldest_data != latest_data):
    page = template(latest_data)
    newnotification(page)
else:
    oldnotification()

@client.event
async def on_ready():
    notification.start()

字符串
我试过使用result和.result(),但它不起作用。如果我使用message_info.result,我将得到:connector:<aiohttp.connector.TCPConnector object at 0x0000018743206550>,如果我使用message_info.result(),我得到:asyncio.exceptions.InvalidStateError:未设置结果。

7gcisfzg

7gcisfzg1#

loop.create_task创建并返回一个任务,而不是任何类型的消息信息(名称是一个很大的提示)。如果你想要channel.send()函数返回的值,你需要await任务并给予它一个运行的机会:

def send(none,divided_channels,page):
    for channel_id in divided_channels:
        message_info = await client.loop.create_task(channel.send(embed=page))

字符串
https://docs.python.org/3/library/asyncio-task.html#awaitables

相关问题