diff options
author | mjk134 <57556877+mjk134@users.noreply.github.com> | 2022-07-12 17:52:15 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-12 17:52:15 +0000 |
commit | 5e54ae9fdcf0dccae8337dd4a394007701dddbd6 (patch) | |
tree | 6e4272d84c4f4d7d0d19f128ea1ddc93f1c39b95 | |
parent | 8ee52e6a348ad6ee32728c8070cc4dbb2a5caa2c (diff) | |
download | discobra-5e54ae9fdcf0dccae8337dd4a394007701dddbd6.tar.gz |
feat(client): Add client caching
-rw-r--r-- | discord/client.py | 118 | ||||
-rw-r--r-- | discord/user.py | 5 |
2 files changed, 117 insertions, 6 deletions
diff --git a/discord/client.py b/discord/client.py index e028a1a..b68edbb 100644 --- a/discord/client.py +++ b/discord/client.py @@ -44,6 +44,33 @@ class GatewayEvents(IntEnum): GUILD_SYNC = 12 +class ClientCache: + """ + A cache for the client. + """ + def __init__(self): + self.users = {} + self._user: User + + def update_user(self, user: User): + self.users[user.id] = user + + def get_user(self, id: str) -> Optional[User]: + """ + Get a user from the cache. + """ + return self.users.get(id) + + @property + def user(self) -> User: + """The cached `discord.user.User` associated with the client.""" + return self._user + + @user.setter + def user(self, user: User): + self._user = user + + class Client: """ Represents a Discord client (i.e. a bot). @@ -51,13 +78,21 @@ class Client: """ _token: str rest_client: RESTClient + client_cache: ClientCache = ClientCache() @property async def user(self): """The `discord.user.User` associated with the client.""" data = await self.rest_client.get('/users/@me') - return User(data) + user = User(data) + self.client_cache.user = user + return user + @property + def cache(self) -> ClientCache: + """The cache for the client.""" + return self.client_cache + def __init__(self, intents: list[Intents]): self.gateway = None self.loop = asyncio.get_event_loop() @@ -137,10 +172,83 @@ class Client: event = msg['t'] - if event == 'READY': - print(data) - - self.event_emitter.emit('on_' + event.lower()) + match(event): + case 'READY': + self.ready = True + self.client_cache.user = User(data['user']) + return self.event_emitter.emit('on_ready') + case 'APPLICATION_COMMAND_PERMISSIONS_UPDATE': + return self.event_emitter.emit('on_application_command_permissions_update') + case 'CHANNEL_CREATE': + return self.event_emitter.emit('on_channel_create', data) + case 'CHANNEL_DELETE': + return self.event_emitter.emit('on_channel_delete', data) + case 'CHANNEL_UPDATE': + return self.event_emitter.emit('on_channel_update', data) + case 'CHANNEL_PINS_ACK': + return self.event_emitter.emit('on_channel_pins_ack', data) + case 'CHANNEL_PINS_UPDATE': + return self.event_emitter.emit('on_channel_pins_update', data) + case 'GUILD_CREATE': + return self.event_emitter.emit('on_guild_create', data) + case 'GUILD_DELETE': + return self.event_emitter.emit('on_guild_delete', data) + case 'GUILD_UPDATE': + return self.event_emitter.emit('on_guild_update', data) + case 'GUILD_BAN_ADD': + return self.event_emitter.emit('on_guild_ban_add', data) + case 'GUILD_BAN_REMOVE': + return self.event_emitter.emit('on_guild_ban_remove', data) + case 'GUILD_EMOJIS_UPDATE': + return self.event_emitter.emit('on_guild_emojis_update', data) + case 'GUILD_INTEGRATIONS_UPDATE': + return self.event_emitter.emit('on_guild_integrations_update', data) + case 'GUILD_MEMBER_ADD': + return self.event_emitter.emit('on_guild_member_add', data) + case 'GUILD_MEMBER_REMOVE': + return self.event_emitter.emit('on_guild_member_remove', data) + case 'GUILD_MEMBER_UPDATE': + return self.event_emitter.emit('on_guild_member_update', data) + case 'GUILD_MEMBERS_CHUNK': + return self.event_emitter.emit('on_guild_members_chunk', data) + case 'GUILD_ROLE_CREATE': + return self.event_emitter.emit('on_guild_role_create', data) + case 'GUILD_ROLE_DELETE': + return self.event_emitter.emit('on_guild_role_delete', data) + case 'GUILD_ROLE_UPDATE': + return self.event_emitter.emit('on_guild_role_update', data) + case 'GUILD_SYNC': + return self.event_emitter.emit('on_guild_sync', data) + case 'GUILD_MEMBERS_CHUNK': + return self.event_emitter.emit('on_guild_members_chunk', data) + case 'MESSAGE_CREATE': + return self.event_emitter.emit('on_message_create', data) + case 'MESSAGE_DELETE': + return self.event_emitter.emit('on_message_delete', data) + case 'MESSAGE_DELETE_BULK': + return self.event_emitter.emit('on_message_delete_bulk', data) + case 'MESSAGE_UPDATE': + return self.event_emitter.emit('on_message_update', data) + case 'MESSAGE_REACTION_ADD': + return self.event_emitter.emit('on_message_reaction_add', data) + case 'MESSAGE_REACTION_REMOVE': + return self.event_emitter.emit('on_message_reaction_remove', data) + case 'MESSAGE_REACTION_REMOVE_ALL': + return self.event_emitter.emit('on_message_reaction_remove_all', data) + case 'MESSAGE_REACTION_REMOVE_EMOJI': + return self.event_emitter.emit('on_message_reaction_remove_emoji', data) + case 'PRESENCE_UPDATE': + return self.event_emitter.emit('on_presence_update', data) + case 'TYPING_START': + return self.event_emitter.emit('on_typing_start', data) + case 'USER_UPDATE': + return self.event_emitter.emit('on_user_update', data) + case 'VOICE_STATE_UPDATE': + return self.event_emitter.emit('on_voice_state_update', data) + case 'VOICE_SERVER_UPDATE': + return self.event_emitter.emit('on_voice_server_update', data) + case 'WEBHOOKS_UPDATE': + return self.event_emitter.emit('on_webhooks_update', data) async def close(self): """ diff --git a/discord/user.py b/discord/user.py index ff86c26..e2625bc 100644 --- a/discord/user.py +++ b/discord/user.py @@ -3,7 +3,7 @@ from discord.premium_type import PremiumType class User: - _id: int + _id: str _username: str _discriminator: str _avatar: str @@ -114,3 +114,6 @@ class User: setattr(self, f"_{k}", PremiumType(data[k])) else: setattr(self, f"_{k}", data[k]) + + def __repr__(self) -> str: + return f"<User id={self.id} username={self.username} discriminator={self.discriminator}>" |