如何使用“atexit”模块修复运行时警告?

问题描述 投票:0回答:1

最近,我一直想为我的不和谐机器人创建一个退出处理程序,每次离线时都会发送一个嵌入。 我通过一些关于

atexit
模块的快速研究发现了这一点。 我尝试将它合并到我的代码中,如下所示:

import atexit
# Also other imports which make discord.py and interactions work...

def my_exit_handler():
    print("Exiting the program")
    time = get_current_timestamp() # this works
    embed = discord.Embed(
        title=" ",
        description=f"## 🔁 Bot is Restarting. Time now: <t:{time}> (<t:{time}:R>)",
        color=0x005300  # Green color
    )
    channel_id = 1149359551464882229
    final = bot.get_channel(channel_id)
    final.send(embed=embed)
    save_thread_data() # For other parts of my code
    save_leaderboard_data() # For other parts of my code

# ALL OF MY MAIN.PY CODE..

atexit.register(my_exit_handler)

bot.run('my token') 

但是,每当我尝试使其工作时,都会弹出这些错误:

/home/container/main.py:186: RuntimeWarning: coroutine 'Messageable.send' was never awaited
  final.send(embed=embed)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

我不确定如何修复它们。非常感谢您的支持。

如有任何疑问,请随时在下面发表评论,我会尽快回复

我希望看到在该 ID 地址处弹出一个嵌入内容,其中包含我要求的嵌入内容。

我也尝试将其变成异步定义,但这带来了一系列其他问题。

discord discord.py bots atexit
1个回答
0
投票

Messageable.send是一个异步函数;异步函数需要使用await关键字来等待。

await final.send(embed=embed)

了解更多:等待

© www.soinside.com 2019 - 2024. All rights reserved.