diff --git a/cogs/general.py b/cogs/general.py index 410961429..58210f3b7 100644 --- a/cogs/general.py +++ b/cogs/general.py @@ -189,7 +189,7 @@ class General: return if not self.getPollByChannel(message): p = NewPoll(message, self) - if p.valid: + if p.valid: self.poll_sessions.append(p) await p.start() else: @@ -214,7 +214,7 @@ class General: return False async def check_poll_votes(self, message): - if message.author.id != self.bot.user.id: + if message.author.id != self.bot.user.id: if self.getPollByChannel(message): self.getPollByChannel(message).checkAnswer(message) @@ -229,7 +229,7 @@ class NewPoll(): msg = msg.split(";") if len(msg) < 2: # Needs at least one question and 2 choices self.valid = False - return None + return None else: self.valid = True self.already_voted = [] @@ -275,4 +275,4 @@ class NewPoll(): def setup(bot): n = General(bot) bot.add_listener(n.check_poll_votes, "on_message") - bot.add_cog(n) \ No newline at end of file + bot.add_cog(n) diff --git a/cogs/image.py b/cogs/image.py new file mode 100644 index 000000000..b2664e32f --- /dev/null +++ b/cogs/image.py @@ -0,0 +1,94 @@ +import discord +from discord.ext import commands +from random import randint +from imgurpython import ImgurClient +import aiohttp +import random + +class Image: + """Image related commands.""" + + def __init__(self, bot): + self.bot = bot + #Reserved for further ... stuff + + """Commands section""" + + @commands.command(no_pm=True) + async def imgur(self, *text): + """Retrieves a random imgur picture. + imgur search [keyword] - retrieves first hit of search query. + imgur [subreddit section] [top or new] - retrieves top 3 hottest or latest pictures of today for given a subreddit section, e.g. 'funny'.""" + imgurclient = ImgurClient("1fd3ef04daf8cab", "f963e574e8e3c17993c933af4f0522e1dc01e230") + if text == (): + rand = randint(0, 59) #60 results per generated page + items = imgurclient.gallery_random(page=0) + await self.bot.say(items[rand].link) + elif text[0] == "search": + items = imgurclient.gallery_search(" ".join(text[1:len(text)]), advanced=None, sort='time', window='all', page=0) + if len(items) < 1: + await self.bot.say("Your search terms gave no results.") + else: + await self.bot.say(items[0].link) + elif text[0] != (): + if text[1] == "top": + imgSort = "top" + elif text[1] == "new": + imgSort = "time" + else: + await self.bot.say("Only top or new is a valid subcommand.") + return + items = imgurclient.subreddit_gallery(text[0], sort=imgSort, window='day', page=0) + if (len(items) < 3): + await self.bot.say("This subreddit section does not exist, try 'funny'") + else: + await self.bot.say("{} {} {}".format(items[0].link, items[1].link, items[2].link)) + + @commands.command(no_pm=True) + async def gif(self, *text): + """ gif [keyword] - retrieves first search result from giphy """ + if len(text) > 0: + if len(text[0]) > 1 and len(text[0]) < 20: + try: + msg = "+".join(text) + search = "http://api.giphy.com/v1/gifs/search?q=" + msg + "&api_key=dc6zaTOxFJmzC" + async with aiohttp.get(search) as r: + result = await r.json() + if result["data"] != []: + url = result["data"][0]["url"] + await self.bot.say(url) + else: + await self.bot.say("Your search terms gave no results.") + except: + await self.bot.say("Error.") + else: + await self.bot.say("Invalid search.") + else: + await self.bot.say("gif [text]") + + @commands.command(no_pm=True) + async def gifr(self, *text): + """ gifr [keyword] - retrieves a random gif from a giphy search """ + random.seed() + if len(text) > 0: + if len(text[0]) > 1 and len(text[0]) < 20: + try: + msg = "+".join(text) + search = "http://api.giphy.com/v1/gifs/search?q=" + msg + "&api_key=dc6zaTOxFJmzC" + async with aiohttp.get(search) as r: + result = await r.json() + if result["data"] != []: + maxarray = len(result) + url = result["data"][random.randint(0,maxarray)]["url"] + await self.bot.say(url) + else: + await self.bot.say("Your search terms gave no results.") + except: + await self.bot.say("Error.") + else: + await self.bot.say("Invalid search.") + else: + await self.bot.say("gif [text]") + +def setup(bot): + bot.add_cog(Image(bot))