From 81991b873da4d36e47b0a8f1aaeb316a69953e44 Mon Sep 17 00:00:00 2001 From: Purpur <43528123+Pemigrade@users.noreply.github.com> Date: Sun, 10 Jan 2021 05:44:25 -0600 Subject: [PATCH 1/5] Create linking_updater.py --- cogs/linking_updater.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 cogs/linking_updater.py diff --git a/cogs/linking_updater.py b/cogs/linking_updater.py new file mode 100644 index 0000000..1831f75 --- /dev/null +++ b/cogs/linking_updater.py @@ -0,0 +1,22 @@ +import os +import discord +import requests +import json +import logging +from discord.ext import commands, tasks +from discord.ext.commands import has_permissions, MissingPermissions +from dotenv import load_dotenv +import aiohttp +import asyncio + +class Linking(commands.Cog): + + def __init__(self, bot): + self.bot = bot + self.guild_id = int(os.getenv('guild_id')) + self.crabwings_role_id = int(os.getenv('crabwings_role_id')) + self.duckfeet_role_id = int(os.getenv('duckfeet_role_id')) + self.elktail_role_id = int(os.getenv('elktail_role_id')) + self.client_role_id = int(os.getenv('client_role_id')) + self.subuser_role_id = int(os.getenv('subuser_role_id')) + self.verified_role_id = int(os.getenv('verified_role_id')) From 77d6348ddb2feb6a91456fafb258a542b7ba5b5c Mon Sep 17 00:00:00 2001 From: Purpur <43528123+Pemigrade@users.noreply.github.com> Date: Sun, 10 Jan 2021 05:48:11 -0600 Subject: [PATCH 2/5] Update linking_updater.py --- cogs/linking_updater.py | 92 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/cogs/linking_updater.py b/cogs/linking_updater.py index 1831f75..fab9894 100644 --- a/cogs/linking_updater.py +++ b/cogs/linking_updater.py @@ -9,7 +9,7 @@ from dotenv import load_dotenv import aiohttp import asyncio -class Linking(commands.Cog): +class Linking_updater(commands.Cog): def __init__(self, bot): self.bot = bot @@ -20,3 +20,93 @@ class Linking(commands.Cog): self.client_role_id = int(os.getenv('client_role_id')) self.subuser_role_id = int(os.getenv('subuser_role_id')) self.verified_role_id = int(os.getenv('verified_role_id')) + + @tasks.loop(minutes=10) + async def linking_updater(self): + logging.info("Synchronizing roles") + file = open('users.json', 'r') + data = json.load(file) + file.close() + guild = self.bot.get_guild(guild_id) + i = -1 + for client in data['users']: + i += 1 + member = guild.get_member(client['discord_id']) + if member: + api_key = client['client_api_key'] + url = "https://panel.birdflop.com/api/client" + headers = { + 'Accept': 'application/json', + 'Authorization': 'Bearer ' + api_key, + } + async with aiohttp.ClientSession() as session: + async with session.get(url, headers=headers) as response: + if response.status == 200: + # Formats response for servers in JSON format + servers_json_response = await response.json() + + user_client = False + user_subuser = False + user_crabwings = False + user_duckfeet = False + user_elktail = False + for server in servers_json_response['data']: + server_owner = server['attributes']['server_owner'] + if server_owner == True: + user_client = True + elif server_owner == False: + user_subuser = True + server_node = server['attributes']['node'] + if server_node == "Crabwings - NYC": + user_crabwings = True + elif server_node == "Duckfeet - EU": + user_duckfeet = True + elif server_node == "Elktail - EU": + user_elktail = True + role = discord.utils.get(guild.roles, id=self.client_role_id) + if user_client == True: + await member.add_roles(role) + else: + await member.remove_roles(role) + role = discord.utils.get(guild.roles, id=self.subuser_role_id) + if user_subuser == True: + await member.add_roles(role) + else: + await member.remove_roles(role) + role = discord.utils.get(guild.roles, id=self.crabwings_role_id) + if user_crabwings == True: + await member.add_roles(role) + else: + await member.remove_roles(role) + role = discord.utils.get(guild.roles, id=self.duckfeet_role_id) + if user_duckfeet == True: + await member.add_roles(role) + else: + await member.remove_roles(role) + role = discord.utils.get(guild.roles, id=self.elktail_role_id) + if user_elktail == True: + await member.add_roles(role) + else: + await member.remove_roles(role) + else: + data['users'].pop(i) + json_dumps = json.dumps(data, indent=2) + file = open('users.json', 'w') + file.write(json_dumps) + file.close() + await member.edit(roles=[]) + logging.info("removed discord_id " + str(client['discord_id']) + " with client_id " + str( + client['client_id']) + " and INVALID client_api_key " + client['client_api_key']) + else: + data['users'].pop(i) + json_dumps = json.dumps(data, indent=2) + file = open('users.json', 'w') + file.write(json_dumps) + file.close() + logging.info("removed discord_id " + str(client['discord_id']) + " with client_id " + str( + client['client_id']) + " and client_api_key " + client['client_api_key']) + + @linking_updater.before_loop + async def before_linking_updater(self): + logging.info('waiting to enter loop') + await bot.wait_until_ready() From d66829b9334b73c9cff860a94e5cb380bc44a3c6 Mon Sep 17 00:00:00 2001 From: Purpur <43528123+Pemigrade@users.noreply.github.com> Date: Sun, 10 Jan 2021 05:48:52 -0600 Subject: [PATCH 3/5] Update bot.py --- bot.py | 83 ---------------------------------------------------------- 1 file changed, 83 deletions(-) diff --git a/bot.py b/bot.py index a6e9485..e3036cd 100644 --- a/bot.py +++ b/bot.py @@ -134,89 +134,6 @@ async def react(ctx, url, reaction): @tasks.loop(minutes=10) async def updater(): - logging.info("Synchronizing roles") - file = open('users.json', 'r') - data = json.load(file) - file.close() - guild = bot.get_guild(guild_id) - i = -1 - for client in data['users']: - i += 1 - member = guild.get_member(client['discord_id']) - if member: - api_key = client['client_api_key'] - url = "https://panel.birdflop.com/api/client" - headers = { - 'Accept': 'application/json', - 'Authorization': 'Bearer ' + api_key, - } - async with aiohttp.ClientSession() as session: - async with session.get(url, headers=headers) as response: - if response.status == 200: - # Formats response for servers in JSON format - servers_json_response = await response.json() - - user_client = False - user_subuser = False - user_crabwings = False - user_duckfeet = False - user_elktail = False - for server in servers_json_response['data']: - server_owner = server['attributes']['server_owner'] - if server_owner == True: - user_client = True - elif server_owner == False: - user_subuser = True - server_node = server['attributes']['node'] - if server_node == "Crabwings - NYC": - user_crabwings = True - elif server_node == "Duckfeet - EU": - user_duckfeet = True - elif server_node == "Elktail - EU": - user_elktail = True - role = discord.utils.get(guild.roles, id=client_role_id) - if user_client == True: - await member.add_roles(role) - else: - await member.remove_roles(role) - role = discord.utils.get(guild.roles, id=subuser_role_id) - if user_subuser == True: - await member.add_roles(role) - else: - await member.remove_roles(role) - role = discord.utils.get(guild.roles, id=crabwings_role_id) - if user_crabwings == True: - await member.add_roles(role) - else: - await member.remove_roles(role) - role = discord.utils.get(guild.roles, id=duckfeet_role_id) - if user_duckfeet == True: - await member.add_roles(role) - else: - await member.remove_roles(role) - role = discord.utils.get(guild.roles, id=elktail_role_id) - if user_elktail == True: - await member.add_roles(role) - else: - await member.remove_roles(role) - else: - data['users'].pop(i) - json_dumps = json.dumps(data, indent=2) - file = open('users.json', 'w') - file.write(json_dumps) - file.close() - await member.edit(roles=[]) - logging.info("removed discord_id " + str(client['discord_id']) + " with client_id " + str( - client['client_id']) + " and INVALID client_api_key " + client['client_api_key']) - else: - data['users'].pop(i) - json_dumps = json.dumps(data, indent=2) - file = open('users.json', 'w') - file.write(json_dumps) - file.close() - logging.info("removed discord_id " + str(client['discord_id']) + " with client_id " + str( - client['client_id']) + " and client_api_key " + client['client_api_key']) - # Update backups logging.info('Ensuring backups') url = "https://panel.birdflop.com/api/application/servers" From 92926922bd96f7deed22dc94152a953245f4c17d Mon Sep 17 00:00:00 2001 From: Purpur <43528123+Pemigrade@users.noreply.github.com> Date: Sun, 10 Jan 2021 05:50:11 -0600 Subject: [PATCH 4/5] debugging --- cogs/linking_updater.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cogs/linking_updater.py b/cogs/linking_updater.py index fab9894..0e01d69 100644 --- a/cogs/linking_updater.py +++ b/cogs/linking_updater.py @@ -42,6 +42,7 @@ class Linking_updater(commands.Cog): async with aiohttp.ClientSession() as session: async with session.get(url, headers=headers) as response: if response.status == 200: + logging.info("200") # Formats response for servers in JSON format servers_json_response = await response.json() From 00114458000ad5470865bb1eb6e298480632a81b Mon Sep 17 00:00:00 2001 From: Purpur <43528123+Pemigrade@users.noreply.github.com> Date: Sun, 10 Jan 2021 05:51:23 -0600 Subject: [PATCH 5/5] Update linking_updater.py --- cogs/linking_updater.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cogs/linking_updater.py b/cogs/linking_updater.py index 0e01d69..672cace 100644 --- a/cogs/linking_updater.py +++ b/cogs/linking_updater.py @@ -29,10 +29,13 @@ class Linking_updater(commands.Cog): file.close() guild = self.bot.get_guild(guild_id) i = -1 + print("1") for client in data['users']: + print("2") i += 1 member = guild.get_member(client['discord_id']) if member: + print("3") api_key = client['client_api_key'] url = "https://panel.birdflop.com/api/client" headers = { @@ -41,8 +44,9 @@ class Linking_updater(commands.Cog): } async with aiohttp.ClientSession() as session: async with session.get(url, headers=headers) as response: + print("4") if response.status == 200: - logging.info("200") + print("5") # Formats response for servers in JSON format servers_json_response = await response.json()