11
11
12
12
13
13
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
15
26
16
27
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
25
54
26
55
27
56
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."""
29
58
30
59
def __init__ (self , bot : ModmailBot ):
31
60
self .bot = bot
@@ -34,18 +63,37 @@ def __init__(self, bot: ModmailBot):
34
63
@commands .group (
35
64
name = "closemessage" ,
36
65
aliases = ("cm" ,),
37
- usage = "[after]" ,
38
- help = f"Close the current thread with the message `{ CLOSING_MESSAGE } `" ,
66
+ usage = "[after] [close message]" ,
39
67
invoke_without_command = True
40
68
)
41
69
@checks .has_permissions (PermissionLevel .SUPPORTER )
42
70
@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.
47
89
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 )
49
97
return await self .close_command (ctx , after = after )
50
98
51
99
@close_message .command (aliases = ('msg' ,))
0 commit comments