diff options
| author | 2020-03-02 13:57:33 +0100 | |
|---|---|---|
| committer | 2020-03-02 13:57:33 +0100 | |
| commit | 1231f384ef9cf3aac6a4318b2ca4c465f2552aa8 (patch) | |
| tree | 63aa46912e7937ad38568b8d9146af9787f11baf | |
| parent | Merge pull request #783 from python-discord/python38-migration (diff) | |
Add HushDurationConverter.
| -rw-r--r-- | bot/converters.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/bot/converters.py b/bot/converters.py index 1945e1da3..976376fce 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -262,6 +262,34 @@ class ISODateTime(Converter): return dt +class HushDurationConverter(Converter): + """Convert passed duration to `int` minutes or `None`.""" + + MINUTES_RE = re.compile(r"(\d+)(?:M|m|$)") + + async def convert(self, ctx: Context, argument: str) -> t.Optional[int]: + """ + Convert `argument` to a duration that's max 15 minutes or None. + + If `"forever"` is passed, None is returned; otherwise an int of the extracted time. + Accepted formats are: + <duration>, + <duration>m, + <duration>M, + forever. + """ + if argument == "forever": + return None + match = self.MINUTES_RE.match(argument) + if not match: + raise BadArgument(f"{argument} is not a valid minutes duration.") + + duration = int(match.group(1)) + if duration > 15: + raise BadArgument("Duration must be below 15 minutes.") + return duration + + def proxy_user(user_id: str) -> discord.Object: """ Create a proxy user object from the given id. |