Browse Source

Fixing iter issue. More linter cleanup

tags/2.0.0
Rocketsoup 2 months ago
parent
commit
d32705d99d

+ 5
- 6
rocketbot/cogs/autokickcog.py View File

1
 from datetime import datetime, timedelta
1
 from datetime import datetime, timedelta
2
-from typing import cast
3
 
2
 
4
 from discord import Guild, Member, Status
3
 from discord import Guild, Member, Status
5
 from discord.ext import commands, tasks
4
 from discord.ext import commands, tasks
59
 		self.add_setting(AutoKickCog.SETTING_BAN_COUNT)
58
 		self.add_setting(AutoKickCog.SETTING_BAN_COUNT)
60
 		self.add_setting(AutoKickCog.SETTING_OFFLINE_ONLY)
59
 		self.add_setting(AutoKickCog.SETTING_OFFLINE_ONLY)
61
 		self.status_check_members = []
60
 		self.status_check_members = []
62
-		timer: Loop = cast(Loop, self.status_check_timer)
61
+		timer: Loop = self.status_check_timer
63
 		timer.start()
62
 		timer.start()
64
 
63
 
65
 	@commands.group(
64
 	@commands.group(
68
 	@commands.has_permissions(ban_members=True)
67
 	@commands.has_permissions(ban_members=True)
69
 	@commands.guild_only()
68
 	@commands.guild_only()
70
 	async def autokick(self, context: commands.Context):
69
 	async def autokick(self, context: commands.Context):
71
-		'Auto-kick'
70
+		"""Auto-kick"""
72
 		if context.invoked_subcommand is None:
71
 		if context.invoked_subcommand is None:
73
 			await context.send_help()
72
 			await context.send_help()
74
 
73
 
75
 	@commands.Cog.listener()
74
 	@commands.Cog.listener()
76
 	async def on_member_join(self, member: Member) -> None:
75
 	async def on_member_join(self, member: Member) -> None:
77
-		'Event handler'
76
+		"""Event handler"""
78
 		guild: Guild = member.guild
77
 		guild: Guild = member.guild
79
 		if not self.get_guild_setting(guild, self.SETTING_ENABLED):
78
 		if not self.get_guild_setting(guild, self.SETTING_ENABLED):
80
 			return
79
 			return
86
 
85
 
87
 	@tasks.loop(seconds=5.0)
86
 	@tasks.loop(seconds=5.0)
88
 	async def status_check_timer(self):
87
 	async def status_check_timer(self):
89
-		'Checks status of new members shortly after joining to see if they go offline'
88
+		"""Checks status of new members shortly after joining to see if they go offline"""
90
 		contexts = self.status_check_members.copy()
89
 		contexts = self.status_check_members.copy()
91
 		self.status_check_members = []
90
 		self.status_check_members = []
92
 		now = datetime.now()
91
 		now = datetime.now()
146
 
145
 
147
 	@staticmethod
146
 	@staticmethod
148
 	def ordinal(val: int):
147
 	def ordinal(val: int):
149
-		'Formats an integer with an ordinal suffix (English only)'
148
+		"""Formats an integer with an ordinal suffix (English only)"""
150
 		if val % 100 < 10 or val % 100 > 20:
149
 		if val % 100 < 10 or val % 100 > 20:
151
 			if val % 10 == 1:
150
 			if val % 10 == 1:
152
 				return f'{val}st'
151
 				return f'{val}st'

+ 3
- 3
rocketbot/cogs/basecog.py View File

38
 	def get_cog_default(cls, key: str):
38
 	def get_cog_default(cls, key: str):
39
 		"""
39
 		"""
40
 		Convenience method for getting a cog configuration default from
40
 		Convenience method for getting a cog configuration default from
41
-		`CONFIG['cogs'][<cogname>][<key>]`. These values are used for
41
+		`CONFIG['cogs'][<cog_name>][<key>]`. These values are used for
42
 		CogSettings when no guild-specific value is configured yet.
42
 		CogSettings when no guild-specific value is configured yet.
43
 		"""
43
 		"""
44
 		cogs: dict = CONFIG['cog_defaults']
44
 		cogs: dict = CONFIG['cog_defaults']
55
 		commands will be created instead which set the setting to True/False.
55
 		commands will be created instead which set the setting to True/False.
56
 
56
 
57
 		If the cog has a command group it will be detected automatically and
57
 		If the cog has a command group it will be detected automatically and
58
-		the commands added to that. Otherwise the commands will be added at
58
+		the commands added to that. Otherwise, the commands will be added at
59
 		the top level.
59
 		the top level.
60
 
60
 
61
 		Changes to settings can be detected by overriding `on_setting_updated`.
61
 		Changes to settings can be detected by overriding `on_setting_updated`.
97
 
97
 
98
 	@commands.Cog.listener()
98
 	@commands.Cog.listener()
99
 	async def on_ready(self):
99
 	async def on_ready(self):
100
-		'Event listener'
100
+		"""Event listener"""
101
 		if not self.are_settings_setup:
101
 		if not self.are_settings_setup:
102
 			self.are_settings_setup = True
102
 			self.are_settings_setup = True
103
 			CogSetting.set_up_all(self, self.bot, self.settings)
103
 			CogSetting.set_up_all(self, self.bot, self.settings)

+ 2
- 2
rocketbot/cogs/crosspostcog.py View File

233
 
233
 
234
 	@commands.Cog.listener()
234
 	@commands.Cog.listener()
235
 	async def on_message(self, message: Message):
235
 	async def on_message(self, message: Message):
236
-		'Event handler'
236
+		"""Event handler"""
237
 		if message.author is None or \
237
 		if message.author is None or \
238
 				message.author.bot or \
238
 				message.author.bot or \
239
 				message.channel is None or \
239
 				message.channel is None or \
251
 	@commands.has_permissions(ban_members=True)
251
 	@commands.has_permissions(ban_members=True)
252
 	@commands.guild_only()
252
 	@commands.guild_only()
253
 	async def crosspost(self, context: commands.Context):
253
 	async def crosspost(self, context: commands.Context):
254
-		'Crosspost detection command group'
254
+		"""Crosspost detection command group"""
255
 		if context.invoked_subcommand is None:
255
 		if context.invoked_subcommand is None:
256
 			await context.send_help()
256
 			await context.send_help()

+ 8
- 8
rocketbot/cogs/generalcog.py View File

28
 
28
 
29
 	@commands.Cog.listener()
29
 	@commands.Cog.listener()
30
 	async def on_connect(self):
30
 	async def on_connect(self):
31
-		'Event handler'
31
+		"""Event handler"""
32
 		if self.is_first_connect:
32
 		if self.is_first_connect:
33
 			self.log(None, 'Connected')
33
 			self.log(None, 'Connected')
34
 			self.is_first_connect = False
34
 			self.is_first_connect = False
38
 
38
 
39
 	@commands.Cog.listener()
39
 	@commands.Cog.listener()
40
 	async def on_disconnect(self):
40
 	async def on_disconnect(self):
41
-		'Event handler'
41
+		"""Event handler"""
42
 		self.log(None, 'Disconnected')
42
 		self.log(None, 'Disconnected')
43
 
43
 
44
 	@commands.Cog.listener()
44
 	@commands.Cog.listener()
45
 	async def on_ready(self):
45
 	async def on_ready(self):
46
-		'Event handler'
46
+		"""Event handler"""
47
 		self.log(None, 'Bot done initializing')
47
 		self.log(None, 'Bot done initializing')
48
 		self.is_ready = True
48
 		self.is_ready = True
49
 		if self.is_first_ready:
49
 		if self.is_first_ready:
52
 
52
 
53
 	@commands.Cog.listener()
53
 	@commands.Cog.listener()
54
 	async def on_resumed(self):
54
 	async def on_resumed(self):
55
-		'Event handler'
55
+		"""Event handler"""
56
 		self.log(None, 'Session resumed')
56
 		self.log(None, 'Session resumed')
57
 
57
 
58
 	@commands.command(
58
 	@commands.command(
64
 	@commands.has_permissions(ban_members=True)
64
 	@commands.has_permissions(ban_members=True)
65
 	@commands.guild_only()
65
 	@commands.guild_only()
66
 	async def testwarn(self, context):
66
 	async def testwarn(self, context):
67
-		'Command handler'
67
+		"""Command handler"""
68
 		if Storage.get_config_value(context.guild, ConfigKey.WARNING_CHANNEL_ID) is None:
68
 		if Storage.get_config_value(context.guild, ConfigKey.WARNING_CHANNEL_ID) is None:
69
 			await context.message.reply(
69
 			await context.message.reply(
70
 				f'{CONFIG["warning_emoji"]} No warning channel set!',
70
 				f'{CONFIG["warning_emoji"]} No warning channel set!',
82
 			'bot is working properly.',
82
 			'bot is working properly.',
83
 	)
83
 	)
84
 	async def hello(self, context):
84
 	async def hello(self, context):
85
-		'Command handler'
85
+		"""Command handler"""
86
 		await context.message.reply(
86
 		await context.message.reply(
87
 			f'Hey, {context.author.name}!',
87
 			f'Hey, {context.author.name}!',
88
 		 	mention_author=False)
88
 		 	mention_author=False)
95
 	@commands.has_permissions(administrator=True)
95
 	@commands.has_permissions(administrator=True)
96
 	@commands.guild_only()
96
 	@commands.guild_only()
97
 	async def shutdown(self, context: commands.Context):
97
 	async def shutdown(self, context: commands.Context):
98
-		'Command handler'
98
+		"""Command handler"""
99
 		await context.message.add_reaction('👋')
99
 		await context.message.add_reaction('👋')
100
 		await self.bot.close()
100
 		await self.bot.close()
101
 
101
 
110
 	@commands.has_permissions(manage_messages=True)
110
 	@commands.has_permissions(manage_messages=True)
111
 	@commands.guild_only()
111
 	@commands.guild_only()
112
 	async def deletemessages(self, context, user: str, age: str) -> None:
112
 	async def deletemessages(self, context, user: str, age: str) -> None:
113
-		'Command handler'
113
+		"""Command handler"""
114
 		member_id = self.__parse_member_id(user)
114
 		member_id = self.__parse_member_id(user)
115
 		if member_id is None:
115
 		if member_id is None:
116
 			await context.message.reply(
116
 			await context.message.reply(

+ 8
- 8
rocketbot/cogs/logcog.py View File

31
 	@commands.has_permissions(ban_members=True)
31
 	@commands.has_permissions(ban_members=True)
32
 	@commands.guild_only()
32
 	@commands.guild_only()
33
 	async def logging(self, context: commands.Context):
33
 	async def logging(self, context: commands.Context):
34
-		'Logging command group'
34
+		"""Logging command group"""
35
 		if context.invoked_subcommand is None:
35
 		if context.invoked_subcommand is None:
36
 			await context.send_help()
36
 			await context.send_help()
37
 
37
 
125
 	@commands.Cog.listener()
125
 	@commands.Cog.listener()
126
 	async def on_invite_create(self, invite: Invite) -> None:
126
 	async def on_invite_create(self, invite: Invite) -> None:
127
 		"""
127
 		"""
128
-		Called when an Invite is created. You must have manage_channels to receive this.
128
+		Called when an `Invite` is created. You must have manage_channels to receive this.
129
 
129
 
130
 		https://discordpy.readthedocs.io/en/stable/api.html#discord.on_invite_create
130
 		https://discordpy.readthedocs.io/en/stable/api.html#discord.on_invite_create
131
 		"""
131
 		"""
143
 	@commands.Cog.listener()
143
 	@commands.Cog.listener()
144
 	async def on_invite_delete(self, invite: Invite) -> None:
144
 	async def on_invite_delete(self, invite: Invite) -> None:
145
 		"""
145
 		"""
146
-		Called when an Invite is deleted. You must have manage_channels to receive this.
146
+		Called when an `Invite` is deleted. You must have manage_channels to receive this.
147
 
147
 
148
 		https://discordpy.readthedocs.io/en/stable/api.html#discord.on_invite_delete
148
 		https://discordpy.readthedocs.io/en/stable/api.html#discord.on_invite_delete
149
 		"""
149
 		"""
305
 			changes.append(f'Guild avatar: <{before_guild_avatar}> -> <{after_guild_avatar}>')
305
 			changes.append(f'Guild avatar: <{before_guild_avatar}> -> <{after_guild_avatar}>')
306
 		if after.flags != before.flags:
306
 		if after.flags != before.flags:
307
 			flag_changes = []
307
 			flag_changes = []
308
-			for (name, after_value) in after.iter():
309
-				before_value = getattr(before, name)
308
+			for (name, after_value) in iter(after.flags):
309
+				before_value = getattr(before.flags, name)
310
 				if after_value != before_value:
310
 				if after_value != before_value:
311
 					flag_changes.append(f'`{name}` = `{before_value}` -> `{after_value}`')
311
 					flag_changes.append(f'`{name}` = `{before_value}` -> `{after_value}`')
312
 			if len(flag_changes) > 0:
312
 			if len(flag_changes) > 0:
315
 		if len(changes) == 0:
315
 		if len(changes) == 0:
316
 			return
316
 			return
317
 		text = f'Details for member {self.__describe_user(before)} changed:\n'
317
 		text = f'Details for member {self.__describe_user(before)} changed:\n'
318
-		text += '* ' + '\n* '.join(changes)
318
+		text += '* ' + ('\n* '.join(changes))
319
 		bot_message = BotMessage(guild, text, BotMessage.TYPE_LOG)
319
 		bot_message = BotMessage(guild, text, BotMessage.TYPE_LOG)
320
 		await bot_message.update()
320
 		await bot_message.update()
321
 
321
 
713
 
713
 
714
 	def __describe_user(self, user: Union[User, Member]) -> str:
714
 	def __describe_user(self, user: Union[User, Member]) -> str:
715
 		"""
715
 		"""
716
-		Standardized markdown describing a user or member.
716
+		Standardized Markdown describing a user or member.
717
 		"""
717
 		"""
718
 		return f'**{user.name}** ({user.display_name} {user.id})'
718
 		return f'**{user.name}** ({user.display_name} {user.id})'
719
 
719
 
779
 		markdown_a = re.sub(r'[\ue000-\uefff]', unsub_token, markdown_a)
779
 		markdown_a = re.sub(r'[\ue000-\uefff]', unsub_token, markdown_a)
780
 		markdown_b = re.sub(r'[\ue000-\uefff]', unsub_token, markdown_b)
780
 		markdown_b = re.sub(r'[\ue000-\uefff]', unsub_token, markdown_b)
781
 
781
 
782
-		return (markdown_a, markdown_b)
782
+		return markdown_a, markdown_b

+ 7
- 7
rocketbot/cogs/patterncog.py View File

30
 class PatternCog(BaseCog, name='Pattern Matching'):
30
 class PatternCog(BaseCog, name='Pattern Matching'):
31
 	"""
31
 	"""
32
 	Highly flexible cog for performing various actions on messages that match
32
 	Highly flexible cog for performing various actions on messages that match
33
-	various critera. Patterns can be defined by mods for each guild.
33
+	various criteria. Patterns can be defined by mods for each guild.
34
 	"""
34
 	"""
35
 
35
 
36
 	SETTING_PATTERNS = CogSetting('patterns', None)
36
 	SETTING_PATTERNS = CogSetting('patterns', None)
82
 
82
 
83
 	@commands.Cog.listener()
83
 	@commands.Cog.listener()
84
 	async def on_message(self, message: Message) -> None:
84
 	async def on_message(self, message: Message) -> None:
85
-		'Event listener'
85
+		"""Event listener"""
86
 		if message.author is None or \
86
 		if message.author is None or \
87
 				message.author.bot or \
87
 				message.author.bot or \
88
 				message.channel is None or \
88
 				message.channel is None or \
195
 	@commands.has_permissions(ban_members=True)
195
 	@commands.has_permissions(ban_members=True)
196
 	@commands.guild_only()
196
 	@commands.guild_only()
197
 	async def pattern(self, context: commands.Context):
197
 	async def pattern(self, context: commands.Context):
198
-		'Message pattern matching command group'
198
+		"""Message pattern matching command group"""
199
 		if context.invoked_subcommand is None:
199
 		if context.invoked_subcommand is None:
200
 			await context.send_help()
200
 			await context.send_help()
201
 
201
 
209
 		ignore_extra=True
209
 		ignore_extra=True
210
 	)
210
 	)
211
 	async def add(self, context: commands.Context, name: str):
211
 	async def add(self, context: commands.Context, name: str):
212
-		'Command handler'
212
+		"""Command handler"""
213
 		pattern_str = PatternCompiler.expression_str_from_context(context, name)
213
 		pattern_str = PatternCompiler.expression_str_from_context(context, name)
214
 		try:
214
 		try:
215
 			statement = PatternCompiler.parse_statement(name, pattern_str)
215
 			statement = PatternCompiler.parse_statement(name, pattern_str)
230
 		usage='<pattern_name>'
230
 		usage='<pattern_name>'
231
 	)
231
 	)
232
 	async def remove(self, context: commands.Context, name: str):
232
 	async def remove(self, context: commands.Context, name: str):
233
-		'Command handler'
233
+		"""Command handler"""
234
 		patterns = self.__get_patterns(context.guild)
234
 		patterns = self.__get_patterns(context.guild)
235
 		if patterns.get(name) is not None:
235
 		if patterns.get(name) is not None:
236
 			del patterns[name]
236
 			del patterns[name]
247
 		brief='Lists all patterns'
247
 		brief='Lists all patterns'
248
 	)
248
 	)
249
 	async def list(self, context: commands.Context) -> None:
249
 	async def list(self, context: commands.Context) -> None:
250
-		'Command handler'
250
+		"""Command handler"""
251
 		patterns = self.__get_patterns(context.guild)
251
 		patterns = self.__get_patterns(context.guild)
252
 		if len(patterns) == 0:
252
 		if len(patterns) == 0:
253
 			await context.message.reply('No patterns defined.', mention_author=False)
253
 			await context.message.reply('No patterns defined.', mention_author=False)
265
 			'priority is 100.',
265
 			'priority is 100.',
266
 	)
266
 	)
267
 	async def setpriority(self, context: commands.Context, name: str, priority: int) -> None:
267
 	async def setpriority(self, context: commands.Context, name: str, priority: int) -> None:
268
-		'Command handler'
268
+		"""Command handler"""
269
 		patterns = self.__get_patterns(context.guild)
269
 		patterns = self.__get_patterns(context.guild)
270
 		statement = patterns.get(name)
270
 		statement = patterns.get(name)
271
 		if statement is None:
271
 		if statement is None:

+ 9
- 9
rocketbot/cogs/urlspamcog.py View File

64
 	@commands.has_permissions(ban_members=True)
64
 	@commands.has_permissions(ban_members=True)
65
 	@commands.guild_only()
65
 	@commands.guild_only()
66
 	async def urlspam(self, context: commands.Context):
66
 	async def urlspam(self, context: commands.Context):
67
-		'URL spam command group'
67
+		"""URL spam command group"""
68
 		if context.invoked_subcommand is None:
68
 		if context.invoked_subcommand is None:
69
 			await context.send_help()
69
 			await context.send_help()
70
 
70
 
71
 	@commands.Cog.listener()
71
 	@commands.Cog.listener()
72
 	async def on_message(self, message: Message):
72
 	async def on_message(self, message: Message):
73
-		'Event listener'
73
+		"""Event listener"""
74
 		if message.author is None or \
74
 		if message.author is None or \
75
 				message.author.bot or \
75
 				message.author.bot or \
76
 				message.guild is None or \
76
 				message.guild is None or \
83
 		await self.check_deceptive_links(message)
83
 		await self.check_deceptive_links(message)
84
 
84
 
85
 	async def check_message_recency(self, message: Message):
85
 	async def check_message_recency(self, message: Message):
86
-		'Checks if the message was sent too recently by a new user'
86
+		"""Checks if the message was sent too recently by a new user"""
87
 		action = self.get_guild_setting(message.guild, self.SETTING_ACTION)
87
 		action = self.get_guild_setting(message.guild, self.SETTING_ACTION)
88
 		join_seconds = self.get_guild_setting(message.guild, self.SETTING_JOIN_AGE)
88
 		join_seconds = self.get_guild_setting(message.guild, self.SETTING_JOIN_AGE)
89
 		min_join_age = timedelta(seconds=join_seconds)
89
 		min_join_age = timedelta(seconds=join_seconds)
143
 
143
 
144
 	async def check_deceptive_links(self, message: Message):
144
 	async def check_deceptive_links(self, message: Message):
145
 		"""
145
 		"""
146
-		Checks if the message contains deceptive URL markdown, e.g.
146
+		Checks if the message contains deceptive URL Markdown, e.g.
147
 		`[nicewebsite.com](https://evilwebsite.com)'`
147
 		`[nicewebsite.com](https://evilwebsite.com)'`
148
 		"""
148
 		"""
149
 		action = self.get_guild_setting(message.guild, self.SETTING_DECEPTIVE_ACTION)
149
 		action = self.get_guild_setting(message.guild, self.SETTING_DECEPTIVE_ACTION)
150
-		if action == None or action == 'nothing':
150
+		if action is None or action == 'nothing':
151
 			return
151
 			return
152
 
152
 
153
 		if not self.contains_deceptive_links(message.content):
153
 		if not self.contains_deceptive_links(message.content):
194
 				delete_message_days=1)
194
 				delete_message_days=1)
195
 
195
 
196
 	def contains_deceptive_links(self, content: str) -> bool:
196
 	def contains_deceptive_links(self, content: str) -> bool:
197
-		# Strip markdown that can safely contain URL sequences
197
+		# Strip Markdown that can safely contain URL sequences
198
 		content = re.sub(r'`[^`]+`', '', content)  # `inline code`
198
 		content = re.sub(r'`[^`]+`', '', content)  # `inline code`
199
 		content = re.sub(r'```.+?```', '', content, re.DOTALL)  # ``` code block ```
199
 		content = re.sub(r'```.+?```', '', content, re.DOTALL)  # ``` code block ```
200
 		matches = re.findall(r'\[([^]]+)]\(([^)]+)\)', content)
200
 		matches = re.findall(r'\[([^]]+)]\(([^)]+)\)', content)
227
 		return False
227
 		return False
228
 
228
 
229
 	def is_url(self, s: str):
229
 	def is_url(self, s: str):
230
-		'Tests if a string is strictly a URL'
230
+		"""Tests if a string is strictly a URL"""
231
 		ipv6_host_pattern = r'\[[0-9a-fA-F:]+\]'
231
 		ipv6_host_pattern = r'\[[0-9a-fA-F:]+\]'
232
 		ipv4_host_pattern = r'[0-9\.]+'
232
 		ipv4_host_pattern = r'[0-9\.]+'
233
 		hostname_pattern = r'[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+'
233
 		hostname_pattern = r'[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+'
235
 		port_pattern = '(?::[0-9]+)?'
235
 		port_pattern = '(?::[0-9]+)?'
236
 		path_pattern = r'(?:/[^ \]\)]*)?'
236
 		path_pattern = r'(?:/[^ \]\)]*)?'
237
 		pattern = r'^http[s]?://' + host_pattern + port_pattern + path_pattern + '$'
237
 		pattern = r'^http[s]?://' + host_pattern + port_pattern + path_pattern + '$'
238
-		return re.match(pattern, s, re.IGNORECASE) != None
238
+		return re.match(pattern, s, re.IGNORECASE) is not None
239
 
239
 
240
 	def is_casual_url(self, s: str):
240
 	def is_casual_url(self, s: str):
241
-		'Tests if a string is a "casual URL" with no scheme included'
241
+		"""Tests if a string is a "casual URL" with no scheme included"""
242
 		ipv6_host_pattern = r'\[[0-9a-fA-F:]+\]'
242
 		ipv6_host_pattern = r'\[[0-9a-fA-F:]+\]'
243
 		ipv4_host_pattern = r'[0-9\.]+'
243
 		ipv4_host_pattern = r'[0-9\.]+'
244
 		hostname_pattern = r'[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+'
244
 		hostname_pattern = r'[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+'

+ 6
- 6
rocketbot/cogs/usernamecog.py View File

79
 	@commands.has_permissions(ban_members=True)
79
 	@commands.has_permissions(ban_members=True)
80
 	@commands.guild_only()
80
 	@commands.guild_only()
81
 	async def username(self, context: commands.Context):
81
 	async def username(self, context: commands.Context):
82
-		'Username pattern command group'
82
+		"""Username pattern command group"""
83
 		if context.invoked_subcommand is None:
83
 		if context.invoked_subcommand is None:
84
 			await context.send_help()
84
 			await context.send_help()
85
 
85
 
89
 		usage='<pattern>'
89
 		usage='<pattern>'
90
 	)
90
 	)
91
 	async def add(self, context: commands.Context, pattern: str) -> None:
91
 	async def add(self, context: commands.Context, pattern: str) -> None:
92
-		'Command handler'
92
+		"""Command handler"""
93
 		norm_pattern = pattern.lower()
93
 		norm_pattern = pattern.lower()
94
 		patterns: list[str] = self.__get_patterns(context.guild)
94
 		patterns: list[str] = self.__get_patterns(context.guild)
95
 		if norm_pattern in patterns:
95
 		if norm_pattern in patterns:
105
 		usage='<pattern>'
105
 		usage='<pattern>'
106
 	)
106
 	)
107
 	async def remove(self, context: commands.Context, pattern: str) -> None:
107
 	async def remove(self, context: commands.Context, pattern: str) -> None:
108
-		'Command handler'
108
+		"""Command handler"""
109
 		norm_pattern = pattern.lower()
109
 		norm_pattern = pattern.lower()
110
 		guild: Guild = context.guild
110
 		guild: Guild = context.guild
111
 		patterns: list[str] = self.__get_patterns(guild)
111
 		patterns: list[str] = self.__get_patterns(guild)
121
 		brief='Lists username patterns'
121
 		brief='Lists username patterns'
122
 	)
122
 	)
123
 	async def list(self, context: commands.Context) -> None:
123
 	async def list(self, context: commands.Context) -> None:
124
-		'Command handler'
124
+		"""Command handler"""
125
 		guild: Guild = context.guild
125
 		guild: Guild = context.guild
126
 		patterns: list[str] = self.__get_patterns(guild)
126
 		patterns: list[str] = self.__get_patterns(guild)
127
 		if len(patterns) == 0:
127
 		if len(patterns) == 0:
132
 
132
 
133
 	@commands.Cog.listener()
133
 	@commands.Cog.listener()
134
 	async def on_member_join(self, member: Member) -> None:
134
 	async def on_member_join(self, member: Member) -> None:
135
-		'Event handler'
135
+		"""Event handler"""
136
 		for pattern in self.__get_patterns(member.guild):
136
 		for pattern in self.__get_patterns(member.guild):
137
 			if self.matches(pattern, member.name):
137
 			if self.matches(pattern, member.name):
138
 				await self.handle_match(member, pattern)
138
 				await self.handle_match(member, pattern)
140
 				await self.handle_match(member, pattern)
140
 				await self.handle_match(member, pattern)
141
 
141
 
142
 	def matches(self, pattern: str, subject: str) -> bool:
142
 	def matches(self, pattern: str, subject: str) -> bool:
143
-		'Checks if a username matches a given pattern'
143
+		"""Checks if a username matches a given pattern"""
144
 		if pattern is None:
144
 		if pattern is None:
145
 			return False
145
 			return False
146
 		if subject is None:
146
 		if subject is None:

+ 7
- 9
rocketbot/pattern.py View File

326
 				if ch in cls.STRING_QUOTE_CHARS:
326
 				if ch in cls.STRING_QUOTE_CHARS:
327
 					if len(current_token) > 0:
327
 					if len(current_token) > 0:
328
 						tokens.append(current_token)
328
 						tokens.append(current_token)
329
-						current_token = ''
330
 						possible_token_types |= all_token_types
329
 						possible_token_types |= all_token_types
331
 					in_quote = ch
330
 					in_quote = ch
332
 					current_token = ch
331
 					current_token = ch
403
 					cls.__validate_action(a)
402
 					cls.__validate_action(a)
404
 					actions.append(a)
403
 					actions.append(a)
405
 				token_index += 1
404
 				token_index += 1
406
-				return (actions, token_index)
405
+				return actions, token_index
407
 			elif token == ',':
406
 			elif token == ',':
408
 				if len(current_action_tokens) < 1:
407
 				if len(current_action_tokens) < 1:
409
 					raise PatternError('Unexpected ,')
408
 					raise PatternError('Unexpected ,')
449
 		while token_index < len(tokens):
448
 		while token_index < len(tokens):
450
 			if one_subexpression:
449
 			if one_subexpression:
451
 				if len(subexpressions) == 1:
450
 				if len(subexpressions) == 1:
452
-					return (subexpressions[0], token_index)
451
+					return subexpressions[0], token_index
453
 				if len(subexpressions) > 1:
452
 				if len(subexpressions) > 1:
454
 					raise PatternError('Too many subexpressions')
453
 					raise PatternError('Too many subexpressions')
455
-			compound_operator = None
456
 			if tokens[token_index] == ')':
454
 			if tokens[token_index] == ')':
457
 				if len(subexpressions) == 0:
455
 				if len(subexpressions) == 0:
458
 					raise PatternError('No subexpressions')
456
 					raise PatternError('No subexpressions')
459
 				if len(subexpressions) == 1:
457
 				if len(subexpressions) == 1:
460
-					return (subexpressions[0], token_index)
458
+					return subexpressions[0], token_index
461
 				return (PatternCompoundExpression(last_compound_operator,
459
 				return (PatternCompoundExpression(last_compound_operator,
462
 					subexpressions), token_index)
460
 					subexpressions), token_index)
463
 			if tokens[token_index] in { "and", "or" }:
461
 			if tokens[token_index] in { "and", "or" }:
490
 		if len(subexpressions) == 0:
488
 		if len(subexpressions) == 0:
491
 			raise PatternError('No subexpressions')
489
 			raise PatternError('No subexpressions')
492
 		elif len(subexpressions) == 1:
490
 		elif len(subexpressions) == 1:
493
-			return (subexpressions[0], token_index)
491
+			return subexpressions[0], token_index
494
 		else:
492
 		else:
495
-			return (PatternCompoundExpression(last_compound_operator,
496
-				subexpressions), token_index)
493
+			return PatternCompoundExpression(last_compound_operator,
494
+				subexpressions), token_index
497
 
495
 
498
 	@classmethod
496
 	@classmethod
499
 	def __read_simple_expression(cls,
497
 	def __read_simple_expression(cls,
546
 
544
 
547
 		token_index += 1
545
 		token_index += 1
548
 		exp = PatternSimpleExpression(field, op, value)
546
 		exp = PatternSimpleExpression(field, op, value)
549
-		return (exp, token_index)
547
+		return exp, token_index
550
 
548
 
551
 	@classmethod
549
 	@classmethod
552
 	def __parse_value(cls, value: str, datatype: str, op: str = None) -> Any:
550
 	def __parse_value(cls, value: str, datatype: str, op: str = None) -> Any:

Loading…
Cancel
Save