diff --git a/aiogram/bot.py b/aiogram/bot.py index 5859810f..b6c35c1a 100644 --- a/aiogram/bot.py +++ b/aiogram/bot.py @@ -34,6 +34,10 @@ class AIOGramBot: def _on_exit(self): self.session.close() + def prepare_object(self, obj): + obj.bot = self + return obj + @property async def me(self) -> User: if not hasattr(self, '_me'): @@ -45,12 +49,12 @@ class AIOGramBot: async def get_me(self) -> User: raw = await self.request(ApiMethods.GET_ME) - return User.de_json(raw) + return self.prepare_object(User.de_json(raw)) async def get_chat(self, chat_id) -> Chat: payload = generate_payload(**locals()) raw = await self.request(ApiMethods.GET_CHAT, payload) - return Chat.de_json(raw) + return self.prepare_object(Chat.de_json(raw)) async def get_updates(self, offset=None, limit=None, timeout=None, allowed_updates=None): """ @@ -64,4 +68,4 @@ class AIOGramBot: """ payload = generate_payload(**locals()) raw = await self.request(ApiMethods.GET_UPDATES, payload) - return [Update.de_json(raw_update) for raw_update in raw] + return [self.prepare_object(Update.de_json(raw_update)) for raw_update in raw] diff --git a/aiogram/types/__init__.py b/aiogram/types/__init__.py index afecda8c..3f63fac1 100644 --- a/aiogram/types/__init__.py +++ b/aiogram/types/__init__.py @@ -22,17 +22,29 @@ class Deserializable: def to_json(self): result = {} - for item in self.__slots__ or list(self.__dict__.keys()): - attr = getattr(self, item) - if not attr: + for name, attr in self.__dict__.items(): + if not attr or name == '_bot': continue if hasattr(attr, 'to_json'): attr = getattr(attr, 'to_json')() elif isinstance(attr, datetime.datetime): attr = int(time.mktime(attr.timetuple())) - result[item] = attr + result[name] = attr return result + @property + def bot(self): + if not hasattr(self, '_bot'): + raise AttributeError(f"{self.__class__.__name__} is not configured.") + return getattr(self, '_bot') + + @bot.setter + def bot(self, bot): + setattr(self, '_bot', bot) + for name, attr in self.__dict__.items(): + if isinstance(attr, Deserializable): + attr.bot = bot + @classmethod def de_json(cls, raw_data): """ diff --git a/aiogram/types/audio.py b/aiogram/types/audio.py index 7d22881b..746cf5c5 100644 --- a/aiogram/types/audio.py +++ b/aiogram/types/audio.py @@ -2,8 +2,6 @@ from . import Deserializable class Audio(Deserializable): - __slots__ = ('file_id', 'duration', 'performer', 'title', 'mime_type', 'file_size') - def __init__(self, file_id, duration, performer, title, mime_type, file_size): self.file_id = file_id self.duration = duration diff --git a/aiogram/types/callback_query.py b/aiogram/types/callback_query.py index db7bdea1..33d451da 100644 --- a/aiogram/types/callback_query.py +++ b/aiogram/types/callback_query.py @@ -2,8 +2,6 @@ from . import Deserializable class CallbackQuery(Deserializable): - __slots__ = ('id', 'from_user', 'message', 'inline_message_id', 'chat_instance', 'data', 'game_short_name') - def __init__(self, id, from_user, message, inline_message_id, chat_instance, data, game_short_name): self.data = data self.id = id diff --git a/aiogram/types/chat.py b/aiogram/types/chat.py index 00ad5af3..22362531 100644 --- a/aiogram/types/chat.py +++ b/aiogram/types/chat.py @@ -2,8 +2,6 @@ from . import Deserializable class Chat(Deserializable): - __slots__ = ('id', 'type', 'title', 'username', 'first_name', 'last_name', 'all_members_are_administrators') - def __init__(self, id, type, title, username, first_name, last_name, all_members_are_administrators): self.id: int = id self.type: str = type diff --git a/aiogram/types/chat_member.py b/aiogram/types/chat_member.py index f7e1ecf7..3464d44e 100644 --- a/aiogram/types/chat_member.py +++ b/aiogram/types/chat_member.py @@ -2,8 +2,6 @@ from . import Deserializable class ChatMember(Deserializable): - __slots__ = ('user', 'status') - def __init__(self, user, status): self.user = user self.status = status @@ -16,19 +14,3 @@ class ChatMember(Deserializable): status = raw_data.get('status') return ChatMember(user, status) - - -class ChatMemberStatus: - CREATOR = 'creator' - ADMINISTRATOR = 'administrator' - MEMBER = 'member' - LEFT = 'left' - KICKED = 'kicked' - - @classmethod - def is_admin(cls, role): - return role in [cls.ADMINISTRATOR, cls.CREATOR] - - @classmethod - def is_member(cls, role): - return role in [cls.MEMBER, cls.ADMINISTRATOR, cls.CREATOR] diff --git a/aiogram/types/contact.py b/aiogram/types/contact.py index 6895b978..630ce02a 100644 --- a/aiogram/types/contact.py +++ b/aiogram/types/contact.py @@ -2,8 +2,6 @@ from . import Deserializable class Contact(Deserializable): - __slots__ = ('phone_number', 'first_name', 'last_name', 'user_id') - def __init__(self, phone_number, first_name, last_name, user_id): self.phone_number = phone_number self.first_name = first_name diff --git a/aiogram/types/document.py b/aiogram/types/document.py index a7126fe2..bef8eae7 100644 --- a/aiogram/types/document.py +++ b/aiogram/types/document.py @@ -2,8 +2,6 @@ from . import Deserializable class Document(Deserializable): - __slots__ = ('file_id', 'thumb', 'file_name', 'mime_type', 'file_size') - def __init__(self, file_id, thumb, file_name, mime_type, file_size): self.file_id = file_id self.thumb = thumb diff --git a/aiogram/types/file.py b/aiogram/types/file.py index e276c01a..88234190 100644 --- a/aiogram/types/file.py +++ b/aiogram/types/file.py @@ -2,8 +2,6 @@ from . import Deserializable class File(Deserializable): - __slots__ = ('file_id', 'file_size', 'file_path') - def __init__(self, file_id, file_size, file_path): self.file_id = file_id self.file_size = file_size diff --git a/aiogram/types/location.py b/aiogram/types/location.py index 3cb80766..6d78006f 100644 --- a/aiogram/types/location.py +++ b/aiogram/types/location.py @@ -2,9 +2,7 @@ from . import Deserializable class Location(Deserializable): - __slots__ = ('longitude', 'latitude') - - def __init__(self, data, longitude, latitude): + def __init__(self, longitude, latitude): self.longitude = longitude self.latitude = latitude diff --git a/aiogram/types/message.py b/aiogram/types/message.py index 46a1852a..8138f357 100644 --- a/aiogram/types/message.py +++ b/aiogram/types/message.py @@ -7,14 +7,6 @@ from .user import User class Message(Deserializable): - __slots__ = ( - 'message_id', 'from_user', 'date', 'chat', 'forward_from', 'forward_from_chat', 'forward_from_message_id', - 'forward_date', 'reply_to_message', 'edit_date', 'text', 'entities', 'audio', 'document', 'game', 'photo', - 'sticker', 'video', 'voice', 'video_note', 'new_chat_members', 'caption', 'contact', 'location', 'venue', - 'new_chat_member', 'left_chat_member', 'new_chat_title', 'new_chat_photo', 'delete_chat_photo', - 'group_chat_created', 'supergroup_chat_created', 'channel_chat_created', 'migrate_to_chat_id', - 'migrate_from_chat_id', 'pinned_message', 'invoice', 'successful_payment', 'content_type') - def __init__(self, message_id, from_user, date, chat, forward_from, forward_from_chat, forward_from_message_id, forward_date, reply_to_message, edit_date, text, entities, audio, document, game, photo, sticker, video, voice, video_note, new_chat_members, caption, contact, location, venue, diff --git a/aiogram/types/message_entity.py b/aiogram/types/message_entity.py index 561ff586..7da9444e 100644 --- a/aiogram/types/message_entity.py +++ b/aiogram/types/message_entity.py @@ -3,8 +3,6 @@ from .user import User class MessageEntity(Deserializable): - __slots__ = ('type', 'offset', 'length', 'url', 'user') - def __init__(self, type, offset, length, url, user): self.type = type self.offset = offset diff --git a/aiogram/types/photo_size.py b/aiogram/types/photo_size.py index ac952b59..9f9b111a 100644 --- a/aiogram/types/photo_size.py +++ b/aiogram/types/photo_size.py @@ -2,8 +2,6 @@ from . import Deserializable class PhotoSize(Deserializable): - __slots__ = ('file_id', 'width', 'height', 'file_size') - def __init__(self, file_id, width, height, file_size): self.file_id = file_id self.width = width diff --git a/aiogram/types/sticker.py b/aiogram/types/sticker.py index 839fb979..249b2aa9 100644 --- a/aiogram/types/sticker.py +++ b/aiogram/types/sticker.py @@ -2,8 +2,6 @@ from . import Deserializable class Sticker(Deserializable): - __slots__ = ('file_id', 'width', 'height', 'thumb', 'emoji', 'file_size') - def __init__(self, file_id, width, height, thumb, emoji, file_size): self.file_id = file_id self.width = width diff --git a/aiogram/types/update.py b/aiogram/types/update.py index 21198c1a..17350120 100644 --- a/aiogram/types/update.py +++ b/aiogram/types/update.py @@ -3,9 +3,6 @@ from .message import Message class Update(Deserializable): - __slots__ = ('update_id', 'message', 'edited_message', 'channel_post', 'edited_channel_post', 'inline_query', - 'chosen_inline_result', 'callback_query', 'shipping_query', 'pre_checkout_query') - def __init__(self, update_id, message, edited_message, channel_post, edited_channel_post, inline_query, chosen_inline_result, callback_query, shipping_query, pre_checkout_query): self.update_id = update_id diff --git a/aiogram/types/user.py b/aiogram/types/user.py index fefd8f20..99747b4e 100644 --- a/aiogram/types/user.py +++ b/aiogram/types/user.py @@ -3,8 +3,6 @@ from ..utils.user_language import get_language class User(Deserializable): - __slots__ = ('id', 'first_name', 'last_name', 'username', 'language_code') - def __init__(self, id, first_name, last_name, username, language_code): self.id: int = id self.first_name: str = first_name diff --git a/aiogram/types/user_profile_photos.py b/aiogram/types/user_profile_photos.py index ed4db003..234350f4 100644 --- a/aiogram/types/user_profile_photos.py +++ b/aiogram/types/user_profile_photos.py @@ -2,8 +2,6 @@ from . import Deserializable class UserProfilePhotos(Deserializable): - __slots__ = ('total_count', 'photos') - def __init__(self, total_count, photos): self.total_count = total_count self.photos = photos diff --git a/aiogram/types/venue.py b/aiogram/types/venue.py index 0847da35..296f5d72 100644 --- a/aiogram/types/venue.py +++ b/aiogram/types/venue.py @@ -2,8 +2,6 @@ from . import Deserializable class Venue(Deserializable): - __slots__ = ('location', 'title', 'address', 'foursquare_id') - def __init__(self, location, title, address, foursquare_id): self.location = location self.title = title diff --git a/aiogram/types/video.py b/aiogram/types/video.py index ae296af6..f160cee8 100644 --- a/aiogram/types/video.py +++ b/aiogram/types/video.py @@ -2,8 +2,6 @@ from . import Deserializable class Video(Deserializable): - __slots__ = ('file_id', 'width', 'height', 'duration', 'thumb', 'mime_type', 'file_size') - def __init__(self, file_id, width, height, duration, thumb, mime_type, file_size): self.file_id = file_id self.width = width diff --git a/aiogram/types/video_note.py b/aiogram/types/video_note.py index dd2ff966..2318a08c 100644 --- a/aiogram/types/video_note.py +++ b/aiogram/types/video_note.py @@ -2,8 +2,6 @@ from . import Deserializable class VideoNote(Deserializable): - __slots__ = ('file_id', 'length', 'duration', 'thumb', 'file_size') - def __init__(self, file_id, length, duration, thumb, file_size): self.file_id = file_id self.length = length diff --git a/aiogram/types/voice.py b/aiogram/types/voice.py index d8c84e5c..68a5022f 100644 --- a/aiogram/types/voice.py +++ b/aiogram/types/voice.py @@ -2,8 +2,6 @@ from . import Deserializable class Voice(Deserializable): - __slots__ = ('file_id', 'duration', 'mime_type', 'file_size') - def __init__(self, file_id, duration, mime_type, file_size): self.file_id = file_id self.duration = duration