diff --git a/app/dragon-bot.py b/app/dragon-bot.py index d9cbdfbf..39f57701 100644 --- a/app/dragon-bot.py +++ b/app/dragon-bot.py @@ -30,6 +30,7 @@ import stock import tts import wallpaper import weather +import meme_gen # Client object client = discord.Client() @@ -126,7 +127,8 @@ async def on_message(message): embed_description="[Direct Link]({})".format(user.avatar_url.replace('.webp', '.png')) ) ) - + if message.content.startswith('!meme'): + await client.send_message(message.channel, meme_gen.parse_message(message.content)) if message.content.startswith('!birb'): await client.send_message( diff --git a/app/help_methods.py b/app/help_methods.py index 326d0dd9..0c39e438 100644 --- a/app/help_methods.py +++ b/app/help_methods.py @@ -94,6 +94,10 @@ def get_help_message(method): 'You can ask me a question directly and I will do my best to answer it.', '\nUsage: @dragon-bot what is the capital of France?' ], + 'meme': [ + 'Generates a meme on the fly!', + '\nUsage: !meme doge top text; bottom text' + ], 'homepage': [ 'This function now outputs the SWEET-ASS picture of the day.', ' Note this picture only changes once a day.', @@ -132,7 +136,7 @@ def get_help_message(method): def get_help_embed(client): categories = { - 'fun': ['clap', 'birb', 'dog', 'excuse', 'greentext', 'lewd', 'message', 'homepage', 'pout', 'smug', 'quake'], + 'fun': ['clap', 'birb', 'dog', 'excuse', 'greentext', 'lewd', 'message', 'meme', 'homepage', 'pout', 'smug', 'quake'], 'util': ['8ball', 'decide', 'wallpaper', 'weather', 'stock', 'tts', 'issue'], 'users': ['help', 'invite', 'purge', 'roles', 'source', 'minecraft'], 'admin': ['emoji', 'cleanup'] diff --git a/app/meme_gen.py b/app/meme_gen.py new file mode 100644 index 00000000..5d0efc27 --- /dev/null +++ b/app/meme_gen.py @@ -0,0 +1,44 @@ +import os, requests + +supported_templates = sorted(list(map(lambda x: x.split('/')[-1], requests.get('https://memegen.link/api/templates/').json().values()))) + +def parse_message(message): + if len(message.split()) < 2: + return get_templates() + + escaped_chars = {'_' : '__', ' ' : '_', '-' : '--', '?' : '~q', '%' : '~p', '#' : '~h', '/' : '~s', '\'\'' : '\"'} + + # Unwrap message from discord + template = message.split()[1] + text = message.replace('!meme {}'.format(template), '').lstrip().split(',') + upper = text[0].split(';')[0].lstrip() + lower = text[0].split(';')[1].lstrip() + + # Escape special characters in upper/lower text + for char, escaped in escaped_chars.items(): + upper = upper.replace(char, escaped) + lower = lower.replace(char, escaped) + + usesTemplate = True + if template.startswith('http'): + usesTemplate = False + elif template not in supported_templates: + return 'Template not supported!' + + return get_meme_url(template, upper, lower, usesTemplate) + +def get_meme_url(template, upper, lower, usesTemplate): + #TODO: Implement format as a parameter? + base_URL = 'https://memegen.link' + custom_URL = '{}/custom'.format(base_URL) + default_format = 'jpg' + + # Generate meme url + meme = '{}/{}/{}.{}?alt={}'.format(custom_URL, upper, lower, default_format, template) + if usesTemplate: + meme = '{}/{}/{}/{}.{}'.format(base_URL, template, upper, lower, default_format) + return meme + +def get_templates(): + return "You must supply a valid template for the meme. Templates are one of the following: \n```" + ', '.join(supported_templates) + '```' +