diff options
| author | 2021-08-30 09:52:14 +0100 | |
|---|---|---|
| committer | 2021-08-30 09:52:14 +0100 | |
| commit | a7f0bb042a3ff8f2ce41504c705d0d0a199734a3 (patch) | |
| tree | 27f9d5752fb93d5e3355fae0ca4d61e7ccb520d3 /bot/exts | |
| parent | Whitelist `.bm` command in occupied help channels (diff) | |
| parent | Add baby anaconda to snake hatch (#822) (diff) | |
Merge branch 'main' into patch-1
Diffstat (limited to 'bot/exts')
| -rw-r--r-- | bot/exts/evergreen/realpython.py | 76 | ||||
| -rw-r--r-- | bot/exts/evergreen/snakes/_utils.py | 3 | ||||
| -rw-r--r-- | bot/exts/internal_eval/_internal_eval.py | 5 | 
3 files changed, 82 insertions, 2 deletions
| diff --git a/bot/exts/evergreen/realpython.py b/bot/exts/evergreen/realpython.py new file mode 100644 index 00000000..e722dd4b --- /dev/null +++ b/bot/exts/evergreen/realpython.py @@ -0,0 +1,76 @@ +import logging +from html import unescape +from urllib.parse import quote_plus + +from discord import Embed +from discord.ext import commands + +from bot import bot +from bot.constants import Colours + +logger = logging.getLogger(__name__) + + +API_ROOT = "https://realpython.com/search/api/v1/" +ARTICLE_URL = "https://realpython.com{article_url}" +SEARCH_URL = "https://realpython.com/search?q={user_search}" + + +ERROR_EMBED = Embed( +    title="Error while searching Real Python", +    description="There was an error while trying to reach Real Python. Please try again shortly.", +    color=Colours.soft_red, +) + + +class RealPython(commands.Cog): +    """User initiated command to search for a Real Python article.""" + +    def __init__(self, bot: bot.Bot): +        self.bot = bot + +    @commands.command(aliases=["rp"]) +    @commands.cooldown(1, 10, commands.cooldowns.BucketType.user) +    async def realpython(self, ctx: commands.Context, *, user_search: str) -> None: +        """Send 5 articles that match the user's search terms.""" +        params = {"q": user_search, "limit": 5} +        async with self.bot.http_session.get(url=API_ROOT, params=params) as response: +            if response.status != 200: +                logger.error( +                    f"Unexpected status code {response.status} from Real Python" +                ) +                await ctx.send(embed=ERROR_EMBED) +                return + +            data = await response.json() + +        articles = data["results"] + +        if len(articles) == 0: +            no_articles = Embed( +                title=f"No articles found for '{user_search}'", color=Colours.soft_red +            ) +            await ctx.send(embed=no_articles) +            return + +        article_embed = Embed( +            title="Search results - Real Python", +            url=SEARCH_URL.format(user_search=quote_plus(user_search)), +            description="Here are the top 5 results:", +            color=Colours.orange, +        ) + +        for article in articles: +            article_embed.add_field( +                name=unescape(article["title"]), +                value=ARTICLE_URL.format(article_url=article["url"]), +                inline=False, +            ) +        article_embed.set_footer(text="Click the links to go to the articles.") + +        await ctx.send(embed=article_embed) + + +def setup(bot: bot.Bot) -> None: +    """Load the Real Python Cog.""" +    bot.add_cog(RealPython(bot)) diff --git a/bot/exts/evergreen/snakes/_utils.py b/bot/exts/evergreen/snakes/_utils.py index 0a5894b7..f996d7f8 100644 --- a/bot/exts/evergreen/snakes/_utils.py +++ b/bot/exts/evergreen/snakes/_utils.py @@ -55,7 +55,8 @@ snakes = {      "Baby Rattle Snake": "https://i.imgur.com/i5jYA8f.png",      "Baby Dragon Snake": "https://i.imgur.com/SuMKM4m.png",      "Baby Garden Snake": "https://i.imgur.com/5vYx3ah.png", -    "Baby Cobra": "https://i.imgur.com/jk14ryt.png" +    "Baby Cobra": "https://i.imgur.com/jk14ryt.png", +    "Baby Anaconda": "https://i.imgur.com/EpdrnNr.png",  }  BOARD_TILE_SIZE = 56         # the size of each board tile diff --git a/bot/exts/internal_eval/_internal_eval.py b/bot/exts/internal_eval/_internal_eval.py index 56bf5add..b7749144 100644 --- a/bot/exts/internal_eval/_internal_eval.py +++ b/bot/exts/internal_eval/_internal_eval.py @@ -7,7 +7,7 @@ import discord  from discord.ext import commands  from bot.bot import Bot -from bot.constants import Roles +from bot.constants import Client, Roles  from bot.utils.decorators import with_role  from bot.utils.extensions import invoke_help_command  from ._helpers import EvalContext @@ -41,6 +41,9 @@ class InternalEval(commands.Cog):          self.bot = bot          self.locals = {} +        if Client.debug: +            self.internal_group.add_check(commands.is_owner().predicate) +      @staticmethod      def shorten_output(              output: str, | 
