diff options
author | 2022-04-03 19:04:08 +0100 | |
---|---|---|
committer | 2022-04-03 19:04:08 +0100 | |
commit | fc0ddf22926d4753e5b646b4cbcba41b0cb64949 (patch) | |
tree | 9c6b707c3a2d4fc7ffb408431220ba23429d36d7 | |
parent | feat: add created at field for message deleted log (#2122) (diff) | |
parent | Merge branch 'main' into 2079 (diff) |
Merge pull request #2117 from iamericfletcher/2079
Add Resources Command
-rw-r--r-- | bot/exts/info/resources.py | 70 | ||||
-rw-r--r-- | bot/resources/tags/resources.md | 6 |
2 files changed, 70 insertions, 6 deletions
diff --git a/bot/exts/info/resources.py b/bot/exts/info/resources.py new file mode 100644 index 000000000..e27357484 --- /dev/null +++ b/bot/exts/info/resources.py @@ -0,0 +1,70 @@ +import re +from typing import Optional +from urllib.parse import quote + +from discord import Embed +from discord.ext import commands + +from bot.bot import Bot + +REGEX_CONSECUTIVE_NON_LETTERS = r"[^A-Za-z0-9]+" +RESOURCE_URL = "https://www.pythondiscord.com/resources/" + + +def to_kebabcase(resource_topic: str) -> str: + """ + Convert any string to kebab-case. + + For example, convert + "__Favorite FROOT¤#/$?is----LeMON???" to + "favorite-froot-is-lemon" + + Code adopted from: + https://github.com/python-discord/site/blob/main/pydis_site/apps/resources/templatetags/to_kebabcase.py + """ + # First, make it lowercase, and just remove any apostrophes. + # We remove the apostrophes because "wasnt" is better than "wasn-t" + resource_topic = resource_topic.casefold() + resource_topic = resource_topic.replace("'", '') + + # Now, replace any non-alphanumerics that remains with a dash. + # If there are multiple consecutive non-letters, just replace them with a single dash. + # my-favorite-class is better than my-favorite------class + resource_topic = re.sub( + REGEX_CONSECUTIVE_NON_LETTERS, + "-", + resource_topic, + ) + + # Now we use strip to get rid of any leading or trailing dashes. + resource_topic = resource_topic.strip("-") + return resource_topic + + +class Resources(commands.Cog): + """Display information about the Python Discord website Resource page.""" + + def __init__(self, bot: Bot): + self.bot = bot + + @commands.command(name="resources", aliases=("res",)) + async def resources_command(self, ctx: commands.Context, *, resource_topic: Optional[str]) -> None: + """Display information and a link to the Python Discord website Resources page.""" + url = RESOURCE_URL + + if resource_topic: + # Capture everything prior to new line allowing users to add messages below the command then prep for url + url = f"{url}?topics={quote(to_kebabcase(resource_topic.splitlines()[0]))}" + + embed = Embed( + title="Resources", + description=f"The [Resources page]({url}) on our website contains a list " + f"of hand-selected learning resources that we " + f"regularly recommend to both beginners and experts." + ) + await ctx.send(embed=embed) + + +def setup(bot: Bot) -> None: + """Load the Resources cog.""" + bot.add_cog(Resources(bot)) diff --git a/bot/resources/tags/resources.md b/bot/resources/tags/resources.md deleted file mode 100644 index 201e0eb1e..000000000 --- a/bot/resources/tags/resources.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -embed: - title: "Resources" ---- - -The [Resources page](https://www.pythondiscord.com/resources/) on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts. |