diff options
Diffstat (limited to 'bot/converters.py')
| -rw-r--r-- | bot/converters.py | 26 | 
1 files changed, 26 insertions, 0 deletions
diff --git a/bot/converters.py b/bot/converters.py index f18b2f6c7..3def4b07a 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -172,3 +172,29 @@ class InfractionSearchQuery(Converter):          except Exception:              return arg          return user or arg + + +class Subreddit(Converter): +    """ +    Forces a string to begin with "r/" and checks if it's a valid subreddit. +    """ + +    @staticmethod +    async def convert(ctx, sub: str): +        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 BadArgument( +                f"The subreddit `{sub}` either doesn't exist, or it has no posts." +            ) + +        return sub  |