From d989cb8321be371258016139395aa7fd53348fd0 Mon Sep 17 00:00:00 2001 From: Phxntxm Date: Sat, 23 Jul 2016 06:59:02 -0500 Subject: [PATCH] Added two cogs for specific categories --- cogs/links.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ cogs/tags.py | 73 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 cogs/links.py create mode 100644 cogs/tags.py diff --git a/cogs/links.py b/cogs/links.py new file mode 100644 index 0000000..7352645 --- /dev/null +++ b/cogs/links.py @@ -0,0 +1,86 @@ +from discord.ext import commands +from .utils import config +from .utils import checks +import urllib.parse +import urllib.request +import json + +class Links: + """This class contains all the commands that make HTTP requests + In other words, all commands here rely on other URL's to complete their requests""" + + def __init__(self, bot): + self.bot = bot + + @commands.command() + @checks.customPermsOrRole("send_messages") + async def urban(self, *msg: str): + """Pulls the top urbandictionary.com definition for a term""" + try: + url = "http://api.urbandictionary.com/v0/define?term={}".format('+'.join(msg)) + response = urllib.request.urlopen(url) + data = json.loads(response.read().decode('utf-8')) + if len(data['list']) == 0: + await self.bot.say("No result with that term!") + else: + await self.bot.say(data['list'][0]['definition']) + except discord.HTTPException: + await self.bot.say('```Error: Definition is too long for me to send```') + + @commands.command(pass_context=True) + @checks.customPermsOrRole("send_messages") + async def derpi(self, ctx, *search: str): + """Provides a random image from the first page of derpibooru.org for the following term""" + if len(search) > 0: + # This sets the url as url?q=search+terms + url = 'https://derpibooru.org/search.json?q={}'.format('+'.join(search)) + + nsfw_channels = config.getContent("nsfw_channels") + if ctx.message.channel.id in nsfw_channels: + url += ",+explicit&filter_id=95938" + + # Get the response from derpibooru and parse the 'searc' result from it + response = urllib.request.urlopen(url) + data = json.loads(response.read().decode('utf-8')) + results = data['search'] + + # Get the link if it exists, if not return saying no results found + if len(results) > 0: + index = random.randint(0, len(results) - 1) + imageLink = 'http://{}'.format(results[index].get('representations').get('full')[2:].strip()) + else: + await self.bot.say("No results with that search term, {0}!".format(ctx.message.author.mention)) + return + else: + # If no search term was provided, search for a random image + with urllib.request.urlopen('https://derpibooru.org/images/random') as response: + imageLink = response.geturl() + await self.bot.say(imageLink) + + + @commands.command(pass_context=True) + @checks.customPermsOrRole("send_messages") + async def e621(self, ctx, *, tags: str): + """Searches for a random image from e621.net + Format for the search terms need to be 'search term 1, search term 2, etc.' + If the channel the command is ran in, is registered as a nsfw channel, this image will be explicit""" + tags = tags.replace(' ', '_') + tags = tags.replace(',_', '%20') + url = 'https://e621.net/post/index.json?limit=320&tags={}'.format(tags) + await self.bot.say("Looking up an image with those tags....") + + if ctx.message.channel.id in config.getContent('nsfw_channels'): + url += "%20rating:explicit" + else: + url += "%20rating:safe" + request = urllib.request.Request(url, headers={'User-Agent': 'Bonfire/1.0'}) + with urllib.request.urlopen(request) as response: + data = json.loads(response.read().decode('utf-8')) + if len(data) == 0: + await self.bot.say("No results with that image {}".format(ctx.message.author.mention)) + return + elif len(data) == 1: + rand_image = data[0]['file_url'] + else: + rand_image = data[random.randint(0, len(data)-1)]['file_url'] + await self.bot.say(rand_image) diff --git a/cogs/tags.py b/cogs/tags.py new file mode 100644 index 0000000..9608be6 --- /dev/null +++ b/cogs/tags.py @@ -0,0 +1,73 @@ +from discord.ext import commands +from .utils import config +from .utils import checks + +class Tags: + """This class contains all the commands for custom tags""" + + def __init__(self, bot): + self.bot = bot + + @commands.command(pass_context=True) + @checks.customPermsOrRole("send_messages") + async def tags(self, ctx): + """Prints all the custom tags that this server currently has""" + tags = config.getContent('tags') + fmt = "\n".join("{}".format(tag['tag']) for tag in tags if tag['server_id']==ctx.message.server.id) + await self.bot.say('```{}```'.format(fmt)) + + @commands.group(pass_context=True, invoke_without_command=True, no_pm=True) + @checks.customPermsOrRole("send_messages") + async def tag(self, ctx, *, tag: str): + """This can be used to call custom tags + The format to call a custom tag is !tag """ + tags = config.getContent('tags') + result = [t for t in tags if t['tag'] == tag and t['server_id'] == ctx.message.server.id] + if len(result) == 0: + await self.bot.say('That tag does not exist!') + return + await self.bot.say("{}".format(result[0]['result'])) + + @tag.command(name='add', aliases=['create', 'start'], pass_context=True, no_pm=True) + @checks.customPermsOrRole("kick_members") + async def add_tag(self, ctx, *, result: str): + """Use this to add a new tag that can be used in this server + Format to add a tag is !tag add - """ + tag = result[0:result.find('-')].strip() + tag_result = result[result.find('-') + 2:].strip() + if len(tag) == 0 or len(result) == 0: + await self.bot.say("Please provide the format for the tag in: !tag add - ") + return + tags = config.getContent('tags') + for t in tags: + if t['tag'] == tag and t['server_id'] == ctx.message.server.id: + t['result'] = tag_result + if config.saveContent('tags', tags): + await self.bot.say("I have just updated the tag `{0}`! You can call this tag by entering !tag {0}".format(tag)) + else: + await self.bot.say("I was unable to save this data") + return + tags.append({'server_id': ctx.message.server.id, 'tag': tag, 'result': tag_result}) + if config.saveContent('tags', tags): + await self.bot.say("I have just added the tag `{0}`! You can call this tag by entering !tag {0}".format(tag)) + else: + await self.bot.say("I was unable to save this data") + + @tag.command(name='delete', aliases=['remove', 'stop'], pass_context=True, no_pm=True) + @checks.customPermsOrRole("kick_members") + async def del_tag(self, ctx, *, tag: str): + """Use this to remove a tag that from use for this server + Format to delete a tag is !tag delete """ + tags = config.getContent('tags') + result = [t for t in tags if t['tag'] == tag and t['server_id'] == ctx.message.server.id] + if len(result) == 0: + await self.bot.say( + "The tag {} does not exist! You can't remove something if it doesn't exist...".format(tag)) + return + for t in tags: + if t['tag'] == tag and t['server_id'] == ctx.message.server.id: + tags.remove(t) + if config.saveContent('tags', tags): + await self.bot.say('I have just removed the tag `{}`'.format(tag)) + else: + await self.bot.say("I was unable to save this data")