diff --git a/Gwendolyn.py b/Gwendolyn.py index 5d366e6..7506c08 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -16,6 +16,7 @@ class Credentials(): self.wordnikKey = data[2][16:].replace(" ","") self.mongoDBUser = data[3][13:].replace(" ","") self.mongoDBPassword = data[4][17:].replace(" ","") + self.wolfKey = data[5][19:].replace(" ","") class Options(): def __init__(self): diff --git a/cogs/MiscCog.py b/cogs/MiscCog.py index 3de075e..1d6449d 100644 --- a/cogs/MiscCog.py +++ b/cogs/MiscCog.py @@ -1,7 +1,8 @@ -import discord, codecs, string +import discord, codecs, string, wolframalpha, requests, os from discord.ext import commands from funcs import logThis, helloFunc, roll_dice, imageFunc, nameGen, tavernGen, movieFunc, cap, findWikiPage +from PIL import Image, ImageDraw, ImageFont class MiscCog(commands.Cog): @@ -118,5 +119,76 @@ class MiscCog(commands.Cog): else: await ctx.send(content) + #Looks up on Wolfram Alpha + @commands.command() + async def wolf(self, ctx, *, content): + fnt = ImageFont.truetype('resources/times-new-roman.ttf', 20) + async with ctx.message.channel.typing(): + logThis("Requesting data") + client = wolframalpha.Client(self.client.credentials.wolfKey) + res = client.query(content) + + logThis("Processing data") + titles = [] + pods = [] + for pod in res.pods: + titles += [pod.title] + for x, sub in enumerate(pod.subpods): + pods += [sub] + if x > 0: + titles += [""] + + podChunks = [pods[x:x+2] for x in range(0, len(pods), 2)] + titleChucks = [titles[x:x+2] for x in range(0, len(titles), 2)] + + for x, chunk in enumerate(podChunks): + width = 0 + for title in titleChucks[x]: + width = max(width,fnt.getsize(title)[0]) + height = 5 + heights = [] + for count, pod in enumerate(chunk): + heights += [height] + width = max(width,int(list(pod.img)[0]["@width"])) + if titleChucks[x][count] == "": + placeForText = 0 + else: + placeForText = 30 + height += int(list(pod.img)[0]["@height"]) + 10 + placeForText + + width += 10 + height += 5 + wolfImage = Image.new("RGB",(width,height),color=(255,255,255)) + + for count, pod in enumerate(chunk): + response = requests.get(list(pod.img)[0]["@src"]) + file = open("resources/wolfTemp.png", "wb") + file.write(response.content) + file.close() + oldImage = Image.open("resources/wolfTemp.png") + oldSize = oldImage.size + if titleChucks[x][count] == "": + placeForText = 0 + else: + placeForText = 30 + newSize = (width,int(oldSize[1]+10+placeForText)) + newImage = Image.new("RGB",newSize,color=(255,255,255)) + newImage.paste(oldImage, (int((int(oldSize[0]+10)-oldSize[0])/2),int(((newSize[1]-placeForText)-oldSize[1])/2)+placeForText)) + if titleChucks[x][count] != "": + d = ImageDraw.Draw(newImage,"RGB") + d.text((5,7),titleChucks[x][count],font=fnt,fill=(150,150,150)) + + wolfImage.paste(newImage,(0,heights[count])) + newImage.close() + oldImage.close() + count += 1 + + wolfImage.save("resources/wolf.png") + wolfImage.close() + await ctx.message.channel.send(file = discord.File("resources/wolf.png")) + + os.remove("resources/wolf.png") + os.remove("resources/wolfTemp.png") + def setup(client): client.add_cog(MiscCog(client)) diff --git a/funcs/miscFuncs.py b/funcs/miscFuncs.py index 4356de4..43af7ea 100644 --- a/funcs/miscFuncs.py +++ b/funcs/miscFuncs.py @@ -184,7 +184,7 @@ def replaceMultiple(mainString, toBeReplaces, newString): # Replace the string mainString = mainString.replace(elem, newString) - return mainString + return mainString def emojiToCommand(emoji): if emoji == "1️⃣": diff --git a/resources/help/help-wolf.txt b/resources/help/help-wolf.txt new file mode 100644 index 0000000..a3dc722 --- /dev/null +++ b/resources/help/help-wolf.txt @@ -0,0 +1 @@ +Lader dig slå ting op på Wolfram Alpha. \ No newline at end of file diff --git a/resources/help/help.txt b/resources/help/help.txt index 920f9e1..52fba13 100644 --- a/resources/help/help.txt +++ b/resources/help/help.txt @@ -36,4 +36,6 @@ `!hangman` - Lader dig spille et spil hangman. +`!wolf` - Lader dig slå ting op på Wolfram Alpha. + Du kan få ekstra information om kommandoerne med "!help [kommando]". diff --git a/resources/times-new-roman.ttf b/resources/times-new-roman.ttf new file mode 100644 index 0000000..f71d84a Binary files /dev/null and b/resources/times-new-roman.ttf differ