From b6b92217c94c82d82ea3dc9e089dbb68a61b5307 Mon Sep 17 00:00:00 2001 From: Purpur <43528123+Pemigrade@users.noreply.github.com> Date: Thu, 14 Jan 2021 04:43:23 -0600 Subject: [PATCH] Make bot behave differently depending on whether it's running on panel --- bot.py | 169 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 92 insertions(+), 77 deletions(-) diff --git a/bot.py b/bot.py index 00b40f2..6099f1c 100644 --- a/bot.py +++ b/bot.py @@ -27,6 +27,7 @@ guild_id = int(os.getenv('guild_id')) verification_channel = int(os.getenv('verification_channel')) verification_message = int(os.getenv('verification_message')) application_api_key = os.getenv('application_api_key') +running_on_panel = bool(os.getenv('running_on_panel')) logging.basicConfig(filename='console.log', level=logging.INFO, @@ -44,91 +45,97 @@ async def on_ready(): @bot.event async def on_message(message): + if running_on_panel == False: # Account link - # Binflop - if len(message.attachments) > 0: - if not message.attachments[0].url.endswith( - ('.png', '.jpg', '.jpeg', '.mp4', '.mov', '.avi', '.gif', '.image')): - download = message.attachments[0].url - async with aiohttp.ClientSession() as session: - async with session.get(download, allow_redirects=True) as r: - -# r = requests.get(download, allow_redirects=True) - text = await r.text() - text = "\n".join(text.splitlines()) - if '�' not in text: # If it's not an image/gif - truncated = False - if len(text) > 100000: - text = text[:99999] - truncated = True - req = requests.post('https://bin.birdflop.com/documents', data=text) - key = json.loads(req.content)['key'] - response = "" - response = response + "https://bin.birdflop.com/" + key - response = response + "\nRequested by " + message.author.mention - if truncated: - response = response + "\n(file was truncated because it was too long.)" - embed_var = discord.Embed(title="Please use a paste service", color=0x1D83D4) - embed_var.description = response - await message.channel.send(embed=embed_var) - timings = bot.get_cog('Timings') - await timings.analyze_timings(message) + # Binflop + if len(message.attachments) > 0: + if message.attachments[0].url.endswith( + ('.png', '.jpg', '.jpeg', '.mp4', '.mov', '.avi', '.gif', '.image')) == False: + download = message.attachments[0].url + async with aiohttp.ClientSession() as session: + async with session.get(download, allow_redirects=True) as r: + + # r = requests.get(download, allow_redirects=True) + text = await r.text() + text = "\n".join(text.splitlines()) + if '�' not in text: # If it's not an image/gif + truncated = False + if len(text) > 100000: + text = text[:99999] + truncated = True + req = requests.post('https://bin.birdflop.com/documents', data=text) + key = json.loads(req.content)['key'] + response = "" + response = response + "https://bin.birdflop.com/" + key + response = response + "\nRequested by " + message.author.mention + if truncated: + response = response + "\n(file was truncated because it was too long.)" + embed_var = discord.Embed(title="Please use a paste service", color=0x1D83D4) + embed_var.description = response + await message.channel.send(embed=embed_var) + timings = bot.get_cog('Timings') + await timings.analyze_timings(message) await bot.process_commands(message) @bot.event async def on_raw_reaction_add(payload): - global verification_message - global verification_channel - if payload.message_id != verification_message: - return - if payload.user_id == bot.user.id: - return - # Remove the reaction - guild = discord.utils.get(bot.guilds, id=guild_id) - verification_channel_obj = await bot.fetch_channel(verification_channel) - verification_message_obj = await verification_channel_obj.fetch_message(verification_message) - member = guild.get_member(payload.user_id) - await verification_message_obj.remove_reaction(payload.emoji, member) - if str(payload.emoji) == "✅": - await member.send( - "Hey there! It looks like you'd like to verify your account. I'm here to help you with that!\n\nIf you're confused at any point, see https://birdflop.com/verification for a tutorial.\n\nWith that said, let's get started! You'll want to start by grabbing some API credentials for your account by signing into https://panel.birdflop.com. Head over to the **Account** section in the top right, then click on the **API Credentials tab**. You'll want to create an API key with description `Verification` and `172.18.0.2` in the **Allowed IPs section**.\n\nWhen you finish entering the necessary information, hit the blue **Create **button.\n\nNext, you'll want to copy your API credentials. After clicking **Create**, you'll receive a long string. Copy it with `ctrl+c` (`cmnd+c` on Mac) or by right-clicking it and selecting **Copy**.\n\nIf you click on the **Close **button before copying the API key, no worries! Delete your API key and create a new one with the same information.\n\nFinally, direct message your API key to Botflop: that's me!\n\nTo verify that you are messaging the key to the correct user, please ensure that the my ID is `Botflop#2403` and that my username is marked with a blue **BOT** badge. Additionally, the only server under the **Mutual Servers** tab should be Birdflop Hosting.\n\nAfter messaging me your API key, you should receive a success message. If you do not receive a success message, please create a ticket in the Birdflop Discord's #support channel.") - logging.info("sent verification challenge to " + member.name + "#" + str(member.discriminator) + " (" + str( - member.id) + ")") - else: - file = open('users.json', 'r') - data = json.load(file) - file.close() - i = 0 - j = -1 - for client in data['users']: - j += 1 - if client['discord_id'] == member.id: - data['users'].pop(j) - i = 1 - if i == 1: - json_dumps = json.dumps(data, indent=2) - file = open('users.json', 'w') - file.write(json_dumps) + if running_on_panel == True: + global verification_message + global verification_channel + if payload.message_id != verification_message: + return + if payload.user_id == bot.user.id: + return + # Remove the reaction + guild = discord.utils.get(bot.guilds, id=guild_id) + verification_channel_obj = await bot.fetch_channel(verification_channel) + verification_message_obj = await verification_channel_obj.fetch_message(verification_message) + member = guild.get_member(payload.user_id) + await verification_message_obj.remove_reaction(payload.emoji, member) + if str(payload.emoji) == "✅": + await member.send( + "Hey there! It looks like you'd like to verify your account. I'm here to help you with that!\n\nIf you're confused at any point, see https://birdflop.com/verification for a tutorial.\n\nWith that said, let's get started! You'll want to start by grabbing some API credentials for your account by signing into https://panel.birdflop.com. Head over to the **Account** section in the top right, then click on the **API Credentials tab**. You'll want to create an API key with description `Verification` and `172.18.0.2` in the **Allowed IPs section**.\n\nWhen you finish entering the necessary information, hit the blue **Create **button.\n\nNext, you'll want to copy your API credentials. After clicking **Create**, you'll receive a long string. Copy it with `ctrl+c` (`cmnd+c` on Mac) or by right-clicking it and selecting **Copy**.\n\nIf you click on the **Close **button before copying the API key, no worries! Delete your API key and create a new one with the same information.\n\nFinally, direct message your API key to Botflop: that's me!\n\nTo verify that you are messaging the key to the correct user, please ensure that the my ID is `Botflop#2403` and that my username is marked with a blue **BOT** badge. Additionally, the only server under the **Mutual Servers** tab should be Birdflop Hosting.\n\nAfter messaging me your API key, you should receive a success message. If you do not receive a success message, please create a ticket in the Birdflop Discord's #support channel.") + logging.info("sent verification challenge to " + member.name + "#" + str(member.discriminator) + " (" + str( + member.id) + ")") + else: + file = open('users.json', 'r') + data = json.load(file) file.close() - await member.edit(roles=[]) - await member.send("Your Discord account has successfully been unlinked from your Panel account!") - logging.info( - 'successfully unlinked ' + member.name + "#" + str(member.discriminator) + " (" + str(member.id) + ")") + i = 0 + j = -1 + for client in data['users']: + j += 1 + if client['discord_id'] == member.id: + data['users'].pop(j) + i = 1 + if i == 1: + json_dumps = json.dumps(data, indent=2) + file = open('users.json', 'w') + file.write(json_dumps) + file.close() + await member.edit(roles=[]) + await member.send("Your Discord account has successfully been unlinked from your Panel account!") + logging.info( + 'successfully unlinked ' + member.name + "#" + str(member.discriminator) + " (" + str(member.id) + ")") @bot.command() async def ping(ctx): - await ctx.send(f'Bot ping is {round(bot.latency * 1000)}ms') + if running_on_panel == True: + await ctx.send(f'Private bot ping is {round(bot.latency * 1000)}ms') + if running_on_panel == False: + await ctx.send(f'Public bot ping is {round(bot.latency * 1000)}ms') @bot.command(name="react", pass_context=True) @has_permissions(administrator=True) async def react(ctx, url, reaction): - channel = await bot.fetch_channel(int(url.split("/")[5])) - message = await channel.fetch_message(int(url.split("/")[6])) - await message.add_reaction(reaction) - logging.info('reacted to ' + url + ' with ' + reaction) + if running_on_panel == False: + channel = await bot.fetch_channel(int(url.split("/")[5])) + message = await channel.fetch_message(int(url.split("/")[6])) + await message.add_reaction(reaction) + logging.info('reacted to ' + url + ' with ' + reaction) @tasks.loop(minutes=10) @@ -157,10 +164,10 @@ async def updater(): i += 1 already_exists = False for server2 in modified_servers['servers']: - if not already_exists: + if already_exists == False: if server['attributes']['uuid'] == server2['uuid']: already_exists = True - if not already_exists: + if already_exists == False: headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', @@ -175,6 +182,7 @@ async def updater(): server['attributes']['feature_limits']['databases']) + ', "allocations": ' + str( server['attributes']['feature_limits']['allocations']) + ', "backups": 3 } }' + async with aiohttp.ClientSession() as session: async with session.patch('https://panel.birdflop.com/api/application/servers/' + str(server['attributes']['id']) + '/build', headers=headers, data=data) as response: if response.status == 200: @@ -257,13 +265,20 @@ async def before_updater(): await bot.wait_until_ready() -for file_name in os.listdir('./cogs'): - if file_name.endswith('.py'): - bot.load_extension(f'cogs.{file_name[:-3]}') +if running_on_panel == True: + for file_name in os.listdir('./cogs'): + if file_name.endswith('_panel.py'): + bot.load_extension(f'cogs.{file_name[:-3]}') +else: + for file_name in os.listdir('./cogs'): + if file_name.endswith('_public.py'): + bot.load_extension(f'cogs.{file_name[:-3]}') + +if running_on_panel == True: + updater.start() + linking_updater = bot.get_cog('Linking_updater') + linking_updater.linking_updater.start() -updater.start() -linking_updater = bot.get_cog('Linking_updater') -linking_updater.linking_updater.start() bot.run(token) # full name: message.author.name + "#" + str(message.author.discriminator) + " (" + str(message.author.id) + ")"