Skip to content

Commit c6ae8ce

Browse files
committed
Improve the converter for the closemessage cmd
1 parent 8738c3b commit c6ae8ce

File tree

1 file changed

+65
-17
lines changed

1 file changed

+65
-17
lines changed

close_message/close_message.py

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,50 @@
1111

1212

1313
class UserFriendlyTimeOnly(time.UserFriendlyTime):
14-
"""A convertor class to convert user friendly time to a duration."""
14+
"""
15+
A converter which parses user-friendly time durations.
16+
17+
Since this converter is meant for parsing close messages while
18+
closing threads, both custom close messages and time durations are
19+
parsed.
20+
21+
A default duration of time to close after can be provided.
22+
"""
23+
24+
def __init__(self, *, default_close_duration: t.Optional[str] = None) -> None:
25+
self.default_close_duration = default_close_duration
1526

1627
async def convert(self, ctx: commands.Context, argument: str) -> str:
17-
"""Convert the given argument to a user friendly time."""
18-
converted = await super().convert(ctx, argument)
19-
if converted.arg:
20-
raise commands.BadArgument(
21-
f'`{argument}` isn\'t a valid duration string.'
22-
)
23-
converted.arg = CLOSING_MESSAGE
24-
return converted
28+
"""
29+
Parse the time duration if provided and prefix any message.
30+
31+
The default close message is used as the close message.
32+
The default close message is appended if a custom close
33+
message is provided.
34+
35+
If only an integer is passed in, it is treated as the number
36+
of minutes to close after.
37+
"""
38+
if argument.strip().isdigit():
39+
argument = f'{argument}m'
40+
41+
await super().convert(ctx, argument)
42+
43+
if self.default_close_duration and self.arg == argument:
44+
# the user didn't enter a time or duration
45+
await super().convert(ctx, f'{self.default_close_duration} {argument}')
46+
47+
if self.arg:
48+
add_period = not self.arg.endswith((".", "!", "?"))
49+
self.arg = self.arg + (". " if add_period else " ") + CLOSING_MESSAGE
50+
else:
51+
self.arg = CLOSING_MESSAGE
52+
53+
return self
2554

2655

2756
class CloseMessage(commands.Cog):
28-
"""A plugin that adds a command to close a thread after a given period with a set message."""
57+
"""A plugin that adds a close command with a default close message."""
2958

3059
def __init__(self, bot: ModmailBot):
3160
self.bot = bot
@@ -34,18 +63,37 @@ def __init__(self, bot: ModmailBot):
3463
@commands.group(
3564
name="closemessage",
3665
aliases=("cm",),
37-
usage="[after]",
38-
help=f"Close the current thread with the message `{CLOSING_MESSAGE}`",
66+
usage="[after] [close message]",
3967
invoke_without_command=True
4068
)
4169
@checks.has_permissions(PermissionLevel.SUPPORTER)
4270
@checks.thread_only()
43-
async def close_message(self, ctx: commands.Context, *, after: t.Union[int, str] = '15m') -> commands.Command:
44-
"""Close the thread after the given duration with the set message."""
45-
if isinstance(after, int):
46-
after = f'{after}m'
71+
async def close_message(
72+
self,
73+
ctx: commands.Context,
74+
*,
75+
after: str = '' # noqa: F722
76+
) -> commands.Command:
77+
"""
78+
Close the current thread with a message.
79+
80+
The default close message is used as the close message.
81+
The default close message is appended if a custom close
82+
message is provided.
83+
84+
15 minutes is the default period of time before the thread
85+
closes.
86+
87+
If only an integer is passed in, it is treated as the number
88+
of minutes to close after.
4789
48-
after = await UserFriendlyTimeOnly().convert(ctx, after)
90+
Run `{prefix}help close` for additional information on how
91+
durations and custom close messages can be provided.
92+
"""
93+
# We're doing the conversion here to make the argument optional
94+
# while still passing the converted argument instead of an
95+
# empty string to the close command.
96+
after = await UserFriendlyTimeOnly(default_close_duration='15m').convert(ctx, after)
4997
return await self.close_command(ctx, after=after)
5098

5199
@close_message.command(aliases=('msg',))

0 commit comments

Comments
 (0)