diff --git a/Gwendolyn.py b/Gwendolyn.py index 96c03d2..c928fab 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -2,7 +2,7 @@ import discord, os, finnhub from discord.ext import commands from pymongo import MongoClient -from funcs import logThis, makeFiles, Money, Funcs, SwChar, SwDestiny, SwRoll, Games, Generators, BedreNetflix +from funcs import logThis, makeFiles, Money, Funcs, SwChar, SwDestiny, SwRoll, Games, Generators, BedreNetflix, NerdShit commandPrefix = "!" @@ -47,6 +47,7 @@ class Gwendolyn(commands.Bot): self.generator = Generators() self.bedreNetflix = BedreNetflix(self) + self.nerdShit = NerdShit(self) Games(self) diff --git a/cogs/MiscCog.py b/cogs/MiscCog.py index 5fb7265..29ad2f5 100644 --- a/cogs/MiscCog.py +++ b/cogs/MiscCog.py @@ -1,8 +1,7 @@ -import discord, codecs, string, wolframalpha, requests, os +import discord, codecs, string from discord.ext import commands from funcs import logThis, helloFunc, roll_dice, imageFunc, movieFunc, cap, findWikiPage -from PIL import Image, ImageDraw, ImageFont class MiscCog(commands.Cog): @@ -12,6 +11,7 @@ class MiscCog(commands.Cog): self.client.remove_command("help") self.generator = client.generator self.bedreNetflix = client.bedreNetflix + self.nerdShit = client.nerdShit @commands.command(name = "help") async def helpCommand(self, ctx, *, content = ""): @@ -139,73 +139,7 @@ class MiscCog(commands.Cog): #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") + await self.nerdShit.wolfSearch(ctx,content) def setup(client): client.add_cog(MiscCog(client)) diff --git a/funcs/__init__.py b/funcs/__init__.py index 7a15845..cfc28f7 100644 --- a/funcs/__init__.py +++ b/funcs/__init__.py @@ -10,7 +10,7 @@ from .games import Money, Games from .lookup import spellFunc, monsterFunc -from .other import Generators, movieFunc, BedreNetflix +from .other import Generators, movieFunc, BedreNetflix, NerdShit from .roll import roll_dice diff --git a/funcs/other/__init__.py b/funcs/other/__init__.py index 7dfa1c3..3d936c6 100644 --- a/funcs/other/__init__.py +++ b/funcs/other/__init__.py @@ -1,7 +1,8 @@ """Misc. functions for Gwendolyn.""" -__all__ = ["Generators", "movieFunc"] +__all__ = ["Generators", "movieFunc","BedreNetflix","NerdShit"] from .bedreNetflix import BedreNetflix from .generators import Generators -from .movie import movieFunc \ No newline at end of file +from .movie import movieFunc +from .nerdShit import NerdShit \ No newline at end of file diff --git a/funcs/other/nerdShit.py b/funcs/other/nerdShit.py new file mode 100644 index 0000000..a786a0b --- /dev/null +++ b/funcs/other/nerdShit.py @@ -0,0 +1,81 @@ +import discord, wolframalpha, requests, os +from PIL import Image, ImageDraw, ImageFont +from funcs import logThis + +class NerdShit(): + def __init__(self,client): + """Runs misc commands.""" + self.client = client + + async def wolfSearch(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 = [] + if int(res.numpods) > 0: + 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") + else: + logThis("No returned data") + await ctx.message.channel.send("Could not find anything relating to your search") \ No newline at end of file