Runtime Warning:Coroutine' SaimeCode'从未等待过。启用TraceMalloc获得对象分配跟踪(Telebot)

发布于 2025-02-03 19:20:23 字数 773 浏览 4 评论 0 原文

我遇到此错误的代码部分。

@bot.message_handler(commands=['redeemcode'])
def get_code(message):
    code = bot.send_message(chatid, "Send the Code to Redeem")
    bot.register_next_step_handler(code, claimcode)


async def claimcode(message):
    code = str(message.text)
    print(code)
    await client.redeem_code(code)

我遇到的错误。

Microsoft Windows [Version 10.0.19044.1645]
(c) Microsoft Corporation. All rights reserved.

D:\Genshin Bot>C:/Python310/python.exe "d:/Genshin Bot/genshinbot.py"
C:\Python310\lib\site-packages\telebot\util.py:89: RuntimeWarning: coroutine 'claimcode' was never awaited
  task(*args, **kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

有人可以帮助并告诉代码怎么了

The portion of the code where i am getting this error.

@bot.message_handler(commands=['redeemcode'])
def get_code(message):
    code = bot.send_message(chatid, "Send the Code to Redeem")
    bot.register_next_step_handler(code, claimcode)


async def claimcode(message):
    code = str(message.text)
    print(code)
    await client.redeem_code(code)

The error that i am getting.

Microsoft Windows [Version 10.0.19044.1645]
(c) Microsoft Corporation. All rights reserved.

D:\Genshin Bot>C:/Python310/python.exe "d:/Genshin Bot/genshinbot.py"
C:\Python310\lib\site-packages\telebot\util.py:89: RuntimeWarning: coroutine 'claimcode' was never awaited
  task(*args, **kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Can someone please help and tell whats wrong with the code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平定天下 2025-02-10 19:20:24

您正在尝试在普通功能中调用coroutine。使用 asyncio.run 。要了解有关Coroutines和任务的更多信息,您可以参考此链接:

上面的代码段可以更改为:

import asyncio

@bot.message_handler(commands=['redeemcode'])
def get_code(message):
    code = bot.send_message(chatid, "Send the Code to Redeem")
    bot.register_next_step_handler(code, asyncio.run(claimcode()))

...

You are trying to call a coroutine inside a normal function. Use asyncio.run. To learn more about coroutines and tasks, you can refer to this link: https://docs.python.org/3/library/asyncio-task.html.

The code snippet from above can be changed to:

import asyncio

@bot.message_handler(commands=['redeemcode'])
def get_code(message):
    code = bot.send_message(chatid, "Send the Code to Redeem")
    bot.register_next_step_handler(code, asyncio.run(claimcode()))

...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文