aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Henrik Böving <[email protected]>2018-06-06 18:05:52 +0200
committerGravatar Gareth Coles <[email protected]>2018-06-06 17:05:52 +0100
commit7f3568e383d3f3b2cbdde361d286394d68fecad9 (patch)
tree444ba26fa8a8225fd4dc8105bc7fa75223fd33b6
parentLinting (diff)
Fetch pep (#77)
* autocodeblocking * ast fix better type annotations * ci there you go * line too long ==> remove comment * deleting message + lowercase python * comments * remove docstrings * you may * not sure what you mean with move to a new line maybe this? * descriptive enough? * better explanation of text filtering * rewrite * fstring * pep8 * better comment * discord message length * ok shady * ok shady * "This is still async discord.py" * didnt see josephs comment * minor comment changes * blank and typo * typo.. * embed sending * adding waiting time + embed dont know if this is the correct way to store the field string * forgot await * but now i know * i heard him * pls * more descriptive variable name * comment * and better string formatting * approve me lemon * here we go aperture * there we go again * name error..... damn linter plugins * you get it aperture * ok lemon * ok aperture * constants instead of plain channel ID * forgot one * constants in bot.py * lemons way ftw * flake8 is weird man * aperture 1 * next aperture * dunno * imports * next lemon * utils first try, command is not found * still not working * pep alias, no debugging output for reasons * pep parser * flake8 * local flake8 didnt show that one.. * More not needed info * ... * better embed * pep8 * removing a blank * upgrading to yml config * fixing my Germanness and other stuff lemon didnt like * addressing more issues
-rw-r--r--bot/__main__.py1
-rw-r--r--bot/cogs/utils.py83
2 files changed, 84 insertions, 0 deletions
diff --git a/bot/__main__.py b/bot/__main__.py
index 50027bf1b..12c446b55 100644
--- a/bot/__main__.py
+++ b/bot/__main__.py
@@ -61,6 +61,7 @@ bot.load_extension("bot.cogs.hiphopify")
bot.load_extension("bot.cogs.snakes")
bot.load_extension("bot.cogs.tags")
bot.load_extension("bot.cogs.verification")
+bot.load_extension("bot.cogs.utils")
bot.run(BotConfig.token)
diff --git a/bot/cogs/utils.py b/bot/cogs/utils.py
new file mode 100644
index 000000000..3d3d0de36
--- /dev/null
+++ b/bot/cogs/utils.py
@@ -0,0 +1,83 @@
+import logging
+from email.parser import HeaderParser
+from io import StringIO
+
+
+from discord import Colour, Embed
+from discord.ext.commands import AutoShardedBot, Context, command
+
+from bot.constants import Roles
+from bot.decorators import with_role
+
+log = logging.getLogger(__name__)
+
+
+class Utils:
+ """
+ A selection of utilities which don't have a clear category.
+ """
+
+ def __init__(self, bot: AutoShardedBot):
+ self.bot = bot
+
+ self.base_pep_url = "http://www.python.org/dev/peps/pep-"
+ self.base_github_pep_url = "https://raw.githubusercontent.com/python/peps/master/pep-"
+
+ @command(name="pep()", aliases=["pep", "get_pep"])
+ @with_role(Roles.verified)
+ async def pep_search(self, ctx: Context, pep_number: str):
+ """
+ Fetches information about a PEP and sends it to the channel.
+ """
+
+ # Attempt to fetch the PEP from Github.
+ pep_url = f"{self.base_github_pep_url}{pep_number:04}.txt"
+ log.trace(f"Requesting PEP {pep_number} with {pep_url}")
+ response = await self.bot.http_session.get(pep_url)
+
+ if response.status == 200:
+ log.trace("PEP found")
+
+ pep_content = await response.text()
+
+ # Taken from https://github.com/python/peps/blob/master/pep0/pep.py#L179
+ pep_header = HeaderParser().parse(StringIO(pep_content))
+
+ # Assemble the embed
+ pep_embed = Embed(
+ title=f"**PEP {pep_number} - {pep_header['Title']}**",
+ description=f"[Link]({self.base_pep_url}{pep_number:04})",
+ )
+
+ pep_embed.set_thumbnail(url="https://www.python.org/static/opengraph-icon-200x200.png")
+
+ # Add the interesting information
+ if "Status" in pep_header:
+ pep_embed.add_field(name="Status", value=pep_header["Status"])
+ if "Python-Version" in pep_header:
+ pep_embed.add_field(name="Python-Version", value=pep_header["Python-Version"])
+ if "Created" in pep_header:
+ pep_embed.add_field(name="Created", value=pep_header["Created"])
+ if "Type" in pep_header:
+ pep_embed.add_field(name="Type", value=pep_header["Type"])
+
+ elif response.status == 404:
+ log.trace("PEP was not found")
+ not_found = f"PEP {pep_number} does not exist."
+ pep_embed = Embed(title="PEP not found", description=not_found)
+ pep_embed.colour = Colour.red()
+
+ else:
+ log.trace(f"The user requested PEP {pep_number}, but the response had an unexpected status code: "
+ f"{response.status}.\n{response.text}")
+
+ error_message = "Unexpected HTTP error during PEP search. Please let us know."
+ pep_embed = Embed(title="Unexpected error", description=error_message)
+ pep_embed.colour = Colour.red()
+
+ await ctx.message.channel.send(embed=pep_embed)
+
+
+def setup(bot):
+ bot.add_cog(Utils(bot))
+ log.info("Utils cog loaded")