RuntimeWarning:从未等待协程 'claimcode'。启用 tracemalloc 以获取对象分配回溯(telebot)

RuntimeWarning: coroutine 'claimcode' was never awaited. Enable tracemalloc to get the object allocation traceback (telebot)

出现此错误的代码部分。

@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

有人可以帮忙告诉我代码有什么问题吗

您正在尝试在普通函数中调用协程。使用 asyncio.run。想了解更多协程和任务,可以参考这个link:https://docs.python.org/3/library/asyncio-task.html.

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

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()))

...