diff options
| author | 2021-02-13 08:19:48 +0200 | |
|---|---|---|
| committer | 2021-02-13 08:19:48 +0200 | |
| commit | fc8e1e9b55a6134b5f5835b67127519d57f096df (patch) | |
| tree | caf73eb32224a6d8b2c943713ca1847072479316 | |
| parent | Merge pull request #1398 from anand2312/patch-1 (diff) | |
| parent | Taking only the first line of multiline fields. (diff) | |
Merge pull request #1416 from python-discord/pypi-command
Creation of the PyPi command.
| -rw-r--r-- | bot/constants.py | 9 | ||||
| -rw-r--r-- | bot/exts/info/pypi.py | 69 | ||||
| -rw-r--r-- | config-default.yml | 9 | 
3 files changed, 81 insertions, 6 deletions
diff --git a/bot/constants.py b/bot/constants.py index 91e41e334..8a93ff9cf 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -246,13 +246,16 @@ class Colours(metaclass=YAMLGetter):      section = "style"      subsection = "colours" +    blue: int      bright_green: int -    soft_green: int -    soft_orange: int -    soft_red: int      orange: int      pink: int      purple: int +    soft_green: int +    soft_orange: int +    soft_red: int +    white: int +    yellow: int  class DuckPond(metaclass=YAMLGetter): diff --git a/bot/exts/info/pypi.py b/bot/exts/info/pypi.py new file mode 100644 index 000000000..cf45b068f --- /dev/null +++ b/bot/exts/info/pypi.py @@ -0,0 +1,69 @@ +import itertools +import logging +import random + +from discord import Embed +from discord.ext.commands import Cog, Context, command + +from bot.bot import Bot +from bot.constants import Colours, NEGATIVE_REPLIES + +URL = "https://pypi.org/pypi/{package}/json" +FIELDS = ("author", "requires_python", "summary", "license") +PYPI_ICON = "https://cdn.discordapp.com/emojis/766274397257334814.png" +PYPI_COLOURS = itertools.cycle((Colours.yellow, Colours.blue, Colours.white)) + +log = logging.getLogger(__name__) + + +class PyPi(Cog): +    """Cog for getting information about PyPi packages.""" + +    def __init__(self, bot: Bot): +        self.bot = bot + +    @command(name="pypi", aliases=("package", "pack")) +    async def get_package_info(self, ctx: Context, package: str) -> None: +        """Provide information about a specific package from PyPI.""" +        embed = Embed( +            title=random.choice(NEGATIVE_REPLIES), +            colour=Colours.soft_red +        ) +        embed.set_thumbnail(url=PYPI_ICON) + +        async with self.bot.http_session.get(URL.format(package=package)) as response: +            if response.status == 404: +                embed.description = "Package could not be found." + +            elif response.status == 200 and response.content_type == "application/json": +                response_json = await response.json() +                info = response_json["info"] + +                embed.title = f"{info['name']} v{info['version']}" +                embed.url = info['package_url'] +                embed.colour = next(PYPI_COLOURS) + +                for field in FIELDS: +                    field_data = info[field] + +                    # Field could be completely empty, in some cases can be a string with whitespaces, or None. +                    if field_data and not field_data.isspace(): +                        if '\n' in field_data and field == "license": +                            field_data = field_data.split('\n')[0] + +                        embed.add_field( +                            name=field.replace("_", " ").title(), +                            value=field_data, +                            inline=False, +                        ) + +            else: +                embed.description = "There was an error when fetching your PyPi package." +                log.trace(f"Error when fetching PyPi package: {response.status}.") + +        await ctx.send(embed=embed) + + +def setup(bot: Bot) -> None: +    """Load the PyPi cog.""" +    bot.add_cog(PyPi(bot)) diff --git a/config-default.yml b/config-default.yml index d7415c821..25bbcc3c5 100644 --- a/config-default.yml +++ b/config-default.yml @@ -24,13 +24,16 @@ bot:  style:      colours: +        blue: 0x3775a8          bright_green: 0x01d277 -        soft_green: 0x68c290 -        soft_orange: 0xf9cb54 -        soft_red: 0xcd6d6d          orange: 0xe67e22          pink: 0xcf84e0          purple: 0xb734eb +        soft_green: 0x68c290 +        soft_orange: 0xf9cb54 +        soft_red: 0xcd6d6d +        white: 0xfffffe +        yellow: 0xffd241      emojis:          badge_bug_hunter: "<:bug_hunter_lvl1:743882896372269137>"  |