From f2e9c420e9df9081ec5a6c6770b5e3765014b984 Mon Sep 17 00:00:00 2001 From: scummboy Date: Wed, 10 Feb 2016 20:26:02 +0000 Subject: [PATCH 1/6] The missing things --- cogs/quote.py | 66 ++++++++++++++++++++++++++++++++++ image.something | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 cogs/quote.py create mode 100644 image.something diff --git a/cogs/quote.py b/cogs/quote.py new file mode 100644 index 000000000..fc9c1b5e9 --- /dev/null +++ b/cogs/quote.py @@ -0,0 +1,66 @@ +import discord +from discord.ext import commands +from .utils.dataIO import fileIO +from .utils import checks +import os + +class Quote: + """Quotes""" + + def __init__(self, bot): + self.bot = bot + self.q_quote = fileIO("data/quote/quote.json", "load") + + @commands.command(pass_context=True, no_pm=True) + async def addquote(self, ctx, quote : str, *text): + """Adds a quote to the database + + Example: + !addquote Enter quote text here + """ + if text == (): + await self.bot.say("addquote [quote text]") + return + server = ctx.message.server + channel = ctx.message.channel + text = " ".join(text) + if not server.id in self.q_quote: + self.q_quote[server.id] = {} + quotelist = self.q_quote[server.id] + if quote not in quotelist: + quotelist[quote] = text + print(quotelist[quote]) + self.q_quote[server.id] = quotelist + print(self.q_quote) + fileIO("data/quote/quote.json", "save", self.q_quote) + await self.bot.say("`Quote added.`") + else: + await self.bot.say("`Quote already exists.`") + + async def checkQ(self, message): + if message.author.id == self.bot.user.id or len(message.content) < 2 or message.channel.is_private: + return + msg = message.content + server = message.server + if msg[0] in self.bot.command_prefix and server.id in self.q_quote.keys(): + quotelist = self.q_quote[server.id] + if msg[1:] in quotelist: + await self.bot.send_message(message.channel, quotelist[msg[1:]]) + +def check_folders(): + if not os.path.exists("data/quote"): + print("Creating data/quote folder...") + os.makedirs("data/quote") + +def check_files(): + f = "data/quote/quote.json" + if not fileIO(f, "check"): + print("Creating empty quote.json...") + fileIO(f, "save", {}) + +def setup(bot): + check_folders() + check_files() + n = Quote(bot) + bot.add_listener(n.checkQ, "on_message") + bot.add_cog(n) diff --git a/image.something b/image.something new file mode 100644 index 000000000..b2664e32f --- /dev/null +++ b/image.something @@ -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)) From 89d443dde57d4a571ba487f67aa2ce296b7ba459 Mon Sep 17 00:00:00 2001 From: scummboy Date: Wed, 10 Feb 2016 21:40:40 +0000 Subject: [PATCH 2/6] Quotes can now be added to quote.json --- cogs/quote.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/cogs/quote.py b/cogs/quote.py index fc9c1b5e9..7fc021825 100644 --- a/cogs/quote.py +++ b/cogs/quote.py @@ -2,6 +2,7 @@ import discord from discord.ext import commands from .utils.dataIO import fileIO from .utils import checks +from datetime import date import os class Quote: @@ -12,7 +13,7 @@ class Quote: self.q_quote = fileIO("data/quote/quote.json", "load") @commands.command(pass_context=True, no_pm=True) - async def addquote(self, ctx, quote : str, *text): + async def addquote(self, ctx : str, *text): """Adds a quote to the database Example: @@ -27,25 +28,16 @@ class Quote: if not server.id in self.q_quote: self.q_quote[server.id] = {} quotelist = self.q_quote[server.id] - if quote not in quotelist: - quotelist[quote] = text - print(quotelist[quote]) + if text not in quotelist: + d = date.today() + quotelist[text] = d.strftime("%d/%m/%Y") self.q_quote[server.id] = quotelist - print(self.q_quote) fileIO("data/quote/quote.json", "save", self.q_quote) await self.bot.say("`Quote added.`") else: await self.bot.say("`Quote already exists.`") - async def checkQ(self, message): - if message.author.id == self.bot.user.id or len(message.content) < 2 or message.channel.is_private: - return - msg = message.content - server = message.server - if msg[0] in self.bot.command_prefix and server.id in self.q_quote.keys(): - quotelist = self.q_quote[server.id] - if msg[1:] in quotelist: - await self.bot.send_message(message.channel, quotelist[msg[1:]]) + def check_folders(): if not os.path.exists("data/quote"): @@ -62,5 +54,4 @@ def setup(bot): check_folders() check_files() n = Quote(bot) - bot.add_listener(n.checkQ, "on_message") bot.add_cog(n) From ffddc09f9eb09ab20c85a13f55752aaeffbb744d Mon Sep 17 00:00:00 2001 From: scummboy Date: Tue, 16 Feb 2016 14:35:36 +0000 Subject: [PATCH 3/6] Let's let Giphy handle the randomness. Works much better --- cogs/image.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cogs/image.py b/cogs/image.py index e7e714c04..458d10c98 100644 --- a/cogs/image.py +++ b/cogs/image.py @@ -81,12 +81,11 @@ class Image: 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" + search = "http://api.giphy.com/v1/gifs/random?&api_key=dc6zaTOxFJmzC&tag=" + msg 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"] + url = result["data"]["url"] await self.bot.say(url) else: await self.bot.say("Your search terms gave no results.") From b6c64f973851eb86880761fd9ebe1d69eef3b989 Mon Sep 17 00:00:00 2001 From: scummboy Date: Tue, 16 Feb 2016 14:39:40 +0000 Subject: [PATCH 4/6] File cleanup --- .gitignore | 3 +- image.something | 94 ------------------------------------------------- 2 files changed, 2 insertions(+), 95 deletions(-) delete mode 100644 image.something diff --git a/.gitignore b/.gitignore index dbccdcb73..0eda60240 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ __pycache__ data !data/trivia/* -!data/audio/playlists/* \ No newline at end of file +!data/audio/playlists/* +cogs/quote.py diff --git a/image.something b/image.something deleted file mode 100644 index b2664e32f..000000000 --- a/image.something +++ /dev/null @@ -1,94 +0,0 @@ -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)) From d19a6a99924db725ab1008fdd485b5d542a90d59 Mon Sep 17 00:00:00 2001 From: Scumm Boy Date: Tue, 16 Feb 2016 09:41:08 -0500 Subject: [PATCH 5/6] Remove unfinished quote cog --- cogs/quote.py | 57 --------------------------------------------------- 1 file changed, 57 deletions(-) delete mode 100644 cogs/quote.py diff --git a/cogs/quote.py b/cogs/quote.py deleted file mode 100644 index 7fc021825..000000000 --- a/cogs/quote.py +++ /dev/null @@ -1,57 +0,0 @@ -import discord -from discord.ext import commands -from .utils.dataIO import fileIO -from .utils import checks -from datetime import date -import os - -class Quote: - """Quotes""" - - def __init__(self, bot): - self.bot = bot - self.q_quote = fileIO("data/quote/quote.json", "load") - - @commands.command(pass_context=True, no_pm=True) - async def addquote(self, ctx : str, *text): - """Adds a quote to the database - - Example: - !addquote Enter quote text here - """ - if text == (): - await self.bot.say("addquote [quote text]") - return - server = ctx.message.server - channel = ctx.message.channel - text = " ".join(text) - if not server.id in self.q_quote: - self.q_quote[server.id] = {} - quotelist = self.q_quote[server.id] - if text not in quotelist: - d = date.today() - quotelist[text] = d.strftime("%d/%m/%Y") - self.q_quote[server.id] = quotelist - fileIO("data/quote/quote.json", "save", self.q_quote) - await self.bot.say("`Quote added.`") - else: - await self.bot.say("`Quote already exists.`") - - - -def check_folders(): - if not os.path.exists("data/quote"): - print("Creating data/quote folder...") - os.makedirs("data/quote") - -def check_files(): - f = "data/quote/quote.json" - if not fileIO(f, "check"): - print("Creating empty quote.json...") - fileIO(f, "save", {}) - -def setup(bot): - check_folders() - check_files() - n = Quote(bot) - bot.add_cog(n) From bebd39e79a2c9940a9441e0790515e103f06263d Mon Sep 17 00:00:00 2001 From: scummboy Date: Wed, 17 Feb 2016 14:25:47 +0000 Subject: [PATCH 6/6] clean up .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0eda60240..f4ca4c697 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ __pycache__ data !data/trivia/* !data/audio/playlists/* -cogs/quote.py