Home > Mobile >  RuntimeWarning: coroutine 'claimcode' was never awaited. Enable tracemalloc to get the obj
RuntimeWarning: coroutine 'claimcode' was never awaited. Enable tracemalloc to get the obj

Time:06-03

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

CodePudding user response:

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

...
  • Related