aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils/converters.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot/utils/converters.py')
-rw-r--r--bot/utils/converters.py29
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