From 6cf7752a10eacf374c54e9d8539889f23f765733 Mon Sep 17 00:00:00 2001 From: thec0sm0s Date: Thu, 14 May 2020 21:14:09 +0530 Subject: [PATCH] Add get_from_cache classmethod --- flask_discord/models/user.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/flask_discord/models/user.py b/flask_discord/models/user.py index fd53c7b..c44dc76 100644 --- a/flask_discord/models/user.py +++ b/flask_discord/models/user.py @@ -91,7 +91,7 @@ class User(DiscordModelsBase): @classmethod def fetch_from_api(cls, guilds=True, connections=False): - """A class method which returns an instance or list of instances of this model by implicitly making an + """A class method which returns an instance of this model by implicitly making an API call to Discord. Parameters @@ -119,6 +119,20 @@ class User(DiscordModelsBase): return self + @classmethod + def get_from_cache(cls): + """A class method which returns an instance of this model if it exists in internal cache. + + Returns + ------- + flask_discord.User + An user instance if it exists in internal cache. + None + If the current doesn't exists in internal cache. + + """ + return current_app.discord.users_cache.get(session.get("DISCORD_USER_ID")) + def add_to_guild(self, guild_id) -> dict: """Method to add user to the guild, provided OAuth2 session has already been created with ``guilds.join`` scope.