Answer the question
In order to leave comments, you need to log in
Are there kogi or something similar in Telegram?
I used to work with telegram bots, but I wrote everything in one file, since the code was not too cumbersome. Then I moved to Discord, where I made bigger bots, and I learned from kogah, with which you can divide the bot into parts and scatter these very parts into files. The fact is that, unlike telebot, discord.py has such a function, and I was wondering if there are cogs (or can they be made) for telegram bots? (I understand that everything can be done, but the method itself is interesting to me) After all, large bots somehow work, and I very much doubt that they are shoved into one file.
Answer the question
In order to leave comments, you need to log in
You can do it yourself.
Look, a key feature in most bot libraries is registering how the bot reacts to events. And most often it is done by a decorator like @bot.command in discord.py or its equivalent in aiogram.
But what is a decorator? If it has no parameters, then
@some_decorator
def some_func():
pass
def some_func():
pass
some_func = some_decorator(some_func)
@some_decorator(params)
def some_func():
pass
def some_func():
pass
wrapper = some_decorator(params)
some_func = wrapper(some_func)
def proxy(storage, *args, **kwargs):
def wrapper(func):
storage.append((func.__name__, args, kwargs))
return func
return wrapper
class MyCustomCog:
methods = []
def __init__(self, bot):
#задача конструктора - прочитать список отмеченных методов класса,
#и отдекорировать методы своего экземпляра класса (не одно и то же)
for fn, args, kwargs in self.methods:
#а тут вызываем декоратор вручную
#что там вместо bot.event? можно сделать несколько прокси, или добавить еще один параметр
decorator = bot.event(*args, **kwargs)
decorator(getattr(self, fn)) #теперь бот знает про наши обработчики событий
#декоратор-прокси запоминает все декорированые методы в список methods
#это можно реализовать и по другому, не суть. Главное, что мы запоминаем,
#с какими параметрами потом вызывать декоратор реального бота - всё, что после methods
@proxy(methods, "параметры", for="декоратора бота")
async def some_handler(self, params):
pass #обработчик того или иного события
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question