From d14f9889ebf3c452fd54c37af0da10fbd01d80a6 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Sun, 8 Jul 2018 20:31:36 +0000 Subject: [PATCH] Try manually adding member if not exist in db for webapp --- webapp/titanembeds/discordrest.py | 5 +++++ webapp/titanembeds/utils.py | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/webapp/titanembeds/discordrest.py b/webapp/titanembeds/discordrest.py index 0ecb4c4..d1b342f 100644 --- a/webapp/titanembeds/discordrest.py +++ b/webapp/titanembeds/discordrest.py @@ -124,6 +124,11 @@ class DiscordREST: _endpoint = "/guilds/{guild_id}/embed".format(guild_id=guild_id) r = self.request("GET", _endpoint) return r + + def get_guild_member(self, guild_id, user_id): + _endpoint = "/guilds/{guild_id}/members/{user_id}".format(guild_id=guild_id, user_id=user_id) + r = self.request("GET", _endpoint) + return r def modify_guild_embed(self, guild_id, **kwargs): _endpoint = "/guilds/{guild_id}/embed".format(guild_id=guild_id) diff --git a/webapp/titanembeds/utils.py b/webapp/titanembeds/utils.py index 7c4f7a5..7a75654 100644 --- a/webapp/titanembeds/utils.py +++ b/webapp/titanembeds/utils.py @@ -195,7 +195,17 @@ def check_user_in_guild(guild_id): def get_member_roles(guild_id, user_id): q = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id).filter(GuildMembers.user_id == user_id).first() - return json.loads(q.roles) + roles = [guild_id] + if not q: + member = discord_api.get_guild_member(guild_id, user_id) + if member["success"]: + roles = list(map(int, member["content"]["roles"])) + member = GuildMembers(guild_id, user_id, member["content"]["user"]["username"], int(member["content"]["user"]["discriminator"]), member["content"].get("nick", None), member["content"]["user"]["avatar"], True, False, roles) + db.session.add(member) + db.session.commit() + else: + roles = json.loads(q.roles) + return roles def get_guild_channels(guild_id, force_everyone=False): if user_unauthenticated() or force_everyone: