mirror of https://github.com/stijndcl/didier
Clean memegen code up more, add more memes (#15)
parent
77addbc30c
commit
7c47c6af73
56
cogs/fun.py
56
cogs/fun.py
|
@ -3,10 +3,10 @@ from decorators import help
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from enums.help_categories import Category
|
from enums.help_categories import Category
|
||||||
from functions import checks, mock, stringFormatters
|
from functions import checks, stringFormatters
|
||||||
from functions.database import memes, trump, dadjoke
|
from functions.database import memes, trump, dadjoke
|
||||||
|
from functions.memes import generate
|
||||||
import json
|
import json
|
||||||
import os
|
|
||||||
import random
|
import random
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
@ -100,54 +100,14 @@ class Fun(commands.Cog):
|
||||||
# Convert to list to support item assignment
|
# Convert to list to support item assignment
|
||||||
fields = list(fields)
|
fields = list(fields)
|
||||||
|
|
||||||
# If there's only one field, the user isn't required to use quotes
|
generated = generate(result, fields)
|
||||||
if result.fields == 1:
|
|
||||||
fields = [" ".join(fields)]
|
|
||||||
|
|
||||||
# Apply mock to mocking spongebob memes
|
# If the request was successful, remove the message calling it
|
||||||
if result.name == "mocking spongebob":
|
if generated["success"]:
|
||||||
fields = list(map(mock.mock, fields))
|
|
||||||
|
|
||||||
# X, X everywhere only takes X as an argument
|
|
||||||
if result.name == "x, x everywhere":
|
|
||||||
fields[0] = " ".join(fields)
|
|
||||||
fields.append(fields[0] + " everywhere")
|
|
||||||
|
|
||||||
# List of fields to send to the API
|
|
||||||
boxes = [{"text": ""}, {"text": ""}, {"text": ""}, {"text": ""}]
|
|
||||||
|
|
||||||
# Add all fields required & ignore the excess ones
|
|
||||||
for i in range(len(fields)):
|
|
||||||
if i > 3:
|
|
||||||
break
|
|
||||||
boxes[i]["text"] = fields[i]
|
|
||||||
|
|
||||||
# Check server status
|
|
||||||
req = requests.get('https://api.imgflip.com/get_memes').json()
|
|
||||||
|
|
||||||
if req["success"]:
|
|
||||||
caption = {
|
|
||||||
"template_id": result.meme_id,
|
|
||||||
"username": os.getenv("IMGFLIPNAME"),
|
|
||||||
"password": os.getenv("IMGFLIPPASSWORD"),
|
|
||||||
"boxes[0][text]": boxes[0]["text"],
|
|
||||||
"boxes[1][text]": boxes[1]["text"],
|
|
||||||
"boxes[2][text]": boxes[2]["text"],
|
|
||||||
"boxes[3][text]": boxes[3]["text"]
|
|
||||||
}
|
|
||||||
|
|
||||||
# Send the POST to the API
|
|
||||||
memeReply = requests.post('https://api.imgflip.com/caption_image', caption).json()
|
|
||||||
|
|
||||||
if memeReply['success']:
|
|
||||||
await ctx.send(str(memeReply['data']['url']))
|
|
||||||
await self.utilsCog.removeMessage(ctx.message)
|
await self.utilsCog.removeMessage(ctx.message)
|
||||||
else:
|
|
||||||
await ctx.send(
|
# Send the meme's url or the error message
|
||||||
"Error! Controleer of je de juiste syntax hebt gebruikt. Gebruik het commando "
|
await ctx.send(generated["message"])
|
||||||
"\"memes\" voor een lijst aan geaccepteerde meme-namen.")
|
|
||||||
else:
|
|
||||||
await ctx.send("Er is een fout opgetreden.")
|
|
||||||
|
|
||||||
@commands.command(name="Memes")
|
@commands.command(name="Memes")
|
||||||
@commands.check(checks.allowedChannels)
|
@commands.check(checks.allowedChannels)
|
||||||
|
|
|
@ -0,0 +1,105 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from functions.database.memes import Meme
|
||||||
|
from functions.mock import mock
|
||||||
|
|
||||||
|
|
||||||
|
def generate(meme: Meme, fields):
|
||||||
|
"""
|
||||||
|
Main function that takes a Meme as input & generates an image.
|
||||||
|
"""
|
||||||
|
# If there's only one field, the user isn't required to use quotes
|
||||||
|
if meme.fields == 1:
|
||||||
|
fields = [" ".join(fields)]
|
||||||
|
|
||||||
|
fields = _applyMeme(meme, fields)
|
||||||
|
|
||||||
|
# List of fields to send to the API
|
||||||
|
boxes = [{"text": ""}, {"text": ""}, {"text": ""}, {"text": ""}]
|
||||||
|
|
||||||
|
# Add all fields required & ignore the excess ones
|
||||||
|
for i in range(len(fields)):
|
||||||
|
if i > 3:
|
||||||
|
break
|
||||||
|
boxes[i]["text"] = fields[i]
|
||||||
|
|
||||||
|
# Check server status
|
||||||
|
req = requests.get('https://api.imgflip.com/get_memes').json()
|
||||||
|
|
||||||
|
# Server is down
|
||||||
|
if not req["success"]:
|
||||||
|
return {"success": False, "message": "Er is een fout opgetreden."}
|
||||||
|
|
||||||
|
# Post meme
|
||||||
|
reply = _postMeme(meme, boxes)
|
||||||
|
|
||||||
|
# Adding a message parameter makes the code in the cog a lot cleaner
|
||||||
|
if not reply["success"]:
|
||||||
|
reply["message"] = "Error! Controleer of je de juiste syntax hebt gebruikt. Gebruik het commando " \
|
||||||
|
"\"memes\" voor een lijst aan geaccepteerde meme-namen."
|
||||||
|
else:
|
||||||
|
reply["message"] = reply["data"]["url"]
|
||||||
|
|
||||||
|
return reply
|
||||||
|
|
||||||
|
|
||||||
|
def _postMeme(meme: Meme, boxes):
|
||||||
|
"""
|
||||||
|
Performs API request to generate the meme
|
||||||
|
"""
|
||||||
|
caption = {
|
||||||
|
"template_id": meme.meme_id,
|
||||||
|
"username": os.getenv("IMGFLIPNAME"),
|
||||||
|
"password": os.getenv("IMGFLIPPASSWORD"),
|
||||||
|
"boxes[0][text]": boxes[0]["text"],
|
||||||
|
"boxes[1][text]": boxes[1]["text"],
|
||||||
|
"boxes[2][text]": boxes[2]["text"],
|
||||||
|
"boxes[3][text]": boxes[3]["text"]
|
||||||
|
}
|
||||||
|
|
||||||
|
# Send the POST to the API
|
||||||
|
memeReply = requests.post('https://api.imgflip.com/caption_image', caption).json()
|
||||||
|
|
||||||
|
return memeReply
|
||||||
|
|
||||||
|
|
||||||
|
def _applyMeme(meme: Meme, fields):
|
||||||
|
"""
|
||||||
|
Some memes are in a special format that only requires
|
||||||
|
a few words to be added, or needs the input to be changed.
|
||||||
|
This function handles all that.
|
||||||
|
|
||||||
|
Callbacks contains a function that modifies the input
|
||||||
|
"""
|
||||||
|
memeDict = {
|
||||||
|
102156234: _mockingSpongebob,
|
||||||
|
91538330: _xXEverywhere,
|
||||||
|
252600902: _alwaysHasBeen
|
||||||
|
}
|
||||||
|
|
||||||
|
# Meme needs no special treatment
|
||||||
|
if meme.meme_id not in memeDict:
|
||||||
|
return fields
|
||||||
|
|
||||||
|
return memeDict[meme.meme_id](fields)
|
||||||
|
|
||||||
|
|
||||||
|
def _mockingSpongebob(fields):
|
||||||
|
for i, field in enumerate(fields):
|
||||||
|
fields[i] = mock(field)
|
||||||
|
|
||||||
|
return fields
|
||||||
|
|
||||||
|
|
||||||
|
def _xXEverywhere(fields):
|
||||||
|
word = fields[0]
|
||||||
|
|
||||||
|
return ["{}".format(word), "{} everywhere".format(word)]
|
||||||
|
|
||||||
|
|
||||||
|
def _alwaysHasBeen(fields):
|
||||||
|
word = fields[0]
|
||||||
|
|
||||||
|
return ["Wait, it's all {}?".format(word), "Always has been"]
|
Loading…
Reference in New Issue