diff options
author | 2021-05-10 09:45:32 -0400 | |
---|---|---|
committer | 2021-05-10 09:45:32 -0400 | |
commit | e15d1eca771901280297bee15d5ee901c965d0d7 (patch) | |
tree | e4c05ee7ab13aa852c68e81464999b5bd054a356 /bot/utils/converters.py | |
parent | chore: Use ctx instead of ctx.channel (diff) | |
parent | Merge pull request #725 from python-discord/change-to-fetch-user (diff) |
fix: Resolve Merge Conflicts
Diffstat (limited to 'bot/utils/converters.py')
-rw-r--r-- | bot/utils/converters.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/bot/utils/converters.py b/bot/utils/converters.py index 72b64848..9e9616d8 100644 --- a/bot/utils/converters.py +++ b/bot/utils/converters.py @@ -85,3 +85,32 @@ class DateConverter(commands.Converter): f"Can't convert `{argument}` to `datetime` in format `YYYY-MM-DD` or `int` in SOL." ) return date + + +class Subreddit(commands.Converter): + """Forces a string to begin with "r/" and checks if it's a valid subreddit.""" + + @staticmethod + async def convert(ctx: commands.Context, sub: str) -> str: + """ + Force sub to begin with "r/" and check if it's a valid subreddit. + + If sub is a valid subreddit, return it prepended with "r/" + """ + sub = sub.lower() + + if not sub.startswith("r/"): + sub = f"r/{sub}" + + resp = await ctx.bot.http_session.get( + "https://www.reddit.com/subreddits/search.json", + params={"q": sub} + ) + + json = await resp.json() + if not json["data"]["children"]: + raise commands.BadArgument( + f"The subreddit `{sub}` either doesn't exist, or it has no posts." + ) + + return sub |