From e1b18661cb60de7f7d997832f8cc78dcd22fc703 Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Wed, 7 Dec 2022 22:04:08 +0100 Subject: [PATCH] refactor imgflip calls --- Shitpost.py | 121 ++++++++++++++++++++++------------------------------ 1 file changed, 51 insertions(+), 70 deletions(-) diff --git a/Shitpost.py b/Shitpost.py index 1cb1300..b963f18 100644 --- a/Shitpost.py +++ b/Shitpost.py @@ -33,6 +33,13 @@ class Shitpost(commands.Cog): 'clownsfiguur','krotenkoker','hansworst','grappenmaker','flapdrol','oen', 'achterlijk gebakje','demente dibbes','geitenbreier','koekwous','sufkop'] self.caldav_client = caldav.DAVClient(url=os.getenv('STROOP_CALDAV_HOST')) + self.imgflips = { + 'tering': '78416340', + 'mark': '112898841', + 'eriku': '93296473', + 'tegeltje': '121572521', + 'zout': '116231806' + } warnings.simplefilter("ignore") @commands.command(pass_context=True, hidden=False) @@ -259,6 +266,25 @@ class Shitpost(commands.Cog): else: yield from ctx.channel.send(ctx.author.mention + ' helaas, geen quote gevonden.') + def get_imgflip_embed(self, template: str, boven: str, onder: str, extra_options: dict = None): + params = dict( + template_id = template, + username = 'Stroopwafel', + password = os.getenv("STROOP_IMGFLIP_PASSWORD"), + text0 = boven, + text1 = onder + ) + if extra_options: + params.update(extra_options) + resp = requests.get(url='https://api.imgflip.com/caption_image', params=params) + if resp: + photourl = resp.json()['data']['url'] + em = discord.Embed(type='photo') + em.set_image(url=photourl) + return em + return None + + @commands.command(pass_context=True, hidden=True) @asyncio.coroutine def tering(self, ctx, boven: str, onder: str): @@ -266,23 +292,13 @@ class Shitpost(commands.Cog): Bijvoorbeeld: !tering \"Vanavond\" \"gaan we helemaal naar de tering\" """ - params = dict( - template_id = '78416340', #party hard - username = 'Stroopwafel', - password = os.getenv("STROOP_IMGFLIP_PASSWORD"), - text0 = boven, - text1 = onder - ) - resp = requests.get(url='https://api.imgflip.com/caption_image', params=params) - #todo check for success - photourl = resp.json()['data']['url'] - em = discord.Embed(type='photo') - em.set_image(url=photourl) + em = self.get_imgflip_embed(self.imgflips['tering'], boven, onder) try: yield from ctx.message.delete() except: print("No permission to delete message") - yield from ctx.channel.send(ctx.author.mention, embed=em) + if em: + yield from ctx.channel.send(ctx.author.mention, embed=em) @commands.command(pass_context=True, hidden=True) @asyncio.coroutine @@ -291,23 +307,13 @@ class Shitpost(commands.Cog): Bijvoorbeeld: !mark \"je moeder\" \"is een hoer\" """ - params = dict( - template_id = '112898841', #de marku - username = 'Stroopwafel', - password = os.getenv("STROOP_IMGFLIP_PASSWORD"), - text0 = boven, - text1 = onder - ) - resp = requests.get(url='https://api.imgflip.com/caption_image', params=params) - #todo check for success - photourl = resp.json()['data']['url'] - em = discord.Embed(title='De Mark heeft gesproken.', type='photo') - em.set_image(url=photourl) + em = self.get_imgflip_embed(self.imgflips['mark'], boven, onder) try: yield from ctx.message.delete() except: print("No permission to delete message") - yield from ctx.channel.send(ctx.author.mention, embed=em) + if em: + yield from ctx.channel.send(ctx.author.mention, embed=em) @commands.command(pass_context=True, hidden=True) @asyncio.coroutine @@ -316,36 +322,16 @@ class Shitpost(commands.Cog): Bijvoorbeeld: !eriku \"je moeder\" \"is een hoer\" """ - params = dict( - template_id = '93296473', #de eriku - username = 'Stroopwafel', - password = os.getenv('STROOP_IMGFLIP_PASSWORD'), - text0 = boven, - text1 = onder - ) - resp = requests.get(url='https://api.imgflip.com/caption_image', params=params) - #todo check for success - photourl = resp.json()['data']['url'] - em = discord.Embed(title='De Eriku heeft gesproken. Prijs hem!', type='photo') - em.set_image(url=photourl) + em = self.get_imgflip_embed(self.imgflips['eriku'], boven, onder) try: yield from ctx.message.delete() except: print("No permission to delete message") - yield from ctx.channel.send(ctx.author.mention, embed=em) - - @commands.command(pass_context=True, hidden=True) - @asyncio.coroutine - def tegeltje(self, ctx, tekst: str, auteur: str=" "): - """Maak een mooi tegeltje met volkswijsheid - Bijvoorbeeld: - !tegeltje \"Oost west, 420 best\" \"Lourens\" - """ + if em: + yield from ctx.channel.send(ctx.author.mention, embed=em) + def get_tegeltje(self, tekst, auteur): params = { - 'template_id': '121572521', #delfts blauw - 'username': 'Stroopwafel', - 'password': os.getenv('STROOP_IMGFLIP_PASSWORD'), 'text0': 'dummy0', 'text1': 'dummy1', 'font': 'arial', @@ -365,17 +351,22 @@ class Shitpost(commands.Cog): 'boxes[1][color]': '#0044cc', 'boxes[1][outline_color]': '#ffffff' } - - resp = requests.get(url='https://api.imgflip.com/caption_image', params=params) - #todo check for success - photourl = resp.json()['data']['url'] - em = discord.Embed(title='Tegeltjeswijsheid:', type='photo') - em.set_image(url=photourl) + return self.get_imgflip_embed(self.imgflips['tegeltje'], "", "", params) + + @commands.command(pass_context=True, hidden=True) + @asyncio.coroutine + def tegeltje(self, ctx, tekst: str, auteur: str=" "): + """Maak een mooi tegeltje met volkswijsheid + Bijvoorbeeld: + !tegeltje \"Oost west, 420 best\" \"Lourens\" + """ + em = get_tegeltje(tekst, auteur) try: yield from ctx.message.delete() except: print("No permission to delete message") - yield from ctx.channel.send(ctx.author.mention, embed=em) + if em: + yield from ctx.channel.send(ctx.author.mention, embed=em) @commands.command(pass_context=True, hidden=True) @asyncio.coroutine @@ -384,23 +375,13 @@ class Shitpost(commands.Cog): Bijvoorbeeld: !zout \"je moeder\" \"is een hoer\" """ - params = dict( - template_id = '116231806', #de martijn - username = 'Stroopwafel', - password = os.getenv('STROOP_IMGFLIP_PASSWORD'), - text0 = boven, - text1 = onder - ) - resp = requests.get(url='https://api.imgflip.com/caption_image', params=params) - #todo check for success - photourl = resp.json()['data']['url'] - em = discord.Embed(title='De Martijn heeft gesproken.', type='photo') - em.set_image(url=photourl) + em = self.get_imgflip_embed(self.imgflips['zout'], boven, onder) try: yield from ctx.message.delete() except: print("No permission to delete message") - yield from ctx.channel.send(ctx.author.mention, embed=em) + if em: + yield from ctx.channel.send(ctx.author.mention, embed=em) @commands.command(pass_context=True, hidden=True) @asyncio.coroutine