Finishing jtans memegen. Fixes #28

This commit is contained in:
Luke Robles 2019-01-03 16:15:26 -08:00
parent cf37b39379
commit 7f7954bc03
3 changed files with 52 additions and 2 deletions

View File

@ -30,6 +30,7 @@ import stock
import tts import tts
import wallpaper import wallpaper
import weather import weather
import meme_gen
# Client object # Client object
client = discord.Client() client = discord.Client()
@ -126,7 +127,8 @@ async def on_message(message):
embed_description="[Direct Link]({})".format(user.avatar_url.replace('.webp', '.png')) 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'): if message.content.startswith('!birb'):
await client.send_message( await client.send_message(

View File

@ -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.', '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?' '\nUsage: @dragon-bot what is the capital of France?'
], ],
'meme': [
'Generates a meme on the fly!',
'\nUsage: !meme doge top text; bottom text'
],
'homepage': [ 'homepage': [
'This function now outputs the SWEET-ASS picture of the day.', 'This function now outputs the SWEET-ASS picture of the day.',
' Note this picture only changes once a day.', ' Note this picture only changes once a day.',
@ -132,7 +136,7 @@ def get_help_message(method):
def get_help_embed(client): def get_help_embed(client):
categories = { 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'], 'util': ['8ball', 'decide', 'wallpaper', 'weather', 'stock', 'tts', 'issue'],
'users': ['help', 'invite', 'purge', 'roles', 'source', 'minecraft'], 'users': ['help', 'invite', 'purge', 'roles', 'source', 'minecraft'],
'admin': ['emoji', 'cleanup'] 'admin': ['emoji', 'cleanup']

44
app/meme_gen.py Normal file
View File

@ -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) + '```'