Handlers autoloading from handlers folder

This commit is contained in:
2023-11-04 02:24:10 +03:00
parent a452639723
commit 68db02c504

12
main.py
View File

@@ -2,6 +2,7 @@ import asyncio
import logging import logging
import os import os
import sys import sys
from os.path import dirname, join
import aioschedule as schedule import aioschedule as schedule
import dotenv import dotenv
@@ -24,8 +25,17 @@ dp = Dispatcher()
async def start_message(message: types.Message): async def start_message(message: types.Message):
await message.answer('Абоба') await message.answer('Абоба')
handlers_dir = join(dirname(__file__), 'handlers')
for filename in os.listdir(handlers_dir):
if filename.endswith('.py'):
module_name = filename[:-3]
setup = __import__(f"handlers.{module_name}", locals(), globals(), ['setup']).setup
dp.include_router(setup(bot))
async def main() -> None: async def main() -> None:
dp.include_router(Admin_commands(bot)()) # dp.include_router(Admin_commands(bot)())
await dp.start_polling(bot, skip_updates=True) await dp.start_polling(bot, skip_updates=True)
if __name__ == '__main__': if __name__ == '__main__':