From 9b63db31fc9b6fe6a726f711383cb38b2c44bd40 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Sat, 3 Oct 2020 20:38:46 -0400 Subject: Replace `map` with a more pythonic list comprehension. --- bot/exts/info/site.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/info/site.py b/bot/exts/info/site.py index 2d3a3d9f3..9e7f6b0a5 100644 --- a/bot/exts/info/site.py +++ b/bot/exts/info/site.py @@ -129,7 +129,7 @@ class Site(Cog): ) if invalid_indices: - indices = ', '.join(map(str, invalid_indices)) + indices = ', '.join(str(index) for index in invalid_indices) await ctx.send(f":x: Invalid rule indices: {indices}") return -- cgit v1.2.3 From bfcd4689b8cf8f0d1a26ffc1e1b0b4b9b9e9b59d Mon Sep 17 00:00:00 2001 From: Den4200 Date: Sat, 3 Oct 2020 20:40:37 -0400 Subject: Remove duplicates from given rule indices and sort them in order. --- bot/exts/info/site.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bot/exts/info/site.py b/bot/exts/info/site.py index 9e7f6b0a5..c8ae8dc96 100644 --- a/bot/exts/info/site.py +++ b/bot/exts/info/site.py @@ -122,10 +122,14 @@ class Site(Cog): return full_rules = await self.bot.api_client.get('rules', params={'link_format': 'md'}) - invalid_indices = tuple( - pick - for pick in rules - if pick < 1 or pick > len(full_rules) + + # Remove duplicates and sort the invalid rule indices + invalid_indices = sorted( + set( + pick + for pick in rules + if pick < 1 or pick > len(full_rules) + ) ) if invalid_indices: @@ -136,6 +140,9 @@ class Site(Cog): for rule in rules: self.bot.stats.incr(f"rule_uses.{rule}") + # Remove duplicates and sort the rule indices + rules = sorted(set(rules)) + final_rules = tuple(f"**{pick}.** {full_rules[pick - 1]}" for pick in rules) await LinePaginator.paginate(final_rules, ctx, rules_embed, max_lines=3) -- cgit v1.2.3 From 6cc110cf93dd109e371dfae7ad93520920883ca8 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Sat, 3 Oct 2020 20:41:17 -0400 Subject: Use `Greedy` converter instead of the splat operator. --- bot/exts/info/site.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/info/site.py b/bot/exts/info/site.py index c8ae8dc96..12c1737a2 100644 --- a/bot/exts/info/site.py +++ b/bot/exts/info/site.py @@ -1,7 +1,7 @@ import logging from discord import Colour, Embed -from discord.ext.commands import Cog, Context, group +from discord.ext.commands import Cog, Context, Greedy, group from bot.bot import Bot from bot.constants import URLs @@ -105,7 +105,7 @@ class Site(Cog): await ctx.send(embed=embed) @site_group.command(name="rules", aliases=("r", "rule"), root_aliases=("rules", "rule")) - async def site_rules(self, ctx: Context, *rules: int) -> None: + async def site_rules(self, ctx: Context, rules: Greedy[int]) -> None: """Provides a link to all rules or, if specified, displays specific rule(s).""" rules_embed = Embed(title='Rules', color=Colour.blurple()) rules_embed.url = f"{PAGES_URL}/rules" -- cgit v1.2.3 From dcad46fd4637fadc16f69da6bb92dd3513f68d76 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Sat, 3 Oct 2020 21:10:10 -0400 Subject: Use `url` argument instead of setting it outside. --- bot/exts/info/site.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bot/exts/info/site.py b/bot/exts/info/site.py index 12c1737a2..bf2547895 100644 --- a/bot/exts/info/site.py +++ b/bot/exts/info/site.py @@ -107,8 +107,7 @@ class Site(Cog): @site_group.command(name="rules", aliases=("r", "rule"), root_aliases=("rules", "rule")) async def site_rules(self, ctx: Context, rules: Greedy[int]) -> None: """Provides a link to all rules or, if specified, displays specific rule(s).""" - rules_embed = Embed(title='Rules', color=Colour.blurple()) - rules_embed.url = f"{PAGES_URL}/rules" + rules_embed = Embed(title='Rules', color=Colour.blurple(), url=f'{PAGES_URL}/rules') if not rules: # Rules were not submitted. Return the default description. -- cgit v1.2.3 From 2553a1d35bf52681dc8b28327e15fbd3ec14910e Mon Sep 17 00:00:00 2001 From: Den4200 Date: Sun, 4 Oct 2020 11:20:08 -0400 Subject: Sort rules before determining invalid indices. This is to avoid sorting twice - once for invalid indices and again for send the rules. --- bot/exts/info/site.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/bot/exts/info/site.py b/bot/exts/info/site.py index bf2547895..fb5b99086 100644 --- a/bot/exts/info/site.py +++ b/bot/exts/info/site.py @@ -122,26 +122,17 @@ class Site(Cog): full_rules = await self.bot.api_client.get('rules', params={'link_format': 'md'}) - # Remove duplicates and sort the invalid rule indices - invalid_indices = sorted( - set( - pick - for pick in rules - if pick < 1 or pick > len(full_rules) - ) - ) + # Remove duplicates and sort the rule indices + rules = sorted(set(rules)) + invalid = ', '.join(str(index) for index in rules if index < 1 or index > len(full_rules)) - if invalid_indices: - indices = ', '.join(str(index) for index in invalid_indices) - await ctx.send(f":x: Invalid rule indices: {indices}") + if invalid: + await ctx.send(f":x: Invalid rule indices: {invalid}") return for rule in rules: self.bot.stats.incr(f"rule_uses.{rule}") - # Remove duplicates and sort the rule indices - rules = sorted(set(rules)) - final_rules = tuple(f"**{pick}.** {full_rules[pick - 1]}" for pick in rules) await LinePaginator.paginate(final_rules, ctx, rules_embed, max_lines=3) -- cgit v1.2.3