Explorar el Código

Adding default_value to CogSetting. Better config value formatting.

pull/13/head
Rocketsoup hace 2 meses
padre
commit
abd45c00f0

+ 17
- 7
rocketbot/cogs/autokickcog.py Ver fichero

@@ -33,23 +33,33 @@ class AutoKickCog(BaseCog, name='Auto Kick'):
33 33
 	"""
34 34
 	Cog for automatically kicking ALL new joins. For temporary use during join raids.
35 35
 	"""
36
-	SETTING_ENABLED = CogSetting('enabled', bool,
36
+	SETTING_ENABLED = CogSetting(
37
+		'enabled',
38
+		bool,
39
+		default_value=False,
37 40
 		brief='autokick',
38
-		description='Whether this cog is enabled for a guild.')
39
-	SETTING_BAN_COUNT = CogSetting('bancount', int,
41
+		description='Whether this cog is enabled for a guild.',
42
+	)
43
+	SETTING_BAN_COUNT = CogSetting(
44
+		'bancount',
45
+		int,
46
+		default_value=0,
40 47
 		brief='number of repeat kicks before a ban',
41 48
 		description='The number of times a user can join and be kicked '
42 49
 					'before the next rejoin will result in a ban. A value of 0 '
43 50
 					'disables this feature (only kick, never ban).',
44
-		usage='<count:int>',
45
-		min_value=0)
46
-	SETTING_OFFLINE_ONLY = CogSetting('offlineonly', bool,
51
+		min_value=0,
52
+	)
53
+	SETTING_OFFLINE_ONLY = CogSetting(
54
+		'offlineonly',
55
+		bool,
56
+		default_value=False,
47 57
 		brief='whether to only kick users whose status is offline',
48 58
 		description='Compromised accounts may have a status of offline. '
49 59
 					'If this setting is enabled, the user\'s status will be '
50 60
 					'checked a few seconds after joining. If it is offline '
51 61
 					'they will be kicked.',
52
-		usage='<true|false>')
62
+	)
53 63
 
54 64
 	STATE_KEY_RECENT_KICKS = "AutoKickCog.recent_joins"
55 65
 

+ 4
- 1
rocketbot/cogs/basecog.py Ver fichero

@@ -138,7 +138,10 @@ class BaseCog(Cog):
138 138
 			if value is not None:
139 139
 				return value
140 140
 		if use_cog_default_if_not_set:
141
-			return cls.get_cog_default(setting.name)
141
+			config_default = cls.get_cog_default(setting.name)
142
+			if config_default is not None:
143
+				return None
144
+			return setting.default_value
142 145
 		return None
143 146
 
144 147
 	@classmethod

+ 44
- 22
rocketbot/cogs/crosspostcog.py Ver fichero

@@ -45,51 +45,73 @@ class CrossPostCog(BaseCog, name='Crosspost Detection'):
45 45
 	often be for a reason or due to trying a failed post when connectivity is
46 46
 	poor. Minimum message length can be enforced for detection.
47 47
 	"""
48
-	SETTING_ENABLED = CogSetting('enabled', bool,
48
+	SETTING_ENABLED = CogSetting(
49
+		'enabled',
50
+		bool,
51
+		default_value=False,
49 52
 		brief='crosspost detection',
50
-		description='Whether crosspost detection is enabled.')
51
-	SETTING_WARN_COUNT = CogSetting('warncount', int,
53
+		description='Whether crosspost detection is enabled.',
54
+	)
55
+	SETTING_WARN_COUNT = CogSetting(
56
+		'warncount',
57
+		int,
58
+		default_value=5,
52 59
 		brief='number of messages to trigger a warning',
53 60
 		description='The number of unique channels messages are ' + \
54 61
 			'posted in by the same user to trigger a mod warning. The ' + \
55 62
 			'messages need not be identical (see dupewarncount).',
56
-		usage='<count:int>',
57
-		min_value=2)
58
-	SETTING_DUPE_WARN_COUNT = CogSetting('dupewarncount', int,
63
+		min_value=2,
64
+	)
65
+	SETTING_DUPE_WARN_COUNT = CogSetting(
66
+		'dupewarncount',
67
+		int,
68
+		default_value=3,
59 69
 		brief='number of identical messages to trigger a warning',
60 70
 		description='The number of unique channels identical messages are ' + \
61 71
 			'posted in by the same user to trigger a mod warning.',
62
-		usage='<count:int>',
63
-		min_value=2)
64
-	SETTING_BAN_COUNT = CogSetting('bancount', int,
72
+		min_value=2,
73
+	)
74
+	SETTING_BAN_COUNT = CogSetting(
75
+		'bancount',
76
+		int,
77
+		default_value=9999,
65 78
 		brief='number of messages to trigger a ban',
66 79
 		description='The number of unique channels messages are ' + \
67 80
 			'posted in by the same user to trigger an automatic ban. The ' + \
68 81
 			'messages need not be identical (see dupebancount). Set ' + \
69 82
 			'to a large value to effectively disable, e.g. 9999.',
70
-		usage='<count:int>',
71
-		min_value=2)
72
-	SETTING_DUPE_BAN_COUNT = CogSetting('dupebancount', int,
83
+		min_value=2,
84
+	)
85
+	SETTING_DUPE_BAN_COUNT = CogSetting(
86
+		'dupebancount',
87
+		int,
88
+		default_value=9999,
73 89
 		brief='number of identical messages to trigger a ban',
74 90
 		description='The number of unique channels identical messages are ' + \
75 91
 			'posted in by the same user to trigger an automatic ban. Set ' + \
76 92
 			'to a large value to effectively disable, e.g. 9999.',
77
-		usage='<count:int>',
78
-		min_value=2)
79
-	SETTING_MIN_LENGTH = CogSetting('minlength', int,
93
+		min_value=2,
94
+	)
95
+	SETTING_MIN_LENGTH = CogSetting(
96
+		'minlength',
97
+		int,
98
+		default_value=1,
80 99
 		brief='minimum message length',
81 100
 		description='The minimum number of characters in a message to be ' + \
82 101
 			'checked for duplicates. This can help ignore common short ' + \
83 102
 			'messages like "lol" or a single emoji.',
84
-		usage='<character_count:int>',
85
-		min_value=1)
86
-	SETTING_TIMESPAN = CogSetting('timespan', timedelta,
103
+		min_value=1,
104
+	)
105
+	SETTING_TIMESPAN = CogSetting(
106
+		'timespan',
107
+		timedelta,
108
+		default_value=timedelta(seconds=60),
87 109
 		brief='time window to look for dupe messages',
88
-		description='The number of seconds of message history to look at ' + \
89
-			'when looking for duplicates. Shorter values are preferred, ' + \
110
+		description='The number of seconds of message history to look at '
111
+			'when looking for duplicates. Shorter values are preferred, '
90 112
 			'both to detect bots and avoid excessive memory usage.',
91
-		usage='<seconds:int>',
92
-		min_value=timedelta(seconds=1))
113
+		min_value=timedelta(seconds=1),
114
+	)
93 115
 
94 116
 	STATE_KEY_RECENT_MESSAGES = "CrossPostCog.recent_messages"
95 117
 	STATE_KEY_SPAM_CONTEXT = "CrossPostCog.spam_context"

+ 27
- 17
rocketbot/cogs/joinraidcog.py Ver fichero

@@ -29,23 +29,33 @@ class JoinRaidCog(BaseCog, name='Join Raids'):
29 29
 	"""
30 30
 	Cog for monitoring member joins and detecting potential bot raids.
31 31
 	"""
32
-	SETTING_ENABLED = CogSetting('enabled', bool,
33
-			brief='join raid detection',
34
-			description='Whether this cog is enabled for a guild.')
35
-	SETTING_JOIN_COUNT = CogSetting('joincount', int,
36
-			brief='number of joins to trigger a warning',
37
-			description='The number of joins occuring within the time ' + \
38
-				'window to trigger a mod warning.',
39
-			usage='<count:int>',
40
-			min_value=2)
41
-	SETTING_JOIN_TIME = CogSetting('jointime', timedelta,
42
-			brief='time window length to look for joins',
43
-			description='The number of seconds of join history to look ' + \
44
-				'at when counting recent joins. If joincount or more ' + \
45
-				'joins occur within jointime seconds a mod warning is issued.',
46
-			usage='<seconds:float>',
47
-			min_value=timedelta(seconds=1.0),
48
-			max_value=timedelta(seconds=900.0))
32
+	SETTING_ENABLED = CogSetting(
33
+		'enabled',
34
+		bool,
35
+		default_value=False,
36
+		brief='join raid detection',
37
+		description='Whether this cog is enabled for a guild.',
38
+	)
39
+	SETTING_JOIN_COUNT = CogSetting(
40
+		'joincount',
41
+		int,
42
+		default_value=5,
43
+		brief='number of joins to trigger a warning',
44
+		description='The number of joins occurring within the time '
45
+					'window to trigger a mod warning.',
46
+		min_value=2,
47
+	)
48
+	SETTING_JOIN_TIME = CogSetting(
49
+		'jointime',
50
+		timedelta,
51
+		default_value=timedelta(seconds=5),
52
+		brief='time window length to look for joins',
53
+		description='The number of seconds of join history to look '
54
+					'at when counting recent joins. If joincount or more '
55
+				    'joins occur within jointime seconds a mod warning is issued.',
56
+		min_value=timedelta(seconds=1.0),
57
+		max_value=timedelta(seconds=900.0),
58
+	)
49 59
 
50 60
 	STATE_KEY_RECENT_JOINS = "JoinRaidCog.recent_joins"
51 61
 	STATE_KEY_LAST_RAID = "JoinRaidCog.last_raid"

+ 7
- 3
rocketbot/cogs/logcog.py Ver fichero

@@ -38,9 +38,13 @@ class LoggingCog(BaseCog, name='Logging'):
38 38
 	"""
39 39
 	Cog for logging notable events to a designated logging channel.
40 40
 	"""
41
-	SETTING_ENABLED = CogSetting('enabled', bool,
42
-			brief='logging',
43
-			description='Whether this cog is enabled for a guild.')
41
+	SETTING_ENABLED = CogSetting(
42
+		'enabled',
43
+		bool,
44
+		default_value=False,
45
+		brief='logging',
46
+		description='Whether this cog is enabled for a guild.',
47
+	)
44 48
 
45 49
 	STATE_EVENT_BUFFER = 'LoggingCog.eventBuffer'
46 50
 

+ 1
- 1
rocketbot/cogs/patterncog.py Ver fichero

@@ -91,7 +91,7 @@ class PatternCog(BaseCog, name='Pattern Matching'):
91 91
 	various criteria. Patterns can be defined by mods for each guild.
92 92
 	"""
93 93
 
94
-	SETTING_PATTERNS = CogSetting('patterns', None)
94
+	SETTING_PATTERNS = CogSetting('patterns', None, default_value=None)
95 95
 
96 96
 	shared: Optional['PatternCog'] = None
97 97
 

+ 38
- 23
rocketbot/cogs/urlspamcog.py Ver fichero

@@ -29,29 +29,44 @@ class URLSpamCog(BaseCog, name='URL Spam'):
29 29
 	Can be configured to take immediate action or just warn the mods.
30 30
 	"""
31 31
 
32
-	SETTING_ENABLED = CogSetting('enabled', bool,
33
-			brief='URL spam detection',
34
-			description='Whether URLs posted soon after joining are flagged.')
35
-	SETTING_ACTION = CogSetting('action', Literal['nothing', 'modwarn', 'delete', 'kick', 'ban'],
36
-			brief='action to take on spam',
37
-			description='The action to take on detected URL spam.',
38
-			enum_values={'nothing', 'modwarn', 'delete', 'kick', 'ban'})
39
-	SETTING_JOIN_AGE = CogSetting('joinage', timedelta,
40
-			brief='seconds since member joined',
41
-			description='The minimum seconds since the user joined the '
42
-				'server before they can post URLs. URLs posted by users '
43
-				'who joined too recently will be flagged. Keep in mind '
44
-				'many servers have a minimum 10 minute cooldown before '
45
-				'new members can say anything. Setting to 0 effectively '
46
-				'disables URL spam detection.',
47
-			usage='<seconds:int>',
48
-			min_value=timedelta(seconds=0))
49
-	SETTING_DECEPTIVE_ACTION = CogSetting('deceptiveaction', Literal['nothing', 'modwarn', 'modwarndelete', 'chatwarn', 'chatwarndelete', 'delete', 'kick', 'ban'],
50
-			brief='action to take on deceptive link markdown',
51
-			description='The action to take on chat messages with links '
52
-				'where the text looks like a different URL than the actual link.',
53
-			enum_values={'nothing', 'modwarn', 'modwarndelete',
54
-				'chatwarn', 'chatwarndelete', 'delete', 'kick', 'ban'})
32
+	SETTING_ENABLED = CogSetting(
33
+		'enabled',
34
+		bool,
35
+		default_value=False,
36
+		brief='URL spam detection',
37
+		description='Whether URLs posted soon after joining are flagged.',
38
+	)
39
+	SETTING_ACTION = CogSetting(
40
+		'action',
41
+		Literal['nothing', 'modwarn', 'delete', 'kick', 'ban'],
42
+		default_value='nothing',
43
+		brief='action to take on spam',
44
+		description='The action to take on detected URL spam.',
45
+		enum_values={'nothing', 'modwarn', 'delete', 'kick', 'ban'},
46
+	)
47
+	SETTING_JOIN_AGE = CogSetting(
48
+		'joinage',
49
+		timedelta,
50
+		default_value=timedelta(minutes=15),
51
+		brief='seconds since member joined',
52
+		description='The minimum seconds since the user joined the '
53
+					'server before they can post URLs. URLs posted by users '
54
+				    'who joined too recently will be flagged. Keep in mind '
55
+				    'many servers have a minimum 10 minute cooldown before '
56
+				    'new members can say anything. Setting to 0 effectively '
57
+				    'disables URL spam detection.',
58
+		min_value=timedelta(seconds=0),
59
+	)
60
+	SETTING_DECEPTIVE_ACTION = CogSetting(
61
+		'deceptiveaction',
62
+		Literal['nothing', 'modwarn', 'modwarndelete', 'chatwarn', 'chatwarndelete', 'delete', 'kick', 'ban'],
63
+		default_value='nothing',
64
+		brief='action to take on deceptive link markdown',
65
+		description='The action to take on chat messages with links '
66
+					'where the text looks like a different URL than the actual link.',
67
+		enum_values={'nothing', 'modwarn', 'modwarndelete',
68
+					 'chatwarn', 'chatwarndelete', 'delete', 'kick', 'ban'},
69
+	)
55 70
 
56 71
 	def __init__(self, bot):
57 72
 		super().__init__(

+ 8
- 4
rocketbot/cogs/usernamecog.py Ver fichero

@@ -47,11 +47,15 @@ class UsernamePatternCog(BaseCog, name='Username Pattern'):
47 47
 	message on a match.
48 48
 	"""
49 49
 
50
-	SETTING_ENABLED = CogSetting('enabled', bool,
51
-			brief='username pattern detection',
52
-			description='Whether new users are checked for common patterns.')
50
+	SETTING_ENABLED = CogSetting(
51
+		'enabled',
52
+		bool,
53
+		default_value=False,
54
+		brief='username pattern detection',
55
+		description='Whether new users are checked for common patterns.',
56
+	)
53 57
 
54
-	SETTING_PATTERNS = CogSetting('patterns', None)
58
+	SETTING_PATTERNS = CogSetting('patterns', None, default_value=None)
55 59
 
56 60
 	def __init__(self, bot):
57 61
 		super().__init__(

+ 22
- 11
rocketbot/cogsetting.py Ver fichero

@@ -11,7 +11,8 @@ from discord.ext.commands import Bot
11 11
 
12 12
 from config import CONFIG
13 13
 from rocketbot.storage import Storage
14
-from rocketbot.utils import bot_log, TimeDeltaTransformer, MOD_PERMISSIONS, dump_stacktrace
14
+from rocketbot.utils import bot_log, TimeDeltaTransformer, MOD_PERMISSIONS, dump_stacktrace, str_from_timedelta
15
+
15 16
 
16 17
 # def _fix_command(command: Command) -> None:
17 18
 # 	"""
@@ -35,9 +36,9 @@ class CogSetting:
35 36
 	def __init__(self,
36 37
 			name: str,
37 38
 			datatype: Optional[Type],
39
+			default_value: Any,
38 40
 			brief: Optional[str] = None,
39 41
 			description: Optional[str] = None,
40
-			usage: Optional[str] = None,
41 42
 			min_value: Optional[Any] = None,
42 43
 			max_value: Optional[Any] = None,
43 44
 			enum_values: Optional[set[Any]] = None):
@@ -48,6 +49,9 @@ class CogSetting:
48 49
 			Setting identifier. Must follow variable naming conventions.
49 50
 		datatype: Optional[Type]
50 51
 		    Datatype of the setting. E.g. int, float, str
52
+		default_value: Any
53
+			Value to use in the absence of a specified value for the guild and
54
+			no value in config.py.
51 55
 		brief: Optional[str]
52 56
 			Description of the setting, starting with lower case.
53 57
 			Will be inserted into phrases like "Sets <brief>" and
@@ -55,8 +59,6 @@ class CogSetting:
55 59
 		description: Optional[str]
56 60
 		  	Long-form description. Min, max, and enum values will be
57 61
 		    appended to the end, so does not need to include these.
58
-		usage: Optional[str]
59
-		    Description of the value argument in a set command, e.g. "<maxcount:int>"
60 62
 		min_value: Optional[Any]
61 63
 		    Smallest allowable value. Must be of the same datatype as
62 64
 		    the value. None for no minimum.
@@ -67,9 +69,9 @@ class CogSetting:
67 69
 		"""
68 70
 		self.name: str = name
69 71
 		self.datatype: Type = datatype
72
+		self.default_value = default_value
70 73
 		self.brief: Optional[str] = brief
71 74
 		self.description: str = description or ''  # Can't be None
72
-		self.usage: Optional[str] = usage
73 75
 		self.min_value: Optional[Any] = min_value
74 76
 		self.max_value: Optional[Any] = max_value
75 77
 		self.enum_values: Optional[set[Any]] = enum_values
@@ -117,6 +119,15 @@ class CogSetting:
117 119
 			return timedelta(seconds=stored_value)
118 120
 		return stored_value
119 121
 
122
+	def native_value_to_str(self, native_value: Any) -> str:
123
+		if native_value is None:
124
+			return '<no value>'
125
+		if isinstance(native_value, timedelta):
126
+			return str_from_timedelta(native_value)
127
+		if isinstance(native_value, bool):
128
+			return 'true' if native_value else 'false'
129
+		return f'{native_value}'
130
+
120 131
 	def __make_getter_command(self, cog: 'BaseCog') -> Command:
121 132
 		setting: CogSetting = self
122 133
 		setting_name = setting.name
@@ -130,12 +141,12 @@ class CogSetting:
130 141
 			if value is None:
131 142
 				value = setting.to_native_value(cog0.get_cog_default(setting.name))
132 143
 				await interaction.response.send_message(
133
-					f'{CONFIG["info_emoji"]} `{setting_name}` is using default of `{value}`',
144
+					f'{CONFIG["info_emoji"]} `{setting_name}` is using default of `{setting.native_value_to_str(value)}`',
134 145
 					ephemeral=True
135 146
 				)
136 147
 			else:
137 148
 				await interaction.response.send_message(
138
-					f'{CONFIG["info_emoji"]} `{setting_name}` is set to `{value}`',
149
+					f'{CONFIG["info_emoji"]} `{setting_name}` is set to `{setting.native_value_to_str(value)}`',
139 150
 					ephemeral=True
140 151
 				)
141 152
 
@@ -178,7 +189,7 @@ class CogSetting:
178 189
 			key = f'{cog0.__class__.__name__}.{setting.name}'
179 190
 			Storage.set_config_value(interaction.guild, key, setting.to_stored_value(new_value))
180 191
 			await interaction.response.send_message(
181
-				f'{CONFIG["success_emoji"]} `{setting_name}` is now set to `{new_value}`',
192
+				f'{CONFIG["success_emoji"]} `{setting_name}` is now set to `{setting.to_native_value(new_value)}`',
182 193
 				ephemeral=True
183 194
 			)
184 195
 			await cog0.on_setting_updated(interaction.guild, setting)
@@ -410,12 +421,12 @@ class CogSetting:
410 421
 							if value is not None:
411 422
 								text += '**' + ('enabled' if value else 'disabled') + '**'
412 423
 							else:
413
-								text += ('enabled' if deflt else 'disabled') + ' _(using default)_'
424
+								text += ('enabled' if deflt else 'disabled') + ' _(default)_'
414 425
 						else:
415 426
 							if value is not None:
416
-								text += f'\n- `{bcog.config_prefix}_{setting.name}` = **{value}**'
427
+								text += f'\n- `{bcog.config_prefix}_{setting.name}` = **{setting.native_value_to_str(value)}**'
417 428
 							else:
418
-								text += f'\n- `{bcog.config_prefix}_{setting.name}` = {deflt} _(using default)_'
429
+								text += f'\n- `{bcog.config_prefix}_{setting.name}` = {setting.native_value_to_str(deflt)} _(using default)_'
419 430
 				await interaction.response.send_message(
420 431
 					text,
421 432
 					ephemeral=True,

Loading…
Cancelar
Guardar