Search code examples
pythonpython-asyncioaiogram

Set_my_commands error aiogram asyncio python


I am working on a bot in aiogram 3, and when I try to use the built-in set_my_commands command, the bot launch is interrupted with the following errors:

raise validation_error
pydantic.error_wrappers.ValidationError: 1 validation error for BotCommand
description
  field required (type=value_error.missing)
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0424A148>
Traceback (most recent call last):
  File "D:\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 116, in __del__
  File "D:\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 108, in close
  File "D:\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 711, in call_soon
  File "D:\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 504, in _check_closed
RuntimeError: Event loop is closed

Here are code snippets that roughly reflect the essence of what is written in the project (the whole project is too big to cite) :

File commands.py with the location project.core.utils.commands

from aiogram import Bot
from aiogram.types import BotCommand, BotCommandScopeDefault

async def set_commands(bot: Bot):
    commands = [BotCommand(command='start', descriptions='Begin work')]
    await bot.set_my_commands(commands, BotCommandScopeDefault())

File basic.py with the location core.handlers.basic.py

from aiogram.types import Message

async def get_start(message: Message):
    await message.answer(f'Hi {message.from_user.first_name}. Welcome!')

File main.py with the location project.main.py

from aiogram import Bot, Dispatcher
from core.handlers.basic import get_start
import asyncio
from core.settings import settings
from aiogram.filters import Command
from core.utils.commands import set_commands

async def start_bot(bot: Bot):
    print('Bot started!')
    await set_commands(bot)

async def stop_bot(bot: Bot):
    print('Bot stopped!')

async def start():
    bot = Bot(token=settings.bots.bot_token, parse_mode='HTML')
    dp = Dispatcher()
    dp.startup.register(start_bot)
    dp.shutdown.register(stop_bot)
    dp.message.register(get_start, Command(commands=['start']))

    try:
        await dp.start_polling(bot)
    finally:
        await bot.session.close()

if __name__ == "__main__":
    asyncio.run(start())

I don't think this code is important in my error, we are specifically interested in set_my_commands.


Some useful information about the error:

  • This error occurs only when I declare await set_commands(bot) in the start_bot function. Without it, the bot runs perfectly and there are no warnings and errors.
  • The last fragment of the error I have given, starting with Exception ignored in: and ending with RuntimeError: is duplicated 10 times in the terminal.
  • I wrote the code based on the video tutorial, the author of the video has a similar code that does not cause any errors. However, I have newer versions of the libraries.
  • Here are the versions of the libraries used:

Python 3.8 aiogram 3.0.0.b7 asyncio 3.4.3 aiohttp 3.8.4 Windows 10


Please do not leave links to similar questions unrelated to aiogram, give a concrete example of a solution to my problem, or describe what exactly the error is.


So a list of what I was looking for and tried:

  • I was looking for a solution to this problem on the Internet, but I only saw solutions related to aiohttp and FastApi.
  • I tried adding await async io.sleep(0.1) after the finally: block.
  • I replaced asyncio.run(start()) with various other "similar" versions written on websites.
  • I also tried a lot of other "questionable" tips I found.
  • I know that you can add commands to telegram via BotFather, but I'm not satisfied with this option, because you can't select BotCommandScope there.
  • I was checking my code against the video I used to write it. I have not found any constructive differences other than the version of the libraries.

All of the above options did not affect the performance of the code. I'm probably doing something wrong.


Solution

  • Looks like in your commands.py when you describe commands in list, your BotCommand's argument 'descriptions' should look like 'description' (without 's' letter at the end)

    Your log: 1 validation error for BotCommand description field required (type=value_error.missing)

    I think that is the reason of error.