From 716699dedf6ec7afc76eb61d5402184d5a808dc7 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 19 May 2020 20:14:06 +0300 Subject: Source: Created initial cog layout + setup function --- bot/cogs/source.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 bot/cogs/source.py diff --git a/bot/cogs/source.py b/bot/cogs/source.py new file mode 100644 index 000000000..4897a16e3 --- /dev/null +++ b/bot/cogs/source.py @@ -0,0 +1,15 @@ +from discord.ext.commands import Cog + +from bot.bot import Bot + + +class Source(Cog): + """Cog of Python Discord project source information.""" + + def __init__(self, bot: Bot): + self.bot = bot + + +def setup(bot: Bot) -> None: + """Load `Source` cog.""" + bot.add_cog(Source(bot)) -- cgit v1.2.3 From c71895c99366f5096442420edde10c70e562a4dd Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 19 May 2020 20:21:58 +0300 Subject: Source: Create converter for source object converting --- bot/cogs/source.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 4897a16e3..76f75f83b 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -1,10 +1,39 @@ -from discord.ext.commands import Cog +from typing import Union + +from discord.ext.commands import BadArgument, Cog, Context, Converter, Command, HelpCommand from bot.bot import Bot +class SourceConverted(Converter): + """Convert argument to help command, command or Cog.""" + + async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog]: + """ + Convert argument into source object. + + Order how arguments is checked: + 1. When argument is `help`, return bot help command + 2. When argument is valid command, return this command + 3. When argument is valid Cog, return this Cog + 4. Otherwise raise `BadArgument` error + """ + if argument.lower() == "help": + return ctx.bot.help_command + + command = ctx.bot.get_command(argument) + if command: + return command + + cog = ctx.bot.get_cog(argument) + if cog: + return cog + + raise BadArgument(f"Unable to convert `{argument}` to help command, command or cog.") + + class Source(Cog): - """Cog of Python Discord project source information.""" + """Cog of Python Discord projects source information.""" def __init__(self, bot: Bot): self.bot = bot -- cgit v1.2.3 From a4ad94904638c94b44006d546861d399eba77ed5 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 08:46:27 +0300 Subject: Source: Create `get_source_link` function that build item's GitHub link --- bot/cogs/source.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 76f75f83b..1f9e0e84d 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -1,11 +1,14 @@ +import inspect +import os from typing import Union -from discord.ext.commands import BadArgument, Cog, Context, Converter, Command, HelpCommand +from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand from bot.bot import Bot +from bot.constants import URLs -class SourceConverted(Converter): +class SourceConverter(Converter): """Convert argument to help command, command or Cog.""" async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog]: @@ -38,6 +41,24 @@ class Source(Cog): def __init__(self, bot: Bot): self.bot = bot + @staticmethod + def get_source_link(source_item: Union[HelpCommand, Command, Cog]) -> str: + """Build GitHub link of source item.""" + if isinstance(source_item, HelpCommand): + src = type(source_item) + filename = inspect.getsourcefile(src) + elif isinstance(source_item, Command): + src = source_item.callback.__code__ + filename = src.co_filename + else: + src = type(source_item) + filename = inspect.getsourcefile(src) + + lines, first_line_no = inspect.getsourcelines(src) + file_location = os.path.relpath(filename) + + return f"{URLs.github_bot_repo}/blob/master/{file_location}#L{first_line_no}-L{first_line_no+len(lines)-1}" + def setup(bot: Bot) -> None: """Load `Source` cog.""" -- cgit v1.2.3 From 6e923cb6386e95b7ad56c9fc8d2374a0feffd49e Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 08:46:48 +0300 Subject: Source: Add cog loading to __main__.py --- bot/__main__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/__main__.py b/bot/__main__.py index aa1d1aee8..d82adc802 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -57,6 +57,7 @@ bot.load_extension("bot.cogs.reddit") bot.load_extension("bot.cogs.reminders") bot.load_extension("bot.cogs.site") bot.load_extension("bot.cogs.snekbox") +bot.load_extension("bot.cogs.source") bot.load_extension("bot.cogs.stats") bot.load_extension("bot.cogs.sync") bot.load_extension("bot.cogs.tags") -- cgit v1.2.3 From dfda0abf922d49688774aae8e6f9c0ba8e44b96d Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 09:10:05 +0300 Subject: Source: Create `build_embed` function that build embed of source item --- bot/cogs/source.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 1f9e0e84d..bce51aa80 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -2,11 +2,18 @@ import inspect import os from typing import Union +from discord import Embed from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand from bot.bot import Bot from bot.constants import URLs +CANT_RUN_MESSAGE = "You can't run this command here." +CAN_RUN_MESSAGE = "You are able to run this command." + +COG_CHECK_FAIL = "You can't use commands what is in this Cog here." +COG_CHECK_PASS = "You can use commands from this Cog." + class SourceConverter(Converter): """Convert argument to help command, command or Cog.""" @@ -59,6 +66,26 @@ class Source(Cog): return f"{URLs.github_bot_repo}/blob/master/{file_location}#L{first_line_no}-L{first_line_no+len(lines)-1}" + @staticmethod + async def build_embed(link: str, source_object: Union[HelpCommand, Command, Cog], ctx: Context) -> Embed: + """Build embed based on source object.""" + if isinstance(source_object, HelpCommand): + title = "Help" + description = source_object.__doc__ + else: + title = source_object.qualified_name + description = source_object.help + + embed = Embed(title=title, description=description, url=link) + embed.add_field(name="Source Code", value=f"[Go to GitHub]({link})") + + if isinstance(source_object, Command): + embed.set_footer(text=CAN_RUN_MESSAGE if await source_object.can_run(ctx) else CANT_RUN_MESSAGE) + elif isinstance(source_object, Cog): + embed.set_footer(text=COG_CHECK_PASS if source_object.cog_check(ctx) else COG_CHECK_FAIL) + + return embed + def setup(bot: Bot) -> None: """Load `Source` cog.""" -- cgit v1.2.3 From b3742b8050aff7edbaccfa9d1d95843f5bf201a0 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 09:11:50 +0300 Subject: Source: Create `source` command with alias `src` --- bot/cogs/source.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index bce51aa80..af29caaaa 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -3,7 +3,7 @@ import os from typing import Union from discord import Embed -from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand +from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand, group from bot.bot import Bot from bot.constants import URLs @@ -48,6 +48,12 @@ class Source(Cog): def __init__(self, bot: Bot): self.bot = bot + @group(name='source', aliases=('src',), invoke_without_command=True) + async def source_command(self, ctx: Context, *, source_item: SourceConverter) -> None: + """Get GitHub link and information about help command, command or Cog.""" + url = self.get_source_link(source_item) + await ctx.send(embed=await self.build_embed(url, source_item, ctx)) + @staticmethod def get_source_link(source_item: Union[HelpCommand, Command, Cog]) -> str: """Build GitHub link of source item.""" -- cgit v1.2.3 From ce34adbc64bacd15eeb1a2bfee7b0d022ec969eb Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 09:17:26 +0300 Subject: Source: Make `source` command to `command` instead `group` --- bot/cogs/source.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index af29caaaa..1774d0085 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -3,7 +3,7 @@ import os from typing import Union from discord import Embed -from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand, group +from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand, command from bot.bot import Bot from bot.constants import URLs @@ -48,7 +48,7 @@ class Source(Cog): def __init__(self, bot: Bot): self.bot = bot - @group(name='source', aliases=('src',), invoke_without_command=True) + @command(name="source", aliases=("src",)) async def source_command(self, ctx: Context, *, source_item: SourceConverter) -> None: """Get GitHub link and information about help command, command or Cog.""" url = self.get_source_link(source_item) -- cgit v1.2.3 From 1bb52f815e93c2e3d3fa565c150bbc5effff94f2 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 09:18:04 +0300 Subject: Source: Remove `command` shadowing on converter --- bot/cogs/source.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 1774d0085..c628c6b29 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -31,9 +31,9 @@ class SourceConverter(Converter): if argument.lower() == "help": return ctx.bot.help_command - command = ctx.bot.get_command(argument) - if command: - return command + cmd = ctx.bot.get_command(argument) + if cmd: + return cmd cog = ctx.bot.get_cog(argument) if cog: -- cgit v1.2.3 From e5239c4b20d2496cf5a96192981f050c87150acd Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 09:37:26 +0300 Subject: Source: Implement no argument GitHub repo response --- bot/cogs/source.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index c628c6b29..22b75e2ee 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -1,9 +1,9 @@ import inspect import os -from typing import Union +from typing import Optional, Union from discord import Embed -from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand, command +from discord.ext.commands import Cog, Command, Context, Converter, HelpCommand, command from bot.bot import Bot from bot.constants import URLs @@ -18,7 +18,7 @@ COG_CHECK_PASS = "You can use commands from this Cog." class SourceConverter(Converter): """Convert argument to help command, command or Cog.""" - async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog]: + async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog, None]: """ Convert argument into source object. @@ -39,7 +39,7 @@ class SourceConverter(Converter): if cog: return cog - raise BadArgument(f"Unable to convert `{argument}` to help command, command or cog.") + return None class Source(Cog): @@ -49,8 +49,14 @@ class Source(Cog): self.bot = bot @command(name="source", aliases=("src",)) - async def source_command(self, ctx: Context, *, source_item: SourceConverter) -> None: + async def source_command(self, ctx: Context, *, source_item: Optional[SourceConverter] = None) -> None: """Get GitHub link and information about help command, command or Cog.""" + if not source_item: + embed = Embed(title="Bot GitHub Repository", url=URLs.github_bot_repo) + embed.add_field(name="Repository", value=f"[Go to GitHub]({URLs.github_bot_repo})") + await ctx.send(embed=embed) + return + url = self.get_source_link(source_item) await ctx.send(embed=await self.build_embed(url, source_item, ctx)) -- cgit v1.2.3 From 6d0a1b0c9e3f278f2b660659efd89db3c4a3595a Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 09:40:26 +0300 Subject: Source: Fix `Cog` instance of source no `help` attribute --- bot/cogs/source.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 22b75e2ee..1820392f3 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -84,9 +84,12 @@ class Source(Cog): if isinstance(source_object, HelpCommand): title = "Help" description = source_object.__doc__ - else: + elif isinstance(source_object, Command): title = source_object.qualified_name description = source_object.help + else: + title = source_object.qualified_name + description = source_object.description embed = Embed(title=title, description=description, url=link) embed.add_field(name="Source Code", value=f"[Go to GitHub]({link})") -- cgit v1.2.3 From 00445f54a8593ff14d2f9c9595be86f15ab78072 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 16:23:05 +0300 Subject: Source: Make converter raising `BadArgument` again --- bot/cogs/source.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 1820392f3..633cb8ccf 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -1,9 +1,9 @@ import inspect import os -from typing import Optional, Union +from typing import Union from discord import Embed -from discord.ext.commands import Cog, Command, Context, Converter, HelpCommand, command +from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand, command from bot.bot import Bot from bot.constants import URLs @@ -18,7 +18,7 @@ COG_CHECK_PASS = "You can use commands from this Cog." class SourceConverter(Converter): """Convert argument to help command, command or Cog.""" - async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog, None]: + async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog]: """ Convert argument into source object. @@ -39,7 +39,7 @@ class SourceConverter(Converter): if cog: return cog - return None + raise BadArgument(f"Unable to convert `{argument}` to valid command or Cog.") class Source(Cog): @@ -49,7 +49,7 @@ class Source(Cog): self.bot = bot @command(name="source", aliases=("src",)) - async def source_command(self, ctx: Context, *, source_item: Optional[SourceConverter] = None) -> None: + async def source_command(self, ctx: Context, *, source_item: SourceConverter = None) -> None: """Get GitHub link and information about help command, command or Cog.""" if not source_item: embed = Embed(title="Bot GitHub Repository", url=URLs.github_bot_repo) -- cgit v1.2.3 From dd05246e29fa46ff53456a499244373c23a24d06 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 16:23:58 +0300 Subject: Source: Remove links from title of embeds --- bot/cogs/source.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 633cb8ccf..b285b4089 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -52,7 +52,7 @@ class Source(Cog): async def source_command(self, ctx: Context, *, source_item: SourceConverter = None) -> None: """Get GitHub link and information about help command, command or Cog.""" if not source_item: - embed = Embed(title="Bot GitHub Repository", url=URLs.github_bot_repo) + embed = Embed(title="Bot GitHub Repository") embed.add_field(name="Repository", value=f"[Go to GitHub]({URLs.github_bot_repo})") await ctx.send(embed=embed) return @@ -91,7 +91,7 @@ class Source(Cog): title = source_object.qualified_name description = source_object.description - embed = Embed(title=title, description=description, url=link) + embed = Embed(title=title, description=description) embed.add_field(name="Source Code", value=f"[Go to GitHub]({link})") if isinstance(source_object, Command): -- cgit v1.2.3 From 36ef3514674812afab6c94b12b3f9d3768b324f5 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 16:25:26 +0300 Subject: Source: Remove Cog check displaying from command --- bot/cogs/source.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index b285b4089..220da535d 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -11,9 +11,6 @@ from bot.constants import URLs CANT_RUN_MESSAGE = "You can't run this command here." CAN_RUN_MESSAGE = "You are able to run this command." -COG_CHECK_FAIL = "You can't use commands what is in this Cog here." -COG_CHECK_PASS = "You can use commands from this Cog." - class SourceConverter(Converter): """Convert argument to help command, command or Cog.""" @@ -96,8 +93,6 @@ class Source(Cog): if isinstance(source_object, Command): embed.set_footer(text=CAN_RUN_MESSAGE if await source_object.can_run(ctx) else CANT_RUN_MESSAGE) - elif isinstance(source_object, Cog): - embed.set_footer(text=COG_CHECK_PASS if source_object.cog_check(ctx) else COG_CHECK_FAIL) return embed -- cgit v1.2.3 From e2ce4ac6f372a92cc8c31c09237521e6b3aeb23b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 16:35:11 +0300 Subject: Source: Rename cog + move checks status from footer to field - Renamed cog from `Source` to `BotSource` for itself (bot will be unable to get cog, because this always return command). - Moved checks status from footer to field and changed it's content. --- bot/cogs/source.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 220da535d..972507762 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -8,9 +8,6 @@ from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, from bot.bot import Bot from bot.constants import URLs -CANT_RUN_MESSAGE = "You can't run this command here." -CAN_RUN_MESSAGE = "You are able to run this command." - class SourceConverter(Converter): """Convert argument to help command, command or Cog.""" @@ -39,7 +36,7 @@ class SourceConverter(Converter): raise BadArgument(f"Unable to convert `{argument}` to valid command or Cog.") -class Source(Cog): +class BotSource(Cog): """Cog of Python Discord projects source information.""" def __init__(self, bot: Bot): @@ -92,11 +89,11 @@ class Source(Cog): embed.add_field(name="Source Code", value=f"[Go to GitHub]({link})") if isinstance(source_object, Command): - embed.set_footer(text=CAN_RUN_MESSAGE if await source_object.can_run(ctx) else CANT_RUN_MESSAGE) + embed.add_field(name="Can be used by you here?", value=await source_object.can_run(ctx)) return embed def setup(bot: Bot) -> None: """Load `Source` cog.""" - bot.add_cog(Source(bot)) + bot.add_cog(BotSource(bot)) -- cgit v1.2.3 From dc96a187cf7af6d4d1d39a325e3ffad67d3549a5 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 20 May 2020 16:38:56 +0300 Subject: Source: Fix description of cog --- bot/cogs/source.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 972507762..5b8d8ded2 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -37,7 +37,7 @@ class SourceConverter(Converter): class BotSource(Cog): - """Cog of Python Discord projects source information.""" + """Cog of Python Discord Python bot project source information.""" def __init__(self, bot: Bot): self.bot = bot -- cgit v1.2.3 From e5534bc73d4b2d7b4b87326ab3b729b955e7c344 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 08:26:23 +0300 Subject: Source: Fix docstrings Co-authored-by: Mark --- bot/cogs/source.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 5b8d8ded2..6e71ae5b2 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -10,7 +10,7 @@ from bot.constants import URLs class SourceConverter(Converter): - """Convert argument to help command, command or Cog.""" + """Convert an argument into a help command, command, or cog.""" async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog]: """ @@ -37,14 +37,14 @@ class SourceConverter(Converter): class BotSource(Cog): - """Cog of Python Discord Python bot project source information.""" + """Displays information about the bot's source code.""" def __init__(self, bot: Bot): self.bot = bot @command(name="source", aliases=("src",)) async def source_command(self, ctx: Context, *, source_item: SourceConverter = None) -> None: - """Get GitHub link and information about help command, command or Cog.""" + """Display information and a GitHub link to the source code of a command or cog.""" if not source_item: embed = Embed(title="Bot GitHub Repository") embed.add_field(name="Repository", value=f"[Go to GitHub]({URLs.github_bot_repo})") @@ -95,5 +95,5 @@ class BotSource(Cog): def setup(bot: Bot) -> None: - """Load `Source` cog.""" + """Load the BotSource cog.""" bot.add_cog(BotSource(bot)) -- cgit v1.2.3 From befbb1afed56b60336c8668a9134e28e069e0eac Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 08:47:46 +0300 Subject: Source: Remove checks running from source command --- bot/cogs/source.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 6e71ae5b2..7076c1eb3 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -52,7 +52,7 @@ class BotSource(Cog): return url = self.get_source_link(source_item) - await ctx.send(embed=await self.build_embed(url, source_item, ctx)) + await ctx.send(embed=await self.build_embed(url, source_item)) @staticmethod def get_source_link(source_item: Union[HelpCommand, Command, Cog]) -> str: @@ -73,7 +73,7 @@ class BotSource(Cog): return f"{URLs.github_bot_repo}/blob/master/{file_location}#L{first_line_no}-L{first_line_no+len(lines)-1}" @staticmethod - async def build_embed(link: str, source_object: Union[HelpCommand, Command, Cog], ctx: Context) -> Embed: + async def build_embed(link: str, source_object: Union[HelpCommand, Command, Cog]) -> Embed: """Build embed based on source object.""" if isinstance(source_object, HelpCommand): title = "Help" @@ -88,9 +88,6 @@ class BotSource(Cog): embed = Embed(title=title, description=description) embed.add_field(name="Source Code", value=f"[Go to GitHub]({link})") - if isinstance(source_object, Command): - embed.add_field(name="Can be used by you here?", value=await source_object.can_run(ctx)) - return embed -- cgit v1.2.3 From 756ed4286339a26e6e1e4edb7431a026d8817881 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 09:09:35 +0300 Subject: Source: Direct aliases to their original commands --- bot/cogs/source.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 7076c1eb3..0880dd62f 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -54,15 +54,20 @@ class BotSource(Cog): url = self.get_source_link(source_item) await ctx.send(embed=await self.build_embed(url, source_item)) - @staticmethod - def get_source_link(source_item: Union[HelpCommand, Command, Cog]) -> str: + def get_source_link(self, source_item: Union[HelpCommand, Command, Cog]) -> str: """Build GitHub link of source item.""" if isinstance(source_item, HelpCommand): src = type(source_item) filename = inspect.getsourcefile(src) elif isinstance(source_item, Command): - src = source_item.callback.__code__ - filename = src.co_filename + if source_item.cog_name == "Alias": + cmd_name = source_item.callback.__name__.replace("_alias", "") + cmd = self.bot.get_command(cmd_name.replace("_", " ")) + src = cmd.callback.__code__ + filename = src.co_filename + else: + src = source_item.callback.__code__ + filename = src.co_filename else: src = type(source_item) filename = inspect.getsourcefile(src) @@ -72,15 +77,20 @@ class BotSource(Cog): return f"{URLs.github_bot_repo}/blob/master/{file_location}#L{first_line_no}-L{first_line_no+len(lines)-1}" - @staticmethod - async def build_embed(link: str, source_object: Union[HelpCommand, Command, Cog]) -> Embed: + async def build_embed(self, link: str, source_object: Union[HelpCommand, Command, Cog]) -> Embed: """Build embed based on source object.""" if isinstance(source_object, HelpCommand): title = "Help" description = source_object.__doc__ elif isinstance(source_object, Command): + if source_object.cog_name == "Alias": + cmd_name = source_object.callback.__name__.replace("_alias", "") + cmd = self.bot.get_command(cmd_name.replace("_", " ")) + description = cmd.help + else: + description = source_object.help + title = source_object.qualified_name - description = source_object.help else: title = source_object.qualified_name description = source_object.description -- cgit v1.2.3 From a064e2b6b4f6fabdb6a0fae5de5ee957dbb85b75 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 09:31:07 +0300 Subject: Source: Implement tags file showing to source command --- bot/cogs/source.py | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 0880dd62f..1c702f81d 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -10,21 +10,22 @@ from bot.constants import URLs class SourceConverter(Converter): - """Convert an argument into a help command, command, or cog.""" - - async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog]: - """ - Convert argument into source object. - - Order how arguments is checked: - 1. When argument is `help`, return bot help command - 2. When argument is valid command, return this command - 3. When argument is valid Cog, return this Cog - 4. Otherwise raise `BadArgument` error - """ + """Convert an argument into a help command, tag, command, or cog.""" + + async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog, str]: + """Convert argument into source object.""" if argument.lower() == "help": return ctx.bot.help_command + tags_cog = ctx.bot.get_cog("Tags") + + if argument.lower() in tags_cog._cache: + tag = argument.lower() + if tags_cog._cache[tag]["restricted_to"] != "developers": + return f"bot/resources/tags/{tags_cog._cache[tag]['restricted_to']}/{tag}.md" + else: + return f"bot/resources/tags/{tag}.md" + cmd = ctx.bot.get_command(argument) if cmd: return cmd @@ -44,7 +45,7 @@ class BotSource(Cog): @command(name="source", aliases=("src",)) async def source_command(self, ctx: Context, *, source_item: SourceConverter = None) -> None: - """Display information and a GitHub link to the source code of a command or cog.""" + """Display information and a GitHub link to the source code of a command, tag, or cog.""" if not source_item: embed = Embed(title="Bot GitHub Repository") embed.add_field(name="Repository", value=f"[Go to GitHub]({URLs.github_bot_repo})") @@ -54,7 +55,7 @@ class BotSource(Cog): url = self.get_source_link(source_item) await ctx.send(embed=await self.build_embed(url, source_item)) - def get_source_link(self, source_item: Union[HelpCommand, Command, Cog]) -> str: + def get_source_link(self, source_item: Union[HelpCommand, Command, Cog, str]) -> str: """Build GitHub link of source item.""" if isinstance(source_item, HelpCommand): src = type(source_item) @@ -68,14 +69,21 @@ class BotSource(Cog): else: src = source_item.callback.__code__ filename = src.co_filename + elif isinstance(source_item, str): + filename = source_item else: src = type(source_item) filename = inspect.getsourcefile(src) - lines, first_line_no = inspect.getsourcelines(src) + if not isinstance(source_item, str): + lines, first_line_no = inspect.getsourcelines(src) + lines_extension = f"#L{first_line_no}-L{first_line_no+len(lines)-1}" + else: + lines_extension = "" + file_location = os.path.relpath(filename) - return f"{URLs.github_bot_repo}/blob/master/{file_location}#L{first_line_no}-L{first_line_no+len(lines)-1}" + return f"{URLs.github_bot_repo}/blob/master/{file_location}{lines_extension}" async def build_embed(self, link: str, source_object: Union[HelpCommand, Command, Cog]) -> Embed: """Build embed based on source object.""" @@ -91,6 +99,9 @@ class BotSource(Cog): description = source_object.help title = source_object.qualified_name + elif isinstance(source_object, str): + title = f"Tag: {source_object.split('/')[-1].split('.')[0]}" + description = "" else: title = source_object.qualified_name description = source_object.description -- cgit v1.2.3 From bb6cc2193cad398d68db29d4f991fce94ae06549 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 09:44:34 +0300 Subject: Source: Migrate from os.path to Path --- bot/cogs/source.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 1c702f81d..21f18f45f 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -1,5 +1,5 @@ import inspect -import os +from pathlib import Path from typing import Union from discord import Embed @@ -22,9 +22,9 @@ class SourceConverter(Converter): if argument.lower() in tags_cog._cache: tag = argument.lower() if tags_cog._cache[tag]["restricted_to"] != "developers": - return f"bot/resources/tags/{tags_cog._cache[tag]['restricted_to']}/{tag}.md" + return f"/bot/bot/resources/tags/{tags_cog._cache[tag]['restricted_to']}/{tag}.md" else: - return f"bot/resources/tags/{tag}.md" + return f"/bot/bot/resources/tags/{tag}.md" cmd = ctx.bot.get_command(argument) if cmd: @@ -74,6 +74,7 @@ class BotSource(Cog): else: src = type(source_item) filename = inspect.getsourcefile(src) + print(filename) if not isinstance(source_item, str): lines, first_line_no = inspect.getsourcelines(src) @@ -81,7 +82,8 @@ class BotSource(Cog): else: lines_extension = "" - file_location = os.path.relpath(filename) + file_location = Path(filename).relative_to("/bot/") + print(file_location) return f"{URLs.github_bot_repo}/blob/master/{file_location}{lines_extension}" -- cgit v1.2.3 From 9db1377d8c4796d0e2eedc8eeee039566a7770b5 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 09:46:15 +0300 Subject: Source: Move big unions to variable of type --- bot/cogs/source.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 21f18f45f..40200eb69 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -8,11 +8,13 @@ from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, from bot.bot import Bot from bot.constants import URLs +SourceType = Union[HelpCommand, Command, Cog, str] + class SourceConverter(Converter): """Convert an argument into a help command, tag, command, or cog.""" - async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog, str]: + async def convert(self, ctx: Context, argument: str) -> SourceType: """Convert argument into source object.""" if argument.lower() == "help": return ctx.bot.help_command @@ -55,7 +57,7 @@ class BotSource(Cog): url = self.get_source_link(source_item) await ctx.send(embed=await self.build_embed(url, source_item)) - def get_source_link(self, source_item: Union[HelpCommand, Command, Cog, str]) -> str: + def get_source_link(self, source_item: SourceType) -> str: """Build GitHub link of source item.""" if isinstance(source_item, HelpCommand): src = type(source_item) @@ -87,7 +89,7 @@ class BotSource(Cog): return f"{URLs.github_bot_repo}/blob/master/{file_location}{lines_extension}" - async def build_embed(self, link: str, source_object: Union[HelpCommand, Command, Cog]) -> Embed: + async def build_embed(self, link: str, source_object: SourceType) -> Embed: """Build embed based on source object.""" if isinstance(source_object, HelpCommand): title = "Help" -- cgit v1.2.3 From 2db5add0893d090b7bdbc172ea2e83044301b2f6 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 09:54:01 +0300 Subject: Source: Implement file and line showing in source embed footer --- bot/cogs/source.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 40200eb69..fbe519c43 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -1,6 +1,6 @@ import inspect from pathlib import Path -from typing import Union +from typing import Optional, Tuple, Union from discord import Embed from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand, command @@ -54,10 +54,10 @@ class BotSource(Cog): await ctx.send(embed=embed) return - url = self.get_source_link(source_item) - await ctx.send(embed=await self.build_embed(url, source_item)) + url, location, first_line = self.get_source_link(source_item) + await ctx.send(embed=await self.build_embed(url, source_item, location, first_line)) - def get_source_link(self, source_item: SourceType) -> str: + def get_source_link(self, source_item: SourceType) -> Tuple[str, str, Optional[int]]: """Build GitHub link of source item.""" if isinstance(source_item, HelpCommand): src = type(source_item) @@ -82,14 +82,15 @@ class BotSource(Cog): lines, first_line_no = inspect.getsourcelines(src) lines_extension = f"#L{first_line_no}-L{first_line_no+len(lines)-1}" else: + first_line_no = None lines_extension = "" file_location = Path(filename).relative_to("/bot/") - print(file_location) + url = f"{URLs.github_bot_repo}/blob/master/{file_location}{lines_extension}" - return f"{URLs.github_bot_repo}/blob/master/{file_location}{lines_extension}" + return url, file_location, first_line_no or None - async def build_embed(self, link: str, source_object: SourceType) -> Embed: + async def build_embed(self, link: str, source_object: SourceType, loc: str, first_line: Optional[int]) -> Embed: """Build embed based on source object.""" if isinstance(source_object, HelpCommand): title = "Help" @@ -112,6 +113,8 @@ class BotSource(Cog): embed = Embed(title=title, description=description) embed.add_field(name="Source Code", value=f"[Go to GitHub]({link})") + line_text = f":{first_line}" if first_line else "" + embed.set_footer(text=f"{loc}{line_text}") return embed -- cgit v1.2.3 From ff308186375eee5ddf734d9d0d49879c49995b29 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 11:53:28 +0300 Subject: Source: Show only first line of every source item docstring instead full --- bot/cogs/source.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index fbe519c43..232ee618e 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -76,7 +76,6 @@ class BotSource(Cog): else: src = type(source_item) filename = inspect.getsourcefile(src) - print(filename) if not isinstance(source_item, str): lines, first_line_no = inspect.getsourcelines(src) @@ -94,14 +93,14 @@ class BotSource(Cog): """Build embed based on source object.""" if isinstance(source_object, HelpCommand): title = "Help" - description = source_object.__doc__ + description = source_object.__doc__.splitlines()[1] elif isinstance(source_object, Command): if source_object.cog_name == "Alias": cmd_name = source_object.callback.__name__.replace("_alias", "") cmd = self.bot.get_command(cmd_name.replace("_", " ")) - description = cmd.help + description = cmd.short_doc else: - description = source_object.help + description = source_object.short_doc title = source_object.qualified_name elif isinstance(source_object, str): @@ -109,7 +108,7 @@ class BotSource(Cog): description = "" else: title = source_object.qualified_name - description = source_object.description + description = source_object.description.splitlines()[0] embed = Embed(title=title, description=description) embed.add_field(name="Source Code", value=f"[Go to GitHub]({link})") -- cgit v1.2.3 From 522c7489ea86d5c150145d610fc3a0fef7bd16bc Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 11:55:14 +0300 Subject: Source: Add thumbnail to source command bot repo embed --- bot/cogs/source.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 232ee618e..3f03f790c 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -51,6 +51,7 @@ class BotSource(Cog): if not source_item: embed = Embed(title="Bot GitHub Repository") embed.add_field(name="Repository", value=f"[Go to GitHub]({URLs.github_bot_repo})") + embed.set_thumbnail(url="https://avatars1.githubusercontent.com/u/9919") await ctx.send(embed=embed) return -- cgit v1.2.3 From ed8298cad287c2bc37566d2688b77dc64d62a7af Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 11:57:29 +0300 Subject: Source: Few text fixes, made help command detection better --- bot/cogs/source.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 3f03f790c..32f8d5e08 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -16,7 +16,7 @@ class SourceConverter(Converter): async def convert(self, ctx: Context, argument: str) -> SourceType: """Convert argument into source object.""" - if argument.lower() == "help": + if argument.lower().startswith("help"): return ctx.bot.help_command tags_cog = ctx.bot.get_cog("Tags") @@ -49,7 +49,7 @@ class BotSource(Cog): async def source_command(self, ctx: Context, *, source_item: SourceConverter = None) -> None: """Display information and a GitHub link to the source code of a command, tag, or cog.""" if not source_item: - embed = Embed(title="Bot GitHub Repository") + embed = Embed(title="Bot's GitHub Repository") embed.add_field(name="Repository", value=f"[Go to GitHub]({URLs.github_bot_repo})") embed.set_thumbnail(url="https://avatars1.githubusercontent.com/u/9919") await ctx.send(embed=embed) @@ -93,7 +93,7 @@ class BotSource(Cog): async def build_embed(self, link: str, source_object: SourceType, loc: str, first_line: Optional[int]) -> Embed: """Build embed based on source object.""" if isinstance(source_object, HelpCommand): - title = "Help" + title = "Help Command" description = source_object.__doc__.splitlines()[1] elif isinstance(source_object, Command): if source_object.cog_name == "Alias": -- cgit v1.2.3 From 0a4b365f5efdb31450647b8d628b3787142d4617 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 11:58:54 +0300 Subject: Source: Add command and cog prefixes to title of embed --- bot/cogs/source.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 32f8d5e08..9e6109ca2 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -103,12 +103,12 @@ class BotSource(Cog): else: description = source_object.short_doc - title = source_object.qualified_name + title = f"Command: {source_object.qualified_name}" elif isinstance(source_object, str): title = f"Tag: {source_object.split('/')[-1].split('.')[0]}" description = "" else: - title = source_object.qualified_name + title = f"Cog: {source_object.qualified_name}" description = source_object.description.splitlines()[0] embed = Embed(title=title, description=description) -- cgit v1.2.3 From 0007fb7e50f273108e70bcafbd736bfc75ce3e51 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 11:59:44 +0300 Subject: Source: In converter move cog checking before command --- bot/cogs/source.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 9e6109ca2..a5f90e490 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -28,14 +28,14 @@ class SourceConverter(Converter): else: return f"/bot/bot/resources/tags/{tag}.md" - cmd = ctx.bot.get_command(argument) - if cmd: - return cmd - cog = ctx.bot.get_cog(argument) if cog: return cog + cmd = ctx.bot.get_command(argument) + if cmd: + return cmd + raise BadArgument(f"Unable to convert `{argument}` to valid command or Cog.") -- cgit v1.2.3 From aa83e72bd28f822c6ba84d73c5be05c6aea5d59b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 12:01:26 +0300 Subject: Source: Simplify imports --- bot/cogs/source.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index a5f90e490..5668ab6c6 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -3,18 +3,18 @@ from pathlib import Path from typing import Optional, Tuple, Union from discord import Embed -from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand, command +from discord.ext import commands from bot.bot import Bot from bot.constants import URLs -SourceType = Union[HelpCommand, Command, Cog, str] +SourceType = Union[commands.HelpCommand, commands.Command, commands.Cog, str] -class SourceConverter(Converter): +class SourceConverter(commands.Converter): """Convert an argument into a help command, tag, command, or cog.""" - async def convert(self, ctx: Context, argument: str) -> SourceType: + async def convert(self, ctx: commands.Context, argument: str) -> SourceType: """Convert argument into source object.""" if argument.lower().startswith("help"): return ctx.bot.help_command @@ -36,17 +36,17 @@ class SourceConverter(Converter): if cmd: return cmd - raise BadArgument(f"Unable to convert `{argument}` to valid command or Cog.") + raise commands.BadArgument(f"Unable to convert `{argument}` to valid command or Cog.") -class BotSource(Cog): +class BotSource(commands.Cog): """Displays information about the bot's source code.""" def __init__(self, bot: Bot): self.bot = bot - @command(name="source", aliases=("src",)) - async def source_command(self, ctx: Context, *, source_item: SourceConverter = None) -> None: + @commands.command(name="source", aliases=("src",)) + async def source_command(self, ctx: commands.Context, *, source_item: SourceConverter = None) -> None: """Display information and a GitHub link to the source code of a command, tag, or cog.""" if not source_item: embed = Embed(title="Bot's GitHub Repository") @@ -60,10 +60,10 @@ class BotSource(Cog): def get_source_link(self, source_item: SourceType) -> Tuple[str, str, Optional[int]]: """Build GitHub link of source item.""" - if isinstance(source_item, HelpCommand): + if isinstance(source_item, commands.HelpCommand): src = type(source_item) filename = inspect.getsourcefile(src) - elif isinstance(source_item, Command): + elif isinstance(source_item, commands.Command): if source_item.cog_name == "Alias": cmd_name = source_item.callback.__name__.replace("_alias", "") cmd = self.bot.get_command(cmd_name.replace("_", " ")) @@ -92,10 +92,10 @@ class BotSource(Cog): async def build_embed(self, link: str, source_object: SourceType, loc: str, first_line: Optional[int]) -> Embed: """Build embed based on source object.""" - if isinstance(source_object, HelpCommand): + if isinstance(source_object, commands.HelpCommand): title = "Help Command" description = source_object.__doc__.splitlines()[1] - elif isinstance(source_object, Command): + elif isinstance(source_object, commands.Command): if source_object.cog_name == "Alias": cmd_name = source_object.callback.__name__.replace("_alias", "") cmd = self.bot.get_command(cmd_name.replace("_", " ")) -- cgit v1.2.3 From 30510d6cfd877e5441022b8a8d893871fbf2a0a9 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 12:16:26 +0300 Subject: Source: Show aliases on title of command source embed --- bot/cogs/source.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 5668ab6c6..8fd8cbed4 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -103,7 +103,8 @@ class BotSource(commands.Cog): else: description = source_object.short_doc - title = f"Command: {source_object.qualified_name}" + aliases_string = f" (or {', '.join(source_object.aliases)})" if source_object.aliases else "" + title = f"Command: {source_object.qualified_name}{aliases_string}" elif isinstance(source_object, str): title = f"Tag: {source_object.split('/')[-1].split('.')[0]}" description = "" -- cgit v1.2.3 From 2c0cb510219a27a875628ff4453be2ba7f0a9d7f Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 31 May 2020 12:17:10 +0300 Subject: Source: Include tag into converter's `BadArgument` raising --- bot/cogs/source.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 8fd8cbed4..a3922297a 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -36,7 +36,7 @@ class SourceConverter(commands.Converter): if cmd: return cmd - raise commands.BadArgument(f"Unable to convert `{argument}` to valid command or Cog.") + raise commands.BadArgument(f"Unable to convert `{argument}` to valid command, tag, or Cog.") class BotSource(commands.Cog): -- cgit v1.2.3 From 00f52a15c67fa2a8dc9dc87769ba56cff9e2cdf4 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 1 Jun 2020 07:55:49 +0300 Subject: Tags: Add tag file location storage to cache --- bot/cogs/tags.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 6f03a3475..571c0ed28 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -47,6 +47,7 @@ class Tags(Cog): "description": file.read_text(encoding="utf8"), }, "restricted_to": "developers", + "location": str(file) } # Convert to a list to allow negative indexing. -- cgit v1.2.3 From 9f93a40bb8d8bd7d0538465f9d4eda79d02e540c Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 1 Jun 2020 08:11:41 +0300 Subject: Source: Simplify tags name and location parsing --- bot/cogs/source.py | 22 ++++++++++++++-------- bot/cogs/tags.py | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index a3922297a..e01209c28 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -21,12 +21,8 @@ class SourceConverter(commands.Converter): tags_cog = ctx.bot.get_cog("Tags") - if argument.lower() in tags_cog._cache: - tag = argument.lower() - if tags_cog._cache[tag]["restricted_to"] != "developers": - return f"/bot/bot/resources/tags/{tags_cog._cache[tag]['restricted_to']}/{tag}.md" - else: - return f"/bot/bot/resources/tags/{tag}.md" + if tags_cog and argument.lower() in tags_cog._cache: + return argument.lower() cog = ctx.bot.get_cog(argument) if cog: @@ -56,6 +52,12 @@ class BotSource(commands.Cog): return url, location, first_line = self.get_source_link(source_item) + + # There is no URL only when bot can't fetch Tags cog + if not url: + await ctx.send("Unable to get `Tags` cog.") + return + await ctx.send(embed=await self.build_embed(url, source_item, location, first_line)) def get_source_link(self, source_item: SourceType) -> Tuple[str, str, Optional[int]]: @@ -73,7 +75,11 @@ class BotSource(commands.Cog): src = source_item.callback.__code__ filename = src.co_filename elif isinstance(source_item, str): - filename = source_item + tags_cog = self.bot.get_cog("Tags") + if not tags_cog: + return "", "", None + + filename = tags_cog._cache[source_item]["location"] else: src = type(source_item) filename = inspect.getsourcefile(src) @@ -106,7 +112,7 @@ class BotSource(commands.Cog): aliases_string = f" (or {', '.join(source_object.aliases)})" if source_object.aliases else "" title = f"Command: {source_object.qualified_name}{aliases_string}" elif isinstance(source_object, str): - title = f"Tag: {source_object.split('/')[-1].split('.')[0]}" + title = f"Tag: {source_object}" description = "" else: title = f"Cog: {source_object.qualified_name}" diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 571c0ed28..3d76c5c08 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -47,7 +47,7 @@ class Tags(Cog): "description": file.read_text(encoding="utf8"), }, "restricted_to": "developers", - "location": str(file) + "location": f"/bot/{file}" } # Convert to a list to allow negative indexing. -- cgit v1.2.3 From c397b871fbce903f251abd32662d40d33a95e0de Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 1 Jun 2020 08:16:59 +0300 Subject: Source: Move calling `get_source_link` to `build_embed` --- bot/cogs/source.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index e01209c28..00a5b344b 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -51,14 +51,11 @@ class BotSource(commands.Cog): await ctx.send(embed=embed) return - url, location, first_line = self.get_source_link(source_item) + embed = await self.build_embed(source_item, ctx) - # There is no URL only when bot can't fetch Tags cog - if not url: - await ctx.send("Unable to get `Tags` cog.") - return - - await ctx.send(embed=await self.build_embed(url, source_item, location, first_line)) + # When embed don't exist, then there was error and this is already handled. + if embed: + await ctx.send(embed=await self.build_embed(source_item, ctx)) def get_source_link(self, source_item: SourceType) -> Tuple[str, str, Optional[int]]: """Build GitHub link of source item.""" @@ -96,8 +93,15 @@ class BotSource(commands.Cog): return url, file_location, first_line_no or None - async def build_embed(self, link: str, source_object: SourceType, loc: str, first_line: Optional[int]) -> Embed: + async def build_embed(self, source_object: SourceType, ctx: commands.Context) -> Optional[Embed]: """Build embed based on source object.""" + url, location, first_line = self.get_source_link(source_object) + + # There is no URL only when bot can't fetch Tags cog + if not url: + await ctx.send("Unable to get `Tags` cog.") + return + if isinstance(source_object, commands.HelpCommand): title = "Help Command" description = source_object.__doc__.splitlines()[1] @@ -119,9 +123,9 @@ class BotSource(commands.Cog): description = source_object.description.splitlines()[0] embed = Embed(title=title, description=description) - embed.add_field(name="Source Code", value=f"[Go to GitHub]({link})") + embed.add_field(name="Source Code", value=f"[Go to GitHub]({url})") line_text = f":{first_line}" if first_line else "" - embed.set_footer(text=f"{loc}{line_text}") + embed.set_footer(text=f"{location}{line_text}") return embed -- cgit v1.2.3 From 18968d27ae5bc3b004c881a0c78bcfb305371158 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 1 Jun 2020 08:18:27 +0300 Subject: Source: Update `get_source_link` docstring --- bot/cogs/source.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 00a5b344b..50fd4599d 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -58,7 +58,7 @@ class BotSource(commands.Cog): await ctx.send(embed=await self.build_embed(source_item, ctx)) def get_source_link(self, source_item: SourceType) -> Tuple[str, str, Optional[int]]: - """Build GitHub link of source item.""" + """Build GitHub link of source item, return this link, file location and first line number.""" if isinstance(source_item, commands.HelpCommand): src = type(source_item) filename = inspect.getsourcefile(src) -- cgit v1.2.3 From ea937e5a69b4cf233216510c96800bdf5940ff16 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 1 Jun 2020 08:19:21 +0300 Subject: Source: Remove showing aliases for commands --- bot/cogs/source.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 50fd4599d..57ae17f77 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -113,8 +113,7 @@ class BotSource(commands.Cog): else: description = source_object.short_doc - aliases_string = f" (or {', '.join(source_object.aliases)})" if source_object.aliases else "" - title = f"Command: {source_object.qualified_name}{aliases_string}" + title = f"Command: {source_object.qualified_name}" elif isinstance(source_object, str): title = f"Tag: {source_object}" description = "" -- cgit v1.2.3 From 8429c2284901675297c78f00bf0e5a6b15d80e31 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 1 Jun 2020 13:17:40 +0300 Subject: Source: Refactor Tags cog missing handling --- bot/cogs/source.py | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 57ae17f77..32a78a0c0 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -8,7 +8,7 @@ from discord.ext import commands from bot.bot import Bot from bot.constants import URLs -SourceType = Union[commands.HelpCommand, commands.Command, commands.Cog, str] +SourceType = Union[commands.HelpCommand, commands.Command, commands.Cog, str, commands.ExtensionNotLoaded] class SourceConverter(commands.Converter): @@ -19,11 +19,6 @@ class SourceConverter(commands.Converter): if argument.lower().startswith("help"): return ctx.bot.help_command - tags_cog = ctx.bot.get_cog("Tags") - - if tags_cog and argument.lower() in tags_cog._cache: - return argument.lower() - cog = ctx.bot.get_cog(argument) if cog: return cog @@ -32,6 +27,14 @@ class SourceConverter(commands.Converter): if cmd: return cmd + tags_cog = ctx.bot.get_cog("Tags") + + if not tags_cog: + await ctx.send("Unable to get `Tags` cog.") + return commands.ExtensionNotLoaded("bot.cogs.tags") + elif argument.lower() in tags_cog._cache: + return argument.lower() + raise commands.BadArgument(f"Unable to convert `{argument}` to valid command, tag, or Cog.") @@ -44,6 +47,10 @@ class BotSource(commands.Cog): @commands.command(name="source", aliases=("src",)) async def source_command(self, ctx: commands.Context, *, source_item: SourceConverter = None) -> None: """Display information and a GitHub link to the source code of a command, tag, or cog.""" + # When we have problem to get Tags cog, exit early + if isinstance(source_item, commands.ExtensionNotLoaded): + return + if not source_item: embed = Embed(title="Bot's GitHub Repository") embed.add_field(name="Repository", value=f"[Go to GitHub]({URLs.github_bot_repo})") @@ -51,11 +58,8 @@ class BotSource(commands.Cog): await ctx.send(embed=embed) return - embed = await self.build_embed(source_item, ctx) - - # When embed don't exist, then there was error and this is already handled. - if embed: - await ctx.send(embed=await self.build_embed(source_item, ctx)) + embed = await self.build_embed(source_item) + await ctx.send(embed=embed) def get_source_link(self, source_item: SourceType) -> Tuple[str, str, Optional[int]]: """Build GitHub link of source item, return this link, file location and first line number.""" @@ -73,9 +77,6 @@ class BotSource(commands.Cog): filename = src.co_filename elif isinstance(source_item, str): tags_cog = self.bot.get_cog("Tags") - if not tags_cog: - return "", "", None - filename = tags_cog._cache[source_item]["location"] else: src = type(source_item) @@ -93,15 +94,10 @@ class BotSource(commands.Cog): return url, file_location, first_line_no or None - async def build_embed(self, source_object: SourceType, ctx: commands.Context) -> Optional[Embed]: + async def build_embed(self, source_object: SourceType) -> Optional[Embed]: """Build embed based on source object.""" url, location, first_line = self.get_source_link(source_object) - # There is no URL only when bot can't fetch Tags cog - if not url: - await ctx.send("Unable to get `Tags` cog.") - return - if isinstance(source_object, commands.HelpCommand): title = "Help Command" description = source_object.__doc__.splitlines()[1] -- cgit v1.2.3 From 5f5a51b1715228ac5b401ef6bed8a83491e313de Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Thu, 4 Jun 2020 03:17:11 -0400 Subject: Improve LinePaginator to support long lines --- bot/cogs/moderation/management.py | 8 ++--- bot/pagination.py | 66 +++++++++++++++++++++++++++++++++++---- tests/bot/test_pagination.py | 41 +++++++++++++++++++----- 3 files changed, 98 insertions(+), 17 deletions(-) diff --git a/bot/cogs/moderation/management.py b/bot/cogs/moderation/management.py index 250a24247..ad17a90b0 100644 --- a/bot/cogs/moderation/management.py +++ b/bot/cogs/moderation/management.py @@ -83,14 +83,14 @@ class ModManagement(commands.Cog): "actor__id": ctx.author.id, "ordering": "-inserted_at" } - infractions = await self.bot.api_client.get(f"bot/infractions", params=params) + infractions = await self.bot.api_client.get("bot/infractions", params=params) if infractions: old_infraction = infractions[0] infraction_id = old_infraction["id"] else: await ctx.send( - f":x: Couldn't find most recent infraction; you have never given an infraction." + ":x: Couldn't find most recent infraction; you have never given an infraction." ) return else: @@ -224,7 +224,7 @@ class ModManagement(commands.Cog): ) -> None: """Send a paginated embed of infractions for the specified user.""" if not infractions: - await ctx.send(f":warning: No infractions could be found for that query.") + await ctx.send(":warning: No infractions could be found for that query.") return lines = tuple( @@ -268,12 +268,12 @@ class ModManagement(commands.Cog): User: {self.bot.get_user(user_id)} (`{user_id}`) Type: **{infraction["type"]}** Shadow: {hidden} - Reason: {infraction["reason"] or "*None*"} Created: {created} Expires: {expires} Remaining: {remaining} Actor: {actor.mention if actor else actor_id} ID: `{infraction["id"]}` + Reason: {infraction["reason"] or "*None*"} {"**===============**" if active else "==============="} """) diff --git a/bot/pagination.py b/bot/pagination.py index 90c8f849c..5c7be564d 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -37,12 +37,19 @@ class LinePaginator(Paginator): The suffix appended at the end of every page. e.g. three backticks. * max_size: `int` The maximum amount of codepoints allowed in a page. + * scale_to_size: `int` + The maximum amount of characters a single line can scale up to. * max_lines: `int` The maximum amount of lines allowed in a page. """ def __init__( - self, prefix: str = '```', suffix: str = '```', max_size: int = 2000, max_lines: int = None + self, + prefix: str = '```', + suffix: str = '```', + max_size: int = 2000, + scale_to_size: int = 2000, + max_lines: t.Optional[int] = None ) -> None: """ This function overrides the Paginator.__init__ from inside discord.ext.commands. @@ -52,6 +59,10 @@ class LinePaginator(Paginator): self.prefix = prefix self.suffix = suffix self.max_size = max_size - len(suffix) + if scale_to_size < max_size: + raise ValueError("scale_to_size must be >= max_size.") + + self.scale_to_size = scale_to_size self.max_lines = max_lines self._current_page = [prefix] self._linecount = 0 @@ -62,14 +73,26 @@ class LinePaginator(Paginator): """ Adds a line to the current page. - If the line exceeds the `self.max_size` then an exception is raised. + If the line exceeds `self.max_size`, then `self.max_size` will go up to `scale_to_size` for + a single line before creating a new page. If it is still exceeded, the excess characters + are stored and placed on the next pages until there are none remaining (by word boundary). + + Raises a RuntimeError if `self.max_size` is still exceeded after attempting to continue + onto the next page. This function overrides the `Paginator.add_line` from inside `discord.ext.commands`. It overrides in order to allow us to configure the maximum number of lines per page. """ - if len(line) > self.max_size - len(self.prefix) - 2: - raise RuntimeError('Line exceeds maximum page size %s' % (self.max_size - len(self.prefix) - 2)) + remaining_words = None + if len(line) > (max_chars := self.max_size - len(self.prefix) - 2): + if len(line) > self.scale_to_size: + line, remaining_words = self._split_remaining_words(line, max_chars) + # If line still exceeds scale_to_size, we were unable to split into a second + # page without truncating. + if len(line) > self.scale_to_size: + raise RuntimeError(f'Line exceeds maximum scale_to_size {self.scale_to_size}' + ' and could not be split.') if self.max_lines is not None: if self._linecount >= self.max_lines: @@ -87,6 +110,36 @@ class LinePaginator(Paginator): self._current_page.append('') self._count += 1 + if remaining_words: + self.add_line(remaining_words) + + def _split_remaining_words(self, line: str, max_chars: int) -> t.Tuple[str, t.Optional[str]]: + """Internal: split a line into two strings; one that fits within *max_chars* characters + (reduced_words) and another for the remaining (remaining_words), rounding down to the + nearest word. + + Return a tuple in the format (reduced_words, remaining_words). + """ + reduced_words = [] + # "(Continued)" is used on a line by itself to indicate the continuation of last page + remaining_words = ["(Continued)\n", "---------------\n"] + reduced_char_count = 0 + is_full = False + + for word in line.split(" "): + if not is_full: + if len(word) + reduced_char_count <= max_chars: + reduced_words.append(word) + reduced_char_count += len(word) + else: + is_full = True + remaining_words.append(word) + else: + remaining_words.append(word) + + return " ".join(reduced_words), " ".join(remaining_words) if len(remaining_words) > 2 \ + else None + @classmethod async def paginate( cls, @@ -97,6 +150,7 @@ class LinePaginator(Paginator): suffix: str = "", max_lines: t.Optional[int] = None, max_size: int = 500, + scale_to_size: int = 2000, empty: bool = True, restrict_to_user: User = None, timeout: int = 300, @@ -147,7 +201,7 @@ class LinePaginator(Paginator): if not lines: if exception_on_empty_embed: - log.exception(f"Pagination asked for empty lines iterable") + log.exception("Pagination asked for empty lines iterable") raise EmptyPaginatorEmbed("No lines to paginate") log.debug("No lines to add to paginator, adding '(nothing to display)' message") @@ -357,7 +411,7 @@ class ImagePaginator(Paginator): if not pages: if exception_on_empty_embed: - log.exception(f"Pagination asked for empty image list") + log.exception("Pagination asked for empty image list") raise EmptyPaginatorEmbed("No images to paginate") log.debug("No images to add to paginator, adding '(no images to display)' message") diff --git a/tests/bot/test_pagination.py b/tests/bot/test_pagination.py index 0a734b505..f2e2c27ce 100644 --- a/tests/bot/test_pagination.py +++ b/tests/bot/test_pagination.py @@ -8,17 +8,44 @@ class LinePaginatorTests(TestCase): def setUp(self): """Create a paginator for the test method.""" - self.paginator = pagination.LinePaginator(prefix='', suffix='', max_size=30) - - def test_add_line_raises_on_too_long_lines(self): - """`add_line` should raise a `RuntimeError` for too long lines.""" - message = f"Line exceeds maximum page size {self.paginator.max_size - 2}" - with self.assertRaises(RuntimeError, msg=message): - self.paginator.add_line('x' * self.paginator.max_size) + self.paginator = pagination.LinePaginator(prefix='', suffix='', max_size=30, + scale_to_size=50) def test_add_line_works_on_small_lines(self): """`add_line` should allow small lines to be added.""" self.paginator.add_line('x' * (self.paginator.max_size - 3)) + # Note that the page isn't added to _pages until it's full. + self.assertEqual(len(self.paginator._pages), 0) + + def test_add_line_works_on_long_lines(self): + """`add_line` should scale long lines up to `scale_to_size`.""" + self.paginator.add_line('x' * self.paginator.scale_to_size) + self.assertEqual(len(self.paginator._pages), 1) + + # Any additional lines should start a new page after `max_size` is exceeded. + self.paginator.add_line('x') + self.assertEqual(len(self.paginator._pages), 2) + + def test_add_line_continuation(self): + """When `scale_to_size` is exceeded, remaining words should be split onto the next page.""" + self.paginator.add_line('zyz ' * (self.paginator.scale_to_size//4 + 1)) + self.assertEqual(len(self.paginator._pages), 2) + + def test_add_line_no_continuation(self): + """If adding a new line to an existing page would exceed `max_size`, it should start a new + page rather than using continuation. + """ + self.paginator.add_line('z' * (self.paginator.max_size - 3)) + self.paginator.add_line('z') + self.assertEqual(len(self.paginator._pages), 1) + + def test_add_line_raises_on_very_long_words(self): + """`add_line` should raise if a single long word is added that exceeds `scale_to_size`. + + Note: truncation is also a potential option, but this should not occur from normal usage. + """ + with self.assertRaises(RuntimeError): + self.paginator.add_line('x' * (self.paginator.scale_to_size + 1)) class ImagePaginatorTests(TestCase): -- cgit v1.2.3 From a465a1eb5d06b342d4fcc14746668bcbe57cd215 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Thu, 4 Jun 2020 04:06:50 -0400 Subject: Fix docstring for _split_remaing_words() --- bot/pagination.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bot/pagination.py b/bot/pagination.py index 5c7be564d..590d9da96 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -114,9 +114,13 @@ class LinePaginator(Paginator): self.add_line(remaining_words) def _split_remaining_words(self, line: str, max_chars: int) -> t.Tuple[str, t.Optional[str]]: - """Internal: split a line into two strings; one that fits within *max_chars* characters - (reduced_words) and another for the remaining (remaining_words), rounding down to the - nearest word. + """Internal: split a line into two strings -- reduced_words and remaining_words. + + reduced_words: the remaining words in `line`, after attempting to remove all words that + exceed `max_chars` (rounding down to the nearest word boundary). + + remaining_words: the words in `line` which exceed `max_chars`. This value is None if + no words could be split from `line`. Return a tuple in the format (reduced_words, remaining_words). """ -- cgit v1.2.3 From 974bf0fe2074d141299b826ce7b8a2479df960b5 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Thu, 4 Jun 2020 04:12:37 -0400 Subject: Fix _split_remaining_words() docstring summary --- bot/pagination.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/pagination.py b/bot/pagination.py index 590d9da96..ba10da57e 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -114,7 +114,8 @@ class LinePaginator(Paginator): self.add_line(remaining_words) def _split_remaining_words(self, line: str, max_chars: int) -> t.Tuple[str, t.Optional[str]]: - """Internal: split a line into two strings -- reduced_words and remaining_words. + """ + Internal: split a line into two strings -- reduced_words and remaining_words. reduced_words: the remaining words in `line`, after attempting to remove all words that exceed `max_chars` (rounding down to the nearest word boundary). -- cgit v1.2.3 From 780ed87ef3d4f24e45d2cba8020342c1195f7801 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Mon, 8 Jun 2020 18:31:09 +0200 Subject: Incidents: add incidents module & new ext boilerplate --- bot/cogs/moderation/__init__.py | 4 +++- bot/cogs/moderation/incidents.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 bot/cogs/moderation/incidents.py diff --git a/bot/cogs/moderation/__init__.py b/bot/cogs/moderation/__init__.py index 6880ca1bd..4455705f7 100644 --- a/bot/cogs/moderation/__init__.py +++ b/bot/cogs/moderation/__init__.py @@ -1,4 +1,5 @@ from bot.bot import Bot +from .incidents import Incidents from .infractions import Infractions from .management import ModManagement from .modlog import ModLog @@ -7,7 +8,8 @@ from .superstarify import Superstarify def setup(bot: Bot) -> None: - """Load the Infractions, ModManagement, ModLog, Silence, and Superstarify cogs.""" + """Load the Incidents, Infractions, ModManagement, ModLog, Silence, and Superstarify cogs.""" + bot.add_cog(Incidents(bot)) bot.add_cog(Infractions(bot)) bot.add_cog(ModLog(bot)) bot.add_cog(ModManagement(bot)) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py new file mode 100644 index 000000000..2ff1e949a --- /dev/null +++ b/bot/cogs/moderation/incidents.py @@ -0,0 +1,14 @@ +import logging + +from discord.ext.commands import Cog + +from bot.bot import Bot + +log = logging.getLogger(__name__) + + +class Incidents(Cog): + """Automation for the #incidents channel.""" + + def __init__(self, bot: Bot) -> None: + self.bot = bot -- cgit v1.2.3 From 29ab6dc350f0063bcac2218aee7c9170e83f980a Mon Sep 17 00:00:00 2001 From: kwzrd Date: Mon, 8 Jun 2020 23:33:58 +0200 Subject: Incidents: add new emoji constants --- bot/constants.py | 4 ++++ config-default.yml | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/bot/constants.py b/bot/constants.py index b31a9c99e..02b82cf23 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -271,6 +271,10 @@ class Emojis(metaclass=YAMLGetter): status_idle: str status_dnd: str + incident_actioned: str + incident_unactioned: str + incident_investigating: str + failmail: str trashcan: str diff --git a/config-default.yml b/config-default.yml index 2c85f5ef3..c59abdc39 100644 --- a/config-default.yml +++ b/config-default.yml @@ -38,6 +38,10 @@ style: status_dnd: "<:status_dnd:470326272082313216>" status_offline: "<:status_offline:470326266537705472>" + incident_actioned: "<:incident_actioned:719645530128646266>" + incident_unactioned: "<:incident_unactioned:719645583245180960>" + incident_investigating: "<:incident_investigating:719645658671480924>" + failmail: "<:failmail:633660039931887616>" trashcan: "<:trashcan:637136429717389331>" -- cgit v1.2.3 From 0d3af0d52b23b3390aadf37a82e905e8ee529a90 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Mon, 8 Jun 2020 23:36:20 +0200 Subject: Incidents: create Signal enum & link members with emojis --- bot/cogs/moderation/incidents.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 2ff1e949a..baceddf0c 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -1,12 +1,22 @@ import logging +from enum import Enum from discord.ext.commands import Cog from bot.bot import Bot +from bot.constants import Emojis log = logging.getLogger(__name__) +class Signal(Enum): + """Recognized incident status signals.""" + + ACTIONED = Emojis.incident_actioned + NOT_ACTIONED = Emojis.incident_unactioned + INVESTIGATING = Emojis.incident_investigating + + class Incidents(Cog): """Automation for the #incidents channel.""" -- cgit v1.2.3 From 78782868040d1b2ca0b655efc4123b3d9b6bfda3 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 10:23:45 +0300 Subject: Jam Tests: Created base test layout --- tests/bot/cogs/test_jams.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/bot/cogs/test_jams.py diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py new file mode 100644 index 000000000..33dee593e --- /dev/null +++ b/tests/bot/cogs/test_jams.py @@ -0,0 +1,14 @@ +import unittest + +from bot.constants import Roles +from tests.helpers import MockBot, MockContext, MockMember, MockRole + + +class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): + """Tests for `createteam` command.""" + + def setUp(self): + self.bot = MockBot() + self.admin_role = MockRole(name="Admins", id=Roles.admins) + self.command_user = MockMember([self.admin_role]) + self.context = MockContext(bot=self.bot, author=self.command_user) -- cgit v1.2.3 From 6242fbdce8935c681fa575b1c208642fe9d2635b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 10:38:41 +0300 Subject: Jam Tests: Created tests for case when too small amount of members given --- tests/bot/cogs/test_jams.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 33dee593e..3e71370c2 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -1,5 +1,7 @@ import unittest +from unittest.mock import patch +from bot.cogs.jams import CodeJams from bot.constants import Roles from tests.helpers import MockBot, MockContext, MockMember, MockRole @@ -11,4 +13,18 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.bot = MockBot() self.admin_role = MockRole(name="Admins", id=Roles.admins) self.command_user = MockMember([self.admin_role]) - self.context = MockContext(bot=self.bot, author=self.command_user) + self.ctx = MockContext(bot=self.bot, author=self.command_user) + self.cog = CodeJams(self.bot) + + @patch("bot.cogs.jams.utils") + async def test_too_small_amount_of_team_members_passed(self, utils_mock): + """Should `ctx.send` and exit early when too small amount of members.""" + for case in (1, 2): + with self.subTest(amount_of_members=case): + self.ctx.reset_mock() + utils_mock.reset_mock() + await self.cog.createteam( + self.cog, self.ctx, team_name="foo", members=(MockMember() for _ in range(case)) + ) + self.ctx.send.assert_awaited_once() + utils_mock.get.assert_not_called() -- cgit v1.2.3 From a9122b781191f93f5dd375b5c1d9e7744943b464 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 10:46:08 +0300 Subject: Jam Tests: Created tests for removing duplicate team members --- tests/bot/cogs/test_jams.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 3e71370c2..1cface1c1 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -28,3 +28,12 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): ) self.ctx.send.assert_awaited_once() utils_mock.get.assert_not_called() + + @patch("bot.cogs.jams.utils") + async def test_duplicate_members_provided(self, utils_mock): + """Should `ctx.send` and exit early because duplicate members provided and total there is only 1 member.""" + self.ctx.reset_mock() + member = MockMember() + await self.cog.createteam(self.cog, self.ctx, "foo", (member for _ in range(5))) + self.ctx.send.assert_awaited_once() + utils_mock.get.assert_not_called() -- cgit v1.2.3 From ebaac5988d7ff1558595008540eab5368312d170 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 17:57:26 +0300 Subject: Jam Tests: Created test for category creating when not exist --- tests/bot/cogs/test_jams.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 1cface1c1..2153178c3 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -3,7 +3,7 @@ from unittest.mock import patch from bot.cogs.jams import CodeJams from bot.constants import Roles -from tests.helpers import MockBot, MockContext, MockMember, MockRole +from tests.helpers import MockBot, MockContext, MockGuild, MockMember, MockRole class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): @@ -13,7 +13,8 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.bot = MockBot() self.admin_role = MockRole(name="Admins", id=Roles.admins) self.command_user = MockMember([self.admin_role]) - self.ctx = MockContext(bot=self.bot, author=self.command_user) + self.guild = MockGuild([self.admin_role]) + self.ctx = MockContext(bot=self.bot, author=self.command_user, guild=self.guild) self.cog = CodeJams(self.bot) @patch("bot.cogs.jams.utils") @@ -37,3 +38,14 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): await self.cog.createteam(self.cog, self.ctx, "foo", (member for _ in range(5))) self.ctx.send.assert_awaited_once() utils_mock.get.assert_not_called() + + @patch("bot.cogs.jams.utils") + async def test_category_dont_exist(self, utils_mock): + """Should create code jam category.""" + utils_mock.get.return_value = None + await self.cog.createteam(self.cog, self.ctx, "foo", (MockMember() for _ in range(5))) + self.ctx.guild.create_category_channel.assert_awaited_once() + category_overwrites = self.ctx.guild.create_category_channel.call_args[1]["overwrites"] + + self.assertFalse(category_overwrites[self.ctx.guild.default_role].read_messages) + self.assertTrue(category_overwrites[self.ctx.guild.me].read_messages) -- cgit v1.2.3 From 14d4eda8b1e7839b286402091ac060d3c869f447 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 17:58:38 +0300 Subject: Jam Tests: Added utils.get assert to category creating test --- tests/bot/cogs/test_jams.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 2153178c3..f5f87761b 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -44,6 +44,7 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Should create code jam category.""" utils_mock.get.return_value = None await self.cog.createteam(self.cog, self.ctx, "foo", (MockMember() for _ in range(5))) + utils_mock.get.assert_called_once() self.ctx.guild.create_category_channel.assert_awaited_once() category_overwrites = self.ctx.guild.create_category_channel.call_args[1]["overwrites"] -- cgit v1.2.3 From 464c4bbb53101d4456314bf7a40243337525d514 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 18:03:17 +0300 Subject: Jam Tests: Created test that make sure when category exist, don't create --- tests/bot/cogs/test_jams.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index f5f87761b..1ce71a942 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -50,3 +50,11 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.assertFalse(category_overwrites[self.ctx.guild.default_role].read_messages) self.assertTrue(category_overwrites[self.ctx.guild.me].read_messages) + + @patch("bot.cogs.jams.utils") + async def test_category_channel_exist(self, utils_mock): + """Should not try to create category channel.""" + utils_mock.return_value = "foo" + await self.cog.createteam(self.cog, self.ctx, "bar", (MockMember() for _ in range(5))) + utils_mock.get.assert_called_once() + self.ctx.guild.create_category_channel.assert_not_awaited() -- cgit v1.2.3 From a63545510f392cf3e36e310b68792177a178b769 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 18:08:29 +0300 Subject: Jam Tests: Created test for creating text channel for team --- tests/bot/cogs/test_jams.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 1ce71a942..9d26628ff 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -58,3 +58,8 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): await self.cog.createteam(self.cog, self.ctx, "bar", (MockMember() for _ in range(5))) utils_mock.get.assert_called_once() self.ctx.guild.create_category_channel.assert_not_awaited() + + async def test_team_text_channel_creation(self): + """Should create text channel for team.""" + await self.cog.createteam(self.cog, self.ctx, "bar", (MockMember() for _ in range(5))) + self.ctx.guild.create_text_channel.assert_awaited_once() -- cgit v1.2.3 From 3df28c1b2a64bee3a52442fe42decaa960c45fde Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 18:18:09 +0300 Subject: Jam Tests: Created test for channel overwrites --- tests/bot/cogs/test_jams.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 9d26628ff..d21c5ea29 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -63,3 +63,27 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Should create text channel for team.""" await self.cog.createteam(self.cog, self.ctx, "bar", (MockMember() for _ in range(5))) self.ctx.guild.create_text_channel.assert_awaited_once() + + async def test_channel_overwrites(self): + """Should have correct permission overwrites for users and roles.""" + leader = MockMember() + members = [leader] + [MockMember() for _ in range(4)] + await self.cog.createteam(self.cog, self.ctx, "foo", members) + overwrites = self.ctx.guild.create_text_channel.call_args[1]["overwrites"] + + # Leader permission overwrites + self.assertTrue(overwrites[leader].manage_messages) + self.assertTrue(overwrites[leader].read_messages) + self.assertTrue(overwrites[leader].manage_webhooks) + self.assertTrue(overwrites[leader].connect) + + # Other members permission overwrites + for member in members[1:]: + self.assertTrue(overwrites[member].read_messages) + self.assertTrue(overwrites[member].connect) + + # Everyone and verified role overwrite + self.assertFalse(overwrites[self.ctx.guild.default_role].read_messages) + self.assertFalse(overwrites[self.ctx.guild.default_role].connect) + self.assertFalse(overwrites[self.ctx.guild.get_role(Roles.verified)].read_messages) + self.assertFalse(overwrites[self.ctx.guild.get_role(Roles.verified)].connect) -- cgit v1.2.3 From 6476d3ba6dfc28441d097aaa15a7c9e13f53f646 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 18:22:02 +0300 Subject: Jam Tests: Make text channel creation test more specific --- tests/bot/cogs/test_jams.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index d21c5ea29..94c48b995 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -59,11 +59,18 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): utils_mock.get.assert_called_once() self.ctx.guild.create_category_channel.assert_not_awaited() - async def test_team_text_channel_creation(self): + @patch("bot.cogs.jams.utils") + async def test_team_text_channel_creation(self, utils_mock): """Should create text channel for team.""" + utils_mock.get.return_value = "foo" await self.cog.createteam(self.cog, self.ctx, "bar", (MockMember() for _ in range(5))) + # Make sure that we awaited function before getting call arguments self.ctx.guild.create_text_channel.assert_awaited_once() + # All other arguments is possible to get somewhere else except this + overwrites = self.ctx.guild.create_text_channel.call_args[1]["overwrites"] + self.ctx.guild.create_text_channel.assert_awaited_once_with("bar", overwrites=overwrites, category="foo") + async def test_channel_overwrites(self): """Should have correct permission overwrites for users and roles.""" leader = MockMember() -- cgit v1.2.3 From b1359f0ed37cdbbb6bae9dbbe92e3bf0db660636 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 18:26:41 +0300 Subject: Jam Tests: Create test for team voice channel creating --- tests/bot/cogs/test_jams.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 94c48b995..2e1419f8e 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -94,3 +94,15 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.assertFalse(overwrites[self.ctx.guild.default_role].connect) self.assertFalse(overwrites[self.ctx.guild.get_role(Roles.verified)].read_messages) self.assertFalse(overwrites[self.ctx.guild.get_role(Roles.verified)].connect) + + @patch("bot.cogs.jams.utils") + async def test_team_voice_channel_creation(self, utils_mock): + """Should create new voice channel for team.""" + utils_mock.get.return_value = "foo" + await self.cog.createteam(self.cog, self.ctx, "my-team", (MockMember() for _ in range(5))) + # Make sure that we awaited function before getting call arguments + self.ctx.guild.create_voice_channel.assert_awaited_once() + + # All other arguments is possible to get somewhere else except this + overwrites = self.ctx.guild.create_voice_channel.call_args[1]["overwrites"] + self.ctx.guild.create_voice_channel.assert_awaited_once_with("My Team", overwrites=overwrites, category="foo") -- cgit v1.2.3 From b5b05adc41e55dd58810608f4ac7ade6281cdf84 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 18:37:27 +0300 Subject: Jam Tests: Create test for team jam roles adding --- tests/bot/cogs/test_jams.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 2e1419f8e..16caa98c6 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -106,3 +106,17 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): # All other arguments is possible to get somewhere else except this overwrites = self.ctx.guild.create_voice_channel.call_args[1]["overwrites"] self.ctx.guild.create_voice_channel.assert_awaited_once_with("My Team", overwrites=overwrites, category="foo") + + async def test_jam_roles_adding(self): + """Should add team leader role to leader and jam role to every team member.""" + leader_role = MockRole(name="Team Leader") + jam_role = MockRole(name="Jammer") + self.ctx.guild.get_role.side_effect = [MockRole(), leader_role, jam_role] + + leader = MockMember() + members = [leader] + [MockMember() for _ in range(4)] + await self.cog.createteam(self.cog, self.ctx, "foo", members) + + leader.add_roles.assert_any_await(leader_role) + for member in members: + member.add_roles.assert_any_await(jam_role) -- cgit v1.2.3 From 76ad4d141027f6351e2feedc466c8acc805f671d Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 18:39:13 +0300 Subject: Jam Tests: Create test for successful `ctx.send` calling --- tests/bot/cogs/test_jams.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 16caa98c6..7db66ff11 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -120,3 +120,9 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): leader.add_roles.assert_any_await(leader_role) for member in members: member.add_roles.assert_any_await(jam_role) + + async def test_result_sending(self): + """Should call `ctx.send` when everything go right.""" + self.ctx.reset_mock() + await self.cog.createteam(self.cog, self.ctx, "foo", (MockMember() for _ in range(5))) + self.ctx.send.assert_awaited_once() -- cgit v1.2.3 From bbe4f137bd583d66a6bcb03102327bc6c586af86 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 23 May 2020 18:42:03 +0300 Subject: Jam Tests: Create test for `setup` function --- tests/bot/cogs/test_jams.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 7db66ff11..2c5cef835 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -1,7 +1,7 @@ import unittest from unittest.mock import patch -from bot.cogs.jams import CodeJams +from bot.cogs.jams import CodeJams, setup from bot.constants import Roles from tests.helpers import MockBot, MockContext, MockGuild, MockMember, MockRole @@ -126,3 +126,13 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.ctx.reset_mock() await self.cog.createteam(self.cog, self.ctx, "foo", (MockMember() for _ in range(5))) self.ctx.send.assert_awaited_once() + + +class CodeJamSetup(unittest.TestCase): + """Test for `setup` function of `CodeJam` cog.""" + + def test_setup(self): + """Should call `bot.add_cog`.""" + bot = MockBot() + setup(bot) + bot.add_cog.assert_called_once() -- cgit v1.2.3 From 5ca860fb3b2bcb77ab8574d83e8159df471f0faf Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 08:54:07 +0300 Subject: Jam Tests: Fix `test_result_sending` docstring Co-authored-by: Mark --- tests/bot/cogs/test_jams.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 2c5cef835..51720d957 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -122,7 +122,7 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): member.add_roles.assert_any_await(jam_role) async def test_result_sending(self): - """Should call `ctx.send` when everything go right.""" + """Should call `ctx.send` when everything goes right.""" self.ctx.reset_mock() await self.cog.createteam(self.cog, self.ctx, "foo", (MockMember() for _ in range(5))) self.ctx.send.assert_awaited_once() -- cgit v1.2.3 From 28f33584b65b1f9d7e7254b4822d8896c7f19284 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 09:26:43 +0300 Subject: Jam Tests: Use class member of patch instead decorator on most of tests --- tests/bot/cogs/test_jams.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 51720d957..bf542458b 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -16,53 +16,52 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.guild = MockGuild([self.admin_role]) self.ctx = MockContext(bot=self.bot, author=self.command_user, guild=self.guild) self.cog = CodeJams(self.bot) + self.utils_mock = patch("bot.cogs.jams.utils").start() - @patch("bot.cogs.jams.utils") - async def test_too_small_amount_of_team_members_passed(self, utils_mock): + def tearDown(self): + self.utils_mock.stop() + + async def test_too_small_amount_of_team_members_passed(self): """Should `ctx.send` and exit early when too small amount of members.""" for case in (1, 2): with self.subTest(amount_of_members=case): self.ctx.reset_mock() - utils_mock.reset_mock() + self.utils_mock.reset_mock() await self.cog.createteam( self.cog, self.ctx, team_name="foo", members=(MockMember() for _ in range(case)) ) self.ctx.send.assert_awaited_once() - utils_mock.get.assert_not_called() + self.utils_mock.get.assert_not_called() - @patch("bot.cogs.jams.utils") - async def test_duplicate_members_provided(self, utils_mock): + async def test_duplicate_members_provided(self): """Should `ctx.send` and exit early because duplicate members provided and total there is only 1 member.""" self.ctx.reset_mock() member = MockMember() await self.cog.createteam(self.cog, self.ctx, "foo", (member for _ in range(5))) self.ctx.send.assert_awaited_once() - utils_mock.get.assert_not_called() + self.utils_mock.get.assert_not_called() - @patch("bot.cogs.jams.utils") - async def test_category_dont_exist(self, utils_mock): + async def test_category_dont_exist(self): """Should create code jam category.""" - utils_mock.get.return_value = None + self.utils_mock.get.return_value = None await self.cog.createteam(self.cog, self.ctx, "foo", (MockMember() for _ in range(5))) - utils_mock.get.assert_called_once() + self.utils_mock.get.assert_called_once() self.ctx.guild.create_category_channel.assert_awaited_once() category_overwrites = self.ctx.guild.create_category_channel.call_args[1]["overwrites"] self.assertFalse(category_overwrites[self.ctx.guild.default_role].read_messages) self.assertTrue(category_overwrites[self.ctx.guild.me].read_messages) - @patch("bot.cogs.jams.utils") - async def test_category_channel_exist(self, utils_mock): + async def test_category_channel_exist(self): """Should not try to create category channel.""" - utils_mock.return_value = "foo" + self.utils_mock.return_value = "foo" await self.cog.createteam(self.cog, self.ctx, "bar", (MockMember() for _ in range(5))) - utils_mock.get.assert_called_once() + self.utils_mock.get.assert_called_once() self.ctx.guild.create_category_channel.assert_not_awaited() - @patch("bot.cogs.jams.utils") - async def test_team_text_channel_creation(self, utils_mock): + async def test_team_text_channel_creation(self): """Should create text channel for team.""" - utils_mock.get.return_value = "foo" + self.utils_mock.get.return_value = "foo" await self.cog.createteam(self.cog, self.ctx, "bar", (MockMember() for _ in range(5))) # Make sure that we awaited function before getting call arguments self.ctx.guild.create_text_channel.assert_awaited_once() @@ -95,10 +94,9 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.assertFalse(overwrites[self.ctx.guild.get_role(Roles.verified)].read_messages) self.assertFalse(overwrites[self.ctx.guild.get_role(Roles.verified)].connect) - @patch("bot.cogs.jams.utils") - async def test_team_voice_channel_creation(self, utils_mock): + async def test_team_voice_channel_creation(self): """Should create new voice channel for team.""" - utils_mock.get.return_value = "foo" + self.utils_mock.get.return_value = "foo" await self.cog.createteam(self.cog, self.ctx, "my-team", (MockMember() for _ in range(5))) # Make sure that we awaited function before getting call arguments self.ctx.guild.create_voice_channel.assert_awaited_once() -- cgit v1.2.3 From 930eaebc185806c25335d9a83c5e0e7f3fddedf4 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 09:32:44 +0300 Subject: Jams: Move category checking and creation to another function --- bot/cogs/jams.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/bot/cogs/jams.py b/bot/cogs/jams.py index 1d062b0c2..0ebff5428 100644 --- a/bot/cogs/jams.py +++ b/bot/cogs/jams.py @@ -1,6 +1,6 @@ import logging -from discord import Member, PermissionOverwrite, utils +from discord import CategoryChannel, Member, PermissionOverwrite, utils from discord.ext import commands from more_itertools import unique_everseen @@ -40,21 +40,7 @@ class CodeJams(commands.Cog): ) return - code_jam_category = utils.get(ctx.guild.categories, name="Code Jam") - - if code_jam_category is None: - log.info("Code Jam category not found, creating it.") - - category_overwrites = { - ctx.guild.default_role: PermissionOverwrite(read_messages=False), - ctx.guild.me: PermissionOverwrite(read_messages=True) - } - - code_jam_category = await ctx.guild.create_category_channel( - "Code Jam", - overwrites=category_overwrites, - reason="It's code jam time!" - ) + code_jam_category = await self.get_category(ctx) # First member is always the team leader team_channel_overwrites = { @@ -108,6 +94,26 @@ class CodeJams(commands.Cog): f"**Team Members:** {' '.join(member.mention for member in members[1:])}" ) + async def get_category(self, ctx: commands.Context) -> CategoryChannel: + """Create Code Jam category when this don't exist and return this.""" + code_jam_category = utils.get(ctx.guild.categories, name="Code Jam") + + if code_jam_category is None: + log.info("Code Jam category not found, creating it.") + + category_overwrites = { + ctx.guild.default_role: PermissionOverwrite(read_messages=False), + ctx.guild.me: PermissionOverwrite(read_messages=True) + } + + code_jam_category = await ctx.guild.create_category_channel( + "Code Jam", + overwrites=category_overwrites, + reason="It's code jam time!" + ) + + return code_jam_category + def setup(bot: Bot) -> None: """Load the CodeJams cog.""" -- cgit v1.2.3 From 4b194e288aaca445947ad7df2c2202989f76a076 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 09:36:38 +0300 Subject: Jams: Move overwrites generation to outside of command --- bot/cogs/jams.py | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/bot/cogs/jams.py b/bot/cogs/jams.py index 0ebff5428..2b4575d5f 100644 --- a/bot/cogs/jams.py +++ b/bot/cogs/jams.py @@ -1,4 +1,5 @@ import logging +import typing as t from discord import CategoryChannel, Member, PermissionOverwrite, utils from discord.ext import commands @@ -41,28 +42,7 @@ class CodeJams(commands.Cog): return code_jam_category = await self.get_category(ctx) - - # First member is always the team leader - team_channel_overwrites = { - members[0]: PermissionOverwrite( - manage_messages=True, - read_messages=True, - manage_webhooks=True, - connect=True - ), - ctx.guild.default_role: PermissionOverwrite(read_messages=False, connect=False), - ctx.guild.get_role(Roles.verified): PermissionOverwrite( - read_messages=False, - connect=False - ) - } - - # Rest of members should just have read_messages - for member in members[1:]: - team_channel_overwrites[member] = PermissionOverwrite( - read_messages=True, - connect=True - ) + team_channel_overwrites = self.get_overwrites(members, ctx) # Create a text channel for the team team_channel = await ctx.guild.create_text_channel( @@ -114,6 +94,32 @@ class CodeJams(commands.Cog): return code_jam_category + def get_overwrites(self, members: t.List[Member], ctx: commands.Context) -> t.Dict[Member, PermissionOverwrite]: + """Get Code Jam team channels permission overwrites.""" + # First member is always the team leader + team_channel_overwrites = { + members[0]: PermissionOverwrite( + manage_messages=True, + read_messages=True, + manage_webhooks=True, + connect=True + ), + ctx.guild.default_role: PermissionOverwrite(read_messages=False, connect=False), + ctx.guild.get_role(Roles.verified): PermissionOverwrite( + read_messages=False, + connect=False + ) + } + + # Rest of members should just have read_messages + for member in members[1:]: + team_channel_overwrites[member] = PermissionOverwrite( + read_messages=True, + connect=True + ) + + return team_channel_overwrites + def setup(bot: Bot) -> None: """Load the CodeJams cog.""" -- cgit v1.2.3 From e8ef1b0f7ae9426da8be66fdeb6cecc81870c070 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 09:44:20 +0300 Subject: Jams: Move channels creation to new function instead inside command --- bot/cogs/jams.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/bot/cogs/jams.py b/bot/cogs/jams.py index 2b4575d5f..9089dcec2 100644 --- a/bot/cogs/jams.py +++ b/bot/cogs/jams.py @@ -41,24 +41,7 @@ class CodeJams(commands.Cog): ) return - code_jam_category = await self.get_category(ctx) - team_channel_overwrites = self.get_overwrites(members, ctx) - - # Create a text channel for the team - team_channel = await ctx.guild.create_text_channel( - team_name, - overwrites=team_channel_overwrites, - category=code_jam_category - ) - - # Create a voice channel for the team - team_voice_name = " ".join(team_name.split("-")).title() - - await ctx.guild.create_voice_channel( - team_voice_name, - overwrites=team_channel_overwrites, - category=code_jam_category - ) + team_channel = await self.create_channels(ctx, team_name, members) # Assign team leader role await members[0].add_roles(ctx.guild.get_role(Roles.team_leaders)) @@ -69,7 +52,7 @@ class CodeJams(commands.Cog): await member.add_roles(jammer_role) await ctx.send( - f":ok_hand: Team created: {team_channel.mention}\n" + f":ok_hand: Team created: {team_channel}\n" f"**Team Leader:** {members[0].mention}\n" f"**Team Members:** {' '.join(member.mention for member in members[1:])}" ) @@ -120,6 +103,30 @@ class CodeJams(commands.Cog): return team_channel_overwrites + async def create_channels(self, ctx: commands.Context, team_name: str, members: t.List[Member]) -> str: + """Create team text and voice channel. Return name of text channel.""" + # Get permission overwrites and category + team_channel_overwrites = self.get_overwrites(members, ctx) + code_jam_category = await self.get_category(ctx) + + # Create a text channel for the team + team_channel = await ctx.guild.create_text_channel( + team_name, + overwrites=team_channel_overwrites, + category=code_jam_category + ) + + # Create a voice channel for the team + team_voice_name = " ".join(team_name.split("-")).title() + + await ctx.guild.create_voice_channel( + team_voice_name, + overwrites=team_channel_overwrites, + category=code_jam_category + ) + + return str(team_channel) + def setup(bot: Bot) -> None: """Load the CodeJams cog.""" -- cgit v1.2.3 From 9719612995f2cd7e5b976031bbfd6a1591d76f23 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 09:45:31 +0300 Subject: Jams: Change return plain text to channel mention in `create_channels` --- bot/cogs/jams.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/jams.py b/bot/cogs/jams.py index 9089dcec2..5576adb2d 100644 --- a/bot/cogs/jams.py +++ b/bot/cogs/jams.py @@ -125,7 +125,7 @@ class CodeJams(commands.Cog): category=code_jam_category ) - return str(team_channel) + return team_channel.mention def setup(bot: Bot) -> None: -- cgit v1.2.3 From 8419531b899fbebb2a1f3378b4e1a98a0f45d812 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 10:49:47 +0300 Subject: Jams: Move roles adding to another function from inside of command --- bot/cogs/jams.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/bot/cogs/jams.py b/bot/cogs/jams.py index 5576adb2d..4173f10fd 100644 --- a/bot/cogs/jams.py +++ b/bot/cogs/jams.py @@ -42,14 +42,7 @@ class CodeJams(commands.Cog): return team_channel = await self.create_channels(ctx, team_name, members) - - # Assign team leader role - await members[0].add_roles(ctx.guild.get_role(Roles.team_leaders)) - - # Assign rest of roles - jammer_role = ctx.guild.get_role(Roles.jammers) - for member in members: - await member.add_roles(jammer_role) + await self.add_roles(ctx, members) await ctx.send( f":ok_hand: Team created: {team_channel}\n" @@ -127,6 +120,16 @@ class CodeJams(commands.Cog): return team_channel.mention + async def add_roles(self, ctx: commands.Context, members: t.List[Member]) -> None: + """Assign team leader and jammer roles.""" + # Assign team leader role + await members[0].add_roles(ctx.guild.get_role(Roles.team_leaders)) + + # Assign rest of roles + jammer_role = ctx.guild.get_role(Roles.jammers) + for member in members: + await member.add_roles(jammer_role) + def setup(bot: Bot) -> None: """Load the CodeJams cog.""" -- cgit v1.2.3 From d3d031fab124b8f147674a2560ae402d469ddb4e Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 11:13:10 +0300 Subject: Jams: Convert some functions to staticmethod --- bot/cogs/jams.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bot/cogs/jams.py b/bot/cogs/jams.py index 4173f10fd..16dda35c8 100644 --- a/bot/cogs/jams.py +++ b/bot/cogs/jams.py @@ -50,7 +50,8 @@ class CodeJams(commands.Cog): f"**Team Members:** {' '.join(member.mention for member in members[1:])}" ) - async def get_category(self, ctx: commands.Context) -> CategoryChannel: + @staticmethod + async def get_category(ctx: commands.Context) -> CategoryChannel: """Create Code Jam category when this don't exist and return this.""" code_jam_category = utils.get(ctx.guild.categories, name="Code Jam") @@ -70,7 +71,8 @@ class CodeJams(commands.Cog): return code_jam_category - def get_overwrites(self, members: t.List[Member], ctx: commands.Context) -> t.Dict[Member, PermissionOverwrite]: + @staticmethod + def get_overwrites(members: t.List[Member], ctx: commands.Context) -> t.Dict[Member, PermissionOverwrite]: """Get Code Jam team channels permission overwrites.""" # First member is always the team leader team_channel_overwrites = { @@ -120,7 +122,8 @@ class CodeJams(commands.Cog): return team_channel.mention - async def add_roles(self, ctx: commands.Context, members: t.List[Member]) -> None: + @staticmethod + async def add_roles(ctx: commands.Context, members: t.List[Member]) -> None: """Assign team leader and jammer roles.""" # Assign team leader role await members[0].add_roles(ctx.guild.get_role(Roles.team_leaders)) -- cgit v1.2.3 From 1c860606a122ff1378cb55e228312acb2bb2d49e Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 11:16:50 +0300 Subject: Jam Tests: Make early exiting test more secure --- tests/bot/cogs/test_jams.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index bf542458b..98fa12f66 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -1,5 +1,5 @@ import unittest -from unittest.mock import patch +from unittest.mock import AsyncMock, patch from bot.cogs.jams import CodeJams, setup from bot.constants import Roles @@ -25,13 +25,17 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Should `ctx.send` and exit early when too small amount of members.""" for case in (1, 2): with self.subTest(amount_of_members=case): + self.cog.create_channels = AsyncMock() + self.cog.add_roles = AsyncMock() + self.ctx.reset_mock() self.utils_mock.reset_mock() await self.cog.createteam( self.cog, self.ctx, team_name="foo", members=(MockMember() for _ in range(case)) ) self.ctx.send.assert_awaited_once() - self.utils_mock.get.assert_not_called() + self.cog.create_channels.assert_not_awaited() + self.cog.add_roles.assert_not_awaited() async def test_duplicate_members_provided(self): """Should `ctx.send` and exit early because duplicate members provided and total there is only 1 member.""" -- cgit v1.2.3 From fd05997c1aa9054024ad62dc0cbf19c1a296f4b7 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 11:22:52 +0300 Subject: Jam Tests: Add more assertions to result message sending test --- tests/bot/cogs/test_jams.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 98fa12f66..4307d7deb 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -126,8 +126,14 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_result_sending(self): """Should call `ctx.send` when everything goes right.""" self.ctx.reset_mock() - await self.cog.createteam(self.cog, self.ctx, "foo", (MockMember() for _ in range(5))) + members = [MockMember() for _ in range(5)] + await self.cog.createteam(self.cog, self.ctx, "foo", members) self.ctx.send.assert_awaited_once() + sent_string = self.ctx.send.call_args[0][0] + + self.assertIn(str(self.ctx.guild.create_text_channel.return_value.mention), sent_string) + self.assertIn(members[0].mention, sent_string) + self.assertIn(" ".join(member.mention for member in members[1:]), sent_string) class CodeJamSetup(unittest.TestCase): -- cgit v1.2.3 From fa4783c5e15709625e21d6a1aa766664eb2423e2 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 11:24:27 +0300 Subject: Jam Tests: Apply recent changes to overwrites test --- tests/bot/cogs/test_jams.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 4307d7deb..1cbff2674 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -78,8 +78,7 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Should have correct permission overwrites for users and roles.""" leader = MockMember() members = [leader] + [MockMember() for _ in range(4)] - await self.cog.createteam(self.cog, self.ctx, "foo", members) - overwrites = self.ctx.guild.create_text_channel.call_args[1]["overwrites"] + overwrites = self.cog.get_overwrites(members, self.ctx) # Leader permission overwrites self.assertTrue(overwrites[leader].manage_messages) -- cgit v1.2.3 From 0d2b61fd72f7b44d0534901c8f2e6ee3ccaad3f7 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 11:36:11 +0300 Subject: Jam Tests: Merge text and voice channel creation tests --- tests/bot/cogs/test_jams.py | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 1cbff2674..54f906ed9 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -1,9 +1,9 @@ import unittest -from unittest.mock import AsyncMock, patch +from unittest.mock import AsyncMock, MagicMock, patch from bot.cogs.jams import CodeJams, setup from bot.constants import Roles -from tests.helpers import MockBot, MockContext, MockGuild, MockMember, MockRole +from tests.helpers import MockBot, MockContext, MockGuild, MockMember, MockRole, MockTextChannel class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): @@ -63,17 +63,6 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.utils_mock.get.assert_called_once() self.ctx.guild.create_category_channel.assert_not_awaited() - async def test_team_text_channel_creation(self): - """Should create text channel for team.""" - self.utils_mock.get.return_value = "foo" - await self.cog.createteam(self.cog, self.ctx, "bar", (MockMember() for _ in range(5))) - # Make sure that we awaited function before getting call arguments - self.ctx.guild.create_text_channel.assert_awaited_once() - - # All other arguments is possible to get somewhere else except this - overwrites = self.ctx.guild.create_text_channel.call_args[1]["overwrites"] - self.ctx.guild.create_text_channel.assert_awaited_once_with("bar", overwrites=overwrites, category="foo") - async def test_channel_overwrites(self): """Should have correct permission overwrites for users and roles.""" leader = MockMember() @@ -97,16 +86,30 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.assertFalse(overwrites[self.ctx.guild.get_role(Roles.verified)].read_messages) self.assertFalse(overwrites[self.ctx.guild.get_role(Roles.verified)].connect) - async def test_team_voice_channel_creation(self): - """Should create new voice channel for team.""" + async def test_team_channels_creation(self): + """Should create new voice and text channel for team.""" self.utils_mock.get.return_value = "foo" - await self.cog.createteam(self.cog, self.ctx, "my-team", (MockMember() for _ in range(5))) - # Make sure that we awaited function before getting call arguments - self.ctx.guild.create_voice_channel.assert_awaited_once() + members = [MockMember() for _ in range(5)] - # All other arguments is possible to get somewhere else except this - overwrites = self.ctx.guild.create_voice_channel.call_args[1]["overwrites"] - self.ctx.guild.create_voice_channel.assert_awaited_once_with("My Team", overwrites=overwrites, category="foo") + self.cog.get_overwrites = MagicMock() + self.cog.get_category = AsyncMock() + self.ctx.guild.create_text_channel.return_value = MockTextChannel(mention="foobar-channel") + actual = await self.cog.create_channels(self.ctx, "my-team", members) + + self.assertEqual("foobar-channel", actual) + self.cog.get_overwrites.assert_called_once_with(members, self.ctx) + self.cog.get_category.assert_awaited_once_with(self.ctx) + + self.ctx.guild.create_text_channel.assert_awaited_once_with( + "my-team", + overwrites=self.cog.get_overwrites.return_value, + category=self.cog.get_category.return_value + ) + self.ctx.guild.create_voice_channel.assert_awaited_once_with( + "My Team", + overwrites=self.cog.get_overwrites.return_value, + category=self.cog.get_category.return_value + ) async def test_jam_roles_adding(self): """Should add team leader role to leader and jam role to every team member.""" -- cgit v1.2.3 From 4af2be7310141ab3ddc34a2184366c0d8212cdd5 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 11:39:46 +0300 Subject: Jam Tests: Simplify and update `test_category_channel_exist` --- tests/bot/cogs/test_jams.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 54f906ed9..ae3e35dbb 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -58,9 +58,7 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_category_channel_exist(self): """Should not try to create category channel.""" - self.utils_mock.return_value = "foo" - await self.cog.createteam(self.cog, self.ctx, "bar", (MockMember() for _ in range(5))) - self.utils_mock.get.assert_called_once() + await self.cog.get_category(self.ctx) self.ctx.guild.create_category_channel.assert_not_awaited() async def test_channel_overwrites(self): -- cgit v1.2.3 From ea91aefe55bf52fca6714897347bb24d4a4efb5b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 11:41:25 +0300 Subject: Jam Tests: Apply recent changes to `test_category_dont_exist` --- tests/bot/cogs/test_jams.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index ae3e35dbb..ecd06179f 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -48,8 +48,7 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_category_dont_exist(self): """Should create code jam category.""" self.utils_mock.get.return_value = None - await self.cog.createteam(self.cog, self.ctx, "foo", (MockMember() for _ in range(5))) - self.utils_mock.get.assert_called_once() + await self.cog.get_category(self.ctx) self.ctx.guild.create_category_channel.assert_awaited_once() category_overwrites = self.ctx.guild.create_category_channel.call_args[1]["overwrites"] -- cgit v1.2.3 From 6e070a43f616f898e328bfc4581ed48551e73b12 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 11:47:13 +0300 Subject: Jam Tests: Implement default arguments To avoid repeating same arguments, added default arguments that is unpacked on function call. --- tests/bot/cogs/test_jams.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index ecd06179f..94be8dd03 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -17,6 +17,7 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.ctx = MockContext(bot=self.bot, author=self.command_user, guild=self.guild) self.cog = CodeJams(self.bot) self.utils_mock = patch("bot.cogs.jams.utils").start() + self.default_args = [self.cog, self.ctx, "foo"] def tearDown(self): self.utils_mock.stop() @@ -30,9 +31,8 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.ctx.reset_mock() self.utils_mock.reset_mock() - await self.cog.createteam( - self.cog, self.ctx, team_name="foo", members=(MockMember() for _ in range(case)) - ) + await self.cog.createteam(*self.default_args, (MockMember() for _ in range(case))) + self.ctx.send.assert_awaited_once() self.cog.create_channels.assert_not_awaited() self.cog.add_roles.assert_not_awaited() @@ -41,7 +41,7 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Should `ctx.send` and exit early because duplicate members provided and total there is only 1 member.""" self.ctx.reset_mock() member = MockMember() - await self.cog.createteam(self.cog, self.ctx, "foo", (member for _ in range(5))) + await self.cog.createteam(*self.default_args, (member for _ in range(5))) self.ctx.send.assert_awaited_once() self.utils_mock.get.assert_not_called() -- cgit v1.2.3 From b129658bf260d458d5fad5925e945c78f881388a Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 11:49:00 +0300 Subject: Jam Tests: Remove unnecessary `Context` mock resets --- tests/bot/cogs/test_jams.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 94be8dd03..0f8ba3574 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -39,7 +39,6 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_duplicate_members_provided(self): """Should `ctx.send` and exit early because duplicate members provided and total there is only 1 member.""" - self.ctx.reset_mock() member = MockMember() await self.cog.createteam(*self.default_args, (member for _ in range(5))) self.ctx.send.assert_awaited_once() @@ -124,7 +123,6 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_result_sending(self): """Should call `ctx.send` when everything goes right.""" - self.ctx.reset_mock() members = [MockMember() for _ in range(5)] await self.cog.createteam(self.cog, self.ctx, "foo", members) self.ctx.send.assert_awaited_once() -- cgit v1.2.3 From 0481bcc1d99dd9d7fe9d41276599437b11670b27 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 11 Jun 2020 11:50:40 +0300 Subject: Jam Tests: Apply recent command splitting to `test_jam_roles_adding` --- tests/bot/cogs/test_jams.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 0f8ba3574..54fe0b5f2 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -111,11 +111,11 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Should add team leader role to leader and jam role to every team member.""" leader_role = MockRole(name="Team Leader") jam_role = MockRole(name="Jammer") - self.ctx.guild.get_role.side_effect = [MockRole(), leader_role, jam_role] + self.ctx.guild.get_role.side_effect = [leader_role, jam_role] leader = MockMember() members = [leader] + [MockMember() for _ in range(4)] - await self.cog.createteam(self.cog, self.ctx, "foo", members) + await self.cog.add_roles(self.ctx, members) leader.add_roles.assert_any_await(leader_role) for member in members: -- cgit v1.2.3 From f4767769afc8c6dfe4ac81d4e9b9e02f2f58054c Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 11 Jun 2020 18:06:59 +0200 Subject: Incidents: add #incidents-archive channel constant --- bot/constants.py | 1 + config-default.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/bot/constants.py b/bot/constants.py index 02b82cf23..02c8adf43 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -401,6 +401,7 @@ class Channels(metaclass=YAMLGetter): helpers: int how_to_get_help: int incidents: int + incidents_archive: int message_log: int meta: int mod_alerts: int diff --git a/config-default.yml b/config-default.yml index c59abdc39..a68647f72 100644 --- a/config-default.yml +++ b/config-default.yml @@ -176,6 +176,7 @@ guild: organisation: &ORGANISATION 551789653284356126 staff_lounge: &STAFF_LOUNGE 464905259261755392 incidents: 714214212200562749 + incidents_archive: 720668923636351037 # Voice admins_voice: &ADMINS_VOICE 500734494840717332 -- cgit v1.2.3 From 5db3a82de9f37d769ed8983c83063dfdd6878fee Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 11 Jun 2020 18:26:21 +0200 Subject: Incidents: add #incidents-archive webhook constant --- bot/constants.py | 1 + config-default.yml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/constants.py b/bot/constants.py index 02c8adf43..c663db333 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -430,6 +430,7 @@ class Webhooks(metaclass=YAMLGetter): reddit: int duck_pond: int dev_log: int + incidents_archive: int class Roles(metaclass=YAMLGetter): diff --git a/config-default.yml b/config-default.yml index a68647f72..974ce508d 100644 --- a/config-default.yml +++ b/config-default.yml @@ -255,7 +255,7 @@ guild: duck_pond: 637821475327311927 dev_log: 680501655111729222 python_news: &PYNEWS_WEBHOOK 704381182279942324 - + incidents_archive: 720671599790915702 filter: -- cgit v1.2.3 From d520203717b8aaa6358071978a1ac9a23418d1c9 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 11 Jun 2020 18:45:50 +0200 Subject: Incidents: define allowed roles and emoji These serve as whitelists, i.e. any reaction using an emoji not explicitly allowed, or from a user not specifically allowed, will be rejected. Such reactions will be removed by the bot. --- bot/cogs/moderation/incidents.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index baceddf0c..49180da7c 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -4,7 +4,7 @@ from enum import Enum from discord.ext.commands import Cog from bot.bot import Bot -from bot.constants import Emojis +from bot.constants import Emojis, Roles log = logging.getLogger(__name__) @@ -17,6 +17,10 @@ class Signal(Enum): INVESTIGATING = Emojis.incident_investigating +ALLOWED_ROLES: t.Set[int] = {Roles.moderators, Roles.admins, Roles.owners} +ALLOWED_EMOJI: t.Set[str] = {signal.value for signal in Signal} + + class Incidents(Cog): """Automation for the #incidents channel.""" -- cgit v1.2.3 From 3195d16cf16f80dca6b66b87bc7b954d10d60e7a Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 13:25:02 +0200 Subject: Incidents: define method stubs for message event handling --- bot/cogs/moderation/incidents.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 49180da7c..c85a68a14 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -1,6 +1,7 @@ import logging from enum import Enum +import discord from discord.ext.commands import Cog from bot.bot import Bot @@ -26,3 +27,12 @@ class Incidents(Cog): def __init__(self, bot: Bot) -> None: self.bot = bot + + async def add_signals(self, incident: discord.Message) -> None: + """Add `Signal` member emoji to `incident` as reactions.""" + ... + + @Cog.listener() + async def on_message(self, message: discord.Message) -> None: + """Pass each incident sent in #incidents to `add_signals`.""" + ... -- cgit v1.2.3 From 781d8f8d4bc76cb2ca9db4f3b7149d11892e714b Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 13:28:32 +0200 Subject: Incidents: implement `add_signals` helper Looks like it can be static, at least for now. --- bot/cogs/moderation/incidents.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index c85a68a14..2424c008d 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -1,4 +1,5 @@ import logging +import typing as t from enum import Enum import discord @@ -28,9 +29,12 @@ class Incidents(Cog): def __init__(self, bot: Bot) -> None: self.bot = bot - async def add_signals(self, incident: discord.Message) -> None: + @staticmethod + async def add_signals(incident: discord.Message) -> None: """Add `Signal` member emoji to `incident` as reactions.""" - ... + for signal_emoji in Signal: + log.debug(f"Adding reaction: {signal_emoji.value}") + await incident.add_reaction(signal_emoji.value) @Cog.listener() async def on_message(self, message: discord.Message) -> None: -- cgit v1.2.3 From e8bb1aa59dece803a920efb5ebbdd6098025bdc6 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 13:34:34 +0200 Subject: Incidents: implement `on_message` listener & guards --- bot/cogs/moderation/incidents.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 2424c008d..91b949173 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -6,7 +6,7 @@ import discord from discord.ext.commands import Cog from bot.bot import Bot -from bot.constants import Emojis, Roles +from bot.constants import Channels, Emojis, Roles log = logging.getLogger(__name__) @@ -38,5 +38,22 @@ class Incidents(Cog): @Cog.listener() async def on_message(self, message: discord.Message) -> None: - """Pass each incident sent in #incidents to `add_signals`.""" - ... + """ + Pass each incident sent in #incidents to `add_signals`. + + We recognize several exceptions. The following will be ignored: + * Messages sent outside of #incidents + * Messages Sent by bots + * Messages starting with the hash symbol # + + Prefix message with # in situations where a verbal response is necessary. + Each such message must be deleted manually. + """ + if message.channel.id != Channels.incidents or message.author.bot: + return + + if message.content.startswith("#"): + log.debug(f"Ignoring comment message: {message.content=}") + return + + await self.add_signals(message) -- cgit v1.2.3 From 5b6b2de2fdfb9b2893b4e9321e4a46b19b4bfb20 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 14:01:47 +0200 Subject: Incidents: implement & schedule `crawl_incidents` task See docstring for further information. This will run on start-up to retroactively add missing emoji. Ratelimit-wise this should be fine, as there should never be too many missing emoji. --- bot/cogs/moderation/incidents.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 91b949173..e773636e7 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -1,3 +1,4 @@ +import asyncio import logging import typing as t from enum import Enum @@ -27,7 +28,38 @@ class Incidents(Cog): """Automation for the #incidents channel.""" def __init__(self, bot: Bot) -> None: + """Schedule `crawl_task` on start-up.""" self.bot = bot + self.crawl_task = self.bot.loop.create_task(self.crawl_incidents()) + + async def crawl_incidents(self) -> None: + """ + Crawl #incidents and add missing emoji where necessary. + + This is to catch-up should an incident be reported while the bot wasn't listening. + Internally, we simply walk the channel history and pass each message to `on_message`. + + In order to avoid drowning in ratelimits, we take breaks after each message. + + Once this task is scheduled, listeners should await it. The crawl assumes that + the channel history doesn't change as we go over it. + """ + await self.bot.wait_until_guild_available() + incidents: discord.TextChannel = self.bot.get_channel(Channels.incidents) + + # Limit the query at 50 as in practice, there should never be this many messages, + # and if there are, something has likely gone very wrong + limit = 50 + + # Seconds to sleep after each message + sleep = 2 + + log.debug(f"Crawling messages in #incidents: {limit=}, {sleep=}") + async for message in incidents.history(limit=limit): + await self.on_message(message) + await asyncio.sleep(sleep) + + log.debug("Crawl task finished!") @staticmethod async def add_signals(incident: discord.Message) -> None: -- cgit v1.2.3 From 1aaaee1144f660af7a69d12f814d0073451da7be Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 14:02:58 +0200 Subject: Incidents: make `on_message` ignore pinned messages This is now necessary as we call the listener ourselves from the crawl task. An already existing, pinned message, can be received. --- bot/cogs/moderation/incidents.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index e773636e7..1b9d26522 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -77,6 +77,7 @@ class Incidents(Cog): * Messages sent outside of #incidents * Messages Sent by bots * Messages starting with the hash symbol # + * Pinned (header) messages Prefix message with # in situations where a verbal response is necessary. Each such message must be deleted manually. @@ -88,4 +89,8 @@ class Incidents(Cog): log.debug(f"Ignoring comment message: {message.content=}") return + if message.pinned: + log.debug(f"Ignoring header message: {message.pinned=}") + return + await self.add_signals(message) -- cgit v1.2.3 From 1f6cd4313c91ed114a1de04de14355648fe88bf9 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 14:06:12 +0200 Subject: Incidents: only `add_signals` if missing --- bot/cogs/moderation/incidents.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 1b9d26522..43b1106ad 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -64,9 +64,17 @@ class Incidents(Cog): @staticmethod async def add_signals(incident: discord.Message) -> None: """Add `Signal` member emoji to `incident` as reactions.""" + existing_reacts = {str(reaction.emoji) for reaction in incident.reactions if reaction.me} + for signal_emoji in Signal: - log.debug(f"Adding reaction: {signal_emoji.value}") - await incident.add_reaction(signal_emoji.value) + + # This will not raise, but it is a superfluous API call that can be avoided + if signal_emoji.value in existing_reacts: + log.debug(f"Skipping emoji as it's already been placed: {signal_emoji}") + + else: + log.debug(f"Adding reaction: {signal_emoji}") + await incident.add_reaction(signal_emoji.value) @Cog.listener() async def on_message(self, message: discord.Message) -> None: -- cgit v1.2.3 From 0f9f25e703325bae172148bb6a30c1118b905fcb Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 14:22:02 +0200 Subject: Incidents: add `event_lock` for simple event synchronization --- bot/cogs/moderation/incidents.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 43b1106ad..1cfa45dc4 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -28,8 +28,10 @@ class Incidents(Cog): """Automation for the #incidents channel.""" def __init__(self, bot: Bot) -> None: - """Schedule `crawl_task` on start-up.""" + """Prepare `event_lock` and schedule `crawl_task` on start-up.""" self.bot = bot + + self.event_lock = asyncio.Lock() self.crawl_task = self.bot.loop.create_task(self.crawl_incidents()) async def crawl_incidents(self) -> None: -- cgit v1.2.3 From 5762e57696978843991058f7bbfa826e3020dbba Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 14:53:00 +0200 Subject: Incidents: abstract incident checking into a helper method The code is now basically self-documenting, the docstring is no longer necessary. The ultimate goal is to allow `crawl_incidents` to be more smart about which messages need to be passed to `add_signals`, so that it doesn't need to sleep after each message. --- bot/cogs/moderation/incidents.py | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 1cfa45dc4..e3c3922a1 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -24,6 +24,17 @@ ALLOWED_ROLES: t.Set[int] = {Roles.moderators, Roles.admins, Roles.owners} ALLOWED_EMOJI: t.Set[str] = {signal.value for signal in Signal} +def is_incident(message: discord.Message) -> bool: + """True if `message` qualifies as an incident, False otherwise.""" + conditions = ( + message.channel.id == Channels.incidents, # Message sent in #incidents + not message.author.bot, # Not by a bot + not message.content.startswith("#"), # Doesn't start with a hash + not message.pinned, # And isn't header + ) + return all(conditions) + + class Incidents(Cog): """Automation for the #incidents channel.""" @@ -80,27 +91,6 @@ class Incidents(Cog): @Cog.listener() async def on_message(self, message: discord.Message) -> None: - """ - Pass each incident sent in #incidents to `add_signals`. - - We recognize several exceptions. The following will be ignored: - * Messages sent outside of #incidents - * Messages Sent by bots - * Messages starting with the hash symbol # - * Pinned (header) messages - - Prefix message with # in situations where a verbal response is necessary. - Each such message must be deleted manually. - """ - if message.channel.id != Channels.incidents or message.author.bot: - return - - if message.content.startswith("#"): - log.debug(f"Ignoring comment message: {message.content=}") - return - - if message.pinned: - log.debug(f"Ignoring header message: {message.pinned=}") - return - - await self.add_signals(message) + """Pass `message` to `add_signals` if and only if it satisfies `is_incident`.""" + if is_incident(message): + await self.add_signals(message) -- cgit v1.2.3 From 166fc5f441a56d86202e857059011fdc75ce2740 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 15:04:24 +0200 Subject: Incidents: implement `own_reactions` helper --- bot/cogs/moderation/incidents.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index e3c3922a1..8a49ec8b1 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -35,6 +35,11 @@ def is_incident(message: discord.Message) -> bool: return all(conditions) +def own_reactions(message: discord.Message) -> t.Set[str]: + """Get the set of reactions placed on `message` by the bot itself.""" + return {str(reaction.emoji) for reaction in message.reactions if reaction.me} + + class Incidents(Cog): """Automation for the #incidents channel.""" @@ -77,7 +82,7 @@ class Incidents(Cog): @staticmethod async def add_signals(incident: discord.Message) -> None: """Add `Signal` member emoji to `incident` as reactions.""" - existing_reacts = {str(reaction.emoji) for reaction in incident.reactions if reaction.me} + existing_reacts = own_reactions(incident) for signal_emoji in Signal: -- cgit v1.2.3 From f7756b0246dec293f9918f3ea3ac4d6139affddd Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 15:05:48 +0200 Subject: Incidents: implement `has_signals` helper --- bot/cogs/moderation/incidents.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 8a49ec8b1..0d146bdc5 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -40,6 +40,12 @@ def own_reactions(message: discord.Message) -> t.Set[str]: return {str(reaction.emoji) for reaction in message.reactions if reaction.me} +def has_signals(message: discord.Message) -> bool: + """True if `message` already has all `Signal` reactions, False otherwise.""" + missing_signals = ALLOWED_EMOJI - own_reactions(message) + return not missing_signals + + class Incidents(Cog): """Automation for the #incidents channel.""" -- cgit v1.2.3 From b7f61a4bf92b19a42dd1f72336d67a092b5d8029 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 15:10:21 +0200 Subject: Incidents: move `add_signals` to module namespace Looks like we'll need quite a few helpers, and I think it's cleaner to keep them at module level. It helps avoid the question of: what do I do if a staticmethod depends on another staticmethod? --- bot/cogs/moderation/incidents.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 0d146bdc5..f7ef86836 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -46,6 +46,21 @@ def has_signals(message: discord.Message) -> bool: return not missing_signals +async def add_signals(incident: discord.Message) -> None: + """Add `Signal` member emoji to `incident` as reactions.""" + existing_reacts = own_reactions(incident) + + for signal_emoji in Signal: + + # This will not raise, but it is a superfluous API call that can be avoided + if signal_emoji.value in existing_reacts: + log.debug(f"Skipping emoji as it's already been placed: {signal_emoji}") + + else: + log.debug(f"Adding reaction: {signal_emoji}") + await incident.add_reaction(signal_emoji.value) + + class Incidents(Cog): """Automation for the #incidents channel.""" @@ -85,23 +100,8 @@ class Incidents(Cog): log.debug("Crawl task finished!") - @staticmethod - async def add_signals(incident: discord.Message) -> None: - """Add `Signal` member emoji to `incident` as reactions.""" - existing_reacts = own_reactions(incident) - - for signal_emoji in Signal: - - # This will not raise, but it is a superfluous API call that can be avoided - if signal_emoji.value in existing_reacts: - log.debug(f"Skipping emoji as it's already been placed: {signal_emoji}") - - else: - log.debug(f"Adding reaction: {signal_emoji}") - await incident.add_reaction(signal_emoji.value) - @Cog.listener() async def on_message(self, message: discord.Message) -> None: """Pass `message` to `add_signals` if and only if it satisfies `is_incident`.""" if is_incident(message): - await self.add_signals(message) + await add_signals(message) -- cgit v1.2.3 From 9a540a344ad79cd5766389d36e75536d751862b0 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 15:19:15 +0200 Subject: Incidents: make `crawl_incidents` smarter The crawler now avoids making API calls for messages which: * Are not incidents * Already have all signals As a result, we can sleep only after making actual calls. This speeds up the task completion considerable, while also making it lighter on the API. Victory! --- bot/cogs/moderation/incidents.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index f7ef86836..d2b4581e7 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -76,12 +76,10 @@ class Incidents(Cog): Crawl #incidents and add missing emoji where necessary. This is to catch-up should an incident be reported while the bot wasn't listening. - Internally, we simply walk the channel history and pass each message to `on_message`. + After adding reactions, we take a short break to avoid drowning in ratelimits. - In order to avoid drowning in ratelimits, we take breaks after each message. - - Once this task is scheduled, listeners should await it. The crawl assumes that - the channel history doesn't change as we go over it. + Once this task is scheduled, listeners that change messages should await it. + The crawl assumes that the channel history doesn't change as we go over it. """ await self.bot.wait_until_guild_available() incidents: discord.TextChannel = self.bot.get_channel(Channels.incidents) @@ -95,7 +93,16 @@ class Incidents(Cog): log.debug(f"Crawling messages in #incidents: {limit=}, {sleep=}") async for message in incidents.history(limit=limit): - await self.on_message(message) + + if not is_incident(message): + log.debug("Skipping message: not an incident") + continue + + if has_signals(message): + log.debug("Skipping message: already has all signals") + continue + + await add_signals(message) await asyncio.sleep(sleep) log.debug("Crawl task finished!") -- cgit v1.2.3 From 9e6835cef2210910db4ad110c0906a09fd5c5411 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 15:40:39 +0200 Subject: Incidents: implement `resolve_message` See docstring. The exception log is DEBUG level as failure does not necessarily indicate that we have done something wrong. We rely on the API to tell us that the message no longer exists in situations where we have 2 coroutines racing to archive the same message. --- bot/cogs/moderation/incidents.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index d2b4581e7..d994054c8 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -107,6 +107,37 @@ class Incidents(Cog): log.debug("Crawl task finished!") + async def resolve_message(self, message_id: int) -> t.Optional[discord.Message]: + """ + Get `discord.Message` for `message_id` from cache, or API. + + We first look into the local cache to see if the message is present. + + If not, we try to fetch the message from the API. This is necessary for messages + which were sent before the bot's current session. + + However, in an edge-case, it is also possible that the message was already deleted, + and the API will return a 404. In such a case, None will be returned. This signals + that the event for `message_id` should be ignored. + """ + await self.bot.wait_until_guild_available() # First make sure that the cache is ready + log.debug(f"Resolving message for: {message_id=}") + message: discord.Message = self.bot._connection._get_message(message_id) # noqa: Private attribute + + if message is not None: + log.debug("Message was found in cache") + return message + + log.debug("Message not found, attempting to fetch") + try: + message = await self.bot.get_channel(Channels.incidents).fetch_message(message_id) + except Exception as exc: + log.debug(f"Failed to fetch message: {exc}") + return None + else: + log.debug("Message fetched successfully!") + return message + @Cog.listener() async def on_message(self, message: discord.Message) -> None: """Pass `message` to `add_signals` if and only if it satisfies `is_incident`.""" -- cgit v1.2.3 From 3c699936bf3cfa076f7791d6b8fe16ad4dd94aa6 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 15:59:15 +0200 Subject: Incidents: implement reaction listener See docstring! --- bot/cogs/moderation/incidents.py | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index d994054c8..88ed04f45 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -138,6 +138,52 @@ class Incidents(Cog): log.debug("Message fetched successfully!") return message + async def process_event(self, reaction: str, message: discord.Message, member: discord.Member) -> None: + log.debug("Processing event...") + + @Cog.listener() + async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> None: + """ + Pre-process `payload` and pass it to `process_event` if appropriate. + + We abort instantly if `payload` doesn't relate to a message sent in #incidents. + + If `payload` relates to a message in #incidents, we first ensure that `crawl_task` has + finished, to make sure we don't mutate channel state as we're crawling it. + + Next, we acquire `event_lock` - to prevent racing, events are processed one at a time. + + Once we have the lock, the `discord.Message` object for this event must be resolved. + If the lock was previously held by an event which successfully relayed the incident, + this will fail and we abort the current event. + + Finally, with both the lock and the `discord.Message` instance in our hands, we delegate + to `process_event` to handle the event. + + The justification for using a raw listener is the need to receive events for messages + which were not cached in the current session. As a result, a certain amount of + complexity is introduced, but at the moment this doesn't appear to be avoidable. + """ + if payload.channel_id != Channels.incidents: + return + + log.debug(f"Received reaction add event in #incidents, waiting for crawler: {self.crawl_task.done()=}") + await self.crawl_task + + log.debug(f"Acquiring event lock: {self.event_lock.locked()=}") + async with self.event_lock: + message = await self.resolve_message(payload.message_id) + + if message is None: + log.debug("Listener will abort as related message does not exist!") + return + + if not is_incident(message): + log.debug("Ignoring event for a non-incident message") + return + + await self.process_event(str(payload.emoji), message, payload.member) + @Cog.listener() async def on_message(self, message: discord.Message) -> None: """Pass `message` to `add_signals` if and only if it satisfies `is_incident`.""" -- cgit v1.2.3 From d7165bf5547340242cb99460a35cabd753d60c42 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 17:17:36 +0200 Subject: Incidents: implement `archive` method --- bot/cogs/moderation/incidents.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 88ed04f45..8781d6749 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -7,7 +7,7 @@ import discord from discord.ext.commands import Cog from bot.bot import Bot -from bot.constants import Channels, Emojis, Roles +from bot.constants import Channels, Emojis, Roles, Webhooks log = logging.getLogger(__name__) @@ -107,6 +107,44 @@ class Incidents(Cog): log.debug("Crawl task finished!") + async def archive(self, incident: discord.Message, outcome: Signal) -> bool: + """ + Relay `incident` to the #incidents-archive channel. + + The following pieces of information are relayed: + * Incident message content (clean, pingless) + * Incident author name (as webhook author) + * Incident author avatar (as webhook avatar) + * Resolution signal (`outcome`) + + Return True if the relay finishes successfully. If anything goes wrong, meaning + not all information was relayed, return False. This signals that the original + message is not safe to be deleted, as we will lose some information. + """ + log.debug(f"Archiving incident: {incident.id} with outcome: {outcome}") + try: + # First we try to grab the webhook + webhook: discord.Webhook = await self.bot.fetch_webhook(Webhooks.incidents_archive) + + # Now relay the incident + message: discord.Message = await webhook.send( + content=incident.clean_content, # Clean content will prevent mentions from pinging + username=incident.author.name, + avatar_url=incident.author.avatar_url, + wait=True, # This makes the method return the sent Message object + ) + + # Finally add the `outcome` emoji + await message.add_reaction(outcome.value) + + except Exception as exc: + log.exception("Failed to archive incident to #incidents-archive", exc_info=exc) + return False + + else: + log.debug("Message archived successfully!") + return True + async def resolve_message(self, message_id: int) -> t.Optional[discord.Message]: """ Get `discord.Message` for `message_id` from cache, or API. -- cgit v1.2.3 From 44e30289d9682a92ef6e6d2ca8e9cf9b669ad65c Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 17:23:26 +0200 Subject: Incidents: implement `make_confirmation_task` method --- bot/cogs/moderation/incidents.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 8781d6749..2186530d9 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -145,6 +145,21 @@ class Incidents(Cog): log.debug("Message archived successfully!") return True + def make_confirmation_task(self, incident: discord.Message, timeout: int = 5) -> asyncio.Task: + """ + Create a task to wait `timeout` seconds for `incident` to be deleted. + + If `timeout` passes, this will raise `asyncio.TimeoutError`, signaling that we haven't + been able to confirm that the message was deleted. + """ + log.debug(f"Confirmation task will wait {timeout=} seconds for {incident.id=} to be deleted") + coroutine = self.bot.wait_for( + event="raw_message_delete", + check=lambda payload: payload.message_id == incident.id, + timeout=timeout, + ) + return self.bot.loop.create_task(coroutine) + async def resolve_message(self, message_id: int) -> t.Optional[discord.Message]: """ Get `discord.Message` for `message_id` from cache, or API. -- cgit v1.2.3 From 7bc6aff14c5a78b708b11cafbd4eba431b3fe52b Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 17:28:56 +0200 Subject: Incidents: implement `process_event` coroutine This contains the main logic for handling reactions and glues all the helpers together. Unfortunately, gracefully handling everything that can go wrong in the process requires quite a lot of code ~ but, at least to me, it seems like this all should now be fairly safe. The idea to await the message delete event before releasing the lock was conceived by Ves, while Mark helped me refine it. Co-authored-by: Sebastiaan Zeeff Co-authored-by: MarkKoz --- bot/cogs/moderation/incidents.py | 55 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 2186530d9..00cceca7d 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -160,6 +160,58 @@ class Incidents(Cog): ) return self.bot.loop.create_task(coroutine) + async def process_event(self, reaction: str, incident: discord.Message, member: discord.Member) -> None: + """ + Process a valid `reaction_add` event in #incidents. + + First, we check that the reaction is a recognized `Signal` member, and that it was sent by + a permitted user (at least one role in `ALLOWED_ROLES`). If not, the reaction is removed. + + If the reaction was either `Signal.ACTIONED` or `Signal.NOT_ACTIONED`, we attempt to relay + the report to #incidents-archive. If successful, the original message is deleted. + + We do not release `event_lock` until we receive the corresponding `message_delete` event. + This ensures that if there is a racing event awaiting the lock, it will fail to find the + message, and will abort. + """ + members_roles: t.Set[int] = {role.id for role in member.roles} + if not members_roles & ALLOWED_ROLES: # Intersection is truthy on at least 1 common element + log.debug(f"Removing invalid reaction: user {member} is not permitted to send signals") + await incident.remove_reaction(reaction, member) + return + + if reaction not in ALLOWED_EMOJI: + log.debug(f"Removing invalid reaction: emoji {reaction} is not a valid signal") + await incident.remove_reaction(reaction, member) + return + + # If we reach this point, we know that `emoji` is a `Signal` member + signal = Signal(reaction) + log.debug(f"Received signal: {signal}") + + if signal not in (Signal.ACTIONED, Signal.NOT_ACTIONED): + log.debug("Reaction was valid, but no action is currently defined for it") + return + + relay_successful = await self.archive(incident, signal) + if not relay_successful: + log.debug("Original message will not be deleted as we failed to relay it to the archive") + return + + timeout = 5 # Seconds + confirmation_task = self.make_confirmation_task(incident, timeout) + + log.debug("Deleting original message") + await incident.delete() + + log.debug(f"Awaiting deletion confirmation: {timeout=} seconds") + try: + await confirmation_task + except asyncio.TimeoutError: + log.warning(f"Did not receive incident deletion confirmation within {timeout} seconds!") + else: + log.debug("Deletion was confirmed") + async def resolve_message(self, message_id: int) -> t.Optional[discord.Message]: """ Get `discord.Message` for `message_id` from cache, or API. @@ -191,9 +243,6 @@ class Incidents(Cog): log.debug("Message fetched successfully!") return message - async def process_event(self, reaction: str, message: discord.Message, member: discord.Member) -> None: - log.debug("Processing event...") - @Cog.listener() async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> None: """ -- cgit v1.2.3 From 506f91a77d3dd1bb92222bd3fce4a7316677ddbb Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 17:37:27 +0200 Subject: Incidents: do not process reaction events from bots --- bot/cogs/moderation/incidents.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 00cceca7d..f19bdb41f 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -248,7 +248,8 @@ class Incidents(Cog): """ Pre-process `payload` and pass it to `process_event` if appropriate. - We abort instantly if `payload` doesn't relate to a message sent in #incidents. + We abort instantly if `payload` doesn't relate to a message sent in #incidents, + or if it was sent by a bot. If `payload` relates to a message in #incidents, we first ensure that `crawl_task` has finished, to make sure we don't mutate channel state as we're crawling it. @@ -266,7 +267,7 @@ class Incidents(Cog): which were not cached in the current session. As a result, a certain amount of complexity is introduced, but at the moment this doesn't appear to be avoidable. """ - if payload.channel_id != Channels.incidents: + if payload.channel_id != Channels.incidents or payload.member.bot: return log.debug(f"Received reaction add event in #incidents, waiting for crawler: {self.crawl_task.done()=}") -- cgit v1.2.3 From f41794d31135209a7a38cc9c17ac62d3e06f6279 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 17:56:05 +0200 Subject: Incidents: log `event_lock` release --- bot/cogs/moderation/incidents.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index f19bdb41f..d69439dc3 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -286,6 +286,7 @@ class Incidents(Cog): return await self.process_event(str(payload.emoji), message, payload.member) + log.debug("Releasing event lock") @Cog.listener() async def on_message(self, message: discord.Message) -> None: -- cgit v1.2.3 From 0eb62724baba42fffffcd47ff4fe5451dc521593 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 18:01:10 +0200 Subject: Incidents: avoid lambda check; make regular function --- bot/cogs/moderation/incidents.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index d69439dc3..16af17f99 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -153,11 +153,11 @@ class Incidents(Cog): been able to confirm that the message was deleted. """ log.debug(f"Confirmation task will wait {timeout=} seconds for {incident.id=} to be deleted") - coroutine = self.bot.wait_for( - event="raw_message_delete", - check=lambda payload: payload.message_id == incident.id, - timeout=timeout, - ) + + def check(payload: discord.RawReactionActionEvent) -> bool: + return payload.message_id == incident.id + + coroutine = self.bot.wait_for(event="raw_message_delete", check=check, timeout=timeout) return self.bot.loop.create_task(coroutine) async def process_event(self, reaction: str, incident: discord.Message, member: discord.Member) -> None: -- cgit v1.2.3 From 39691c052d50907c049f3294cc5eef6536461656 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 21:08:14 +0200 Subject: Incidents: extend documentation This adds a proper class docstring & small touch-ups to local comments where necessary. --- bot/cogs/moderation/incidents.py | 60 ++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 16af17f99..151584d38 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -13,13 +13,19 @@ log = logging.getLogger(__name__) class Signal(Enum): - """Recognized incident status signals.""" + """ + Recognized incident status signals. + + This binds emoji to actions. The bot will only react to emoji linked here. + All other signals are seen as invalid. + """ ACTIONED = Emojis.incident_actioned NOT_ACTIONED = Emojis.incident_unactioned INVESTIGATING = Emojis.incident_investigating +# Reactions from roles not listed here, or using emoji not listed here, will be removed ALLOWED_ROLES: t.Set[int] = {Roles.moderators, Roles.admins, Roles.owners} ALLOWED_EMOJI: t.Set[str] = {signal.value for signal in Signal} @@ -42,12 +48,16 @@ def own_reactions(message: discord.Message) -> t.Set[str]: def has_signals(message: discord.Message) -> bool: """True if `message` already has all `Signal` reactions, False otherwise.""" - missing_signals = ALLOWED_EMOJI - own_reactions(message) + missing_signals = ALLOWED_EMOJI - own_reactions(message) # In `ALLOWED_EMOJI` but not in `own_reactions(message)` return not missing_signals async def add_signals(incident: discord.Message) -> None: - """Add `Signal` member emoji to `incident` as reactions.""" + """ + Add `Signal` member emoji to `incident` as reactions. + + If the emoji has already been placed on `incident` by the bot, it will be skipped. + """ existing_reacts = own_reactions(incident) for signal_emoji in Signal: @@ -62,7 +72,34 @@ async def add_signals(incident: discord.Message) -> None: class Incidents(Cog): - """Automation for the #incidents channel.""" + """ + Automation for the #incidents channel. + + This cog does not provide a command API, it only reacts to the following events. + + On start-up: + * Crawl #incidents and add missing `Signal` emoji where appropriate + * This is to retro-actively add the available options for messages which + were sent while the bot wasn't listening + * Pinned messages and message starting with # do not qualify as incidents + * See: `crawl_incidents` + + On message: + * Add `Signal` member emoji if message qualifies as an incident + * Ignore messages starting with # + * Use this if verbal communication is necessary + * Each such message must be deleted manually once appropriate + * See: `on_message` + + On reaction: + * Remove reaction if not permitted (`ALLOWED_EMOJI`, `ALLOWED_ROLES`) + * If `Signal.ACTIONED` or `Signal.NOT_ACTIONED` were chosen, attempt to + relay the incident message to #incidents-archive + * If relay successful, delete original message + * See: `on_raw_reaction_add` + + Please refer to function docstrings for implementation details. + """ def __init__(self, bot: Bot) -> None: """Prepare `event_lock` and schedule `crawl_task` on start-up.""" @@ -76,7 +113,7 @@ class Incidents(Cog): Crawl #incidents and add missing emoji where necessary. This is to catch-up should an incident be reported while the bot wasn't listening. - After adding reactions, we take a short break to avoid drowning in ratelimits. + After adding each reaction, we take a short break to avoid drowning in ratelimits. Once this task is scheduled, listeners that change messages should await it. The crawl assumes that the channel history doesn't change as we go over it. @@ -88,7 +125,7 @@ class Incidents(Cog): # and if there are, something has likely gone very wrong limit = 50 - # Seconds to sleep after each message + # Seconds to sleep after adding reactions to a message sleep = 2 log.debug(f"Crawling messages in #incidents: {limit=}, {sleep=}") @@ -162,7 +199,7 @@ class Incidents(Cog): async def process_event(self, reaction: str, incident: discord.Message, member: discord.Member) -> None: """ - Process a valid `reaction_add` event in #incidents. + Process a `reaction_add` event in #incidents. First, we check that the reaction is a recognized `Signal` member, and that it was sent by a permitted user (at least one role in `ALLOWED_ROLES`). If not, the reaction is removed. @@ -172,7 +209,8 @@ class Incidents(Cog): We do not release `event_lock` until we receive the corresponding `message_delete` event. This ensures that if there is a racing event awaiting the lock, it will fail to find the - message, and will abort. + message, and will abort. There is a `timeout` to ensure that this doesn't hold the lock + forever should something go wrong. """ members_roles: t.Set[int] = {role.id for role in member.roles} if not members_roles & ALLOWED_ROLES: # Intersection is truthy on at least 1 common element @@ -221,9 +259,9 @@ class Incidents(Cog): If not, we try to fetch the message from the API. This is necessary for messages which were sent before the bot's current session. - However, in an edge-case, it is also possible that the message was already deleted, - and the API will return a 404. In such a case, None will be returned. This signals - that the event for `message_id` should be ignored. + In an edge-case, it is also possible that the message was already deleted, and + the API will respond with a 404. In such a case, None will be returned. + This signals that the event for `message_id` should be ignored. """ await self.bot.wait_until_guild_available() # First make sure that the cache is ready log.debug(f"Resolving message for: {message_id=}") -- cgit v1.2.3 From 5c70a7dad3ee59e865df08affe7905a843a823ce Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 22:05:15 +0200 Subject: Incidents tests: create new test module --- tests/bot/cogs/moderation/test_incidents.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/bot/cogs/moderation/test_incidents.py diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py new file mode 100644 index 000000000..e69de29bb -- cgit v1.2.3 From ae5028d5966ba126f902783db8ad685646f45f37 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 12 Jun 2020 23:14:41 +0200 Subject: Incidents tests: write tests for module-level helpers --- tests/bot/cogs/moderation/test_incidents.py | 135 ++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index e69de29bb..4c1f9bc07 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -0,0 +1,135 @@ +import enum +import unittest +from unittest.mock import AsyncMock, MagicMock, call, patch + +import discord + +from bot.cogs.moderation import incidents + + +@patch("bot.constants.Channels.incidents", 123) +class TestIsIncident(unittest.TestCase): + """ + Collection of tests for the `is_incident` helper function. + + In `setUp`, we will create a mock message which should qualify as an incident. Each + test case will then mutate this instance to make it **not** qualify, in various ways. + + Notice that we patch the #incidents channel id globally for this class. + """ + + def setUp(self) -> None: + """Prepare a mock message which should qualify as an incident.""" + self.incident = MagicMock( + discord.Message, + channel=MagicMock(discord.TextChannel, id=123), + content="this is an incident", + author=MagicMock(discord.User, bot=False), + pinned=False, + ) + + def test_is_incident_true(self): + """Message qualifies as an incident if unchanged.""" + self.assertTrue(incidents.is_incident(self.incident)) + + def check_false(self): + """Assert that `self.incident` does **not** qualify as an incident.""" + self.assertFalse(incidents.is_incident(self.incident)) + + def test_is_incident_false_channel(self): + """Message doesn't qualify if sent outside of #incidents.""" + self.incident.channel = MagicMock(discord.TextChannel, id=456) + self.check_false() + + def test_is_incident_false_content(self): + """Message doesn't qualify if content begins with hash symbol.""" + self.incident.content = "# this is a comment message" + self.check_false() + + def test_is_incident_false_author(self): + """Message doesn't qualify if author is a bot.""" + self.incident.author = MagicMock(discord.User, bot=True) + self.check_false() + + def test_is_incident_false_pinned(self): + """Message doesn't qualify if it is pinned.""" + self.incident.pinned = True + self.check_false() + + +class TestOwnReactions(unittest.TestCase): + """Assertions for the `own_reactions` function.""" + + def test_own_reactions(self): + """Only bot's own emoji are extracted from the input incident.""" + reactions = ( + MagicMock(discord.Reaction, emoji="A", me=True), + MagicMock(discord.Reaction, emoji="B", me=True), + MagicMock(discord.Reaction, emoji="C", me=False), + ) + message = MagicMock(discord.Message, reactions=reactions) + self.assertSetEqual(incidents.own_reactions(message), {"A", "B"}) + + +@patch("bot.cogs.moderation.incidents.ALLOWED_EMOJI", {"A", "B"}) +class TestHasSignals(unittest.TestCase): + """ + Assertions for the `has_signals` function. + + We patch `ALLOWED_EMOJI` globally. Each test function then patches `own_reactions` + as appropriate. + """ + + def test_has_signals_true(self): + """True when `own_reactions` returns all emoji in `ALLOWED_EMOJI`.""" + message = MagicMock(discord.Message) + own_reactions = MagicMock(return_value={"A", "B"}) + + with patch("bot.cogs.moderation.incidents.own_reactions", own_reactions): + self.assertTrue(incidents.has_signals(message)) + + def test_has_signals_false(self): + """False when `own_reactions` does not return all emoji in `ALLOWED_EMOJI`.""" + message = MagicMock(discord.Message) + own_reactions = MagicMock(return_value={"A", "C"}) + + with patch("bot.cogs.moderation.incidents.own_reactions", own_reactions): + self.assertFalse(incidents.has_signals(message)) + + +class Signal(enum.Enum): + A = "A" + B = "B" + + +@patch("bot.cogs.moderation.incidents.Signal", Signal) +class TestAddSignals(unittest.IsolatedAsyncioTestCase): + """ + Assertions for the `add_signals` coroutine. + + These are all fairly similar and could go into a single test function, but I found the + patching & sub-testing fairly awkward in that case and decided to split them up + to avoid unnecessary syntax noise. + """ + + def setUp(self): + """Prepare a mock incident message for tests to use.""" + self.incident = MagicMock(discord.Message, add_reaction=AsyncMock()) + + @patch("bot.cogs.moderation.incidents.own_reactions", MagicMock(return_value=set())) + async def test_add_signals_missing(self): + """All emoji are added when none are present.""" + await incidents.add_signals(self.incident) + self.incident.add_reaction.assert_has_calls([call("A"), call("B")]) + + @patch("bot.cogs.moderation.incidents.own_reactions", MagicMock(return_value={"A"})) + async def test_add_signals_partial(self): + """Only missing emoji are added when some are present.""" + await incidents.add_signals(self.incident) + self.incident.add_reaction.assert_has_calls([call("B")]) + + @patch("bot.cogs.moderation.incidents.own_reactions", MagicMock(return_value={"A", "B"})) + async def test_add_signals_present(self): + """No emoji are added when all are present.""" + await incidents.add_signals(self.incident) + self.incident.add_reaction.assert_not_called() -- cgit v1.2.3 From 314f9a829a6bc12677bac17ff04b2501b4d93f0c Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 13 Jun 2020 08:08:36 +0300 Subject: Fix `create_channels`, `get_category` docstrings --- bot/cogs/jams.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/jams.py b/bot/cogs/jams.py index 16dda35c8..74140b9db 100644 --- a/bot/cogs/jams.py +++ b/bot/cogs/jams.py @@ -52,7 +52,7 @@ class CodeJams(commands.Cog): @staticmethod async def get_category(ctx: commands.Context) -> CategoryChannel: - """Create Code Jam category when this don't exist and return this.""" + """Create a Code Jam category if it doesn't exist and return it.""" code_jam_category = utils.get(ctx.guild.categories, name="Code Jam") if code_jam_category is None: @@ -99,7 +99,7 @@ class CodeJams(commands.Cog): return team_channel_overwrites async def create_channels(self, ctx: commands.Context, team_name: str, members: t.List[Member]) -> str: - """Create team text and voice channel. Return name of text channel.""" + """Create team text and voice channels. Return the mention for the text channel.""" # Get permission overwrites and category team_channel_overwrites = self.get_overwrites(members, ctx) code_jam_category = await self.get_category(ctx) -- cgit v1.2.3 From 8bb1dca65121b0ceb9ba7a1f26642f7e0b73860c Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 13 Jun 2020 08:17:04 +0300 Subject: Jams: Use `Guild` instead `Context` for helper functions --- bot/cogs/jams.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/bot/cogs/jams.py b/bot/cogs/jams.py index 74140b9db..75cf8fe6b 100644 --- a/bot/cogs/jams.py +++ b/bot/cogs/jams.py @@ -1,7 +1,7 @@ import logging import typing as t -from discord import CategoryChannel, Member, PermissionOverwrite, utils +from discord import CategoryChannel, Guild, Member, PermissionOverwrite, utils from discord.ext import commands from more_itertools import unique_everseen @@ -41,8 +41,8 @@ class CodeJams(commands.Cog): ) return - team_channel = await self.create_channels(ctx, team_name, members) - await self.add_roles(ctx, members) + team_channel = await self.create_channels(ctx.guild, team_name, members) + await self.add_roles(ctx.guild, members) await ctx.send( f":ok_hand: Team created: {team_channel}\n" @@ -51,19 +51,19 @@ class CodeJams(commands.Cog): ) @staticmethod - async def get_category(ctx: commands.Context) -> CategoryChannel: + async def get_category(guild: Guild) -> CategoryChannel: """Create a Code Jam category if it doesn't exist and return it.""" - code_jam_category = utils.get(ctx.guild.categories, name="Code Jam") + code_jam_category = utils.get(guild.categories, name="Code Jam") if code_jam_category is None: log.info("Code Jam category not found, creating it.") category_overwrites = { - ctx.guild.default_role: PermissionOverwrite(read_messages=False), - ctx.guild.me: PermissionOverwrite(read_messages=True) + guild.default_role: PermissionOverwrite(read_messages=False), + guild.me: PermissionOverwrite(read_messages=True) } - code_jam_category = await ctx.guild.create_category_channel( + code_jam_category = await guild.create_category_channel( "Code Jam", overwrites=category_overwrites, reason="It's code jam time!" @@ -72,7 +72,7 @@ class CodeJams(commands.Cog): return code_jam_category @staticmethod - def get_overwrites(members: t.List[Member], ctx: commands.Context) -> t.Dict[Member, PermissionOverwrite]: + def get_overwrites(members: t.List[Member], guild: Guild) -> t.Dict[Member, PermissionOverwrite]: """Get Code Jam team channels permission overwrites.""" # First member is always the team leader team_channel_overwrites = { @@ -82,8 +82,8 @@ class CodeJams(commands.Cog): manage_webhooks=True, connect=True ), - ctx.guild.default_role: PermissionOverwrite(read_messages=False, connect=False), - ctx.guild.get_role(Roles.verified): PermissionOverwrite( + guild.default_role: PermissionOverwrite(read_messages=False, connect=False), + guild.get_role(Roles.verified): PermissionOverwrite( read_messages=False, connect=False ) @@ -98,14 +98,14 @@ class CodeJams(commands.Cog): return team_channel_overwrites - async def create_channels(self, ctx: commands.Context, team_name: str, members: t.List[Member]) -> str: + async def create_channels(self, guild: Guild, team_name: str, members: t.List[Member]) -> str: """Create team text and voice channels. Return the mention for the text channel.""" # Get permission overwrites and category - team_channel_overwrites = self.get_overwrites(members, ctx) - code_jam_category = await self.get_category(ctx) + team_channel_overwrites = self.get_overwrites(members, guild) + code_jam_category = await self.get_category(guild) # Create a text channel for the team - team_channel = await ctx.guild.create_text_channel( + team_channel = await guild.create_text_channel( team_name, overwrites=team_channel_overwrites, category=code_jam_category @@ -114,7 +114,7 @@ class CodeJams(commands.Cog): # Create a voice channel for the team team_voice_name = " ".join(team_name.split("-")).title() - await ctx.guild.create_voice_channel( + await guild.create_voice_channel( team_voice_name, overwrites=team_channel_overwrites, category=code_jam_category @@ -123,13 +123,13 @@ class CodeJams(commands.Cog): return team_channel.mention @staticmethod - async def add_roles(ctx: commands.Context, members: t.List[Member]) -> None: + async def add_roles(guild: Guild, members: t.List[Member]) -> None: """Assign team leader and jammer roles.""" # Assign team leader role - await members[0].add_roles(ctx.guild.get_role(Roles.team_leaders)) + await members[0].add_roles(guild.get_role(Roles.team_leaders)) # Assign rest of roles - jammer_role = ctx.guild.get_role(Roles.jammers) + jammer_role = guild.get_role(Roles.jammers) for member in members: await member.add_roles(jammer_role) -- cgit v1.2.3 From 9dbfe7da4cbc4d1820507e25ce56929b7fb55327 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 13 Jun 2020 08:26:19 +0300 Subject: Jam Tests: Update `Context` to `Guild` for tests too --- tests/bot/cogs/test_jams.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 54fe0b5f2..17b86601f 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -47,23 +47,23 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_category_dont_exist(self): """Should create code jam category.""" self.utils_mock.get.return_value = None - await self.cog.get_category(self.ctx) - self.ctx.guild.create_category_channel.assert_awaited_once() - category_overwrites = self.ctx.guild.create_category_channel.call_args[1]["overwrites"] + await self.cog.get_category(self.guild) + self.guild.create_category_channel.assert_awaited_once() + category_overwrites = self.guild.create_category_channel.call_args[1]["overwrites"] - self.assertFalse(category_overwrites[self.ctx.guild.default_role].read_messages) - self.assertTrue(category_overwrites[self.ctx.guild.me].read_messages) + self.assertFalse(category_overwrites[self.guild.default_role].read_messages) + self.assertTrue(category_overwrites[self.guild.me].read_messages) async def test_category_channel_exist(self): """Should not try to create category channel.""" - await self.cog.get_category(self.ctx) - self.ctx.guild.create_category_channel.assert_not_awaited() + await self.cog.get_category(self.guild) + self.guild.create_category_channel.assert_not_awaited() async def test_channel_overwrites(self): """Should have correct permission overwrites for users and roles.""" leader = MockMember() members = [leader] + [MockMember() for _ in range(4)] - overwrites = self.cog.get_overwrites(members, self.ctx) + overwrites = self.cog.get_overwrites(members, self.guild) # Leader permission overwrites self.assertTrue(overwrites[leader].manage_messages) @@ -77,10 +77,10 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.assertTrue(overwrites[member].connect) # Everyone and verified role overwrite - self.assertFalse(overwrites[self.ctx.guild.default_role].read_messages) - self.assertFalse(overwrites[self.ctx.guild.default_role].connect) - self.assertFalse(overwrites[self.ctx.guild.get_role(Roles.verified)].read_messages) - self.assertFalse(overwrites[self.ctx.guild.get_role(Roles.verified)].connect) + self.assertFalse(overwrites[self.guild.default_role].read_messages) + self.assertFalse(overwrites[self.guild.default_role].connect) + self.assertFalse(overwrites[self.guild.get_role(Roles.verified)].read_messages) + self.assertFalse(overwrites[self.guild.get_role(Roles.verified)].connect) async def test_team_channels_creation(self): """Should create new voice and text channel for team.""" @@ -90,18 +90,18 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.cog.get_overwrites = MagicMock() self.cog.get_category = AsyncMock() self.ctx.guild.create_text_channel.return_value = MockTextChannel(mention="foobar-channel") - actual = await self.cog.create_channels(self.ctx, "my-team", members) + actual = await self.cog.create_channels(self.guild, "my-team", members) self.assertEqual("foobar-channel", actual) - self.cog.get_overwrites.assert_called_once_with(members, self.ctx) - self.cog.get_category.assert_awaited_once_with(self.ctx) + self.cog.get_overwrites.assert_called_once_with(members, self.guild) + self.cog.get_category.assert_awaited_once_with(self.guild) - self.ctx.guild.create_text_channel.assert_awaited_once_with( + self.guild.create_text_channel.assert_awaited_once_with( "my-team", overwrites=self.cog.get_overwrites.return_value, category=self.cog.get_category.return_value ) - self.ctx.guild.create_voice_channel.assert_awaited_once_with( + self.guild.create_voice_channel.assert_awaited_once_with( "My Team", overwrites=self.cog.get_overwrites.return_value, category=self.cog.get_category.return_value @@ -111,11 +111,11 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Should add team leader role to leader and jam role to every team member.""" leader_role = MockRole(name="Team Leader") jam_role = MockRole(name="Jammer") - self.ctx.guild.get_role.side_effect = [leader_role, jam_role] + self.guild.get_role.side_effect = [leader_role, jam_role] leader = MockMember() members = [leader] + [MockMember() for _ in range(4)] - await self.cog.add_roles(self.ctx, members) + await self.cog.add_roles(self.guild, members) leader.add_roles.assert_any_await(leader_role) for member in members: -- cgit v1.2.3 From d0f8272818095fc692e03ce2630fe2302b09393c Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 13 Jun 2020 08:31:16 +0300 Subject: Jams: Fix `get_overwrites` return type --- bot/cogs/jams.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/jams.py b/bot/cogs/jams.py index 75cf8fe6b..a48dbc49a 100644 --- a/bot/cogs/jams.py +++ b/bot/cogs/jams.py @@ -1,7 +1,7 @@ import logging import typing as t -from discord import CategoryChannel, Guild, Member, PermissionOverwrite, utils +from discord import CategoryChannel, Guild, Member, PermissionOverwrite, Role, utils from discord.ext import commands from more_itertools import unique_everseen @@ -72,7 +72,7 @@ class CodeJams(commands.Cog): return code_jam_category @staticmethod - def get_overwrites(members: t.List[Member], guild: Guild) -> t.Dict[Member, PermissionOverwrite]: + def get_overwrites(members: t.List[Member], guild: Guild) -> t.Dict[t.Union[Member, Role], PermissionOverwrite]: """Get Code Jam team channels permission overwrites.""" # First member is always the team leader team_channel_overwrites = { -- cgit v1.2.3 From 2489b144b5bf131ec8b1b42e2ae1dd249cce4d3f Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 13 Jun 2020 08:35:35 +0300 Subject: Jam Tests: Simplify and make tests more secure --- tests/bot/cogs/test_jams.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 17b86601f..2d2eebabf 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -42,7 +42,8 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): member = MockMember() await self.cog.createteam(*self.default_args, (member for _ in range(5))) self.ctx.send.assert_awaited_once() - self.utils_mock.get.assert_not_called() + self.cog.create_channels.assert_now_awaited() + self.cog.add_roles.assert_not_awaited() async def test_category_dont_exist(self): """Should create code jam category.""" @@ -125,12 +126,9 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Should call `ctx.send` when everything goes right.""" members = [MockMember() for _ in range(5)] await self.cog.createteam(self.cog, self.ctx, "foo", members) + self.cog.create_channel.assert_awaited_once() + self.cog.add_roles.assert_awaited_once() self.ctx.send.assert_awaited_once() - sent_string = self.ctx.send.call_args[0][0] - - self.assertIn(str(self.ctx.guild.create_text_channel.return_value.mention), sent_string) - self.assertIn(members[0].mention, sent_string) - self.assertIn(" ".join(member.mention for member in members[1:]), sent_string) class CodeJamSetup(unittest.TestCase): -- cgit v1.2.3 From 95ae613173bb87719155a95494fe448a45a2d6bc Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 13 Jun 2020 08:39:35 +0300 Subject: Jam Tests: Fix wrong function name and convert them to mocks --- tests/bot/cogs/test_jams.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 2d2eebabf..a66658134 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -124,9 +124,11 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_result_sending(self): """Should call `ctx.send` when everything goes right.""" + self.cog.create_channels = AsyncMock() + self.cog.add_roles = AsyncMock() members = [MockMember() for _ in range(5)] await self.cog.createteam(self.cog, self.ctx, "foo", members) - self.cog.create_channel.assert_awaited_once() + self.cog.create_channels.assert_awaited_once() self.cog.add_roles.assert_awaited_once() self.ctx.send.assert_awaited_once() -- cgit v1.2.3 From ef67747e59892d1307246bcad4d32e245098ff58 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 13 Jun 2020 08:44:56 +0300 Subject: Jam Tests: Fix `test_duplicate_member_provided` assertions --- tests/bot/cogs/test_jams.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index a66658134..2f2cb4695 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -39,10 +39,12 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_duplicate_members_provided(self): """Should `ctx.send` and exit early because duplicate members provided and total there is only 1 member.""" + self.cog.create_channels = AsyncMock() + self.cog.add_roles = AsyncMock() member = MockMember() await self.cog.createteam(*self.default_args, (member for _ in range(5))) self.ctx.send.assert_awaited_once() - self.cog.create_channels.assert_now_awaited() + self.cog.create_channels.assert_not_awaited() self.cog.add_roles.assert_not_awaited() async def test_category_dont_exist(self): -- cgit v1.2.3 From e9724dad79e7dab3bb801f50770bb06cf8461019 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Jun 2020 15:12:38 +0200 Subject: Incidents tests: use our own helper mocks No reason to build own MagicMocks as we already have helpers that more accurately mimic the mocked behaviour. --- tests/bot/cogs/moderation/test_incidents.py | 30 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 4c1f9bc07..d7cc84734 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -1,10 +1,9 @@ import enum import unittest -from unittest.mock import AsyncMock, MagicMock, call, patch - -import discord +from unittest.mock import MagicMock, call, patch from bot.cogs.moderation import incidents +from tests.helpers import MockMessage, MockReaction, MockTextChannel, MockUser @patch("bot.constants.Channels.incidents", 123) @@ -20,11 +19,10 @@ class TestIsIncident(unittest.TestCase): def setUp(self) -> None: """Prepare a mock message which should qualify as an incident.""" - self.incident = MagicMock( - discord.Message, - channel=MagicMock(discord.TextChannel, id=123), + self.incident = MockMessage( + channel=MockTextChannel(id=123), content="this is an incident", - author=MagicMock(discord.User, bot=False), + author=MockUser(bot=False), pinned=False, ) @@ -38,7 +36,7 @@ class TestIsIncident(unittest.TestCase): def test_is_incident_false_channel(self): """Message doesn't qualify if sent outside of #incidents.""" - self.incident.channel = MagicMock(discord.TextChannel, id=456) + self.incident.channel = MockTextChannel(id=456) self.check_false() def test_is_incident_false_content(self): @@ -48,7 +46,7 @@ class TestIsIncident(unittest.TestCase): def test_is_incident_false_author(self): """Message doesn't qualify if author is a bot.""" - self.incident.author = MagicMock(discord.User, bot=True) + self.incident.author = MockUser(bot=True) self.check_false() def test_is_incident_false_pinned(self): @@ -63,11 +61,11 @@ class TestOwnReactions(unittest.TestCase): def test_own_reactions(self): """Only bot's own emoji are extracted from the input incident.""" reactions = ( - MagicMock(discord.Reaction, emoji="A", me=True), - MagicMock(discord.Reaction, emoji="B", me=True), - MagicMock(discord.Reaction, emoji="C", me=False), + MockReaction(emoji="A", me=True), + MockReaction(emoji="B", me=True), + MockReaction(emoji="C", me=False), ) - message = MagicMock(discord.Message, reactions=reactions) + message = MockMessage(reactions=reactions) self.assertSetEqual(incidents.own_reactions(message), {"A", "B"}) @@ -82,7 +80,7 @@ class TestHasSignals(unittest.TestCase): def test_has_signals_true(self): """True when `own_reactions` returns all emoji in `ALLOWED_EMOJI`.""" - message = MagicMock(discord.Message) + message = MockMessage() own_reactions = MagicMock(return_value={"A", "B"}) with patch("bot.cogs.moderation.incidents.own_reactions", own_reactions): @@ -90,7 +88,7 @@ class TestHasSignals(unittest.TestCase): def test_has_signals_false(self): """False when `own_reactions` does not return all emoji in `ALLOWED_EMOJI`.""" - message = MagicMock(discord.Message) + message = MockMessage() own_reactions = MagicMock(return_value={"A", "C"}) with patch("bot.cogs.moderation.incidents.own_reactions", own_reactions): @@ -114,7 +112,7 @@ class TestAddSignals(unittest.IsolatedAsyncioTestCase): def setUp(self): """Prepare a mock incident message for tests to use.""" - self.incident = MagicMock(discord.Message, add_reaction=AsyncMock()) + self.incident = MockMessage() @patch("bot.cogs.moderation.incidents.own_reactions", MagicMock(return_value=set())) async def test_add_signals_missing(self): -- cgit v1.2.3 From 00a44226cb659319b9df5f568b0f67f9a0ed3360 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Jun 2020 15:51:34 +0200 Subject: Incidents tests: improve mock `Signal` name & move def Let's make it clear that this is our own mock. We also move the definition to the top of the module. --- tests/bot/cogs/moderation/test_incidents.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index d7cc84734..a349c1cb7 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -6,6 +6,11 @@ from bot.cogs.moderation import incidents from tests.helpers import MockMessage, MockReaction, MockTextChannel, MockUser +class MockSignal(enum.Enum): + A = "A" + B = "B" + + @patch("bot.constants.Channels.incidents", 123) class TestIsIncident(unittest.TestCase): """ @@ -95,12 +100,7 @@ class TestHasSignals(unittest.TestCase): self.assertFalse(incidents.has_signals(message)) -class Signal(enum.Enum): - A = "A" - B = "B" - - -@patch("bot.cogs.moderation.incidents.Signal", Signal) +@patch("bot.cogs.moderation.incidents.Signal", MockSignal) class TestAddSignals(unittest.IsolatedAsyncioTestCase): """ Assertions for the `add_signals` coroutine. -- cgit v1.2.3 From c66b4a618503352803f73e9272a1d27b6e0a4d52 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Jun 2020 17:24:31 +0200 Subject: Incidents tests: set up base class for `Incidents` For cleanliness, I've decided to make a separate class for each method. Since most tests will want to have an `Incident` instance ready, they can inherit the `setUp` from `TestIncidents`, which does not make any assertions on its own. --- tests/bot/cogs/moderation/test_incidents.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index a349c1cb7..d52932e0a 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -2,8 +2,8 @@ import enum import unittest from unittest.mock import MagicMock, call, patch -from bot.cogs.moderation import incidents -from tests.helpers import MockMessage, MockReaction, MockTextChannel, MockUser +from bot.cogs.moderation import Incidents, incidents +from tests.helpers import MockBot, MockMessage, MockReaction, MockTextChannel, MockUser class MockSignal(enum.Enum): @@ -131,3 +131,22 @@ class TestAddSignals(unittest.IsolatedAsyncioTestCase): """No emoji are added when all are present.""" await incidents.add_signals(self.incident) self.incident.add_reaction.assert_not_called() + + +class TestIncidents(unittest.IsolatedAsyncioTestCase): + """ + Tests for bound methods of the `Incidents` cog. + + Use this as a base class for `Incidents` tests - it will prepare a fresh instance + for each test function, but not make any assertions on its own. Tests can mutate + the instance as they wish. + """ + + def setUp(self): + """ + Prepare a fresh `Incidents` instance for each test. + + Note that this will not schedule `crawl_incidents` in the background, as everything + is being mocked. The `crawl_task` attribute will end up being None. + """ + self.cog_instance = Incidents(MockBot()) -- cgit v1.2.3 From 3c2d227cd067466668e3089f63a6548736edf8ab Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Jun 2020 17:56:31 +0200 Subject: Incidents tests: write tests for `archive` --- tests/bot/cogs/moderation/test_incidents.py | 65 ++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index d52932e0a..7500235cf 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -1,9 +1,12 @@ import enum import unittest -from unittest.mock import MagicMock, call, patch +from unittest.mock import AsyncMock, MagicMock, call, patch + +import aiohttp +import discord from bot.cogs.moderation import Incidents, incidents -from tests.helpers import MockBot, MockMessage, MockReaction, MockTextChannel, MockUser +from tests.helpers import MockAsyncWebhook, MockBot, MockMessage, MockReaction, MockTextChannel, MockUser class MockSignal(enum.Enum): @@ -150,3 +153,61 @@ class TestIncidents(unittest.IsolatedAsyncioTestCase): is being mocked. The `crawl_task` attribute will end up being None. """ self.cog_instance = Incidents(MockBot()) + + +class TestArchive(TestIncidents): + """Tests for the `Incidents.archive` coroutine.""" + + async def test_archive_webhook_not_found(self): + """ + Method recovers and returns False when the webhook is not found. + + Implicitly, this also tests that the error is handled internally and doesn't + propagate out of the method, which is just as important. + """ + mock_404 = discord.NotFound( + response=MagicMock(aiohttp.ClientResponse), # Mock the erroneous response + message="Webhook not found", + ) + + self.cog_instance.bot.fetch_webhook = AsyncMock(side_effect=mock_404) + self.assertFalse(await self.cog_instance.archive(incident=MockMessage(), outcome=MagicMock())) + + async def test_archive_relays_incident(self): + """ + If webhook is found, method relays `incident` properly. + + This test will assert the following: + * The fetched webhook's `send` method is fed the correct arguments + * The message returned by `send` will have `outcome` reaction added + * Finally, the `archive` method returns True + + Assertions are made specifically in this order. + """ + webhook_message = MockMessage() # The message that will be returned by the webhook's `send` method + webhook = MockAsyncWebhook(send=AsyncMock(return_value=webhook_message)) + + self.cog_instance.bot.fetch_webhook = AsyncMock(return_value=webhook) # Patch in our webhook + + # Now we'll pas our own `incident` to `archive` and capture the return value + incident = MockMessage( + clean_content="pingless message", + content="pingful message", + author=MockUser(name="author_name", avatar_url="author_avatar"), + id=123, + ) + archive_return = await self.cog_instance.archive(incident, outcome=MagicMock(value="A")) + + # Check that the webhook was dispatched correctly + webhook.send.assert_called_once_with( + content="pingless message", + username="author_name", + avatar_url="author_avatar", + wait=True, + ) + + # Now check that the correct emoji was added to the relayed message + webhook_message.add_reaction.assert_called_once_with("A") + + # Finally check that the method returned True + self.assertTrue(archive_return) -- cgit v1.2.3 From 39dc3cd229888acac2782237db4b9389c0788478 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Jun 2020 21:52:20 +0200 Subject: Incidents tests: move `mock_404` into module namespace This will be useful for others tests as well. --- tests/bot/cogs/moderation/test_incidents.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 7500235cf..e51bda114 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -14,6 +14,12 @@ class MockSignal(enum.Enum): B = "B" +mock_404 = discord.NotFound( + response=MagicMock(aiohttp.ClientResponse), # Mock the erroneous response + message="Not found", +) + + @patch("bot.constants.Channels.incidents", 123) class TestIsIncident(unittest.TestCase): """ @@ -165,11 +171,6 @@ class TestArchive(TestIncidents): Implicitly, this also tests that the error is handled internally and doesn't propagate out of the method, which is just as important. """ - mock_404 = discord.NotFound( - response=MagicMock(aiohttp.ClientResponse), # Mock the erroneous response - message="Webhook not found", - ) - self.cog_instance.bot.fetch_webhook = AsyncMock(side_effect=mock_404) self.assertFalse(await self.cog_instance.archive(incident=MockMessage(), outcome=MagicMock())) -- cgit v1.2.3 From 8ed5cc7ef5e38885a8e439602b59e56449d3633c Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Jun 2020 21:52:34 +0200 Subject: Incidents tests: write tests for `resolve_message` --- tests/bot/cogs/moderation/test_incidents.py | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index e51bda114..b3beec3ab 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -212,3 +212,59 @@ class TestArchive(TestIncidents): # Finally check that the method returned True self.assertTrue(archive_return) + + +class TestResolveMessage(TestIncidents): + """Tests for the `Incidents.resolve_message` coroutine.""" + + async def test_resolve_message_pass_message_id(self): + """Method will call `_get_message` with the passed `message_id`.""" + await self.cog_instance.resolve_message(123) + self.cog_instance.bot._connection._get_message.assert_called_once_with(123) + + async def test_resolve_message_in_cache(self): + """ + No API call is made if the queried message exists in the cache. + + We mock the `_get_message` return value regardless of input. Whether it finds the message + internally is considered d.py's responsibility, not ours. + """ + cached_message = MockMessage(id=123) + self.cog_instance.bot._connection._get_message = MagicMock(return_value=cached_message) + + return_value = await self.cog_instance.resolve_message(123) + + self.assertIs(return_value, cached_message) + self.cog_instance.bot.get_channel.assert_not_called() # The `fetch_message` line was never hit + + async def test_resolve_message_not_in_cache(self): + """ + The message is retrieved from the API if it isn't cached. + + This is desired behaviour for messages which exist, but were sent before the bot's + current session. + """ + self.cog_instance.bot._connection._get_message = MagicMock(return_value=None) # Cache returns None + + # API returns our message + uncached_message = MockMessage() + fetch_message = AsyncMock(return_value=uncached_message) + self.cog_instance.bot.get_channel = MagicMock(return_value=MockTextChannel(fetch_message=fetch_message)) + + retrieved_message = await self.cog_instance.resolve_message(123) + self.assertIs(retrieved_message, uncached_message) + + async def test_resolve_message_doesnt_exist(self): + """ + If the API returns a 404, the function handles it gracefully and returns None. + + This is an edge-case happening with racing events - event A will relay the message + to the archive and delete the original. Once event B acquires the `event_lock`, + it will not find the message in the cache, and will ask the API. + """ + self.cog_instance.bot._connection._get_message = MagicMock(return_value=None) # Cache returns None + + fetch_message = AsyncMock(side_effect=mock_404) + self.cog_instance.bot.get_channel = MagicMock(return_value=MockTextChannel(fetch_message=fetch_message)) + + self.assertIsNone(await self.cog_instance.resolve_message(123)) -- cgit v1.2.3 From 7c43eff17a07471799174c0a0e8813b9f58d2ab5 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Jun 2020 22:09:44 +0200 Subject: Incidents: log error on non-404 response We do not wish to log 404 exceptions as those are expected, however, if something else goes wrong, we shouldn't silence it. This also removes the explicit None return as it only adds syntax noise. --- bot/cogs/moderation/incidents.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 151584d38..16286bdab 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -274,9 +274,10 @@ class Incidents(Cog): log.debug("Message not found, attempting to fetch") try: message = await self.bot.get_channel(Channels.incidents).fetch_message(message_id) + except discord.NotFound: + log.debug("Message doesn't exist, it was likely already relayed") except Exception as exc: - log.debug(f"Failed to fetch message: {exc}") - return None + log.exception("Failed to fetch message!", exc_info=exc) else: log.debug("Message fetched successfully!") return message -- cgit v1.2.3 From bbedcb377c4c31973f43f076c3f62646f25733b3 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Jun 2020 22:15:38 +0200 Subject: Incidents tests: test non-404 error response --- tests/bot/cogs/moderation/test_incidents.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index b3beec3ab..cbeb3342c 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -1,4 +1,5 @@ import enum +import logging import unittest from unittest.mock import AsyncMock, MagicMock, call, patch @@ -268,3 +269,22 @@ class TestResolveMessage(TestIncidents): self.cog_instance.bot.get_channel = MagicMock(return_value=MockTextChannel(fetch_message=fetch_message)) self.assertIsNone(await self.cog_instance.resolve_message(123)) + + async def test_resolve_message_fetch_fails(self): + """ + Non-404 errors are handled, logged & None is returned. + + In contrast with a 404, this should make an error-level log. We assert that at least + one such log was made - we do not make any assertions about the log's message. + """ + self.cog_instance.bot._connection._get_message = MagicMock(return_value=None) # Cache returns None + + arbitrary_error = discord.HTTPException( + response=MagicMock(aiohttp.ClientResponse), + message="Arbitrary error", + ) + fetch_message = AsyncMock(side_effect=arbitrary_error) + self.cog_instance.bot.get_channel = MagicMock(return_value=MockTextChannel(fetch_message=fetch_message)) + + with self.assertLogs(logger=incidents.log, level=logging.ERROR): + self.assertIsNone(await self.cog_instance.resolve_message(123)) -- cgit v1.2.3 From 14b7fee42ddf6a2cc75526506ef2028bdc742c9a Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Jun 2020 22:42:58 +0200 Subject: Incidents tests: write tests for `on_message` --- tests/bot/cogs/moderation/test_incidents.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index cbeb3342c..0eb13df70 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -288,3 +288,30 @@ class TestResolveMessage(TestIncidents): with self.assertLogs(logger=incidents.log, level=logging.ERROR): self.assertIsNone(await self.cog_instance.resolve_message(123)) + + +class TestOnMessage(TestIncidents): + """ + Tests for the `Incidents.on_message` listener. + + Notice the decorators mocking the `is_incident` return value. The `is_incidents` + function is tested in `TestIsIncident` - here we do not worry about it. + """ + + @patch("bot.cogs.moderation.incidents.is_incident", MagicMock(return_value=True)) + async def test_on_message_incident(self): + """Messages qualifying as incidents are passed to `add_signals`.""" + incident = MockMessage() + + with patch("bot.cogs.moderation.incidents.add_signals", AsyncMock()) as mock_add_signals: + await self.cog_instance.on_message(incident) + + mock_add_signals.assert_called_once_with(incident) + + @patch("bot.cogs.moderation.incidents.is_incident", MagicMock(return_value=False)) + async def test_on_message_non_incident(self): + """Messages not qualifying as incidents are ignored.""" + with patch("bot.cogs.moderation.incidents.add_signals", AsyncMock()) as mock_add_signals: + await self.cog_instance.on_message(MockMessage()) + + mock_add_signals.assert_not_called() -- cgit v1.2.3 From 9d35846a67c2bf9ed9e935f8b5e3500ae4b49327 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Jun 2020 23:24:14 +0200 Subject: Incidents tests: write tests for `make_confirmation_task` --- tests/bot/cogs/moderation/test_incidents.py | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 0eb13df70..c093afc8a 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -215,6 +215,41 @@ class TestArchive(TestIncidents): self.assertTrue(archive_return) +class TestMakeConfirmationTask(TestIncidents): + """ + Tests for the `Incidents.make_confirmation_task` method. + + Writing tests for this method is difficult, as it mostly just delegates the provided + information elsewhere. There is very little internal logic. Whether our approach + works conceptually is difficult to prove using unit tests. + """ + + def test_make_confirmation_task_check(self): + """ + The internal check will recognize the passed incident. + + This is a little tricky - we first pass a message with a specific `id` in, and then + retrieve the built check from the `call_args` of the `wait_for` method. This relies + on the check being passed as a kwarg. + + Once the check is retrieved, we assert that it gives True for our incident's `id`, + and False for any other. + + If this function begins to fail, first check that `created_check` is being retrieved + correctly. It should be the function that is built locally in the tested method. + """ + self.cog_instance.make_confirmation_task(MockMessage(id=123)) + + self.cog_instance.bot.wait_for.assert_called_once() + created_check = self.cog_instance.bot.wait_for.call_args.kwargs["check"] + + # The `message_id` matches the `id` of our incident + self.assertTrue(created_check(payload=MagicMock(message_id=123))) + + # This `message_id` does not match + self.assertFalse(created_check(payload=MagicMock(message_id=0))) + + class TestResolveMessage(TestIncidents): """Tests for the `Incidents.resolve_message` coroutine.""" -- cgit v1.2.3 From d9ed643c41c8cf96ec208d6fc096882fc64c5d15 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sun, 14 Jun 2020 21:06:39 -0700 Subject: ModLog: fix AttributeError in on_member_update `iterable_item_removed` and `iterable_item_added` lack `new_value` and `old_value`. Instead, they just contain the actual value added or removed. The code was incorrectly trying to access old and new values for the iterable changes. The iterable changes are only useful for the role diff, but they aren't even needed for that. The role diff calculation has been refactored to always get the diff rather than doing it only if it sees there has been a change to the `_roles` attribute. To be clear, `_roles` only has IDs, which is why its diff isn't that useful anyway. To use it, the code would have to get the Role objects, which is basically what the `roles` property already does. `_cs_roles` seems to be some Role object cache, but its reliability is unclear. --- bot/cogs/moderation/modlog.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/bot/cogs/moderation/modlog.py b/bot/cogs/moderation/modlog.py index 41472c64c..02396e1c5 100644 --- a/bot/cogs/moderation/modlog.py +++ b/bot/cogs/moderation/modlog.py @@ -452,6 +452,21 @@ class ModLog(Cog, name="ModLog"): channel_id=Channels.mod_log ) + @staticmethod + def get_role_diff(before: t.List[discord.Role], after: t.List[discord.Role]) -> t.List[str]: + """Return a list of strings describing the roles added and removed.""" + changes = [] + before_roles = set(before) + after_roles = set(after) + + for role in (before_roles - after_roles): + changes.append(f"**Role removed:** {role.name} (`{role.id}`)") + + for role in (after_roles - before_roles): + changes.append(f"**Role added:** {role.name} (`{role.id}`)") + + return changes + @Cog.listener() async def on_member_update(self, before: discord.Member, after: discord.Member) -> None: """Log member update event to user log.""" @@ -463,22 +478,18 @@ class ModLog(Cog, name="ModLog"): return diff = DeepDiff(before, after) - changes = [] + changes = self.get_role_diff(before.roles, after.roles) done = [] diff_values = {} diff_values.update(diff.get("values_changed", {})) diff_values.update(diff.get("type_changes", {})) - diff_values.update(diff.get("iterable_item_removed", {})) - diff_values.update(diff.get("iterable_item_added", {})) diff_user = DeepDiff(before._user, after._user) diff_values.update(diff_user.get("values_changed", {})) diff_values.update(diff_user.get("type_changes", {})) - diff_values.update(diff_user.get("iterable_item_removed", {})) - diff_values.update(diff_user.get("iterable_item_added", {})) for key, value in diff_values.items(): if not key: # Not sure why, but it happens @@ -495,24 +506,11 @@ class ModLog(Cog, name="ModLog"): if key in done or key in MEMBER_CHANGES_SUPPRESSED: continue - if key == "_roles": - new_roles = after.roles - old_roles = before.roles + new = value.get("new_value") + old = value.get("old_value") - for role in old_roles: - if role not in new_roles: - changes.append(f"**Role removed:** {role.name} (`{role.id}`)") - - for role in new_roles: - if role not in old_roles: - changes.append(f"**Role added:** {role.name} (`{role.id}`)") - - else: - new = value.get("new_value") - old = value.get("old_value") - - if new and old: - changes.append(f"**{key.title()}:** `{old}` **→** `{new}`") + if new and old: + changes.append(f"**{key.title()}:** `{old}` **→** `{new}`") done.append(key) -- cgit v1.2.3 From 9133c4a7b79020d507b9cecbb9ce6d957b52fd9d Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sun, 14 Jun 2020 21:57:53 -0700 Subject: ModLog: remove user diff in on_member_update The correct event for user changes is on_user_update, so this code does nothing in the on_member_update event. --- bot/cogs/moderation/modlog.py | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/bot/cogs/moderation/modlog.py b/bot/cogs/moderation/modlog.py index 02396e1c5..703da4ee7 100644 --- a/bot/cogs/moderation/modlog.py +++ b/bot/cogs/moderation/modlog.py @@ -24,7 +24,7 @@ GUILD_CHANNEL = t.Union[discord.CategoryChannel, discord.TextChannel, discord.Vo CHANNEL_CHANGES_UNSUPPORTED = ("permissions",) CHANNEL_CHANGES_SUPPRESSED = ("_overwrites", "position") -MEMBER_CHANGES_SUPPRESSED = ("status", "activities", "_client_status", "nick") +MEMBER_CHANGES_SUPPRESSED = ("status", "activities", "_client_status") ROLE_CHANGES_UNSUPPORTED = ("colour", "permissions") VOICE_STATE_ATTRIBUTES = { @@ -486,11 +486,6 @@ class ModLog(Cog, name="ModLog"): diff_values.update(diff.get("values_changed", {})) diff_values.update(diff.get("type_changes", {})) - diff_user = DeepDiff(before._user, after._user) - - diff_values.update(diff_user.get("values_changed", {})) - diff_values.update(diff_user.get("type_changes", {})) - for key, value in diff_values.items(): if not key: # Not sure why, but it happens continue @@ -514,21 +509,6 @@ class ModLog(Cog, name="ModLog"): done.append(key) - if before.name != after.name: - changes.append( - f"**Username:** `{before.name}` **→** `{after.name}`" - ) - - if before.discriminator != after.discriminator: - changes.append( - f"**Discriminator:** `{before.discriminator}` **→** `{after.discriminator}`" - ) - - if before.display_name != after.display_name: - changes.append( - f"**Display name:** `{before.display_name}` **→** `{after.display_name}`" - ) - if not changes: return -- cgit v1.2.3 From 17858e4d65d5592d1da6178cb80415de615f21ab Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sun, 14 Jun 2020 22:05:18 -0700 Subject: ModLog: fix excluded None values in on_member_update This was preventing diffs for added nicknames from showing, among other things. --- bot/cogs/moderation/modlog.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bot/cogs/moderation/modlog.py b/bot/cogs/moderation/modlog.py index 703da4ee7..163721e1c 100644 --- a/bot/cogs/moderation/modlog.py +++ b/bot/cogs/moderation/modlog.py @@ -504,8 +504,7 @@ class ModLog(Cog, name="ModLog"): new = value.get("new_value") old = value.get("old_value") - if new and old: - changes.append(f"**{key.title()}:** `{old}` **→** `{new}`") + changes.append(f"**{key.title()}:** `{old}` **→** `{new}`") done.append(key) -- cgit v1.2.3 From 35fc846e671192199bde7e98e43b2ac21513f629 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sun, 14 Jun 2020 21:16:02 -0700 Subject: ModLog: refactor on_member_update * Exclude all sequences/mapping types rather than excluding by name * Replace MEMBER_CHANGES_SUPPRESSED with excludes as DeepDiff args * Don't keep track of "done" attributes - there shouldn't be dupes --- bot/cogs/moderation/modlog.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/bot/cogs/moderation/modlog.py b/bot/cogs/moderation/modlog.py index 163721e1c..bd805f590 100644 --- a/bot/cogs/moderation/modlog.py +++ b/bot/cogs/moderation/modlog.py @@ -24,7 +24,6 @@ GUILD_CHANNEL = t.Union[discord.CategoryChannel, discord.TextChannel, discord.Vo CHANNEL_CHANGES_UNSUPPORTED = ("permissions",) CHANNEL_CHANGES_SUPPRESSED = ("_overwrites", "position") -MEMBER_CHANGES_SUPPRESSED = ("status", "activities", "_client_status") ROLE_CHANGES_UNSUPPORTED = ("colour", "permissions") VOICE_STATE_ATTRIBUTES = { @@ -477,36 +476,27 @@ class ModLog(Cog, name="ModLog"): self._ignored[Event.member_update].remove(before.id) return - diff = DeepDiff(before, after) changes = self.get_role_diff(before.roles, after.roles) - done = [] - diff_values = {} + # The regex is a simple way to exclude all sequence and mapping types. + diff = DeepDiff(before, after, exclude_regex_paths=r".*\[.*") - diff_values.update(diff.get("values_changed", {})) - diff_values.update(diff.get("type_changes", {})) + # A type change seems to always take precedent over a value change. Furthermore, it will + # include the value change along with the type change anyway. Therefore, it's OK to + # "overwrite" values_changed; in practice there will never even be anything to overwrite. + diff_values = {**diff.get("values_changed", {}), **diff.get("type_changes", {})} - for key, value in diff_values.items(): - if not key: # Not sure why, but it happens + for attr, value in diff_values.items(): + if not attr: # Not sure why, but it happens. continue - key = key[5:] # Remove "root." prefix - - if "[" in key: - key = key.split("[", 1)[0] - - if "." in key: - key = key.split(".", 1)[0] - - if key in done or key in MEMBER_CHANGES_SUPPRESSED: - continue + attr = attr[len("root."):] # Remove "root." prefix. + attr = attr.replace("_", " ").replace(".", " ").capitalize() new = value.get("new_value") old = value.get("old_value") - changes.append(f"**{key.title()}:** `{old}` **→** `{new}`") - - done.append(key) + changes.append(f"**{attr}:** `{old}` **→** `{new}`") if not changes: return @@ -520,8 +510,10 @@ class ModLog(Cog, name="ModLog"): message = f"**{member_str}** (`{after.id}`)\n{message}" await self.send_log_message( - Icons.user_update, Colour.blurple(), - "Member updated", message, + icon_url=Icons.user_update, + colour=Colour.blurple(), + title="Member updated", + text=message, thumbnail=after.avatar_url_as(static_format="png"), channel_id=Channels.user_log ) -- cgit v1.2.3 From b2972e0f816c60395517412011e312a3040491a0 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 16 Jun 2020 13:12:58 -0700 Subject: Use int literal instead of len for slice Co-authored-by: Kieran Siek --- bot/cogs/moderation/modlog.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/moderation/modlog.py b/bot/cogs/moderation/modlog.py index bd805f590..ffbb87bbe 100644 --- a/bot/cogs/moderation/modlog.py +++ b/bot/cogs/moderation/modlog.py @@ -490,7 +490,7 @@ class ModLog(Cog, name="ModLog"): if not attr: # Not sure why, but it happens. continue - attr = attr[len("root."):] # Remove "root." prefix. + attr = attr[5:] # Remove "root." prefix. attr = attr.replace("_", " ").replace(".", " ").capitalize() new = value.get("new_value") -- cgit v1.2.3 From 51d681654d9a9acc71763edffcea0d5eb1ef1b29 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 18 Jun 2020 08:13:15 +0300 Subject: Source: Simplify missing tag cog handling --- bot/cogs/source.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 32a78a0c0..d59371c6e 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -29,10 +29,7 @@ class SourceConverter(commands.Converter): tags_cog = ctx.bot.get_cog("Tags") - if not tags_cog: - await ctx.send("Unable to get `Tags` cog.") - return commands.ExtensionNotLoaded("bot.cogs.tags") - elif argument.lower() in tags_cog._cache: + if tags_cog and argument.lower() in tags_cog._cache: return argument.lower() raise commands.BadArgument(f"Unable to convert `{argument}` to valid command, tag, or Cog.") @@ -47,10 +44,6 @@ class BotSource(commands.Cog): @commands.command(name="source", aliases=("src",)) async def source_command(self, ctx: commands.Context, *, source_item: SourceConverter = None) -> None: """Display information and a GitHub link to the source code of a command, tag, or cog.""" - # When we have problem to get Tags cog, exit early - if isinstance(source_item, commands.ExtensionNotLoaded): - return - if not source_item: embed = Embed(title="Bot's GitHub Repository") embed.add_field(name="Repository", value=f"[Go to GitHub]({URLs.github_bot_repo})") -- cgit v1.2.3 From 31f53259fd73503399b904e5a7075ceeded4c742 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 18 Jun 2020 08:58:18 +0300 Subject: Source: Split handling tag and other source items file location --- bot/cogs/source.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index d59371c6e..2ca852af3 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -82,7 +82,12 @@ class BotSource(commands.Cog): first_line_no = None lines_extension = "" - file_location = Path(filename).relative_to("/bot/") + # Handle tag file location differently than others to avoid errors in some cases + if not first_line_no: + file_location = Path(filename).relative_to("/bot/") + else: + file_location = Path(filename).relative_to(Path.cwd()).as_posix() + url = f"{URLs.github_bot_repo}/blob/master/{file_location}{lines_extension}" return url, file_location, first_line_no or None -- cgit v1.2.3 From caa421054669f886750a54fb2fdbea3315c58a58 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 18 Jun 2020 09:07:47 +0300 Subject: Source: Exclude `tag` from error message when tags cog not loaded --- bot/cogs/source.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 2ca852af3..223552651 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -28,11 +28,14 @@ class SourceConverter(commands.Converter): return cmd tags_cog = ctx.bot.get_cog("Tags") + show_tag = True - if tags_cog and argument.lower() in tags_cog._cache: + if not tags_cog: + show_tag = False + elif argument.lower() in tags_cog._cache: return argument.lower() - raise commands.BadArgument(f"Unable to convert `{argument}` to valid command, tag, or Cog.") + raise commands.BadArgument(f"Unable to convert `{argument}` to valid command{', tag,' if show_tag else ''} or Cog.") class BotSource(commands.Cog): -- cgit v1.2.3 From 4c9a62f93fd7b92051dd40e4d799236d65e154ab Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 18 Jun 2020 09:10:55 +0300 Subject: Source: Split to multiple lines to fix too long line on error raising --- bot/cogs/source.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 223552651..f1db745cd 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -35,7 +35,9 @@ class SourceConverter(commands.Converter): elif argument.lower() in tags_cog._cache: return argument.lower() - raise commands.BadArgument(f"Unable to convert `{argument}` to valid command{', tag,' if show_tag else ''} or Cog.") + raise commands.BadArgument( + f"Unable to convert `{argument}` to valid command{', tag,' if show_tag else ''} or Cog." + ) class BotSource(commands.Cog): -- cgit v1.2.3 From 40e00ff17465fc5a5fe6b46487bfea37655cd7b9 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 18 Jun 2020 19:33:59 +0200 Subject: Incidents tests: write tests for `process_event` This also breaks the helpers import statement into a vertical list, as the amount of imports has grown too much. I still believe that this is a preferred alternative to accessing the helpers via module namespace, as we use them a lot, and the added visual noise would be annoying to read - their names are already descriptive enough. --- tests/bot/cogs/moderation/test_incidents.py | 102 +++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index c093afc8a..6158d5d20 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -1,3 +1,4 @@ +import asyncio import enum import logging import unittest @@ -7,7 +8,16 @@ import aiohttp import discord from bot.cogs.moderation import Incidents, incidents -from tests.helpers import MockAsyncWebhook, MockBot, MockMessage, MockReaction, MockTextChannel, MockUser +from tests.helpers import ( + MockAsyncWebhook, + MockBot, + MockMember, + MockMessage, + MockReaction, + MockRole, + MockTextChannel, + MockUser, +) class MockSignal(enum.Enum): @@ -250,6 +260,96 @@ class TestMakeConfirmationTask(TestIncidents): self.assertFalse(created_check(payload=MagicMock(message_id=0))) +@patch("bot.cogs.moderation.incidents.ALLOWED_ROLES", {1, 2}) +@patch("bot.cogs.moderation.incidents.Incidents.make_confirmation_task", AsyncMock()) # Generic awaitable +class TestProcessEvent(TestIncidents): + """Tests for the `Incidents.process_event` coroutine.""" + + @patch("bot.cogs.moderation.incidents.ALLOWED_ROLES", {1, 2}) + async def test_process_event_bad_role(self): + """The reaction is removed when the author lacks all allowed roles.""" + incident = MockMessage() + member = MockMember(roles=[MockRole(id=0)]) # Must have role 1 or 2 + + await self.cog_instance.process_event("reaction", incident, member) + incident.remove_reaction.assert_called_once_with("reaction", member) + + async def test_process_event_bad_emoji(self): + """ + The reaction is removed when an invalid emoji is used. + + This requires that we pass in a `member` with valid roles, as we need the role check + to succeed. + """ + incident = MockMessage() + member = MockMember(roles=[MockRole(id=1)]) # Member has allowed role + + await self.cog_instance.process_event("invalid_signal", incident, member) + incident.remove_reaction.assert_called_once_with("invalid_signal", member) + + async def test_process_event_no_archive_on_investigating(self): + """Message is not archived on `Signal.INVESTIGATING`.""" + with patch("bot.cogs.moderation.incidents.Incidents.archive", AsyncMock()) as mocked_archive: + await self.cog_instance.process_event( + reaction=incidents.Signal.INVESTIGATING.value, + incident=MockMessage(), + member=MockMember(roles=[MockRole(id=1)]), + ) + + mocked_archive.assert_not_called() + + async def test_process_event_no_delete_if_archive_fails(self): + """ + Original message is not deleted when `Incidents.archive` returns False. + + This is the way of signaling that the relay failed, and we should not remove the original, + as that would result in losing the incident record. + """ + incident = MockMessage() + + with patch("bot.cogs.moderation.incidents.Incidents.archive", AsyncMock(return_value=False)): + await self.cog_instance.process_event( + reaction=incidents.Signal.ACTIONED.value, + incident=incident, + member=MockMember(roles=[MockRole(id=1)]) + ) + + incident.delete.assert_not_called() + + async def test_process_event_confirmation_task_is_awaited(self): + """Task given by `Incidents.make_confirmation_task` is awaited before method exits.""" + mock_task = AsyncMock() + + with patch("bot.cogs.moderation.incidents.Incidents.make_confirmation_task", mock_task): + await self.cog_instance.process_event( + reaction=incidents.Signal.ACTIONED.value, + incident=MockMessage(), + member=MockMember(roles=[MockRole(id=1)]) + ) + + mock_task.assert_awaited() + + async def test_process_event_confirmation_task_timeout_is_handled(self): + """ + Confirmation task `asyncio.TimeoutError` is handled gracefully. + + We have `make_confirmation_task` return a mock with a side effect, and then catch the + exception should it propagate out of `process_event`. This is so that we can then manually + fail the test with a more informative message than just the plain traceback. + """ + mock_task = AsyncMock(side_effect=asyncio.TimeoutError()) + + try: + with patch("bot.cogs.moderation.incidents.Incidents.make_confirmation_task", mock_task): + await self.cog_instance.process_event( + reaction=incidents.Signal.ACTIONED.value, + incident=MockMessage(), + member=MockMember(roles=[MockRole(id=1)]) + ) + except asyncio.TimeoutError: + self.fail("TimeoutError was not handled gracefully, and propagated out of `process_event`!") + + class TestResolveMessage(TestIncidents): """Tests for the `Incidents.resolve_message` coroutine.""" -- cgit v1.2.3 From ed4097629601704f0c65fc40cceb5fd6757d4779 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 19 Jun 2020 14:32:31 +0200 Subject: Incidents tests: add helper for mocking async for-loops See the docstring. This does not make the ambition to be powerful enough to be included in `tests.helpers`, and is only intended for local purposes. --- tests/bot/cogs/moderation/test_incidents.py | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 6158d5d20..7fa8847ef 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -1,6 +1,7 @@ import asyncio import enum import logging +import typing as t import unittest from unittest.mock import AsyncMock, MagicMock, call, patch @@ -20,6 +21,42 @@ from tests.helpers import ( ) +class MockAsyncIterable: + """ + Helper for mocking asynchronous for loops. + + It does not appear that the `unittest` library currently provides anything that would + allow us to simply mock an async iterator, such as `discord.TextChannel.history`. + + We therefore write our own helper to wrap a regular synchronous iterable, and feed + its values via `__anext__` rather than `__next__`. + + This class was written for the purposes of testing the `Incidents` cog - it may not + be generic enough to be placed in the `tests.helpers` module. + """ + + def __init__(self, messages: t.Iterable): + """Take a sync iterable to be wrapped.""" + self.iter_messages = iter(messages) + + def __aiter__(self): + """Return `self` as we provide the `__anext__` method.""" + return self + + async def __anext__(self): + """ + Feed the next item, or raise `StopAsyncIteration`. + + Since we're wrapping a sync iterator, it will communicate that it has been depleted + by raising a `StopIteration`. The `async for` construct does not expect it, and we + therefore need to substitute it for the appropriate exception type. + """ + try: + return next(self.iter_messages) + except StopIteration: + raise StopAsyncIteration + + class MockSignal(enum.Enum): A = "A" B = "B" -- cgit v1.2.3 From d93ed5d801c08b7fb084427906e7ac484ac3563f Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 19 Jun 2020 14:37:44 +0200 Subject: Incidents tests: write tests for `crawl_incidents` --- tests/bot/cogs/moderation/test_incidents.py | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 7fa8847ef..4e6dfd5f7 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -209,6 +209,64 @@ class TestIncidents(unittest.IsolatedAsyncioTestCase): self.cog_instance = Incidents(MockBot()) +@patch("asyncio.sleep", AsyncMock()) # Prevent the coro from sleeping to speed up the test +class TestCrawlIncidents(TestIncidents): + """ + Tests for the `Incidents.crawl_incidents` coroutine. + + Apart from `test_crawl_incidents_waits_until_cache_ready`, all tests in this class + will patch the return values of `is_incident` and `has_signal` and then observe + whether the `AsyncMock` for `add_signals` was awaited or not. + + The `add_signals` mock is added by each test separately to ensure it is clean (has not + been awaited by another test yet). The mock can be reset, but this appears to be the + cleaner way. + + For each test, we inject a mock channel with a history of 1 message only (see: `setUp`). + """ + + def setUp(self): + """For each test, ensure `bot.get_channel` returns a channel with 1 arbitrary message.""" + super().setUp() # First ensure we get `cog_instance` from parent + + incidents_history = MagicMock(return_value=MockAsyncIterable([MockMessage()])) + self.cog_instance.bot.get_channel = MagicMock(return_value=MockTextChannel(history=incidents_history)) + + async def test_crawl_incidents_waits_until_cache_ready(self): + """ + The coroutine will await the `wait_until_guild_available` event. + + Since this task is schedule in the `__init__`, it is critical that it waits for the + cache to be ready, so that it can safely get the #incidents channel. + """ + await self.cog_instance.crawl_incidents() + self.cog_instance.bot.wait_until_guild_available.assert_awaited() + + @patch("bot.cogs.moderation.incidents.add_signals", AsyncMock()) + @patch("bot.cogs.moderation.incidents.is_incident", MagicMock(return_value=False)) # Message doesn't qualify + @patch("bot.cogs.moderation.incidents.has_signals", MagicMock(return_value=False)) + async def test_crawl_incidents_noop_if_is_not_incident(self): + """Signals are not added for a non-incident message.""" + await self.cog_instance.crawl_incidents() + incidents.add_signals.assert_not_awaited() + + @patch("bot.cogs.moderation.incidents.add_signals", AsyncMock()) + @patch("bot.cogs.moderation.incidents.is_incident", MagicMock(return_value=True)) # Message qualifies + @patch("bot.cogs.moderation.incidents.has_signals", MagicMock(return_value=True)) # But already has signals + async def test_crawl_incidents_noop_if_message_already_has_signals(self): + """Signals are not added for messages which already have them.""" + await self.cog_instance.crawl_incidents() + incidents.add_signals.assert_not_awaited() + + @patch("bot.cogs.moderation.incidents.add_signals", AsyncMock()) + @patch("bot.cogs.moderation.incidents.is_incident", MagicMock(return_value=True)) # Message qualifies + @patch("bot.cogs.moderation.incidents.has_signals", MagicMock(return_value=False)) # And doesn't have signals + async def test_crawl_incidents_add_signals_called(self): + """Message has signals added as it does not have them yet and qualifies as an incident.""" + await self.cog_instance.crawl_incidents() + incidents.add_signals.assert_awaited_once() + + class TestArchive(TestIncidents): """Tests for the `Incidents.archive` coroutine.""" -- cgit v1.2.3 From 9a58b45cad51c961ad34fa9de9aaa060446c54fd Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 19 Jun 2020 16:57:15 +0200 Subject: Incidents tests: write tests for `on_raw_reaction_add` --- tests/bot/cogs/moderation/test_incidents.py | 128 ++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 4e6dfd5f7..55b15ec9e 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -520,6 +520,134 @@ class TestResolveMessage(TestIncidents): self.assertIsNone(await self.cog_instance.resolve_message(123)) +@patch("bot.constants.Channels.incidents", 123) +class TestOnRawReactionAdd(TestIncidents): + """ + Tests for the `Incidents.on_raw_reaction_add` listener. + + Writing tests for this listener comes with additional complexity due to the listener + awaiting the `crawl_task` task. See `asyncSetUp` for further details, which attempts + to make unit testing this function possible. + """ + + def setUp(self): + """ + Prepare & assign `payload` attribute. + + This attribute represents an *ideal* payload which will not be rejected by the + listener. As each test will receive a fresh instance, it can be mutated to + observe how the listener's behaviour changes with different attributes on + the passed payload. + """ + super().setUp() # Ensure `cog_instance` is assigned + + self.payload = MagicMock( + discord.RawReactionActionEvent, + channel_id=123, # Patched at class level + message_id=456, + member=MockMember(bot=False), + emoji="reaction", + ) + + async def asyncSetUp(self): # noqa: N802 + """ + Prepare an empty task and assign it as `crawl_task`. + + It appears that the `unittest` framework does not provide anything for mocking + asyncio tasks. An `AsyncMock` instance can be called and then awaited, however, + it does not provide the `done` method or any other parts of the `asyncio.Task` + interface. + + Although we do not need to make any assertions about the task itself while + testing the listener, the code will still await it and call the `done` method, + and so we must inject something that will not fail on either action. + + Note that this is done in an `asyncSetUp`, which runs after `setUp`. + The justification is that creating an actual task requires the event + loop to be ready, which is not the case in the `setUp`. + """ + mock_task = asyncio.create_task(AsyncMock()()) # Mock async func, then a coro + self.cog_instance.crawl_task = mock_task + + async def test_on_raw_reaction_add_wrong_channel(self): + """ + Events outside of #incidents will be ignored. + + We check this by asserting that `resolve_message` was never queried. + """ + self.payload.channel_id = 0 + self.cog_instance.resolve_message = AsyncMock() + + await self.cog_instance.on_raw_reaction_add(self.payload) + self.cog_instance.resolve_message.assert_not_called() + + async def test_on_raw_reaction_add_user_is_bot(self): + """ + Events dispatched by bot accounts will be ignored. + + We check this by asserting that `resolve_message` was never queried. + """ + self.payload.member = MockMember(bot=True) + self.cog_instance.resolve_message = AsyncMock() + + await self.cog_instance.on_raw_reaction_add(self.payload) + self.cog_instance.resolve_message.assert_not_called() + + async def test_on_raw_reaction_add_message_doesnt_exist(self): + """ + Listener gracefully handles the case where `resolve_message` gives None. + + We check this by asserting that `process_event` was never called. + """ + self.cog_instance.process_event = AsyncMock() + self.cog_instance.resolve_message = AsyncMock(return_value=None) + + await self.cog_instance.on_raw_reaction_add(self.payload) + self.cog_instance.process_event.assert_not_called() + + async def test_on_raw_reaction_add_message_is_not_an_incident(self): + """ + The event won't be processed if the related message is not an incident. + + This is an edge-case that can happen if someone manually leaves a reaction + on a pinned message, or a comment. + + We check this by asserting that `process_event` was never called. + """ + self.cog_instance.process_event = AsyncMock() + self.cog_instance.resolve_message = AsyncMock(return_value=MockMessage()) + + with patch("bot.cogs.moderation.incidents.is_incident", MagicMock(return_value=False)): + await self.cog_instance.on_raw_reaction_add(self.payload) + + self.cog_instance.process_event.assert_not_called() + + async def test_on_raw_reaction_add_valid_event_is_processed(self): + """ + If the reaction event is valid, it is passed to `process_event`. + + This is the case when everything goes right: + * The reaction was placed in #incidents, and not by a bot + * The message was found successfully + * The message qualifies as an incident + + Additionally, we check that all arguments were passed as expected. + """ + incident = MockMessage(id=1) + + self.cog_instance.process_event = AsyncMock() + self.cog_instance.resolve_message = AsyncMock(return_value=incident) + + with patch("bot.cogs.moderation.incidents.is_incident", MagicMock(return_value=True)): + await self.cog_instance.on_raw_reaction_add(self.payload) + + self.cog_instance.process_event.assert_called_with( + "reaction", # Defined in `self.payload` + incident, + self.payload.member, + ) + + class TestOnMessage(TestIncidents): """ Tests for the `Incidents.on_message` listener. -- cgit v1.2.3 From e760bd38b5d625011318a9ddfc98bb52570d1c3a Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 19 Jun 2020 17:08:50 +0200 Subject: Incidents: review log levels; use `trace` where appropriate Logs useful when observing the internals but too verbose for DEBUG are reduced to TRACE. --- bot/cogs/moderation/incidents.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 16286bdab..da04c7d0d 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -64,10 +64,10 @@ async def add_signals(incident: discord.Message) -> None: # This will not raise, but it is a superfluous API call that can be avoided if signal_emoji.value in existing_reacts: - log.debug(f"Skipping emoji as it's already been placed: {signal_emoji}") + log.trace(f"Skipping emoji as it's already been placed: {signal_emoji}") else: - log.debug(f"Adding reaction: {signal_emoji}") + log.trace(f"Adding reaction: {signal_emoji}") await incident.add_reaction(signal_emoji.value) @@ -132,11 +132,11 @@ class Incidents(Cog): async for message in incidents.history(limit=limit): if not is_incident(message): - log.debug("Skipping message: not an incident") + log.trace("Skipping message: not an incident") continue if has_signals(message): - log.debug("Skipping message: already has all signals") + log.trace("Skipping message: already has all signals") continue await add_signals(message) @@ -179,7 +179,7 @@ class Incidents(Cog): return False else: - log.debug("Message archived successfully!") + log.trace("Message archived successfully!") return True def make_confirmation_task(self, incident: discord.Message, timeout: int = 5) -> asyncio.Task: @@ -189,7 +189,7 @@ class Incidents(Cog): If `timeout` passes, this will raise `asyncio.TimeoutError`, signaling that we haven't been able to confirm that the message was deleted. """ - log.debug(f"Confirmation task will wait {timeout=} seconds for {incident.id=} to be deleted") + log.trace(f"Confirmation task will wait {timeout=} seconds for {incident.id=} to be deleted") def check(payload: discord.RawReactionActionEvent) -> bool: return payload.message_id == incident.id @@ -225,7 +225,7 @@ class Incidents(Cog): # If we reach this point, we know that `emoji` is a `Signal` member signal = Signal(reaction) - log.debug(f"Received signal: {signal}") + log.trace(f"Received signal: {signal}") if signal not in (Signal.ACTIONED, Signal.NOT_ACTIONED): log.debug("Reaction was valid, but no action is currently defined for it") @@ -233,22 +233,22 @@ class Incidents(Cog): relay_successful = await self.archive(incident, signal) if not relay_successful: - log.debug("Original message will not be deleted as we failed to relay it to the archive") + log.trace("Original message will not be deleted as we failed to relay it to the archive") return timeout = 5 # Seconds confirmation_task = self.make_confirmation_task(incident, timeout) - log.debug("Deleting original message") + log.trace("Deleting original message") await incident.delete() - log.debug(f"Awaiting deletion confirmation: {timeout=} seconds") + log.trace(f"Awaiting deletion confirmation: {timeout=} seconds") try: await confirmation_task except asyncio.TimeoutError: log.warning(f"Did not receive incident deletion confirmation within {timeout} seconds!") else: - log.debug("Deletion was confirmed") + log.trace("Deletion was confirmed") async def resolve_message(self, message_id: int) -> t.Optional[discord.Message]: """ @@ -264,22 +264,22 @@ class Incidents(Cog): This signals that the event for `message_id` should be ignored. """ await self.bot.wait_until_guild_available() # First make sure that the cache is ready - log.debug(f"Resolving message for: {message_id=}") + log.trace(f"Resolving message for: {message_id=}") message: discord.Message = self.bot._connection._get_message(message_id) # noqa: Private attribute if message is not None: - log.debug("Message was found in cache") + log.trace("Message was found in cache") return message - log.debug("Message not found, attempting to fetch") + log.trace("Message not found, attempting to fetch") try: message = await self.bot.get_channel(Channels.incidents).fetch_message(message_id) except discord.NotFound: - log.debug("Message doesn't exist, it was likely already relayed") + log.trace("Message doesn't exist, it was likely already relayed") except Exception as exc: log.exception("Failed to fetch message!", exc_info=exc) else: - log.debug("Message fetched successfully!") + log.trace("Message fetched successfully!") return message @Cog.listener() @@ -309,10 +309,10 @@ class Incidents(Cog): if payload.channel_id != Channels.incidents or payload.member.bot: return - log.debug(f"Received reaction add event in #incidents, waiting for crawler: {self.crawl_task.done()=}") + log.trace(f"Received reaction add event in #incidents, waiting for crawler: {self.crawl_task.done()=}") await self.crawl_task - log.debug(f"Acquiring event lock: {self.event_lock.locked()=}") + log.trace(f"Acquiring event lock: {self.event_lock.locked()=}") async with self.event_lock: message = await self.resolve_message(payload.message_id) @@ -325,7 +325,7 @@ class Incidents(Cog): return await self.process_event(str(payload.emoji), message, payload.member) - log.debug("Releasing event lock") + log.trace("Releasing event lock") @Cog.listener() async def on_message(self, message: discord.Message) -> None: -- cgit v1.2.3 From 8a0263f5a591be51e74c2b26369f74c6d8dfee09 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 19 Jun 2020 17:55:20 +0200 Subject: Incidents: remove broad noqa This was originally in place to silence a PyCharm warning regarding accessing the private attributes. However, since there is no corresponding error code to specify, the noqa will silence any linter warning, which is potentially dangerous, and seems to be bad practice. --- bot/cogs/moderation/incidents.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index da04c7d0d..c733607e6 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -265,7 +265,7 @@ class Incidents(Cog): """ await self.bot.wait_until_guild_available() # First make sure that the cache is ready log.trace(f"Resolving message for: {message_id=}") - message: discord.Message = self.bot._connection._get_message(message_id) # noqa: Private attribute + message: discord.Message = self.bot._connection._get_message(message_id) if message is not None: log.trace("Message was found in cache") -- cgit v1.2.3 From 8a08ca3a29f6d6bda2ab71bc9fd70782be9869e4 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 19 Jun 2020 20:26:06 +0200 Subject: Incidents: annotate possible None type Caught during review by ks129. Co-authored-by: ks129 <45097959+ks129@users.noreply.github.com> --- bot/cogs/moderation/incidents.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index c733607e6..c09d8e1a7 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -265,7 +265,7 @@ class Incidents(Cog): """ await self.bot.wait_until_guild_available() # First make sure that the cache is ready log.trace(f"Resolving message for: {message_id=}") - message: discord.Message = self.bot._connection._get_message(message_id) + message: t.Optional[discord.Message] = self.bot._connection._get_message(message_id) if message is not None: log.trace("Message was found in cache") -- cgit v1.2.3 From b563063c1a25a0a775ea1fb6cf31b7ef9725e14e Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 19 Jun 2020 20:33:17 +0200 Subject: Incidents: reduce excessive whitespace This is way too spacious for how little is happening here. Suggested by ks129. Co-authored-by: ks129 <45097959+ks129@users.noreply.github.com> --- bot/cogs/moderation/incidents.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index c09d8e1a7..70921462d 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -61,11 +61,8 @@ async def add_signals(incident: discord.Message) -> None: existing_reacts = own_reactions(incident) for signal_emoji in Signal: - - # This will not raise, but it is a superfluous API call that can be avoided - if signal_emoji.value in existing_reacts: + if signal_emoji.value in existing_reacts: # This would not raise, but it is a superfluous API call log.trace(f"Skipping emoji as it's already been placed: {signal_emoji}") - else: log.trace(f"Adding reaction: {signal_emoji}") await incident.add_reaction(signal_emoji.value) -- cgit v1.2.3 From 5e02d5ded0b9d1947e0e9d5455b134d9e2299a7d Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 19 Jun 2020 21:48:18 -0700 Subject: Scheduler: use separate logger for each instance Each instance now requires a name to be specified, which will be used as the suffix of the logger's name. This removes the need to manually prepend every log message with the name. --- bot/utils/scheduling.py | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 8b778a093..002ef42cf 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -7,16 +7,14 @@ from functools import partial from bot.utils import CogABCMeta -log = logging.getLogger(__name__) - class Scheduler(metaclass=CogABCMeta): """Task scheduler.""" - def __init__(self): - # Keep track of the child cog's name so the logs are clear. - self.cog_name = self.__class__.__name__ + def __init__(self, name: str): + self.name = name + self._log = logging.getLogger(f"{__name__}.{name}") self._scheduled_tasks: t.Dict[t.Hashable, asyncio.Task] = {} @abstractmethod @@ -37,19 +35,17 @@ class Scheduler(metaclass=CogABCMeta): `task_data` is passed to the `Scheduler._scheduled_task()` coroutine. """ - log.trace(f"{self.cog_name}: scheduling task #{task_id}...") + self._log.trace(f"Scheduling task #{task_id}...") if task_id in self._scheduled_tasks: - log.debug( - f"{self.cog_name}: did not schedule task #{task_id}; task was already scheduled." - ) + self._log.debug(f"Did not schedule task #{task_id}; task was already scheduled.") return task = asyncio.create_task(self._scheduled_task(task_data)) task.add_done_callback(partial(self._task_done_callback, task_id)) self._scheduled_tasks[task_id] = task - log.debug(f"{self.cog_name}: scheduled task #{task_id} {id(task)}.") + self._log.debug(f"Scheduled task #{task_id} {id(task)}.") def cancel_task(self, task_id: t.Hashable, ignore_missing: bool = False) -> None: """ @@ -57,22 +53,22 @@ class Scheduler(metaclass=CogABCMeta): If `ignore_missing` is True, a warning will not be sent if a task isn't found. """ - log.trace(f"{self.cog_name}: cancelling task #{task_id}...") + self._log.trace(f"Cancelling task #{task_id}...") task = self._scheduled_tasks.get(task_id) if not task: if not ignore_missing: - log.warning(f"{self.cog_name}: failed to unschedule {task_id} (no task found).") + self._log.warning(f"Failed to unschedule {task_id} (no task found).") return del self._scheduled_tasks[task_id] task.cancel() - log.debug(f"{self.cog_name}: unscheduled task #{task_id} {id(task)}.") + self._log.debug(f"Unscheduled task #{task_id} {id(task)}.") def cancel_all(self) -> None: """Unschedule all known tasks.""" - log.debug(f"{self.cog_name}: unscheduling all tasks") + self._log.debug("Unscheduling all tasks") for task_id in self._scheduled_tasks.copy(): self.cancel_task(task_id, ignore_missing=True) @@ -84,24 +80,24 @@ class Scheduler(metaclass=CogABCMeta): If `done_task` and the task associated with `task_id` are different, then the latter will not be deleted. In this case, a new task was likely rescheduled with the same ID. """ - log.trace(f"{self.cog_name}: performing done callback for task #{task_id} {id(done_task)}.") + self._log.trace(f"Performing done callback for task #{task_id} {id(done_task)}.") scheduled_task = self._scheduled_tasks.get(task_id) if scheduled_task and done_task is scheduled_task: # A task for the ID exists and its the same as the done task. # Since this is the done callback, the task is already done so no need to cancel it. - log.trace(f"{self.cog_name}: deleting task #{task_id} {id(done_task)}.") + self._log.trace(f"Deleting task #{task_id} {id(done_task)}.") del self._scheduled_tasks[task_id] elif scheduled_task: # A new task was likely rescheduled with the same ID. - log.debug( - f"{self.cog_name}: the scheduled task #{task_id} {id(scheduled_task)} " + self._log.debug( + f"The scheduled task #{task_id} {id(scheduled_task)} " f"and the done task {id(done_task)} differ." ) elif not done_task.cancelled(): - log.warning( - f"{self.cog_name}: task #{task_id} not found while handling task {id(done_task)}! " + self._log.warning( + f"Task #{task_id} not found while handling task {id(done_task)}! " f"A task somehow got unscheduled improperly (i.e. deleted but not cancelled)." ) @@ -109,7 +105,4 @@ class Scheduler(metaclass=CogABCMeta): exception = done_task.exception() # Log the exception if one exists. if exception: - log.error( - f"{self.cog_name}: error in task #{task_id} {id(done_task)}!", - exc_info=exception - ) + self._log.error(f"Error in task #{task_id} {id(done_task)}!", exc_info=exception) -- cgit v1.2.3 From 5ded9651ab260c43053a660f2fc239aa722db5c7 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 19 Jun 2020 21:59:05 -0700 Subject: Scheduler: directly take the awaitable to schedule This is a major change which simplifies the interface. It removes the need to implement an abstract method, which means the class can now be instantiated rather than subclassed. --- bot/utils/scheduling.py | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 002ef42cf..70fb1972b 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -2,13 +2,10 @@ import asyncio import contextlib import logging import typing as t -from abc import abstractmethod from functools import partial -from bot.utils import CogABCMeta - -class Scheduler(metaclass=CogABCMeta): +class Scheduler: """Task scheduler.""" def __init__(self, name: str): @@ -17,31 +14,15 @@ class Scheduler(metaclass=CogABCMeta): self._log = logging.getLogger(f"{__name__}.{name}") self._scheduled_tasks: t.Dict[t.Hashable, asyncio.Task] = {} - @abstractmethod - async def _scheduled_task(self, task_object: t.Any) -> None: - """ - A coroutine which handles the scheduling. - - This is added to the scheduled tasks, and should wait the task duration, execute the desired - code, then clean up the task. - - For example, in Reminders this will wait for the reminder duration, send the reminder, - then make a site API request to delete the reminder from the database. - """ - - def schedule_task(self, task_id: t.Hashable, task_data: t.Any) -> None: - """ - Schedules a task. - - `task_data` is passed to the `Scheduler._scheduled_task()` coroutine. - """ + def schedule_task(self, task_id: t.Hashable, task: t.Awaitable) -> None: + """Schedule the execution of a task.""" self._log.trace(f"Scheduling task #{task_id}...") if task_id in self._scheduled_tasks: self._log.debug(f"Did not schedule task #{task_id}; task was already scheduled.") return - task = asyncio.create_task(self._scheduled_task(task_data)) + task = asyncio.create_task(task) task.add_done_callback(partial(self._task_done_callback, task_id)) self._scheduled_tasks[task_id] = task -- cgit v1.2.3 From 4bb6bde1c79f3ffd3d452dd7ffe489d9b093f567 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 19 Jun 2020 22:00:26 -0700 Subject: Scheduler: name tasks Makes them easier to identify when debugging. --- bot/utils/scheduling.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 70fb1972b..f2640ed5e 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -22,7 +22,7 @@ class Scheduler: self._log.debug(f"Did not schedule task #{task_id}; task was already scheduled.") return - task = asyncio.create_task(task) + task = asyncio.create_task(task, name=f"{self.name}_{task_id}") task.add_done_callback(partial(self._task_done_callback, task_id)) self._scheduled_tasks[task_id] = task -- cgit v1.2.3 From 5130611719735d8e58c1d0faeeeaffe4553843dd Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 19 Jun 2020 22:49:41 -0700 Subject: Scheduler: add support for in operator --- bot/utils/scheduling.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index f2640ed5e..00fca4169 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -14,6 +14,10 @@ class Scheduler: self._log = logging.getLogger(f"{__name__}.{name}") self._scheduled_tasks: t.Dict[t.Hashable, asyncio.Task] = {} + def __contains__(self, task_id: t.Hashable) -> bool: + """Return True if a task with the given `task_id` is currently scheduled.""" + return task_id in self._scheduled_tasks + def schedule_task(self, task_id: t.Hashable, task: t.Awaitable) -> None: """Schedule the execution of a task.""" self._log.trace(f"Scheduling task #{task_id}...") -- cgit v1.2.3 From c81d3bdd1769a02ba02af18e52150629e655e3c9 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 19 Jun 2020 23:02:24 -0700 Subject: Scheduler: use pop instead of get when cancelling --- bot/utils/scheduling.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 00fca4169..6f498348d 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -39,17 +39,17 @@ class Scheduler: If `ignore_missing` is True, a warning will not be sent if a task isn't found. """ self._log.trace(f"Cancelling task #{task_id}...") - task = self._scheduled_tasks.get(task_id) - if not task: + try: + task = self._scheduled_tasks.pop(task_id) + except KeyError: if not ignore_missing: self._log.warning(f"Failed to unschedule {task_id} (no task found).") - return - - del self._scheduled_tasks[task_id] - task.cancel() + else: + del self._scheduled_tasks[task_id] + task.cancel() - self._log.debug(f"Unscheduled task #{task_id} {id(task)}.") + self._log.debug(f"Unscheduled task #{task_id} {id(task)}.") def cancel_all(self) -> None: """Unschedule all known tasks.""" -- cgit v1.2.3 From 662ca588ac352fc346fae973dead5052c7b4af59 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 20 Jun 2020 12:22:11 +0200 Subject: Incidents: remove redundant `exc_info` passing Pointed out by Mark during review that this is unnecessary, as logging using `exception` automatically appends the `exc_info` of the handled exception when done in an except block. Co-authored-by: MarkKoz --- bot/cogs/moderation/incidents.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 70921462d..5f4291953 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -171,8 +171,8 @@ class Incidents(Cog): # Finally add the `outcome` emoji await message.add_reaction(outcome.value) - except Exception as exc: - log.exception("Failed to archive incident to #incidents-archive", exc_info=exc) + except Exception: + log.exception("Failed to archive incident to #incidents-archive") return False else: @@ -273,8 +273,8 @@ class Incidents(Cog): message = await self.bot.get_channel(Channels.incidents).fetch_message(message_id) except discord.NotFound: log.trace("Message doesn't exist, it was likely already relayed") - except Exception as exc: - log.exception("Failed to fetch message!", exc_info=exc) + except Exception: + log.exception("Failed to fetch message!") else: log.trace("Message fetched successfully!") return message -- cgit v1.2.3 From 20b27f32c68673b603a6e6e41859f7672b6e0133 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 20 Jun 2020 12:28:18 +0200 Subject: Incidents: make logs contain the message id they pertain to Suggested by Mark during review. Co-authored-by: MarkKoz --- bot/cogs/moderation/incidents.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 5f4291953..33c3bee51 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -129,11 +129,11 @@ class Incidents(Cog): async for message in incidents.history(limit=limit): if not is_incident(message): - log.trace("Skipping message: not an incident") + log.trace(f"Skipping message {message.id}: not an incident") continue if has_signals(message): - log.trace("Skipping message: already has all signals") + log.trace(f"Skipping message {message.id}: already has all signals") continue await add_signals(message) @@ -172,7 +172,7 @@ class Incidents(Cog): await message.add_reaction(outcome.value) except Exception: - log.exception("Failed to archive incident to #incidents-archive") + log.exception(f"Failed to archive incident {incident.id} to #incidents-archive") return False else: @@ -274,7 +274,7 @@ class Incidents(Cog): except discord.NotFound: log.trace("Message doesn't exist, it was likely already relayed") except Exception: - log.exception("Failed to fetch message!") + log.exception(f"Failed to fetch message {message_id}!") else: log.trace("Message fetched successfully!") return message -- cgit v1.2.3 From 6d3a91cf5d51e6e2a2f10c08718a7c8de0d521ed Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 20 Jun 2020 12:35:46 +0200 Subject: Incidents: make crawl limit & sleep module-level constants Requested during review. Co-authored-by: ks129 <45097959+ks129@users.noreply.github.com> Co-authored-by: Joseph Banks --- bot/cogs/moderation/incidents.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 33c3bee51..4e6743224 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -11,6 +11,14 @@ from bot.constants import Channels, Emojis, Roles, Webhooks log = logging.getLogger(__name__) +# Amount of messages for `crawl_task` to process at most on start-up - limited to 50 +# as in practice, there should never be this many messages, and if there are, +# something has likely gone very wrong +CRAWL_LIMIT = 50 + +# Seconds for `crawl_task` to sleep after adding reactions to a message +CRAWL_SLEEP = 2 + class Signal(Enum): """ @@ -114,19 +122,14 @@ class Incidents(Cog): Once this task is scheduled, listeners that change messages should await it. The crawl assumes that the channel history doesn't change as we go over it. + + Behaviour is configured by: `CRAWL_LIMIT`, `CRAWL_SLEEP`. """ await self.bot.wait_until_guild_available() incidents: discord.TextChannel = self.bot.get_channel(Channels.incidents) - # Limit the query at 50 as in practice, there should never be this many messages, - # and if there are, something has likely gone very wrong - limit = 50 - - # Seconds to sleep after adding reactions to a message - sleep = 2 - - log.debug(f"Crawling messages in #incidents: {limit=}, {sleep=}") - async for message in incidents.history(limit=limit): + log.debug(f"Crawling messages in #incidents: {CRAWL_LIMIT=}, {CRAWL_SLEEP=}") + async for message in incidents.history(limit=CRAWL_LIMIT): if not is_incident(message): log.trace(f"Skipping message {message.id}: not an incident") @@ -137,7 +140,7 @@ class Incidents(Cog): continue await add_signals(message) - await asyncio.sleep(sleep) + await asyncio.sleep(CRAWL_SLEEP) log.debug("Crawl task finished!") -- cgit v1.2.3 From b8ada89bd45e6b8efd17fba79e70ce91a59b24fc Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 20 Jun 2020 12:42:53 +0200 Subject: Incidents: simplify set operation in `has_signals` Using `issubset` is a much simpler & more readable way of expressing the relationship between the two sets. Suggested by Mark during review. Co-authored-by: MarkKoz --- bot/cogs/moderation/incidents.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 4e6743224..089a5bc9f 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -56,8 +56,7 @@ def own_reactions(message: discord.Message) -> t.Set[str]: def has_signals(message: discord.Message) -> bool: """True if `message` already has all `Signal` reactions, False otherwise.""" - missing_signals = ALLOWED_EMOJI - own_reactions(message) # In `ALLOWED_EMOJI` but not in `own_reactions(message)` - return not missing_signals + return ALLOWED_EMOJI.issubset(own_reactions(message)) async def add_signals(incident: discord.Message) -> None: -- cgit v1.2.3 From 98b8947ab7865e33f18da8e2a62b26405676e8e4 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 20 Jun 2020 13:13:45 +0200 Subject: Incidents: try-except Signal creation Suggested by Mark during review. This follows the "ask for forgiveness rather than permission" paradigm, ends up being less code to read, and may be seen as more logical / safer. The `ALLOWED_EMOJI` set was renamed to `ALL_SIGNALS` as this now better communicates the set's purpose. Tests adjusted as appropriate. Co-authored-by: MarkKoz --- bot/cogs/moderation/incidents.py | 18 +++++++++++------- tests/bot/cogs/moderation/test_incidents.py | 8 ++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 089a5bc9f..41a98bcb7 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -33,9 +33,11 @@ class Signal(Enum): INVESTIGATING = Emojis.incident_investigating -# Reactions from roles not listed here, or using emoji not listed here, will be removed +# Reactions from roles not listed here will be removed ALLOWED_ROLES: t.Set[int] = {Roles.moderators, Roles.admins, Roles.owners} -ALLOWED_EMOJI: t.Set[str] = {signal.value for signal in Signal} + +# Message must have all of these emoji to pass the `has_signals` check +ALL_SIGNALS: t.Set[str] = {signal.value for signal in Signal} def is_incident(message: discord.Message) -> bool: @@ -56,7 +58,7 @@ def own_reactions(message: discord.Message) -> t.Set[str]: def has_signals(message: discord.Message) -> bool: """True if `message` already has all `Signal` reactions, False otherwise.""" - return ALLOWED_EMOJI.issubset(own_reactions(message)) + return ALL_SIGNALS.issubset(own_reactions(message)) async def add_signals(incident: discord.Message) -> None: @@ -96,7 +98,9 @@ class Incidents(Cog): * See: `on_message` On reaction: - * Remove reaction if not permitted (`ALLOWED_EMOJI`, `ALLOWED_ROLES`) + * Remove reaction if not permitted + * User does not have any of the roles in `ALLOWED_ROLES` + * Used emoji is not a `Signal` member * If `Signal.ACTIONED` or `Signal.NOT_ACTIONED` were chosen, attempt to relay the incident message to #incidents-archive * If relay successful, delete original message @@ -217,13 +221,13 @@ class Incidents(Cog): await incident.remove_reaction(reaction, member) return - if reaction not in ALLOWED_EMOJI: + try: + signal = Signal(reaction) + except ValueError: log.debug(f"Removing invalid reaction: emoji {reaction} is not a valid signal") await incident.remove_reaction(reaction, member) return - # If we reach this point, we know that `emoji` is a `Signal` member - signal = Signal(reaction) log.trace(f"Received signal: {signal}") if signal not in (Signal.ACTIONED, Signal.NOT_ACTIONED): diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 55b15ec9e..862736785 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -131,17 +131,17 @@ class TestOwnReactions(unittest.TestCase): self.assertSetEqual(incidents.own_reactions(message), {"A", "B"}) -@patch("bot.cogs.moderation.incidents.ALLOWED_EMOJI", {"A", "B"}) +@patch("bot.cogs.moderation.incidents.ALL_SIGNALS", {"A", "B"}) class TestHasSignals(unittest.TestCase): """ Assertions for the `has_signals` function. - We patch `ALLOWED_EMOJI` globally. Each test function then patches `own_reactions` + We patch `ALL_SIGNALS` globally. Each test function then patches `own_reactions` as appropriate. """ def test_has_signals_true(self): - """True when `own_reactions` returns all emoji in `ALLOWED_EMOJI`.""" + """True when `own_reactions` returns all emoji in `ALL_SIGNALS`.""" message = MockMessage() own_reactions = MagicMock(return_value={"A", "B"}) @@ -149,7 +149,7 @@ class TestHasSignals(unittest.TestCase): self.assertTrue(incidents.has_signals(message)) def test_has_signals_false(self): - """False when `own_reactions` does not return all emoji in `ALLOWED_EMOJI`.""" + """False when `own_reactions` does not return all emoji in `ALL_SIGNALS`.""" message = MockMessage() own_reactions = MagicMock(return_value={"A", "C"}) -- cgit v1.2.3 From 20dbd177f227511b9c3cb678ab45a67558cd3d7f Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 20 Jun 2020 13:15:43 +0200 Subject: Incidents tests: remove unnecessary patch This is already being patched at class-level. --- tests/bot/cogs/moderation/test_incidents.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 862736785..9f0553216 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -360,7 +360,6 @@ class TestMakeConfirmationTask(TestIncidents): class TestProcessEvent(TestIncidents): """Tests for the `Incidents.process_event` coroutine.""" - @patch("bot.cogs.moderation.incidents.ALLOWED_ROLES", {1, 2}) async def test_process_event_bad_role(self): """The reaction is removed when the author lacks all allowed roles.""" incident = MockMessage() -- cgit v1.2.3 From a8b4e394d9da57287cd9497cd9bb0a97fa467e84 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 20 Jun 2020 14:02:48 +0200 Subject: Incidents: de-clyde archive webhook username With PR #1009 merged, we now apply the same fix to our relay function. This prevents the "clyde" word from sneaking into the webhook username, which is forbidden and will return a 400. --- bot/cogs/moderation/incidents.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 41a98bcb7..040f2c0c8 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -8,6 +8,7 @@ from discord.ext.commands import Cog from bot.bot import Bot from bot.constants import Channels, Emojis, Roles, Webhooks +from bot.utils.messages import sub_clyde log = logging.getLogger(__name__) @@ -169,7 +170,7 @@ class Incidents(Cog): # Now relay the incident message: discord.Message = await webhook.send( content=incident.clean_content, # Clean content will prevent mentions from pinging - username=incident.author.name, + username=sub_clyde(incident.author.name), avatar_url=incident.author.avatar_url, wait=True, # This makes the method return the sent Message object ) -- cgit v1.2.3 From f240a970c6b97d201959d25a79a8babafed1c2b1 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 20 Jun 2020 14:15:34 +0200 Subject: Incidents tests: assert webhook username is de-clyded See: a8b4e394d9da57287cd9497cd9bb0a97fa467e84 --- tests/bot/cogs/moderation/test_incidents.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 9f0553216..2fc9180cf 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -319,6 +319,25 @@ class TestArchive(TestIncidents): # Finally check that the method returned True self.assertTrue(archive_return) + async def test_archive_clyde_username(self): + """ + The archive webhook username is cleansed using `sub_clyde`. + + Discord will reject any webhook with "clyde" in the username field, as it impersonates + the official Clyde bot. Since we do not control what the username will be (the incident + author name is used), we must ensure the name is cleansed, otherwise the relay may fail. + + This test assumes the username is passed as a kwarg. If this test fails, please review + whether the passed argument is being retrieved correctly. + """ + webhook = MockAsyncWebhook() + self.cog_instance.bot.fetch_webhook = AsyncMock(return_value=webhook) + + message_from_clyde = MockMessage(author=MockUser(name="clyde the great")) + await self.cog_instance.archive(message_from_clyde, MagicMock(incidents.Signal)) + + self.assertNotIn("clyde", webhook.send.call_args.kwargs["username"]) + class TestMakeConfirmationTask(TestIncidents): """ -- cgit v1.2.3 From e09276191f5bcaa0dbf34fdbff51654027528688 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 20 Jun 2020 09:37:21 -0700 Subject: Scheduler: remove ignore_missing param The ability to use the `in` operator makes this obsolete. Callers can check themselves if a task exists before they try to cancel it. --- bot/utils/scheduling.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 6f498348d..d9b48034b 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -32,19 +32,14 @@ class Scheduler: self._scheduled_tasks[task_id] = task self._log.debug(f"Scheduled task #{task_id} {id(task)}.") - def cancel_task(self, task_id: t.Hashable, ignore_missing: bool = False) -> None: - """ - Unschedule the task identified by `task_id`. - - If `ignore_missing` is True, a warning will not be sent if a task isn't found. - """ + def cancel_task(self, task_id: t.Hashable) -> None: + """Unschedule the task identified by `task_id`. Log a warning if the task doesn't exist.""" self._log.trace(f"Cancelling task #{task_id}...") try: task = self._scheduled_tasks.pop(task_id) except KeyError: - if not ignore_missing: - self._log.warning(f"Failed to unschedule {task_id} (no task found).") + self._log.warning(f"Failed to unschedule {task_id} (no task found).") else: del self._scheduled_tasks[task_id] task.cancel() @@ -56,7 +51,7 @@ class Scheduler: self._log.debug("Unscheduling all tasks") for task_id in self._scheduled_tasks.copy(): - self.cancel_task(task_id, ignore_missing=True) + self.cancel_task(task_id) def _task_done_callback(self, task_id: t.Hashable, done_task: asyncio.Task) -> None: """ @@ -70,7 +65,7 @@ class Scheduler: scheduled_task = self._scheduled_tasks.get(task_id) if scheduled_task and done_task is scheduled_task: - # A task for the ID exists and its the same as the done task. + # A task for the ID exists and is the same as the done task. # Since this is the done callback, the task is already done so no need to cancel it. self._log.trace(f"Deleting task #{task_id} {id(done_task)}.") del self._scheduled_tasks[task_id] -- cgit v1.2.3 From 19e41aae30e19374054d9ed37f36faa2104f751c Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 20 Jun 2020 13:20:24 -0700 Subject: Scheduler: drop _task suffix from method names It's redundant. After all, this scheduler cannot schedule anything else. --- bot/utils/scheduling.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index d9b48034b..4a003d4fe 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -18,7 +18,7 @@ class Scheduler: """Return True if a task with the given `task_id` is currently scheduled.""" return task_id in self._scheduled_tasks - def schedule_task(self, task_id: t.Hashable, task: t.Awaitable) -> None: + def schedule(self, task_id: t.Hashable, task: t.Awaitable) -> None: """Schedule the execution of a task.""" self._log.trace(f"Scheduling task #{task_id}...") @@ -32,7 +32,7 @@ class Scheduler: self._scheduled_tasks[task_id] = task self._log.debug(f"Scheduled task #{task_id} {id(task)}.") - def cancel_task(self, task_id: t.Hashable) -> None: + def cancel(self, task_id: t.Hashable) -> None: """Unschedule the task identified by `task_id`. Log a warning if the task doesn't exist.""" self._log.trace(f"Cancelling task #{task_id}...") @@ -51,7 +51,7 @@ class Scheduler: self._log.debug("Unscheduling all tasks") for task_id in self._scheduled_tasks.copy(): - self.cancel_task(task_id) + self.cancel(task_id) def _task_done_callback(self, task_id: t.Hashable, done_task: asyncio.Task) -> None: """ -- cgit v1.2.3 From ee47b2afda1f8f409c1c60bd874d15b1d1a52ca6 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 20 Jun 2020 13:23:50 -0700 Subject: Scheduler: rename "task" param to "coroutine" Naming it "task" is inaccurate because `create_task` accepts a coroutine rather than a Task. What it does is wrap the coroutine in a Task. --- bot/utils/scheduling.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 4a003d4fe..625b726d2 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -18,15 +18,15 @@ class Scheduler: """Return True if a task with the given `task_id` is currently scheduled.""" return task_id in self._scheduled_tasks - def schedule(self, task_id: t.Hashable, task: t.Awaitable) -> None: - """Schedule the execution of a task.""" + def schedule(self, task_id: t.Hashable, coroutine: t.Coroutine) -> None: + """Schedule the execution of a coroutine.""" self._log.trace(f"Scheduling task #{task_id}...") if task_id in self._scheduled_tasks: self._log.debug(f"Did not schedule task #{task_id}; task was already scheduled.") return - task = asyncio.create_task(task, name=f"{self.name}_{task_id}") + task = asyncio.create_task(coroutine, name=f"{self.name}_{task_id}") task.add_done_callback(partial(self._task_done_callback, task_id)) self._scheduled_tasks[task_id] = task -- cgit v1.2.3 From f807bf72fa649242b910e309d7043c8bdc2b1fdc Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 20 Jun 2020 13:54:20 -0700 Subject: Scheduler: add a method to schedule with a delay --- bot/utils/scheduling.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 625b726d2..ac67278f6 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -32,6 +32,10 @@ class Scheduler: self._scheduled_tasks[task_id] = task self._log.debug(f"Scheduled task #{task_id} {id(task)}.") + def schedule_later(self, delay: t.Union[int, float], task_id: t.Hashable, coroutine: t.Coroutine) -> None: + """Schedule `coroutine` to be executed after the given `delay` number of seconds.""" + self.schedule(task_id, self._await_later(delay, coroutine)) + def cancel(self, task_id: t.Hashable) -> None: """Unschedule the task identified by `task_id`. Log a warning if the task doesn't exist.""" self._log.trace(f"Cancelling task #{task_id}...") @@ -53,6 +57,21 @@ class Scheduler: for task_id in self._scheduled_tasks.copy(): self.cancel(task_id) + async def _await_later(self, delay: t.Union[int, float], coroutine: t.Coroutine) -> None: + """Await `coroutine` after the given `delay` number of seconds.""" + try: + self._log.trace(f"Waiting {delay} seconds before awaiting the coroutine.") + await asyncio.sleep(delay) + + # Use asyncio.shield to prevent the coroutine from cancelling itself. + self._log.trace("Done waiting; now awaiting the coroutine.") + await asyncio.shield(coroutine) + finally: + # Close it to prevent unawaited coroutine warnings, + # which would happen if the task was cancelled during the sleep. + self._log.trace("Explicitly closing the coroutine.") + coroutine.close() + def _task_done_callback(self, task_id: t.Hashable, done_task: asyncio.Task) -> None: """ Delete the task and raise its exception if one exists. -- cgit v1.2.3 From dfcf71f36c85e357028ea2f86aac7e38c6b8ab47 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 20 Jun 2020 14:02:23 -0700 Subject: Scheduler: add a method to schedule at a specific datetime --- bot/utils/scheduling.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index ac67278f6..f5308059a 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -2,6 +2,7 @@ import asyncio import contextlib import logging import typing as t +from datetime import datetime from functools import partial @@ -32,6 +33,18 @@ class Scheduler: self._scheduled_tasks[task_id] = task self._log.debug(f"Scheduled task #{task_id} {id(task)}.") + def schedule_at(self, time: datetime, task_id: t.Hashable, coroutine: t.Coroutine) -> None: + """ + Schedule `coroutine` to be executed at the given naïve UTC `time`. + + If `time` is in the past, schedule `coroutine` immediately. + """ + delay = (time - datetime.utcnow()).total_seconds() + if delay > 0: + coroutine = self._await_later(delay, coroutine) + + self.schedule(task_id, coroutine) + def schedule_later(self, delay: t.Union[int, float], task_id: t.Hashable, coroutine: t.Coroutine) -> None: """Schedule `coroutine` to be executed after the given `delay` number of seconds.""" self.schedule(task_id, self._await_later(delay, coroutine)) -- cgit v1.2.3 From f2f4b425dc8988ffaf9b1ebe8c2a5b449a50a48e Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 20 Jun 2020 16:25:48 -0700 Subject: Update Filtering's scheduler to the new API --- bot/cogs/filtering.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 76ea68660..099606b82 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -19,7 +19,6 @@ from bot.constants import ( ) from bot.utils.redis_cache import RedisCache from bot.utils.scheduling import Scheduler -from bot.utils.time import wait_until log = logging.getLogger(__name__) @@ -60,7 +59,7 @@ def expand_spoilers(text: str) -> str: OFFENSIVE_MSG_DELETE_TIME = timedelta(days=Filter.offensive_msg_delete_days) -class Filtering(Cog, Scheduler): +class Filtering(Cog): """Filtering out invites, blacklisting domains, and warning us of certain regular expressions.""" # Redis cache mapping a user ID to the last timestamp a bad nickname alert was sent @@ -68,8 +67,7 @@ class Filtering(Cog, Scheduler): def __init__(self, bot: Bot): self.bot = bot - super().__init__() - + self.scheduler = Scheduler(self.__class__.__name__) self.name_lock = asyncio.Lock() staff_mistake_str = "If you believe this was a mistake, please let staff know!" @@ -268,7 +266,7 @@ class Filtering(Cog, Scheduler): } await self.bot.api_client.post('bot/offensive-messages', json=data) - self.schedule_task(msg.id, data) + self.schedule_msg_delete(data) log.trace(f"Offensive message {msg.id} will be deleted on {delete_date}") if is_private: @@ -457,12 +455,10 @@ class Filtering(Cog, Scheduler): except discord.errors.Forbidden: await channel.send(f"{filtered_member.mention} {reason}") - async def _scheduled_task(self, msg: dict) -> None: + def schedule_msg_delete(self, msg: dict) -> None: """Delete an offensive message once its deletion date is reached.""" delete_at = dateutil.parser.isoparse(msg['delete_date']).replace(tzinfo=None) - - await wait_until(delete_at) - await self.delete_offensive_msg(msg) + self.scheduler.schedule_at(delete_at, msg['id'], self.delete_offensive_msg(msg)) async def reschedule_offensive_msg_deletion(self) -> None: """Get all the pending message deletion from the API and reschedule them.""" @@ -477,7 +473,7 @@ class Filtering(Cog, Scheduler): if delete_at < now: await self.delete_offensive_msg(msg) else: - self.schedule_task(msg['id'], msg) + self.schedule_msg_delete(msg) async def delete_offensive_msg(self, msg: Mapping[str, str]) -> None: """Delete an offensive message, and then delete it from the db.""" -- cgit v1.2.3 From 90f0cb34cefdc362336cfb27b2e94f8925f312f4 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 20 Jun 2020 16:42:26 -0700 Subject: Update Reminders's scheduler to the new API --- bot/cogs/reminders.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index c242d2920..0d20bdb2b 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -17,7 +17,7 @@ from bot.converters import Duration from bot.pagination import LinePaginator from bot.utils.checks import without_role_check from bot.utils.scheduling import Scheduler -from bot.utils.time import humanize_delta, wait_until +from bot.utils.time import humanize_delta log = logging.getLogger(__name__) @@ -25,12 +25,12 @@ WHITELISTED_CHANNELS = Guild.reminder_whitelist MAXIMUM_REMINDERS = 5 -class Reminders(Scheduler, Cog): +class Reminders(Cog): """Provide in-channel reminder functionality.""" def __init__(self, bot: Bot): self.bot = bot - super().__init__() + self.scheduler = Scheduler(self.__class__.__name__) self.bot.loop.create_task(self.reschedule_reminders()) @@ -56,7 +56,7 @@ class Reminders(Scheduler, Cog): late = relativedelta(now, remind_at) await self.send_reminder(reminder, late) else: - self.schedule_task(reminder["id"], reminder) + self.schedule_reminder(reminder) def ensure_valid_reminder( self, @@ -99,17 +99,18 @@ class Reminders(Scheduler, Cog): await ctx.send(embed=embed) - async def _scheduled_task(self, reminder: dict) -> None: + def schedule_reminder(self, reminder: dict) -> None: """A coroutine which sends the reminder once the time is reached, and cancels the running task.""" reminder_id = reminder["id"] reminder_datetime = isoparse(reminder['expiration']).replace(tzinfo=None) - # Send the reminder message once the desired duration has passed - await wait_until(reminder_datetime) - await self.send_reminder(reminder) + async def _remind() -> None: + await self.send_reminder(reminder) - log.debug(f"Deleting reminder {reminder_id} (the user has been reminded).") - await self._delete_reminder(reminder_id) + log.debug(f"Deleting reminder {reminder_id} (the user has been reminded).") + await self._delete_reminder(reminder_id) + + self.scheduler.schedule_at(reminder_datetime, reminder_id, _remind()) async def _delete_reminder(self, reminder_id: str, cancel_task: bool = True) -> None: """Delete a reminder from the database, given its ID, and cancel the running task.""" @@ -117,15 +118,15 @@ class Reminders(Scheduler, Cog): if cancel_task: # Now we can remove it from the schedule list - self.cancel_task(reminder_id) + self.scheduler.cancel(reminder_id) async def _reschedule_reminder(self, reminder: dict) -> None: """Reschedule a reminder object.""" log.trace(f"Cancelling old task #{reminder['id']}") - self.cancel_task(reminder["id"]) + self.scheduler.cancel(reminder["id"]) log.trace(f"Scheduling new task #{reminder['id']}") - self.schedule_task(reminder["id"], reminder) + self.schedule_reminder(reminder) async def send_reminder(self, reminder: dict, late: relativedelta = None) -> None: """Send the reminder.""" @@ -223,7 +224,7 @@ class Reminders(Scheduler, Cog): delivery_dt=expiration, ) - self.schedule_task(reminder["id"], reminder) + self.schedule_reminder(reminder) @remind_group.command(name="list") async def list_reminders(self, ctx: Context) -> t.Optional[discord.Message]: -- cgit v1.2.3 From 6c76a04dab61de0ae4ea786c97f160805640d0c5 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 20 Jun 2020 16:45:10 -0700 Subject: Update Silence's scheduler to the new API --- bot/cogs/moderation/silence.py | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index c8ab6443b..ae4fb7b64 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -1,7 +1,7 @@ import asyncio import logging from contextlib import suppress -from typing import NamedTuple, Optional +from typing import Optional from discord import TextChannel from discord.ext import commands, tasks @@ -16,13 +16,6 @@ from bot.utils.scheduling import Scheduler log = logging.getLogger(__name__) -class TaskData(NamedTuple): - """Data for a scheduled task.""" - - delay: int - ctx: Context - - class SilenceNotifier(tasks.Loop): """Loop notifier for posting notices to `alert_channel` containing added channels.""" @@ -61,25 +54,17 @@ class SilenceNotifier(tasks.Loop): await self._alert_channel.send(f"<@&{Roles.moderators}> currently silenced channels: {channels_text}") -class Silence(Scheduler, commands.Cog): +class Silence(commands.Cog): """Commands for stopping channel messages for `verified` role in a channel.""" def __init__(self, bot: Bot): - super().__init__() self.bot = bot + self.scheduler = Scheduler(self.__class__.__name__) self.muted_channels = set() + self._get_instance_vars_task = self.bot.loop.create_task(self._get_instance_vars()) self._get_instance_vars_event = asyncio.Event() - async def _scheduled_task(self, task: TaskData) -> None: - """Calls `self.unsilence` on expired silenced channel to unsilence it.""" - await asyncio.sleep(task.delay) - log.info("Unsilencing channel after set delay.") - - # Because `self.unsilence` explicitly cancels this scheduled task, it is shielded - # to avoid prematurely cancelling itself - await asyncio.shield(task.ctx.invoke(self.unsilence)) - async def _get_instance_vars(self) -> None: """Get instance variables after they're available to get from the guild.""" await self.bot.wait_until_guild_available() @@ -109,12 +94,7 @@ class Silence(Scheduler, commands.Cog): await ctx.send(f"{Emojis.check_mark} silenced current channel for {duration} minute(s).") - task_data = TaskData( - delay=duration*60, - ctx=ctx - ) - - self.schedule_task(ctx.channel.id, task_data) + self.scheduler.schedule_later(duration * 60, ctx.channel.id, ctx.invoke(self.unsilence)) @commands.command(aliases=("unhush",)) async def unsilence(self, ctx: Context) -> None: @@ -164,7 +144,7 @@ class Silence(Scheduler, commands.Cog): if current_overwrite.send_messages is False: await channel.set_permissions(self._verified_role, **dict(current_overwrite, send_messages=None)) log.info(f"Unsilenced channel #{channel} ({channel.id}).") - self.cancel_task(channel.id) + self.scheduler.cancel(channel.id) self.notifier.remove_channel(channel) self.muted_channels.discard(channel) return True -- cgit v1.2.3 From 0e69211295c6d7656b776870aa2bd8aab9244f5f Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 20 Jun 2020 16:56:14 -0700 Subject: Update HelpChannels's scheduler to the new API --- bot/cogs/help_channels.py | 70 ++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 50 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 187adfe51..93ef07c84 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -1,5 +1,4 @@ import asyncio -import inspect import json import logging import random @@ -57,14 +56,7 @@ through our guide for [asking a good question]({ASKING_GUIDE_URL}). CoroutineFunc = t.Callable[..., t.Coroutine] -class TaskData(t.NamedTuple): - """Data for a scheduled task.""" - - wait_time: int - callback: t.Awaitable - - -class HelpChannels(Scheduler, commands.Cog): +class HelpChannels(commands.Cog): """ Manage the help channel system of the guild. @@ -114,9 +106,8 @@ class HelpChannels(Scheduler, commands.Cog): claim_times = RedisCache() def __init__(self, bot: Bot): - super().__init__() - self.bot = bot + self.scheduler = Scheduler(self.__class__.__name__) # Categories self.available_category: discord.CategoryChannel = None @@ -145,7 +136,7 @@ class HelpChannels(Scheduler, commands.Cog): for task in self.queue_tasks: task.cancel() - self.cancel_all() + self.scheduler.cancel_all() def create_channel_queue(self) -> asyncio.Queue: """ @@ -229,10 +220,11 @@ class HelpChannels(Scheduler, commands.Cog): await self.remove_cooldown_role(ctx.author) # Ignore missing task when cooldown has passed but the channel still isn't dormant. - self.cancel_task(ctx.author.id, ignore_missing=True) + if ctx.author.id in self.scheduler: + self.scheduler.cancel(ctx.author.id) await self.move_to_dormant(ctx.channel, "command") - self.cancel_task(ctx.channel.id) + self.scheduler.cancel(ctx.channel.id) else: log.debug(f"{ctx.author} invoked command 'dormant' outside an in-use help channel") @@ -474,16 +466,15 @@ class HelpChannels(Scheduler, commands.Cog): else: # Cancel the existing task, if any. if has_task: - self.cancel_task(channel.id) - - data = TaskData(idle_seconds - time_elapsed, self.move_idle_channel(channel)) + self.scheduler.cancel(channel.id) + delay = idle_seconds - time_elapsed log.info( f"#{channel} ({channel.id}) is still active; " - f"scheduling it to be moved after {data.wait_time} seconds." + f"scheduling it to be moved after {delay} seconds." ) - self.schedule_task(channel.id, data) + self.scheduler.schedule_later(delay, channel.id, self.move_idle_channel(channel)) async def move_to_bottom_position(self, channel: discord.TextChannel, category_id: int, **options) -> None: """ @@ -588,8 +579,7 @@ class HelpChannels(Scheduler, commands.Cog): timeout = constants.HelpChannels.idle_minutes * 60 log.trace(f"Scheduling #{channel} ({channel.id}) to become dormant in {timeout} sec.") - data = TaskData(timeout, self.move_idle_channel(channel)) - self.schedule_task(channel.id, data) + self.scheduler.schedule_later(timeout, channel.id, self.move_idle_channel(channel)) self.report_stats() async def notify(self) -> None: @@ -722,10 +712,10 @@ class HelpChannels(Scheduler, commands.Cog): log.info(f"Claimant of #{msg.channel} ({msg.author}) deleted message, channel is empty now. Rescheduling task.") # Cancel existing dormant task before scheduling new. - self.cancel_task(msg.channel.id) + self.scheduler.cancel(msg.channel.id) - task = TaskData(constants.HelpChannels.deleted_idle_minutes * 60, self.move_idle_channel(msg.channel)) - self.schedule_task(msg.channel.id, task) + delay = constants.HelpChannels.deleted_idle_minutes * 60 + self.scheduler.schedule_later(delay, msg.channel.id, self.move_idle_channel(msg.channel)) async def is_empty(self, channel: discord.TextChannel) -> bool: """Return True if the most recent message in `channel` is the bot's `AVAILABLE_MSG`.""" @@ -752,8 +742,8 @@ class HelpChannels(Scheduler, commands.Cog): await self.remove_cooldown_role(member) else: # The member is still on a cooldown; re-schedule it for the remaining time. - remaining = cooldown - in_use_time.seconds - await self.schedule_cooldown_expiration(member, remaining) + delay = cooldown - in_use_time.seconds + self.scheduler.schedule_later(delay, member.id, self.remove_cooldown_role(member)) async def add_cooldown_role(self, member: discord.Member) -> None: """Add the help cooldown role to `member`.""" @@ -804,16 +794,11 @@ class HelpChannels(Scheduler, commands.Cog): # Cancel the existing task, if any. # Would mean the user somehow bypassed the lack of permissions (e.g. user is guild owner). - self.cancel_task(member.id, ignore_missing=True) + if member.id in self.scheduler: + self.scheduler.cancel(member.id) - await self.schedule_cooldown_expiration(member, constants.HelpChannels.claim_minutes * 60) - - async def schedule_cooldown_expiration(self, member: discord.Member, seconds: int) -> None: - """Schedule the cooldown role for `member` to be removed after a duration of `seconds`.""" - log.trace(f"Scheduling removal of {member}'s ({member.id}) cooldown.") - - callback = self.remove_cooldown_role(member) - self.schedule_task(member.id, TaskData(seconds, callback)) + delay = constants.HelpChannels.claim_minutes * 60 + self.scheduler.schedule_later(delay, member.id, self.remove_cooldown_role(member)) async def send_available_message(self, channel: discord.TextChannel) -> None: """Send the available message by editing a dormant message or sending a new message.""" @@ -855,21 +840,6 @@ class HelpChannels(Scheduler, commands.Cog): return channel - async def _scheduled_task(self, data: TaskData) -> None: - """Await the `data.callback` coroutine after waiting for `data.wait_time` seconds.""" - try: - log.trace(f"Waiting {data.wait_time} seconds before awaiting callback.") - await asyncio.sleep(data.wait_time) - - # Use asyncio.shield to prevent callback from cancelling itself. - # The parent task (_scheduled_task) will still get cancelled. - log.trace("Done waiting; now awaiting the callback.") - await asyncio.shield(data.callback) - finally: - if inspect.iscoroutine(data.callback): - log.trace("Explicitly closing coroutine.") - data.callback.close() - def validate_config() -> None: """Raise a ValueError if the cog's config is invalid.""" -- cgit v1.2.3 From 23e663d5ff992d13a7685b44f09da0f21b390b0c Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 20 Jun 2020 17:17:56 -0700 Subject: Update InfractionScheduler's scheduler to the new API --- bot/cogs/moderation/management.py | 4 ++-- bot/cogs/moderation/scheduler.py | 23 +++++++++-------------- bot/cogs/moderation/superstarify.py | 2 +- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/bot/cogs/moderation/management.py b/bot/cogs/moderation/management.py index c39c7f3bc..e87f3d7a4 100644 --- a/bot/cogs/moderation/management.py +++ b/bot/cogs/moderation/management.py @@ -135,11 +135,11 @@ class ModManagement(commands.Cog): if 'expires_at' in request_data: # A scheduled task should only exist if the old infraction wasn't permanent if old_infraction['expires_at']: - self.infractions_cog.cancel_task(new_infraction['id']) + self.infractions_cog.scheduler.cancel(new_infraction['id']) # If the infraction was not marked as permanent, schedule a new expiration task if request_data['expires_at']: - self.infractions_cog.schedule_task(new_infraction['id'], new_infraction) + self.infractions_cog.scheduler.schedule(new_infraction['id'], new_infraction) log_text += f""" Previous expiry: {old_infraction['expires_at'] or "Permanent"} diff --git a/bot/cogs/moderation/scheduler.py b/bot/cogs/moderation/scheduler.py index d75a72ddb..601e238c9 100644 --- a/bot/cogs/moderation/scheduler.py +++ b/bot/cogs/moderation/scheduler.py @@ -1,4 +1,3 @@ -import asyncio import logging import textwrap import typing as t @@ -23,13 +22,13 @@ from .utils import UserSnowflake log = logging.getLogger(__name__) -class InfractionScheduler(Scheduler): +class InfractionScheduler: """Handles the application, pardoning, and expiration of infractions.""" def __init__(self, bot: Bot, supported_infractions: t.Container[str]): - super().__init__() - self.bot = bot + self.scheduler = Scheduler(self.__class__.__name__) + self.bot.loop.create_task(self.reschedule_infractions(supported_infractions)) @property @@ -49,7 +48,7 @@ class InfractionScheduler(Scheduler): ) for infraction in infractions: if infraction["expires_at"] is not None and infraction["type"] in supported_infractions: - self.schedule_task(infraction["id"], infraction) + self.schedule_expiration(infraction) async def reapply_infraction( self, @@ -155,7 +154,7 @@ class InfractionScheduler(Scheduler): await action_coro if expiry: # Schedule the expiration of the infraction. - self.schedule_task(infraction["id"], infraction) + self.schedule_expiration(infraction) except discord.HTTPException as e: # Accordingly display that applying the infraction failed. confirm_msg = ":x: failed to apply" @@ -278,7 +277,7 @@ class InfractionScheduler(Scheduler): # Cancel pending expiration task. if infraction["expires_at"] is not None: - self.cancel_task(infraction["id"]) + self.scheduler.cancel(infraction["id"]) # Accordingly display whether the user was successfully notified via DM. dm_emoji = "" @@ -415,7 +414,7 @@ class InfractionScheduler(Scheduler): # Cancel the expiration task. if infraction["expires_at"] is not None: - self.cancel_task(infraction["id"]) + self.scheduler.cancel(infraction["id"]) # Send a log message to the mod log. if send_log: @@ -449,7 +448,7 @@ class InfractionScheduler(Scheduler): """ raise NotImplementedError - async def _scheduled_task(self, infraction: utils.Infraction) -> None: + def schedule_expiration(self, infraction: utils.Infraction) -> None: """ Marks an infraction expired after the delay from time of scheduling to time of expiration. @@ -457,8 +456,4 @@ class InfractionScheduler(Scheduler): expiration task is cancelled. """ expiry = dateutil.parser.isoparse(infraction["expires_at"]).replace(tzinfo=None) - await time.wait_until(expiry) - - # Because deactivate_infraction() explicitly cancels this scheduled task, it is shielded - # to avoid prematurely cancelling itself. - await asyncio.shield(self.deactivate_infraction(infraction)) + self.scheduler.schedule_at(expiry, infraction["id"], self.deactivate_infraction(infraction)) diff --git a/bot/cogs/moderation/superstarify.py b/bot/cogs/moderation/superstarify.py index 45a010f00..867de815a 100644 --- a/bot/cogs/moderation/superstarify.py +++ b/bot/cogs/moderation/superstarify.py @@ -146,7 +146,7 @@ class Superstarify(InfractionScheduler, Cog): log.debug(f"Changing nickname of {member} to {forced_nick}.") self.mod_log.ignore(constants.Event.member_update, member.id) await member.edit(nick=forced_nick, reason=reason) - self.schedule_task(id_, infraction) + self.schedule_expiration(infraction) # Send a DM to the user to notify them of their new infraction. await utils.notify_infraction( -- cgit v1.2.3 From 6fa8caed037b247a7c194f58a4635de7dae21fd2 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sun, 21 Jun 2020 13:51:17 +0200 Subject: Incidents: implement `make_username` helper The justification is to incorporate the `actioned_by` name into the username in some way, and so the logical thing to do is to abstract this process into a helper so that it can easily be adjusted in the future. For now, I've chosen to separate the names by a pipe. Discord webhook username cannot exceed 80 characters in length, and so we cap it at this length by default. This is seen as more of an edge-case, but it should be accounted for, as we're not joining two names. The `max_length` param is configurable primarily for testing purposes, it probably should never be passed explicitly. This commit also provides two tests for the function. --- bot/cogs/moderation/incidents.py | 24 ++++++++++++++++++++++++ tests/bot/cogs/moderation/test_incidents.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 040f2c0c8..2cce9b6fe 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -41,6 +41,30 @@ ALLOWED_ROLES: t.Set[int] = {Roles.moderators, Roles.admins, Roles.owners} ALL_SIGNALS: t.Set[str] = {signal.value for signal in Signal} +def make_username(reported_by: discord.Member, actioned_by: discord.Member, max_length: int = 80) -> str: + """ + Create a webhook-friendly username from the names of `reported_by` and `actioned_by`. + + If the resulting username length exceeds `max_length`, it will be capped at `max_length - 3` + and have 3 dots appended to the end. The default value is 80, which corresponds to the limit + Discord imposes on webhook username length. + + If the value of `max_length` is < 3, ValueError is raised. + """ + if max_length < 3: + raise ValueError(f"Maximum length cannot be less than 3: {max_length=}") + + username = f"{reported_by.name} | {actioned_by.name}" + log.trace(f"Generated webhook username: {username} (length: {len(username)})") + + if len(username) > max_length: + stop = max_length - 3 + username = f"{username[:stop]}..." + log.trace(f"Username capped at {max_length=}: {username}") + + return username + + def is_incident(message: discord.Message) -> bool: """True if `message` qualifies as an incident, False otherwise.""" conditions = ( diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 2fc9180cf..5700a5a35 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -68,6 +68,35 @@ mock_404 = discord.NotFound( ) +class TestMakeUsername(unittest.TestCase): + """Collection of tests for the `make_username` helper function.""" + + def test_make_username_raises(self): + """Raises `ValueError` on `max_length` < 3.""" + with self.assertRaises(ValueError): + incidents.make_username(MockMember(), MockMember(), max_length=2) + + def test_make_username_never_exceed_limit(self): + """ + The return string length is always less than or equal to `max_length`. + + For this test we pass `max_length=10` for convenience. The name of the first + user (`reported_by`) is always 1 character in length, but we generate names + for the `actioned_by` user starting at length 1 and up to length 20. + + Finally, we assert that the output length never exceeded 10 in total. + """ + user_a = MockMember(name="A") + + max_length = 10 + test_cases = (MockMember(name="B" * n) for n in range(1, 20)) + + for user_b in test_cases: + with self.subTest(user_a=user_a, user_b=user_b, max_length=max_length): + generated_username = incidents.make_username(user_a, user_b, max_length) + self.assertLessEqual(len(generated_username), max_length) + + @patch("bot.constants.Channels.incidents", 123) class TestIsIncident(unittest.TestCase): """ -- cgit v1.2.3 From a8d179d9b04f54b20c5e870bcfa85c78c42c8dca Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sun, 21 Jun 2020 14:21:18 +0200 Subject: Incidents: append `actioned_by` to webhook username Incident author and the moderator who actioned report are now passed through `make_username` to create the webhook username. Tests adjusted as appropriate. --- bot/cogs/moderation/incidents.py | 9 +++++---- tests/bot/cogs/moderation/test_incidents.py | 23 +++++++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 2cce9b6fe..72cc4b26c 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -172,13 +172,14 @@ class Incidents(Cog): log.debug("Crawl task finished!") - async def archive(self, incident: discord.Message, outcome: Signal) -> bool: + async def archive(self, incident: discord.Message, outcome: Signal, actioned_by: discord.Member) -> bool: """ Relay `incident` to the #incidents-archive channel. The following pieces of information are relayed: * Incident message content (clean, pingless) - * Incident author name (as webhook author) + * Incident author name (as webhook username) + * Name of user who actioned the incident (appended to webhook username) * Incident author avatar (as webhook avatar) * Resolution signal (`outcome`) @@ -194,7 +195,7 @@ class Incidents(Cog): # Now relay the incident message: discord.Message = await webhook.send( content=incident.clean_content, # Clean content will prevent mentions from pinging - username=sub_clyde(incident.author.name), + username=sub_clyde(make_username(incident.author, actioned_by)), avatar_url=incident.author.avatar_url, wait=True, # This makes the method return the sent Message object ) @@ -259,7 +260,7 @@ class Incidents(Cog): log.debug("Reaction was valid, but no action is currently defined for it") return - relay_successful = await self.archive(incident, signal) + relay_successful = await self.archive(incident, signal, actioned_by=member) if not relay_successful: log.trace("Original message will not be deleted as we failed to relay it to the archive") return diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 5700a5a35..a811868e5 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -307,7 +307,9 @@ class TestArchive(TestIncidents): propagate out of the method, which is just as important. """ self.cog_instance.bot.fetch_webhook = AsyncMock(side_effect=mock_404) - self.assertFalse(await self.cog_instance.archive(incident=MockMessage(), outcome=MagicMock())) + + result = await self.cog_instance.archive(incident=MockMessage(), outcome=MagicMock(), actioned_by=MockMember()) + self.assertFalse(result) async def test_archive_relays_incident(self): """ @@ -332,12 +334,18 @@ class TestArchive(TestIncidents): author=MockUser(name="author_name", avatar_url="author_avatar"), id=123, ) - archive_return = await self.cog_instance.archive(incident, outcome=MagicMock(value="A")) + + with patch("bot.cogs.moderation.incidents.make_username", MagicMock(return_value="generated_username")): + archive_return = await self.cog_instance.archive( + incident=incident, + outcome=MagicMock(value="A"), + actioned_by=MockMember(name="moderator"), + ) # Check that the webhook was dispatched correctly webhook.send.assert_called_once_with( content="pingless message", - username="author_name", + username="generated_username", avatar_url="author_avatar", wait=True, ) @@ -354,7 +362,8 @@ class TestArchive(TestIncidents): Discord will reject any webhook with "clyde" in the username field, as it impersonates the official Clyde bot. Since we do not control what the username will be (the incident - author name is used), we must ensure the name is cleansed, otherwise the relay may fail. + author name, and actioning moderator names are used), we must ensure the name is cleansed, + otherwise the relay may fail. This test assumes the username is passed as a kwarg. If this test fails, please review whether the passed argument is being retrieved correctly. @@ -362,9 +371,11 @@ class TestArchive(TestIncidents): webhook = MockAsyncWebhook() self.cog_instance.bot.fetch_webhook = AsyncMock(return_value=webhook) - message_from_clyde = MockMessage(author=MockUser(name="clyde the great")) - await self.cog_instance.archive(message_from_clyde, MagicMock(incidents.Signal)) + # The `make_username` helper will return a string with "clyde" in it + with patch("bot.cogs.moderation.incidents.make_username", MagicMock(return_value="clyde the great")): + await self.cog_instance.archive(MockMessage(), MagicMock(incidents.Signal), MockMember()) + # Assert that the "clyde" was never passed to `send` self.assertNotIn("clyde", webhook.send.call_args.kwargs["username"]) -- cgit v1.2.3 From 58d20203870f293de9410db4bf0e602696d04c2c Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 23 Jun 2020 23:52:50 -0700 Subject: Scheduler: close coroutine if task ID already exists This prevents unawaited coroutine warnings. --- bot/utils/scheduling.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index f5308059a..4e99db76c 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -20,11 +20,17 @@ class Scheduler: return task_id in self._scheduled_tasks def schedule(self, task_id: t.Hashable, coroutine: t.Coroutine) -> None: - """Schedule the execution of a coroutine.""" + """ + Schedule the execution of a coroutine. + + If a task with `task_id` already exists, close `coroutine` instead of scheduling it. + This prevents unawaited coroutine warnings. + """ self._log.trace(f"Scheduling task #{task_id}...") if task_id in self._scheduled_tasks: self._log.debug(f"Did not schedule task #{task_id}; task was already scheduled.") + coroutine.close() return task = asyncio.create_task(coroutine, name=f"{self.name}_{task_id}") -- cgit v1.2.3 From bc6817536a7db4242cfa725ce809ced45f7cb556 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 24 Jun 2020 16:46:14 -0700 Subject: Scheduler: remove duplicate dict delete The task is already popped from the dict, so there is no need to delete it afterwards. --- bot/utils/scheduling.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 4e99db76c..4110598d5 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -64,7 +64,6 @@ class Scheduler: except KeyError: self._log.warning(f"Failed to unschedule {task_id} (no task found).") else: - del self._scheduled_tasks[task_id] task.cancel() self._log.debug(f"Unscheduled task #{task_id} {id(task)}.") -- cgit v1.2.3 From e09307e0f8f570279271c99525e0cde6cfa84d5b Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Thu, 25 Jun 2020 11:51:19 -0700 Subject: Scheduler: only close unawaited coroutines The coroutine may cancel the scheduled task, which would also trigger the finally block. The coroutine isn't necessarily finished when it cancels the task, so it shouldn't be closed in this case. --- bot/utils/scheduling.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 4110598d5..cf2a1f110 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -1,5 +1,6 @@ import asyncio import contextlib +import inspect import logging import typing as t from datetime import datetime @@ -87,8 +88,11 @@ class Scheduler: finally: # Close it to prevent unawaited coroutine warnings, # which would happen if the task was cancelled during the sleep. - self._log.trace("Explicitly closing the coroutine.") - coroutine.close() + # Only close it if it's not been awaited yet. This check is important because the + # coroutine may cancel this task, which would also trigger the finally block. + if inspect.getcoroutinestate(coroutine) == "CORO_CREATED": + self._log.trace("Explicitly closing the coroutine.") + coroutine.close() def _task_done_callback(self, task_id: t.Hashable, done_task: asyncio.Task) -> None: """ -- cgit v1.2.3 From 98bbae201af3a125663025901eb8586914e99df6 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Thu, 25 Jun 2020 23:45:50 -0400 Subject: Account for spaces in LinePaginator._split_remaining_lines() --- bot/pagination.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/pagination.py b/bot/pagination.py index 30e74b1b1..e41f9a521 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -135,7 +135,7 @@ class LinePaginator(Paginator): if not is_full: if len(word) + reduced_char_count <= max_chars: reduced_words.append(word) - reduced_char_count += len(word) + reduced_char_count += len(word) + 1 else: is_full = True remaining_words.append(word) -- cgit v1.2.3 From be4ce9ee81a0487e9e2417bc952505a3db81fec6 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Fri, 26 Jun 2020 01:52:15 -0400 Subject: Fix LinePaginator new page creation --- bot/pagination.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/bot/pagination.py b/bot/pagination.py index e41f9a521..be3f82343 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -62,7 +62,7 @@ class LinePaginator(Paginator): if scale_to_size < max_size: raise ValueError("scale_to_size must be >= max_size.") - self.scale_to_size = scale_to_size + self.scale_to_size = scale_to_size - len(suffix) self.max_lines = max_lines self._current_page = [prefix] self._linecount = 0 @@ -94,14 +94,14 @@ class LinePaginator(Paginator): raise RuntimeError(f'Line exceeds maximum scale_to_size {self.scale_to_size}' ' and could not be split.') - if self.max_lines is not None: - if self._linecount >= self.max_lines: - self._linecount = 0 - self.close_page() + if self.max_lines is not None and self._linecount >= self.max_lines: + log.debug("max_lines exceeded, creating new page.") + self._new_page() + elif self._count + len(line) + 1 > self.max_size and self._linecount > 0: + log.debug("max_size exceeded on page with lines, creating new page.") + self._new_page() - self._linecount += 1 - if self._count + len(line) + 1 > self.max_size: - self.close_page() + self._linecount += 1 self._count += len(line) + 1 self._current_page.append(line) @@ -111,8 +111,14 @@ class LinePaginator(Paginator): self._count += 1 if remaining_words: + self._new_page() self.add_line(remaining_words) + def _new_page(self) -> None: + self._linecount = 0 + self._count = len(self.prefix) + 1 + self.close_page() + def _split_remaining_words(self, line: str, max_chars: int) -> t.Tuple[str, t.Optional[str]]: """ Internal: split a line into two strings -- reduced_words and remaining_words. -- cgit v1.2.3 From 7cb56d44eb2b6db3e0e20c9b8277b00d9aa4ce3a Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Fri, 26 Jun 2020 02:00:27 -0400 Subject: Simplify LinePaginator continuation header --- bot/pagination.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bot/pagination.py b/bot/pagination.py index be3f82343..230cc5add 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -132,8 +132,9 @@ class LinePaginator(Paginator): Return a tuple in the format (reduced_words, remaining_words). """ reduced_words = [] + remaining_words = [] # "(Continued)" is used on a line by itself to indicate the continuation of last page - remaining_words = ["(Continued)\n", "---------------\n"] + continuation_header = "(Continued)\n-----------\n" reduced_char_count = 0 is_full = False @@ -147,9 +148,11 @@ class LinePaginator(Paginator): remaining_words.append(word) else: remaining_words.append(word) - - return " ".join(reduced_words), " ".join(remaining_words) if len(remaining_words) > 2 \ - else None + + return ( + " ".join(reduced_words), + continuation_header + " ".join(remaining_words) if remaining_words else None + ) @classmethod async def paginate( -- cgit v1.2.3 From 195e0f9407d2a8b7ac5b3028b4f10c1b73af0a4f Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Fri, 26 Jun 2020 02:08:48 -0400 Subject: Update LinePaginator.add_line() tests --- tests/bot/test_pagination.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/bot/test_pagination.py b/tests/bot/test_pagination.py index f2e2c27ce..74896f010 100644 --- a/tests/bot/test_pagination.py +++ b/tests/bot/test_pagination.py @@ -18,18 +18,18 @@ class LinePaginatorTests(TestCase): self.assertEqual(len(self.paginator._pages), 0) def test_add_line_works_on_long_lines(self): - """`add_line` should scale long lines up to `scale_to_size`.""" - self.paginator.add_line('x' * self.paginator.scale_to_size) - self.assertEqual(len(self.paginator._pages), 1) + """After additional lines after `max_size` is exceeded should go on the next page.""" + self.paginator.add_line('x' * self.paginator.max_size) + self.assertEqual(len(self.paginator._pages), 0) # Any additional lines should start a new page after `max_size` is exceeded. self.paginator.add_line('x') - self.assertEqual(len(self.paginator._pages), 2) + self.assertEqual(len(self.paginator._pages), 1) def test_add_line_continuation(self): """When `scale_to_size` is exceeded, remaining words should be split onto the next page.""" self.paginator.add_line('zyz ' * (self.paginator.scale_to_size//4 + 1)) - self.assertEqual(len(self.paginator._pages), 2) + self.assertEqual(len(self.paginator._pages), 1) def test_add_line_no_continuation(self): """If adding a new line to an existing page would exceed `max_size`, it should start a new -- cgit v1.2.3 From b204339e4301592a475296af2629c7a986c74148 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Fri, 26 Jun 2020 02:23:03 -0400 Subject: Correctly pass scale_to_size in LinePaginator.paginate() --- bot/pagination.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/pagination.py b/bot/pagination.py index 230cc5add..746ec3696 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -210,7 +210,8 @@ class LinePaginator(Paginator): )) ) - paginator = cls(prefix=prefix, suffix=suffix, max_size=max_size, max_lines=max_lines) + paginator = cls(prefix=prefix, suffix=suffix, max_size=max_size, max_lines=max_lines, + scale_to_size=scale_to_size) current_page = 0 if not lines: -- cgit v1.2.3 From 77ce4c88695ca748059a7076de88d5b42b37d5f5 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Fri, 26 Jun 2020 03:22:30 -0400 Subject: In LinePaginator, truncate words that exceed scale_to_size --- bot/pagination.py | 11 ++++++----- tests/bot/test_pagination.py | 12 +++++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/bot/pagination.py b/bot/pagination.py index 746ec3696..cd602c715 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -88,11 +88,9 @@ class LinePaginator(Paginator): if len(line) > (max_chars := self.max_size - len(self.prefix) - 2): if len(line) > self.scale_to_size: line, remaining_words = self._split_remaining_words(line, max_chars) - # If line still exceeds scale_to_size, we were unable to split into a second - # page without truncating. if len(line) > self.scale_to_size: - raise RuntimeError(f'Line exceeds maximum scale_to_size {self.scale_to_size}' - ' and could not be split.') + log.debug("Could not continue to next page, truncating line.") + line = line[:self.scale_to_size] if self.max_lines is not None and self._linecount >= self.max_lines: log.debug("max_lines exceeded, creating new page.") @@ -144,11 +142,14 @@ class LinePaginator(Paginator): reduced_words.append(word) reduced_char_count += len(word) + 1 else: + # If reduced_words is empty, we were unable to split the words across pages + if not reduced_words: + return line, None is_full = True remaining_words.append(word) else: remaining_words.append(word) - + return ( " ".join(reduced_words), continuation_header + " ".join(remaining_words) if remaining_words else None diff --git a/tests/bot/test_pagination.py b/tests/bot/test_pagination.py index 74896f010..ce880d457 100644 --- a/tests/bot/test_pagination.py +++ b/tests/bot/test_pagination.py @@ -39,13 +39,11 @@ class LinePaginatorTests(TestCase): self.paginator.add_line('z') self.assertEqual(len(self.paginator._pages), 1) - def test_add_line_raises_on_very_long_words(self): - """`add_line` should raise if a single long word is added that exceeds `scale_to_size`. - - Note: truncation is also a potential option, but this should not occur from normal usage. - """ - with self.assertRaises(RuntimeError): - self.paginator.add_line('x' * (self.paginator.scale_to_size + 1)) + def test_add_line_truncates_very_long_words(self): + """`add_line` should truncate if a single long word exceeds `scale_to_size`.""" + self.paginator.add_line('x' * (self.paginator.scale_to_size + 1)) + # Note: item at index 1 is the truncated line, index 0 is prefix + self.assertEqual(self.paginator._current_page[1], 'x' * self.paginator.scale_to_size) class ImagePaginatorTests(TestCase): -- cgit v1.2.3 From 73b1720f299b517450069ce11f5b29e740301eb0 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Fri, 26 Jun 2020 04:31:28 -0400 Subject: Improve LinePaginator.__init__() ValueError message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Leon Sandøy --- bot/pagination.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/pagination.py b/bot/pagination.py index cd602c715..ef1c9a176 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -60,7 +60,7 @@ class LinePaginator(Paginator): self.suffix = suffix self.max_size = max_size - len(suffix) if scale_to_size < max_size: - raise ValueError("scale_to_size must be >= max_size.") + raise ValueError(f"scale_to_size must be >= max_size. ({scale_to_size} < {max_size})") self.scale_to_size = scale_to_size - len(suffix) self.max_lines = max_lines -- cgit v1.2.3 From 1a2325754cc511b7ff500dcd74cc5703f2359927 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Fri, 26 Jun 2020 04:42:15 -0400 Subject: Add space before comment in LinePaginator._split_remaining_words() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Leon Sandøy --- bot/pagination.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/pagination.py b/bot/pagination.py index ef1c9a176..632e54873 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -131,6 +131,7 @@ class LinePaginator(Paginator): """ reduced_words = [] remaining_words = [] + # "(Continued)" is used on a line by itself to indicate the continuation of last page continuation_header = "(Continued)\n-----------\n" reduced_char_count = 0 -- cgit v1.2.3 From be809454cab8343ce8df8de30689481b9c90998d Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Sat, 27 Jun 2020 21:12:32 -0400 Subject: Improve LinePaginator docstrings --- bot/pagination.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/bot/pagination.py b/bot/pagination.py index 632e54873..71e385020 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -73,12 +73,16 @@ class LinePaginator(Paginator): """ Adds a line to the current page. - If the line exceeds `self.max_size`, then `self.max_size` will go up to `scale_to_size` for - a single line before creating a new page. If it is still exceeded, the excess characters - are stored and placed on the next pages until there are none remaining (by word boundary). + If a line on a page exceeds `max_size` characters, then `max_size` will go up to + `scale_to_size` for a single line before creating a new page for the overflow words. If it + is still exceeded, the excess characters are stored and placed on the next pages unti + there are none remaining (by word boundary). The line is truncated if `scale_to_size` is + still exceeded after attempting to continue onto the next page. - Raises a RuntimeError if `self.max_size` is still exceeded after attempting to continue - onto the next page. + In the case that the page already contains one or more lines and the new lines would cause + `max_size` to be exceeded, a new page is created. This is done in order to make a best + effort to avoid breaking up single lines across pages, but to keep the total length of the + page at a reasonable size. This function overrides the `Paginator.add_line` from inside `discord.ext.commands`. @@ -113,6 +117,12 @@ class LinePaginator(Paginator): self.add_line(remaining_words) def _new_page(self) -> None: + """ + Internal: start a new page for the paginator. + + This closes the current page and resets the counters for the new page's line count and + character count. + """ self._linecount = 0 self._count = len(self.prefix) + 1 self.close_page() -- cgit v1.2.3 From 0e223f7f197419ec99d6a00996c5d2c980c57c38 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Sat, 27 Jun 2020 21:19:49 -0400 Subject: Add block comments to LinePaginator.add_line() --- bot/pagination.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bot/pagination.py b/bot/pagination.py index 71e385020..441a63a7b 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -96,6 +96,7 @@ class LinePaginator(Paginator): log.debug("Could not continue to next page, truncating line.") line = line[:self.scale_to_size] + # Check if we should start a new page or continue the line on the current one if self.max_lines is not None and self._linecount >= self.max_lines: log.debug("max_lines exceeded, creating new page.") self._new_page() @@ -112,6 +113,7 @@ class LinePaginator(Paginator): self._current_page.append('') self._count += 1 + # Start a new page if there were any overflow words if remaining_words: self._new_page() self.add_line(remaining_words) -- cgit v1.2.3 From e1def9b0704674b94fbceb9f180f535a53952630 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Sat, 27 Jun 2020 21:50:05 -0400 Subject: In LinePaginator, use ellipses to show line continuation --- bot/pagination.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/pagination.py b/bot/pagination.py index 441a63a7b..34ce7317b 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -139,6 +139,10 @@ class LinePaginator(Paginator): remaining_words: the words in `line` which exceed `max_chars`. This value is None if no words could be split from `line`. + If there are any remaining_words, an ellipses is appended to reduced_words and a + continuation header is inserted before remaining_words to visually communicate the line + continuation. + Return a tuple in the format (reduced_words, remaining_words). """ reduced_words = [] @@ -164,7 +168,7 @@ class LinePaginator(Paginator): remaining_words.append(word) return ( - " ".join(reduced_words), + " ".join(reduced_words) + "..." if remaining_words else "", continuation_header + " ".join(remaining_words) if remaining_words else None ) -- cgit v1.2.3 From 7a8de415255ef0e504982bb4c74976aeeba52c71 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Sat, 27 Jun 2020 21:51:39 -0400 Subject: Remove shortening of nomination reasons * Since LinePaginator now supports long lines, there's no need to shorten the nomination reason to 200 characters. --- bot/cogs/watchchannels/talentpool.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/cogs/watchchannels/talentpool.py b/bot/cogs/watchchannels/talentpool.py index 14547105f..33550f68e 100644 --- a/bot/cogs/watchchannels/talentpool.py +++ b/bot/cogs/watchchannels/talentpool.py @@ -224,7 +224,7 @@ class TalentPool(WatchChannel, Cog, name="Talentpool"): Status: **Active** Date: {start_date} Actor: {actor.mention if actor else actor_id} - Reason: {textwrap.shorten(nomination_object["reason"], width=200, placeholder="...")} + Reason: {nomination_object["reason"]} Nomination ID: `{nomination_object["id"]}` =============== """ @@ -237,10 +237,10 @@ class TalentPool(WatchChannel, Cog, name="Talentpool"): Status: Inactive Date: {start_date} Actor: {actor.mention if actor else actor_id} - Reason: {textwrap.shorten(nomination_object["reason"], width=200, placeholder="...")} + Reason: {nomination_object["reason"]} End date: {end_date} - Unwatch reason: {textwrap.shorten(nomination_object["end_reason"], width=200, placeholder="...")} + Unwatch reason: {nomination_object["end_reason"]} Nomination ID: `{nomination_object["id"]}` =============== """ -- cgit v1.2.3 From 145af384ededb05ad1e2e11733d3aa53495312fb Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Sun, 28 Jun 2020 00:24:54 -0400 Subject: In LinePaginator, add limit of 2000 for max_size and scale_to_size args --- bot/cogs/help.py | 2 +- bot/pagination.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/bot/cogs/help.py b/bot/cogs/help.py index 542f19139..f59d30c9a 100644 --- a/bot/cogs/help.py +++ b/bot/cogs/help.py @@ -299,7 +299,7 @@ class CustomHelpCommand(HelpCommand): embed, prefix=description, max_lines=COMMANDS_PER_PAGE, - max_size=2040, + max_size=2000, ) async def send_bot_help(self, mapping: dict) -> None: diff --git a/bot/pagination.py b/bot/pagination.py index 34ce7317b..97ef08ad6 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -58,10 +58,20 @@ class LinePaginator(Paginator): """ self.prefix = prefix self.suffix = suffix + + # Embeds that exceed 2048 characters will result in an HTTPException + # (Discord API limit), so we've set a limit of 2000 + if max_size > 2000: + raise ValueError(f"max_size must be <= 2,000 characters. ({max_size} > 2000)") + self.max_size = max_size - len(suffix) + if scale_to_size < max_size: raise ValueError(f"scale_to_size must be >= max_size. ({scale_to_size} < {max_size})") + if scale_to_size > 2000: + raise ValueError(f"max_size must be <= 2,000 characters. ({scale_to_size} > 2000)") + self.scale_to_size = scale_to_size - len(suffix) self.max_lines = max_lines self._current_page = [prefix] -- cgit v1.2.3 From 20872a5f93fe3734ed4a84f8e1fe3d45bebb9181 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Sun, 28 Jun 2020 00:26:53 -0400 Subject: Fix grammar in LinePaginator.add_lines() docstring --- bot/pagination.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/pagination.py b/bot/pagination.py index 97ef08ad6..b047cf5fb 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -91,7 +91,7 @@ class LinePaginator(Paginator): In the case that the page already contains one or more lines and the new lines would cause `max_size` to be exceeded, a new page is created. This is done in order to make a best - effort to avoid breaking up single lines across pages, but to keep the total length of the + effort to avoid breaking up single lines across pages, while keeping the total length of the page at a reasonable size. This function overrides the `Paginator.add_line` from inside `discord.ext.commands`. -- cgit v1.2.3 From 3fd39e84a3f8d86839ed17766a7e7b2d72ed6074 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Sun, 28 Jun 2020 00:36:51 -0400 Subject: Lower LinePaginator max_size arg in CustomHelpCommand.send_bot_help --- bot/cogs/help.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/help.py b/bot/cogs/help.py index f59d30c9a..832f6ea6b 100644 --- a/bot/cogs/help.py +++ b/bot/cogs/help.py @@ -346,7 +346,7 @@ class CustomHelpCommand(HelpCommand): # add any remaining command help that didn't get added in the last iteration above. pages.append(page) - await LinePaginator.paginate(pages, self.context, embed=embed, max_lines=1, max_size=2040) + await LinePaginator.paginate(pages, self.context, embed=embed, max_lines=1, max_size=2000) class Help(Cog): -- cgit v1.2.3 From b3ba0b59940559881bc39ef39818a934753ff1c3 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Sun, 28 Jun 2020 01:11:10 -0400 Subject: In LinePaginator.__init__(), fix scale_to_size ValueError message Co-authored-by: Mark --- bot/pagination.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/pagination.py b/bot/pagination.py index b047cf5fb..94c2d7c0c 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -70,7 +70,7 @@ class LinePaginator(Paginator): raise ValueError(f"scale_to_size must be >= max_size. ({scale_to_size} < {max_size})") if scale_to_size > 2000: - raise ValueError(f"max_size must be <= 2,000 characters. ({scale_to_size} > 2000)") + raise ValueError(f"scale_to_size must be <= 2,000 characters. ({scale_to_size} > 2000)") self.scale_to_size = scale_to_size - len(suffix) self.max_lines = max_lines -- cgit v1.2.3 From 4fd2ff500cd889c1086334e82f695857689ae328 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 29 Jun 2020 19:11:52 -0700 Subject: Scheduler: add details to class docstring --- bot/utils/scheduling.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index cf2a1f110..fc453f19e 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -8,7 +8,17 @@ from functools import partial class Scheduler: - """Task scheduler.""" + """ + Schedule the execution of coroutines and keep track of them. + + Coroutines can be scheduled immediately with `schedule` or in the future with `schedule_at` + or `schedule_later`. A unique ID is required to be given in order to keep track of the + resulting Tasks. Any scheduled task can be cancelled prematurely using `cancel` by providing + the same ID used to schedule it. The `in` operator is supported for checking if a task with a + given ID is currently scheduled. + + Any exception raised in a scheduled task is logged when the task is done. + """ def __init__(self, name: str): self.name = name -- cgit v1.2.3 From c641f7fbbebd4c4c18539c32eb3d3907c8e71dee Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 29 Jun 2020 19:15:43 -0700 Subject: Scheduler: explain the name param in the docstring --- bot/utils/scheduling.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index fc453f19e..0987c5de8 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -11,6 +11,10 @@ class Scheduler: """ Schedule the execution of coroutines and keep track of them. + When instantiating a Scheduler, a name must be provided. This name is used to distinguish the + instance's log messages from other instances. Using the name of the class or module containing + the instance is suggested. + Coroutines can be scheduled immediately with `schedule` or in the future with `schedule_at` or `schedule_later`. A unique ID is required to be given in order to keep track of the resulting Tasks. Any scheduled task can be cancelled prematurely using `cancel` by providing -- cgit v1.2.3 From be4a61fb70c485262d36ca2aabf992f3118abcff Mon Sep 17 00:00:00 2001 From: kwzrd Date: Tue, 30 Jun 2020 23:09:00 +0200 Subject: Incidents: revert latest 2 commits Decision was made to use embeds to archive incidents instead of webhooking the raw message. As such, we're reverting the branch to a state from which the adjustments will be easier to make. Reverted commits: * a8d179d9b04f54b20c5e870bcfa85c78c42c8dca * 6fa8caed037b247a7c194f58a4635de7dae21fd2 --- bot/cogs/moderation/incidents.py | 33 +++--------------- tests/bot/cogs/moderation/test_incidents.py | 52 ++++------------------------- 2 files changed, 10 insertions(+), 75 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 72cc4b26c..040f2c0c8 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -41,30 +41,6 @@ ALLOWED_ROLES: t.Set[int] = {Roles.moderators, Roles.admins, Roles.owners} ALL_SIGNALS: t.Set[str] = {signal.value for signal in Signal} -def make_username(reported_by: discord.Member, actioned_by: discord.Member, max_length: int = 80) -> str: - """ - Create a webhook-friendly username from the names of `reported_by` and `actioned_by`. - - If the resulting username length exceeds `max_length`, it will be capped at `max_length - 3` - and have 3 dots appended to the end. The default value is 80, which corresponds to the limit - Discord imposes on webhook username length. - - If the value of `max_length` is < 3, ValueError is raised. - """ - if max_length < 3: - raise ValueError(f"Maximum length cannot be less than 3: {max_length=}") - - username = f"{reported_by.name} | {actioned_by.name}" - log.trace(f"Generated webhook username: {username} (length: {len(username)})") - - if len(username) > max_length: - stop = max_length - 3 - username = f"{username[:stop]}..." - log.trace(f"Username capped at {max_length=}: {username}") - - return username - - def is_incident(message: discord.Message) -> bool: """True if `message` qualifies as an incident, False otherwise.""" conditions = ( @@ -172,14 +148,13 @@ class Incidents(Cog): log.debug("Crawl task finished!") - async def archive(self, incident: discord.Message, outcome: Signal, actioned_by: discord.Member) -> bool: + async def archive(self, incident: discord.Message, outcome: Signal) -> bool: """ Relay `incident` to the #incidents-archive channel. The following pieces of information are relayed: * Incident message content (clean, pingless) - * Incident author name (as webhook username) - * Name of user who actioned the incident (appended to webhook username) + * Incident author name (as webhook author) * Incident author avatar (as webhook avatar) * Resolution signal (`outcome`) @@ -195,7 +170,7 @@ class Incidents(Cog): # Now relay the incident message: discord.Message = await webhook.send( content=incident.clean_content, # Clean content will prevent mentions from pinging - username=sub_clyde(make_username(incident.author, actioned_by)), + username=sub_clyde(incident.author.name), avatar_url=incident.author.avatar_url, wait=True, # This makes the method return the sent Message object ) @@ -260,7 +235,7 @@ class Incidents(Cog): log.debug("Reaction was valid, but no action is currently defined for it") return - relay_successful = await self.archive(incident, signal, actioned_by=member) + relay_successful = await self.archive(incident, signal) if not relay_successful: log.trace("Original message will not be deleted as we failed to relay it to the archive") return diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index a811868e5..2fc9180cf 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -68,35 +68,6 @@ mock_404 = discord.NotFound( ) -class TestMakeUsername(unittest.TestCase): - """Collection of tests for the `make_username` helper function.""" - - def test_make_username_raises(self): - """Raises `ValueError` on `max_length` < 3.""" - with self.assertRaises(ValueError): - incidents.make_username(MockMember(), MockMember(), max_length=2) - - def test_make_username_never_exceed_limit(self): - """ - The return string length is always less than or equal to `max_length`. - - For this test we pass `max_length=10` for convenience. The name of the first - user (`reported_by`) is always 1 character in length, but we generate names - for the `actioned_by` user starting at length 1 and up to length 20. - - Finally, we assert that the output length never exceeded 10 in total. - """ - user_a = MockMember(name="A") - - max_length = 10 - test_cases = (MockMember(name="B" * n) for n in range(1, 20)) - - for user_b in test_cases: - with self.subTest(user_a=user_a, user_b=user_b, max_length=max_length): - generated_username = incidents.make_username(user_a, user_b, max_length) - self.assertLessEqual(len(generated_username), max_length) - - @patch("bot.constants.Channels.incidents", 123) class TestIsIncident(unittest.TestCase): """ @@ -307,9 +278,7 @@ class TestArchive(TestIncidents): propagate out of the method, which is just as important. """ self.cog_instance.bot.fetch_webhook = AsyncMock(side_effect=mock_404) - - result = await self.cog_instance.archive(incident=MockMessage(), outcome=MagicMock(), actioned_by=MockMember()) - self.assertFalse(result) + self.assertFalse(await self.cog_instance.archive(incident=MockMessage(), outcome=MagicMock())) async def test_archive_relays_incident(self): """ @@ -334,18 +303,12 @@ class TestArchive(TestIncidents): author=MockUser(name="author_name", avatar_url="author_avatar"), id=123, ) - - with patch("bot.cogs.moderation.incidents.make_username", MagicMock(return_value="generated_username")): - archive_return = await self.cog_instance.archive( - incident=incident, - outcome=MagicMock(value="A"), - actioned_by=MockMember(name="moderator"), - ) + archive_return = await self.cog_instance.archive(incident, outcome=MagicMock(value="A")) # Check that the webhook was dispatched correctly webhook.send.assert_called_once_with( content="pingless message", - username="generated_username", + username="author_name", avatar_url="author_avatar", wait=True, ) @@ -362,8 +325,7 @@ class TestArchive(TestIncidents): Discord will reject any webhook with "clyde" in the username field, as it impersonates the official Clyde bot. Since we do not control what the username will be (the incident - author name, and actioning moderator names are used), we must ensure the name is cleansed, - otherwise the relay may fail. + author name is used), we must ensure the name is cleansed, otherwise the relay may fail. This test assumes the username is passed as a kwarg. If this test fails, please review whether the passed argument is being retrieved correctly. @@ -371,11 +333,9 @@ class TestArchive(TestIncidents): webhook = MockAsyncWebhook() self.cog_instance.bot.fetch_webhook = AsyncMock(return_value=webhook) - # The `make_username` helper will return a string with "clyde" in it - with patch("bot.cogs.moderation.incidents.make_username", MagicMock(return_value="clyde the great")): - await self.cog_instance.archive(MockMessage(), MagicMock(incidents.Signal), MockMember()) + message_from_clyde = MockMessage(author=MockUser(name="clyde the great")) + await self.cog_instance.archive(message_from_clyde, MagicMock(incidents.Signal)) - # Assert that the "clyde" was never passed to `send` self.assertNotIn("clyde", webhook.send.call_args.kwargs["username"]) -- cgit v1.2.3 From 968251660768297383401576902a71f8ac9edada Mon Sep 17 00:00:00 2001 From: kwzrd Date: Tue, 30 Jun 2020 23:15:02 +0200 Subject: Incidents: pass `actioned_by` to `archive` This is an important piece of information that shall be relayed. --- bot/cogs/moderation/incidents.py | 4 ++-- tests/bot/cogs/moderation/test_incidents.py | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 040f2c0c8..580a258fe 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -148,7 +148,7 @@ class Incidents(Cog): log.debug("Crawl task finished!") - async def archive(self, incident: discord.Message, outcome: Signal) -> bool: + async def archive(self, incident: discord.Message, outcome: Signal, actioned_by: discord.Member) -> bool: """ Relay `incident` to the #incidents-archive channel. @@ -235,7 +235,7 @@ class Incidents(Cog): log.debug("Reaction was valid, but no action is currently defined for it") return - relay_successful = await self.archive(incident, signal) + relay_successful = await self.archive(incident, signal, actioned_by=member) if not relay_successful: log.trace("Original message will not be deleted as we failed to relay it to the archive") return diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 2fc9180cf..c2e32fe6b 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -278,7 +278,9 @@ class TestArchive(TestIncidents): propagate out of the method, which is just as important. """ self.cog_instance.bot.fetch_webhook = AsyncMock(side_effect=mock_404) - self.assertFalse(await self.cog_instance.archive(incident=MockMessage(), outcome=MagicMock())) + self.assertFalse( + await self.cog_instance.archive(incident=MockMessage(), outcome=MagicMock(), actioned_by=MockMember()) + ) async def test_archive_relays_incident(self): """ @@ -303,7 +305,7 @@ class TestArchive(TestIncidents): author=MockUser(name="author_name", avatar_url="author_avatar"), id=123, ) - archive_return = await self.cog_instance.archive(incident, outcome=MagicMock(value="A")) + archive_return = await self.cog_instance.archive(incident, MagicMock(value="A"), MockMember()) # Check that the webhook was dispatched correctly webhook.send.assert_called_once_with( @@ -334,7 +336,7 @@ class TestArchive(TestIncidents): self.cog_instance.bot.fetch_webhook = AsyncMock(return_value=webhook) message_from_clyde = MockMessage(author=MockUser(name="clyde the great")) - await self.cog_instance.archive(message_from_clyde, MagicMock(incidents.Signal)) + await self.cog_instance.archive(message_from_clyde, MagicMock(incidents.Signal), MockMember()) self.assertNotIn("clyde", webhook.send.call_args.kwargs["username"]) -- cgit v1.2.3 From 7e2450bb650312ee79ac159621c4376c784a8398 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 00:24:00 +0000 Subject: Add base Slowmode cog --- bot/__main__.py | 1 + bot/cogs/slowmode.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 bot/cogs/slowmode.py diff --git a/bot/__main__.py b/bot/__main__.py index 4e0d4a111..bbd9c9144 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -62,6 +62,7 @@ bot.load_extension("bot.cogs.off_topic_names") bot.load_extension("bot.cogs.reddit") bot.load_extension("bot.cogs.reminders") bot.load_extension("bot.cogs.site") +bot.load_extension("bot.cogs.slowmode") bot.load_extension("bot.cogs.snekbox") bot.load_extension("bot.cogs.stats") bot.load_extension("bot.cogs.sync") diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py new file mode 100644 index 000000000..96c069ab8 --- /dev/null +++ b/bot/cogs/slowmode.py @@ -0,0 +1,14 @@ +from discord.ext.commands import Cog + +from bot.bot import Bot + + +class Slowmode(Cog): + + def __init__(self, bot: Bot) -> None: + self.bot = bot + + +def setup(bot: Bot) -> None: + """Load the Slowmode cog.""" + bot.add_cog(Slowmode(bot)) -- cgit v1.2.3 From 3ec5a69f8e1709aca55da3abc24cb2e632ae1ddb Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 00:28:05 +0000 Subject: Create boilerplate code for the commands --- bot/cogs/slowmode.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 96c069ab8..9140f3e8f 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -1,6 +1,9 @@ -from discord.ext.commands import Cog +from discord import TextChannel +from discord.ext.commands import Cog, Context, group from bot.bot import Bot +from bot.constants import MODERATION_ROLES +from bot.decorators import with_role class Slowmode(Cog): @@ -8,6 +11,20 @@ class Slowmode(Cog): def __init__(self, bot: Bot) -> None: self.bot = bot + @group(name='slowmode', aliases=['sm'], invoke_without_command=True) + async def slowmode_group(self, ctx: Context) -> None: + """Get and set the slowmode delay for a given text channel.""" + await ctx.send_help(ctx.command) + + @slowmode_group.command(name='get', aliases=['g']) + async def get_slowmode(self, ctx: Context, channel: TextChannel) -> None: + """Get the slowmode delay for a given text channel.""" + + @slowmode_group.command(name='set', aliases=['s']) + @with_role(*MODERATION_ROLES) + async def set_slowmode(self, ctx: Context, channel: TextChannel, seconds: int) -> None: + """Set the slowmode delay for a given text channel.""" + def setup(bot: Bot) -> None: """Load the Slowmode cog.""" -- cgit v1.2.3 From 38bd45d97127504ac38a098d86ebc0a83723110a Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 00:30:00 +0000 Subject: Implement the get_slowmode function --- bot/cogs/slowmode.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 9140f3e8f..d4226acec 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -19,6 +19,8 @@ class Slowmode(Cog): @slowmode_group.command(name='get', aliases=['g']) async def get_slowmode(self, ctx: Context, channel: TextChannel) -> None: """Get the slowmode delay for a given text channel.""" + slowmode_delay = channel.slowmode_delay + await ctx.send(f'The slowmode delay for {channel.mention} is {slowmode_delay} seconds.') @slowmode_group.command(name='set', aliases=['s']) @with_role(*MODERATION_ROLES) -- cgit v1.2.3 From 2172154c8cfe77b495e3c71716c3df339bf573b1 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 00:31:12 +0000 Subject: Implement the set_slowmode function --- bot/cogs/slowmode.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index d4226acec..bab6eccd0 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -2,7 +2,7 @@ from discord import TextChannel from discord.ext.commands import Cog, Context, group from bot.bot import Bot -from bot.constants import MODERATION_ROLES +from bot.constants import Emojis, MODERATION_ROLES from bot.decorators import with_role @@ -26,6 +26,10 @@ class Slowmode(Cog): @with_role(*MODERATION_ROLES) async def set_slowmode(self, ctx: Context, channel: TextChannel, seconds: int) -> None: """Set the slowmode delay for a given text channel.""" + await channel.edit(slowmode_delay=seconds) + await ctx.send( + f'{Emojis.check_mark} The slowmode delay for {channel.mention} is now {seconds} seconds.' + ) def setup(bot: Bot) -> None: -- cgit v1.2.3 From 7af6b6f52e1dff19e04bb106f27f0f2409788e10 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 00:37:38 +0000 Subject: Ensure slowmode delay is between 0 and 21600 seconds before setting it --- bot/cogs/slowmode.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index bab6eccd0..4a10d3fac 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -26,10 +26,16 @@ class Slowmode(Cog): @with_role(*MODERATION_ROLES) async def set_slowmode(self, ctx: Context, channel: TextChannel, seconds: int) -> None: """Set the slowmode delay for a given text channel.""" - await channel.edit(slowmode_delay=seconds) - await ctx.send( - f'{Emojis.check_mark} The slowmode delay for {channel.mention} is now {seconds} seconds.' - ) + if 0 <= seconds <= 21600: + await channel.edit(slowmode_delay=seconds) + await ctx.send( + f'{Emojis.check_mark} The slowmode delay for {channel.mention} is now {seconds} seconds.' + ) + + else: + await ctx.send( + f'{Emojis.cross_mark} The slowmode delay must be between 0 and 21600 seconds.' + ) def setup(bot: Bot) -> None: -- cgit v1.2.3 From 743f729d8ec039ef616a24eb291c8af5bec84c26 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 00:57:47 +0000 Subject: Add reset_slowmode function --- bot/cogs/slowmode.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 4a10d3fac..a4eb428e9 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -37,6 +37,15 @@ class Slowmode(Cog): f'{Emojis.cross_mark} The slowmode delay must be between 0 and 21600 seconds.' ) + @slowmode_group.command(name='reset', aliases=['r']) + @with_role(*MODERATION_ROLES) + async def reset_slowmode(self, ctx: Context, channel: TextChannel) -> None: + """Reset the slowmode delay for a given text channel to 0 seconds.""" + await channel.edit(slowmode_delay=0) + await ctx.send( + f'{Emojis.check_mark} The slowmode delay for {channel.mention} has been reset to 0 seconds.' + ) + def setup(bot: Bot) -> None: """Load the Slowmode cog.""" -- cgit v1.2.3 From 7b90754f74170d4a8db0008a9c08a690c01a7618 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 01:17:44 +0000 Subject: Create docstring for Slowmode cog --- bot/cogs/slowmode.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index a4eb428e9..a650ac395 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -7,6 +7,7 @@ from bot.decorators import with_role class Slowmode(Cog): + """Commands for getting and setting slowmode delays of text channels.""" def __init__(self, bot: Bot) -> None: self.bot = bot -- cgit v1.2.3 From 18dace4da6868f0a8aa6c64728994c68695fed95 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 01:36:25 +0000 Subject: Add some logging for the Slowmode cog --- bot/cogs/slowmode.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index a650ac395..7bbd61623 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -1,3 +1,5 @@ +import logging + from discord import TextChannel from discord.ext.commands import Cog, Context, group @@ -5,6 +7,8 @@ from bot.bot import Bot from bot.constants import Emojis, MODERATION_ROLES from bot.decorators import with_role +log = logging.getLogger(__name__) + class Slowmode(Cog): """Commands for getting and setting slowmode delays of text channels.""" @@ -33,10 +37,16 @@ class Slowmode(Cog): f'{Emojis.check_mark} The slowmode delay for {channel.mention} is now {seconds} seconds.' ) + log.info(f'{ctx.author} set the slowmode delay for #{channel} to {seconds} seconds.') + else: await ctx.send( f'{Emojis.cross_mark} The slowmode delay must be between 0 and 21600 seconds.' ) + log.info( + f'{ctx.author} tried to set the slowmode delay of #{channel} to {seconds} seconds, ' + 'which is not between 0 and 21600 seconds.' + ) @slowmode_group.command(name='reset', aliases=['r']) @with_role(*MODERATION_ROLES) @@ -46,6 +56,7 @@ class Slowmode(Cog): await ctx.send( f'{Emojis.check_mark} The slowmode delay for {channel.mention} has been reset to 0 seconds.' ) + log.info(f'{ctx.author} reset the slowmode delay for #{channel} to 0 seconds.') def setup(bot: Bot) -> None: -- cgit v1.2.3 From da93dc5d2eb06eae05c6180de2bd66f3fca90c1d Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 30 Jun 2020 18:41:44 -0700 Subject: Scheduler: more verbose logging in _await_later Showing the task ID in the logs makes them distinguishable from logs for other tasks. The coroutine state is logged because it may come in handy while debugging; the coroutine inspection check hasn't been proven yet in production. --- bot/utils/scheduling.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 0987c5de8..9fc519393 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -62,13 +62,13 @@ class Scheduler: """ delay = (time - datetime.utcnow()).total_seconds() if delay > 0: - coroutine = self._await_later(delay, coroutine) + coroutine = self._await_later(delay, task_id, coroutine) self.schedule(task_id, coroutine) def schedule_later(self, delay: t.Union[int, float], task_id: t.Hashable, coroutine: t.Coroutine) -> None: """Schedule `coroutine` to be executed after the given `delay` number of seconds.""" - self.schedule(task_id, self._await_later(delay, coroutine)) + self.schedule(task_id, self._await_later(delay, task_id, coroutine)) def cancel(self, task_id: t.Hashable) -> None: """Unschedule the task identified by `task_id`. Log a warning if the task doesn't exist.""" @@ -90,23 +90,26 @@ class Scheduler: for task_id in self._scheduled_tasks.copy(): self.cancel(task_id) - async def _await_later(self, delay: t.Union[int, float], coroutine: t.Coroutine) -> None: + async def _await_later(self, delay: t.Union[int, float], task_id: t.Hashable, coroutine: t.Coroutine) -> None: """Await `coroutine` after the given `delay` number of seconds.""" try: - self._log.trace(f"Waiting {delay} seconds before awaiting the coroutine.") + self._log.trace(f"Waiting {delay} seconds before awaiting coroutine for #{task_id}.") await asyncio.sleep(delay) # Use asyncio.shield to prevent the coroutine from cancelling itself. - self._log.trace("Done waiting; now awaiting the coroutine.") + self._log.trace(f"Done waiting for #{task_id}; now awaiting the coroutine.") await asyncio.shield(coroutine) finally: # Close it to prevent unawaited coroutine warnings, # which would happen if the task was cancelled during the sleep. # Only close it if it's not been awaited yet. This check is important because the # coroutine may cancel this task, which would also trigger the finally block. - if inspect.getcoroutinestate(coroutine) == "CORO_CREATED": - self._log.trace("Explicitly closing the coroutine.") + state = inspect.getcoroutinestate(coroutine) + if state == "CORO_CREATED": + self._log.debug(f"Explicitly closing the coroutine for #{task_id}.") coroutine.close() + else: + self._log.debug(f"Finally block reached for #{task_id}; {state=}") def _task_done_callback(self, task_id: t.Hashable, done_task: asyncio.Task) -> None: """ -- cgit v1.2.3 From dd74105d4a4433bb9e9e6fa57960a4956c0f1231 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Tue, 30 Jun 2020 23:42:32 +0200 Subject: Incidents: implement `make_embed` helper & tests See `make_embed` docstring for further information. The tests are fairly loose and should be easily adjustable in the future should changes be made. --- bot/cogs/moderation/incidents.py | 32 ++++++++++++++++++++++++++++- tests/bot/cogs/moderation/test_incidents.py | 26 +++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 580a258fe..ca591fc6e 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -1,13 +1,14 @@ import asyncio import logging import typing as t +from datetime import datetime from enum import Enum import discord from discord.ext.commands import Cog from bot.bot import Bot -from bot.constants import Channels, Emojis, Roles, Webhooks +from bot.constants import Channels, Colours, Emojis, Roles, Webhooks from bot.utils.messages import sub_clyde log = logging.getLogger(__name__) @@ -41,6 +42,35 @@ ALLOWED_ROLES: t.Set[int] = {Roles.moderators, Roles.admins, Roles.owners} ALL_SIGNALS: t.Set[str] = {signal.value for signal in Signal} +def make_embed(incident: discord.Message, outcome: Signal, actioned_by: discord.Member) -> discord.Embed: + """ + Create an embed representation of `incident` for the #incidents-archive channel. + + The name & discriminator of `actioned_by` and `outcome` will be presented in the + embed footer. Additionally, the embed is coloured based on `outcome`. + + The author of `incident` is not shown in the embed. It is assumed that this piece + of information will be relayed in other ways, e.g. webhook username. + + As mentions in embeds do not ping, we do not need to use `incident.clean_content`. + """ + if outcome is Signal.ACTIONED: + colour = Colours.soft_green + footer = f"Actioned by {actioned_by}" + else: + colour = Colours.soft_red + footer = f"Rejected by {actioned_by}" + + embed = discord.Embed( + description=incident.content, + timestamp=datetime.utcnow(), + colour=colour, + ) + embed.set_footer(text=footer, icon_url=actioned_by.avatar_url) + + return embed + + def is_incident(message: discord.Message) -> bool: """True if `message` qualifies as an incident, False otherwise.""" conditions = ( diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index c2e32fe6b..4731a786d 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -9,6 +9,7 @@ import aiohttp import discord from bot.cogs.moderation import Incidents, incidents +from bot.constants import Colours from tests.helpers import ( MockAsyncWebhook, MockBot, @@ -68,6 +69,31 @@ mock_404 = discord.NotFound( ) +class TestMakeEmbed(unittest.TestCase): + """Collection of tests for the `make_embed` helper function.""" + + def test_make_embed_actioned(self): + """Embed is coloured green and footer contains 'Actioned' when `outcome=Signal.ACTIONED`.""" + embed = incidents.make_embed(MockMessage(), incidents.Signal.ACTIONED, MockMember()) + + self.assertEqual(embed.colour.value, Colours.soft_green) + self.assertIn("Actioned", embed.footer.text) + + def test_make_embed_not_actioned(self): + """Embed is coloured red and footer contains 'Rejected' when `outcome=Signal.NOT_ACTIONED`.""" + embed = incidents.make_embed(MockMessage(), incidents.Signal.NOT_ACTIONED, MockMember()) + + self.assertEqual(embed.colour.value, Colours.soft_red) + self.assertIn("Rejected", embed.footer.text) + + def test_make_embed_content(self): + """Incident content appears as embed description.""" + incident = MockMessage(content="this is an incident") + embed = incidents.make_embed(incident, incidents.Signal.ACTIONED, MockMember()) + + self.assertEqual(incident.content, embed.description) + + @patch("bot.constants.Channels.incidents", 123) class TestIsIncident(unittest.TestCase): """ -- cgit v1.2.3 From 744aed585162cb0547e61a538734f116459ab510 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Wed, 1 Jul 2020 16:52:58 +0200 Subject: Incidents: relay incidents as embeds rather than raw content This applies the previously defined `make_embed` function. As the `archive` function is now simpler, I decided to reduce the amount of whitespace ~ it's a lot more compact now. Tests are adjusted as appropriate. --- bot/cogs/moderation/incidents.py | 24 ++++++++-------------- tests/bot/cogs/moderation/test_incidents.py | 32 ++++++++++------------------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index ca591fc6e..3a1a3d84e 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -180,38 +180,30 @@ class Incidents(Cog): async def archive(self, incident: discord.Message, outcome: Signal, actioned_by: discord.Member) -> bool: """ - Relay `incident` to the #incidents-archive channel. + Relay an embed representation of `incident` to the #incidents-archive channel. The following pieces of information are relayed: - * Incident message content (clean, pingless) + * Incident message content (as embed description) * Incident author name (as webhook author) * Incident author avatar (as webhook avatar) - * Resolution signal (`outcome`) + * Resolution signal `outcome` (as embed colour & footer) + * Moderator `actioned_by` (name & discriminator shown in footer) Return True if the relay finishes successfully. If anything goes wrong, meaning not all information was relayed, return False. This signals that the original message is not safe to be deleted, as we will lose some information. """ - log.debug(f"Archiving incident: {incident.id} with outcome: {outcome}") + log.debug(f"Archiving incident: {incident.id} (outcome: {outcome}, actioned by: {actioned_by})") try: - # First we try to grab the webhook - webhook: discord.Webhook = await self.bot.fetch_webhook(Webhooks.incidents_archive) - - # Now relay the incident - message: discord.Message = await webhook.send( - content=incident.clean_content, # Clean content will prevent mentions from pinging + webhook = await self.bot.fetch_webhook(Webhooks.incidents_archive) + await webhook.send( + embed=make_embed(incident, outcome, actioned_by), username=sub_clyde(incident.author.name), avatar_url=incident.author.avatar_url, - wait=True, # This makes the method return the sent Message object ) - - # Finally add the `outcome` emoji - await message.add_reaction(outcome.value) - except Exception: log.exception(f"Failed to archive incident {incident.id} to #incidents-archive") return False - else: log.trace("Message archived successfully!") return True diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 4731a786d..70dfe6b5f 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -312,39 +312,29 @@ class TestArchive(TestIncidents): """ If webhook is found, method relays `incident` properly. - This test will assert the following: - * The fetched webhook's `send` method is fed the correct arguments - * The message returned by `send` will have `outcome` reaction added - * Finally, the `archive` method returns True - - Assertions are made specifically in this order. + This test will assert that the fetched webhook's `send` method is fed the correct arguments, + and that the `archive` method returns True. """ - webhook_message = MockMessage() # The message that will be returned by the webhook's `send` method - webhook = MockAsyncWebhook(send=AsyncMock(return_value=webhook_message)) - + webhook = MockAsyncWebhook() self.cog_instance.bot.fetch_webhook = AsyncMock(return_value=webhook) # Patch in our webhook - # Now we'll pas our own `incident` to `archive` and capture the return value + # Define our own `incident` for archivation incident = MockMessage( - clean_content="pingless message", - content="pingful message", + content="this is an incident", author=MockUser(name="author_name", avatar_url="author_avatar"), id=123, ) - archive_return = await self.cog_instance.archive(incident, MagicMock(value="A"), MockMember()) + built_embed = MagicMock(discord.Embed, id=123) # We patch `make_embed` to return this - # Check that the webhook was dispatched correctly + with patch("bot.cogs.moderation.incidents.make_embed", MagicMock(return_value=built_embed)): + archive_return = await self.cog_instance.archive(incident, MagicMock(value="A"), MockMember()) + + # Now we check that the webhook was given the correct args, and that `archive` returned True webhook.send.assert_called_once_with( - content="pingless message", + embed=built_embed, username="author_name", avatar_url="author_avatar", - wait=True, ) - - # Now check that the correct emoji was added to the relayed message - webhook_message.add_reaction.assert_called_once_with("A") - - # Finally check that the method returned True self.assertTrue(archive_return) async def test_archive_clyde_username(self): -- cgit v1.2.3 From bd041ef4363ad8750d619d97fb7e8f3a4c6ae757 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 16:37:48 +0000 Subject: Create DurationDelta converter and humanize timedelta output for Slowmode cog. The DurationDelta converter will allow the Slowmode cog to use a formatted timestamp instead of an integer representing seconds. I created a new converter because the Duration converter returned a datetime.datetime object, instead of a time delta. Joe mentioned that I could just subtract the datetime.datetime object from datetime.utcnow(), but there is a small delay between conversion and when the function is actually executed. This caused something like `!slowmode set #python-general 5s` to set the slowmode delay to 4 seconds instead of 5. Now, with this new converter, the set command can be invoked using a formatted timestamp like so: `!slowmode set #python-general 4h23M19s`. This would set the slowmode delay in #python-general to 4 hours, 23 minutes, and 19 seconds. Of course that delay would be quite overkill for #python-general, but that's just for the sake of this example. --- bot/cogs/slowmode.py | 31 +++++++++++++++++++++---------- bot/converters.py | 22 ++++++++++++++++++---- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 7bbd61623..898f4bf52 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -1,11 +1,15 @@ import logging +from datetime import datetime +from dateutil.relativedelta import relativedelta from discord import TextChannel from discord.ext.commands import Cog, Context, group from bot.bot import Bot from bot.constants import Emojis, MODERATION_ROLES +from bot.converters import DurationDelta from bot.decorators import with_role +from bot.utils import time log = logging.getLogger(__name__) @@ -24,28 +28,35 @@ class Slowmode(Cog): @slowmode_group.command(name='get', aliases=['g']) async def get_slowmode(self, ctx: Context, channel: TextChannel) -> None: """Get the slowmode delay for a given text channel.""" - slowmode_delay = channel.slowmode_delay - await ctx.send(f'The slowmode delay for {channel.mention} is {slowmode_delay} seconds.') + delay = relativedelta(seconds=channel.slowmode_delay) + await ctx.send(f'The slowmode delay for {channel.mention} is {time.humanize_delta(delay, precision=3)}.') @slowmode_group.command(name='set', aliases=['s']) @with_role(*MODERATION_ROLES) - async def set_slowmode(self, ctx: Context, channel: TextChannel, seconds: int) -> None: + async def set_slowmode(self, ctx: Context, channel: TextChannel, delay: DurationDelta) -> None: """Set the slowmode delay for a given text channel.""" - if 0 <= seconds <= 21600: - await channel.edit(slowmode_delay=seconds) + # Convert `dateutil.relativedelta.relativedelta` to `datetime.timedelta` + # Must do this to get the delta in a particular unit of time + utcnow = datetime.utcnow() + slowmode_delay = (utcnow + delay - utcnow).seconds + + humanized_delay = time.humanize_delta(delay, precision=3) + + if 0 <= slowmode_delay <= 21600: + await channel.edit(slowmode_delay=slowmode_delay) await ctx.send( - f'{Emojis.check_mark} The slowmode delay for {channel.mention} is now {seconds} seconds.' + f'{Emojis.check_mark} The slowmode delay for {channel.mention} is now {humanized_delay}.' ) - log.info(f'{ctx.author} set the slowmode delay for #{channel} to {seconds} seconds.') + log.info(f'{ctx.author} set the slowmode delay for #{channel} to {humanized_delay}.') else: await ctx.send( - f'{Emojis.cross_mark} The slowmode delay must be between 0 and 21600 seconds.' + f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.' ) log.info( - f'{ctx.author} tried to set the slowmode delay of #{channel} to {seconds} seconds, ' - 'which is not between 0 and 21600 seconds.' + f'{ctx.author} tried to set the slowmode delay of #{channel} to {humanized_delay}, ' + 'which is not between 0 and 6 hours.' ) @slowmode_group.command(name='reset', aliases=['r']) diff --git a/bot/converters.py b/bot/converters.py index 4deb59f87..65963f513 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -181,8 +181,8 @@ class TagContentConverter(Converter): return tag_content -class Duration(Converter): - """Convert duration strings into UTC datetime.datetime objects.""" +class DurationDelta(Converter): + """Convert duration strings into dateutil.relativedelta.relativedelta objects.""" duration_parser = re.compile( r"((?P\d+?) ?(years|year|Y|y) ?)?" @@ -194,9 +194,9 @@ class Duration(Converter): r"((?P\d+?) ?(seconds|second|S|s))?" ) - async def convert(self, ctx: Context, duration: str) -> datetime: + async def convert(self, ctx: Context, duration: str) -> relativedelta: """ - Converts a `duration` string to a datetime object that's `duration` in the future. + Converts a `duration` string to a relativedelta object. The converter supports the following symbols for each unit of time: - years: `Y`, `y`, `year`, `years` @@ -215,6 +215,20 @@ class Duration(Converter): duration_dict = {unit: int(amount) for unit, amount in match.groupdict(default=0).items()} delta = relativedelta(**duration_dict) + + return delta + + +class Duration(DurationDelta): + """Convert duration strings into UTC datetime.datetime objects.""" + + async def convert(self, ctx: Context, duration: str) -> datetime: + """ + Converts a `duration` string to a datetime object that's `duration` in the future. + + The converter supports the same symbols for each unit of time as its parent class. + """ + delta = super().convert(ctx, duration) now = datetime.utcnow() try: -- cgit v1.2.3 From 7eb3a5a7c1a38ad56f1e9584a24f2da9f00d0a40 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 17:03:02 +0000 Subject: Forgot an await in the Duration converter --- bot/converters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/converters.py b/bot/converters.py index 65963f513..898822165 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -228,7 +228,7 @@ class Duration(DurationDelta): The converter supports the same symbols for each unit of time as its parent class. """ - delta = super().convert(ctx, duration) + delta = await super().convert(ctx, duration) now = datetime.utcnow() try: -- cgit v1.2.3 From 933a154ccbb83c4ee5ad1fa87e1bea9d8c012f27 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 17:14:46 +0000 Subject: Catch TypeError when the slowmode delay is 0 seconds --- bot/cogs/slowmode.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 898f4bf52..b8b3bb65c 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -29,7 +29,15 @@ class Slowmode(Cog): async def get_slowmode(self, ctx: Context, channel: TextChannel) -> None: """Get the slowmode delay for a given text channel.""" delay = relativedelta(seconds=channel.slowmode_delay) - await ctx.send(f'The slowmode delay for {channel.mention} is {time.humanize_delta(delay, precision=3)}.') + + try: + humanized_delay = time.humanize_delta(delay, precision=3) + + except TypeError: + humanized_delay = '0 seconds' + + finally: + await ctx.send(f'The slowmode delay for {channel.mention} is {humanized_delay}.') @slowmode_group.command(name='set', aliases=['s']) @with_role(*MODERATION_ROLES) -- cgit v1.2.3 From 1906cf7caaf580f37a0d689713d5252d1649f4ec Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 17:17:00 +0000 Subject: Add comment explaining TypeError --- bot/cogs/slowmode.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index b8b3bb65c..7e1bee61d 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -34,6 +34,8 @@ class Slowmode(Cog): humanized_delay = time.humanize_delta(delay, precision=3) except TypeError: + # The slowmode delay is 0 seconds, + # which causes `time.humanize_delta` to raise a TypeError humanized_delay = '0 seconds' finally: -- cgit v1.2.3 From c8bcaff2b7bc5b7a66c0307650d6f72b65eac659 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 1 Jul 2020 18:06:08 +0000 Subject: Use total_seconds method instead of seconds attribute --- bot/cogs/slowmode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 7e1bee61d..c2ca97a7f 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -48,7 +48,7 @@ class Slowmode(Cog): # Convert `dateutil.relativedelta.relativedelta` to `datetime.timedelta` # Must do this to get the delta in a particular unit of time utcnow = datetime.utcnow() - slowmode_delay = (utcnow + delay - utcnow).seconds + slowmode_delay = (utcnow + delay - utcnow).total_seconds() humanized_delay = time.humanize_delta(delay, precision=3) -- cgit v1.2.3 From 36de1ea49bb6597179bf9931adfef41ed59e5d5f Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 2 Jul 2020 15:53:22 +0300 Subject: Help System: Implement question message pinning --- bot/cogs/help_channels.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 187adfe51..bb97759ee 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -113,6 +113,10 @@ class HelpChannels(Scheduler, commands.Cog): # RedisCache[discord.TextChannel.id, UtcPosixTimestamp] claim_times = RedisCache() + # This cache maps a help channel to original question message in same channel. + # RedisCache[discord.TextChannel.id, discord.Message.id] + question_messages = RedisCache() + def __init__(self, bot: Bot): super().__init__() @@ -548,6 +552,22 @@ class HelpChannels(Scheduler, commands.Cog): A caller argument is provided for metrics. """ + msg_id = await self.question_messages.pop(channel.id) + + # When message ID exist in cache, try to get it from cache first. When this fail, use API request. + # When this return 404, this mean that message is deleted and can't be unpinned. + if msg_id: + msg = discord.utils.get(self.bot.cached_messages, id=msg_id) + if msg is None: + try: + msg = await channel.fetch_message(msg_id) + except discord.NotFound: + log.debug(f"Can't unpin message {msg_id} because this is deleted.") + + # When we got message, then unpin it + if msg: + await msg.unpin() + log.info(f"Moving #{channel} ({channel.id}) to the Dormant category.") await self.move_to_bottom_position( @@ -688,6 +708,14 @@ class HelpChannels(Scheduler, commands.Cog): log.info(f"Channel #{channel} was claimed by `{message.author.id}`.") await self.move_to_in_use(channel) await self.revoke_send_permissions(message.author) + # Pin message for better access and storage this to cache + try: + await message.pin() + except discord.NotFound: + log.info(f"Pinning message {message.id} ({channel}) failed because message got deleted.") + else: + await self.question_messages.set(channel.id, message.id) + # Add user with channel for dormant check. await self.help_channel_claimants.set(channel.id, message.author.id) -- cgit v1.2.3 From d2732ce299cf2071b92fdf0c1eecbb0f16f0afbd Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 2 Jul 2020 15:52:05 +0200 Subject: Incidents: trace-level log incident embed creation --- bot/cogs/moderation/incidents.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 3a1a3d84e..8970c2c5c 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -54,6 +54,8 @@ def make_embed(incident: discord.Message, outcome: Signal, actioned_by: discord. As mentions in embeds do not ping, we do not need to use `incident.clean_content`. """ + log.trace(f"Creating embed for {incident.id=}") + if outcome is Signal.ACTIONED: colour = Colours.soft_green footer = f"Actioned by {actioned_by}" -- cgit v1.2.3 From 83544ca0f91dd7bc8510e4fc7a64bc73712ddaf8 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Fri, 3 Jul 2020 10:47:47 +0200 Subject: Incidents: archive incident attachments There is no handling of file types as explained in the `archive` docstring. Testing indicates that relaying incidents with e.g. a text file attachment is simply a noop in the Discord GUI. If there is at least one attachment, we always only relay the one at index 0, as it is believed the user-sent messages can only contain one attachment at maximum. This also adds an extra test asserting the behaviour when an incident with an attachment is archived. The existing test for `archive` is adjusted to assume no attachments. Joe helped me conceive & test this. Co-authored-by: Joseph Banks --- bot/cogs/moderation/incidents.py | 21 +++++++++++++++++++- tests/bot/cogs/moderation/test_incidents.py | 30 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 8970c2c5c..1a12c8bbd 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -186,22 +186,41 @@ class Incidents(Cog): The following pieces of information are relayed: * Incident message content (as embed description) + * Incident attachment (if image, shown in archive embed) * Incident author name (as webhook author) * Incident author avatar (as webhook avatar) * Resolution signal `outcome` (as embed colour & footer) * Moderator `actioned_by` (name & discriminator shown in footer) + If `incident` contains an attachment, we try to add it to the archive embed. There is + no handing of extensions / file types - we simply dispatch the attachment file with the + webhook, and try to display it in the embed. Testing indicates that if the attachment + cannot be displayed (e.g. a text file), it's invisible in the embed, with no error. + Return True if the relay finishes successfully. If anything goes wrong, meaning not all information was relayed, return False. This signals that the original message is not safe to be deleted, as we will lose some information. """ log.debug(f"Archiving incident: {incident.id} (outcome: {outcome}, actioned by: {actioned_by})") + embed = make_embed(incident, outcome, actioned_by) + + # If the incident had an attachment, we will try to relay it + if incident.attachments: + attachment = incident.attachments[0] # User-sent messages can only contain one attachment + log.debug(f"Attempting to archive incident attachment: {attachment.filename}") + + attachment_file = await attachment.to_file() # The file will be sent with the webhook + embed.set_image(url=f"attachment://{attachment.filename}") # Embed displays the attached file + else: + attachment_file = None + try: webhook = await self.bot.fetch_webhook(Webhooks.incidents_archive) await webhook.send( - embed=make_embed(incident, outcome, actioned_by), + embed=embed, username=sub_clyde(incident.author.name), avatar_url=incident.author.avatar_url, + file=attachment_file, ) except Exception: log.exception(f"Failed to archive incident {incident.id} to #incidents-archive") diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 70dfe6b5f..f8d479cef 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -323,6 +323,7 @@ class TestArchive(TestIncidents): content="this is an incident", author=MockUser(name="author_name", avatar_url="author_avatar"), id=123, + attachments=[], # This incident has no attachments ) built_embed = MagicMock(discord.Embed, id=123) # We patch `make_embed` to return this @@ -334,9 +335,38 @@ class TestArchive(TestIncidents): embed=built_embed, username="author_name", avatar_url="author_avatar", + file=None, ) self.assertTrue(archive_return) + async def test_archive_relays_incident_with_attachments(self): + """ + Incident attachments are relayed and displayed in the embed. + + This test asserts the two things that need to happen in order to relay the attachment. + The embed returned by `make_embed` must have the `set_image` method called with the + attachment's filename, and the file must be passed to the webhook's send method. + """ + attachment_file = MagicMock(discord.File) + attachment = MagicMock( + discord.Attachment, + filename="abc.png", + to_file=AsyncMock(return_value=attachment_file), + ) + incident = MockMessage( + attachments=[attachment], + ) + built_embed = MagicMock(discord.Embed) + + with patch("bot.cogs.moderation.incidents.make_embed", MagicMock(return_value=built_embed)): + await self.cog_instance.archive(incident, incidents.Signal.ACTIONED, actioned_by=MockMember()) + + built_embed.set_image.assert_called_once_with(url="attachment://abc.png") + + send_kwargs = self.cog_instance.bot.fetch_webhook.return_value.send.call_args.kwargs + self.assertIn("file", send_kwargs) + self.assertIs(send_kwargs["file"], attachment_file) + async def test_archive_clyde_username(self): """ The archive webhook username is cleansed using `sub_clyde`. -- cgit v1.2.3 From a92bbddd092d4779a922f7d02b945ff3cb835350 Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Fri, 3 Jul 2020 14:28:54 +0100 Subject: Outdated badge in README upset me --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1e7b21271..cae7c3454 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Python Utility Bot -[![Discord](https://img.shields.io/static/v1?label=Python%20Discord&logo=discord&message=%3E30k%20members&color=%237289DA&logoColor=white)](https://discord.gg/2B963hn) +[![Discord](https://img.shields.io/static/v1?label=Python%20Discord&logo=discord&message=%3E60k%20members&color=%237289DA&logoColor=white)](https://discord.gg/2B963hn) [![Build Status](https://dev.azure.com/python-discord/Python%20Discord/_apis/build/status/Bot?branchName=master)](https://dev.azure.com/python-discord/Python%20Discord/_build/latest?definitionId=1&branchName=master) [![Tests](https://img.shields.io/azure-devops/tests/python-discord/Python%20Discord/1?compact_message)](https://dev.azure.com/python-discord/Python%20Discord/_apis/build/status/Bot?branchName=master) [![Coverage](https://img.shields.io/azure-devops/coverage/python-discord/Python%20Discord/1/master)](https://dev.azure.com/python-discord/Python%20Discord/_apis/build/status/Bot?branchName=master) -- cgit v1.2.3 From e7be2215dc0c800655c9985d655d5d6d687932f0 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Fri, 3 Jul 2020 15:51:41 +0000 Subject: Remove precision kwarg usage --- bot/cogs/slowmode.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index c2ca97a7f..9f69d30e0 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -30,16 +30,13 @@ class Slowmode(Cog): """Get the slowmode delay for a given text channel.""" delay = relativedelta(seconds=channel.slowmode_delay) - try: - humanized_delay = time.humanize_delta(delay, precision=3) - - except TypeError: - # The slowmode delay is 0 seconds, - # which causes `time.humanize_delta` to raise a TypeError + # Say "0 seconds" instead of "less than a second" + if channel.slowmode_delay == 0: humanized_delay = '0 seconds' + else: + humanized_delay = time.humanize_delta(delay) - finally: - await ctx.send(f'The slowmode delay for {channel.mention} is {humanized_delay}.') + await ctx.send(f'The slowmode delay for {channel.mention} is {humanized_delay}.') @slowmode_group.command(name='set', aliases=['s']) @with_role(*MODERATION_ROLES) @@ -50,7 +47,7 @@ class Slowmode(Cog): utcnow = datetime.utcnow() slowmode_delay = (utcnow + delay - utcnow).total_seconds() - humanized_delay = time.humanize_delta(delay, precision=3) + humanized_delay = time.humanize_delta(delay) if 0 <= slowmode_delay <= 21600: await channel.edit(slowmode_delay=slowmode_delay) -- cgit v1.2.3 From 5cfad8c592388bfff4152a684e10f7d8a04e6426 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Fri, 3 Jul 2020 15:53:31 +0000 Subject: Move log to before what it's logging executes. This makes sure the log will be made, since the operations executed are now below it. --- bot/cogs/slowmode.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 9f69d30e0..593208bea 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -50,31 +50,33 @@ class Slowmode(Cog): humanized_delay = time.humanize_delta(delay) if 0 <= slowmode_delay <= 21600: + log.info(f'{ctx.author} set the slowmode delay for #{channel} to {humanized_delay}.') + await channel.edit(slowmode_delay=slowmode_delay) await ctx.send( f'{Emojis.check_mark} The slowmode delay for {channel.mention} is now {humanized_delay}.' ) - log.info(f'{ctx.author} set the slowmode delay for #{channel} to {humanized_delay}.') - else: - await ctx.send( - f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.' - ) log.info( f'{ctx.author} tried to set the slowmode delay of #{channel} to {humanized_delay}, ' 'which is not between 0 and 6 hours.' ) + await ctx.send( + f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.' + ) + @slowmode_group.command(name='reset', aliases=['r']) @with_role(*MODERATION_ROLES) async def reset_slowmode(self, ctx: Context, channel: TextChannel) -> None: """Reset the slowmode delay for a given text channel to 0 seconds.""" + log.info(f'{ctx.author} reset the slowmode delay for #{channel} to 0 seconds.') + await channel.edit(slowmode_delay=0) await ctx.send( f'{Emojis.check_mark} The slowmode delay for {channel.mention} has been reset to 0 seconds.' ) - log.info(f'{ctx.author} reset the slowmode delay for #{channel} to 0 seconds.') def setup(bot: Bot) -> None: -- cgit v1.2.3 From 7f430c7ca99030c31c019093019139caa6d81d9c Mon Sep 17 00:00:00 2001 From: Den4200 Date: Fri, 3 Jul 2020 22:55:19 +0000 Subject: Only allow moderators to use the entire cog --- bot/cogs/slowmode.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 593208bea..ec5e9cc0d 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -8,7 +8,7 @@ from discord.ext.commands import Cog, Context, group from bot.bot import Bot from bot.constants import Emojis, MODERATION_ROLES from bot.converters import DurationDelta -from bot.decorators import with_role +from bot.decorators import with_role_check from bot.utils import time log = logging.getLogger(__name__) @@ -39,7 +39,6 @@ class Slowmode(Cog): await ctx.send(f'The slowmode delay for {channel.mention} is {humanized_delay}.') @slowmode_group.command(name='set', aliases=['s']) - @with_role(*MODERATION_ROLES) async def set_slowmode(self, ctx: Context, channel: TextChannel, delay: DurationDelta) -> None: """Set the slowmode delay for a given text channel.""" # Convert `dateutil.relativedelta.relativedelta` to `datetime.timedelta` @@ -68,7 +67,6 @@ class Slowmode(Cog): ) @slowmode_group.command(name='reset', aliases=['r']) - @with_role(*MODERATION_ROLES) async def reset_slowmode(self, ctx: Context, channel: TextChannel) -> None: """Reset the slowmode delay for a given text channel to 0 seconds.""" log.info(f'{ctx.author} reset the slowmode delay for #{channel} to 0 seconds.') @@ -78,6 +76,10 @@ class Slowmode(Cog): f'{Emojis.check_mark} The slowmode delay for {channel.mention} has been reset to 0 seconds.' ) + def cog_check(self, ctx: Context) -> bool: + """Only allow moderators to invoke the commands in this cog.""" + return with_role_check(ctx, *MODERATION_ROLES) + def setup(bot: Bot) -> None: """Load the Slowmode cog.""" -- cgit v1.2.3 From c4c4dfa698321912eb15ff3c1d77d1170968d124 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 00:29:53 +0000 Subject: Create a constant for the max slowmode delay --- bot/cogs/slowmode.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index ec5e9cc0d..830273174 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -13,6 +13,8 @@ from bot.utils import time log = logging.getLogger(__name__) +SLOWMODE_MAX_DELAY = 21600 # seconds + class Slowmode(Cog): """Commands for getting and setting slowmode delays of text channels.""" @@ -48,7 +50,8 @@ class Slowmode(Cog): humanized_delay = time.humanize_delta(delay) - if 0 <= slowmode_delay <= 21600: + # Ensure the delay is within discord's limits + if slowmode_delay <= SLOWMODE_MAX_DELAY: log.info(f'{ctx.author} set the slowmode delay for #{channel} to {humanized_delay}.') await channel.edit(slowmode_delay=slowmode_delay) -- cgit v1.2.3 From 9804e84cdf5903c3aac3783a66b81e5865680c62 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 00:52:41 +0000 Subject: Remove monkeypatch and apply appropriate changes to _stringify_time_unit --- bot/cogs/slowmode.py | 7 +------ bot/utils/time.py | 4 +++- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 830273174..88f19b2f1 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -31,12 +31,7 @@ class Slowmode(Cog): async def get_slowmode(self, ctx: Context, channel: TextChannel) -> None: """Get the slowmode delay for a given text channel.""" delay = relativedelta(seconds=channel.slowmode_delay) - - # Say "0 seconds" instead of "less than a second" - if channel.slowmode_delay == 0: - humanized_delay = '0 seconds' - else: - humanized_delay = time.humanize_delta(delay) + humanized_delay = time.humanize_delta(delay) await ctx.send(f'The slowmode delay for {channel.mention} is {humanized_delay}.') diff --git a/bot/utils/time.py b/bot/utils/time.py index 77060143c..47e49904b 100644 --- a/bot/utils/time.py +++ b/bot/utils/time.py @@ -20,7 +20,9 @@ def _stringify_time_unit(value: int, unit: str) -> str: >>> _stringify_time_unit(0, "minutes") "less than a minute" """ - if value == 1: + if unit == "seconds" and value == 0: + return "0 seconds" + elif value == 1: return f"{value} {unit[:-1]}" elif value == 0: return f"less than a {unit[:-1]}" -- cgit v1.2.3 From 539030a1c2a79efe23541704f0026a072ba064ed Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 00:59:15 +0000 Subject: Default to the channel that `slowmode get` was invoked in --- bot/cogs/slowmode.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 88f19b2f1..7405c1e7f 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -1,5 +1,6 @@ import logging from datetime import datetime +from typing import Optional from dateutil.relativedelta import relativedelta from discord import TextChannel @@ -28,8 +29,12 @@ class Slowmode(Cog): await ctx.send_help(ctx.command) @slowmode_group.command(name='get', aliases=['g']) - async def get_slowmode(self, ctx: Context, channel: TextChannel) -> None: + async def get_slowmode(self, ctx: Context, channel: Optional[TextChannel] = None) -> None: """Get the slowmode delay for a given text channel.""" + # Use the channel this command was invoked in if one was not given + if channel is None: + channel = ctx.channel + delay = relativedelta(seconds=channel.slowmode_delay) humanized_delay = time.humanize_delta(delay) -- cgit v1.2.3 From 758568f2d39212737f15b871850597185b254fcd Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 01:02:23 +0000 Subject: Default to the channel that `slowmode reset` was invoked in --- bot/cogs/slowmode.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 7405c1e7f..0b9b64976 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -70,8 +70,12 @@ class Slowmode(Cog): ) @slowmode_group.command(name='reset', aliases=['r']) - async def reset_slowmode(self, ctx: Context, channel: TextChannel) -> None: + async def reset_slowmode(self, ctx: Context, channel: Optional[TextChannel] = None) -> None: """Reset the slowmode delay for a given text channel to 0 seconds.""" + # Use the channel this command was invoked in if one was not given + if channel is None: + channel = ctx.channel + log.info(f'{ctx.author} reset the slowmode delay for #{channel} to 0 seconds.') await channel.edit(slowmode_delay=0) -- cgit v1.2.3 From b04c4163f97bb3c811096587ed1db51d9754114b Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 01:41:21 +0000 Subject: Default to the channel that `slowmode set` was invoked in --- bot/cogs/slowmode.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 0b9b64976..93ddf4b19 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -41,8 +41,12 @@ class Slowmode(Cog): await ctx.send(f'The slowmode delay for {channel.mention} is {humanized_delay}.') @slowmode_group.command(name='set', aliases=['s']) - async def set_slowmode(self, ctx: Context, channel: TextChannel, delay: DurationDelta) -> None: + async def set_slowmode(self, ctx: Context, channel: Optional[TextChannel], delay: DurationDelta) -> None: """Set the slowmode delay for a given text channel.""" + # Use the channel this command was invoked in if one was not given + if not channel: + channel = ctx.channel + # Convert `dateutil.relativedelta.relativedelta` to `datetime.timedelta` # Must do this to get the delta in a particular unit of time utcnow = datetime.utcnow() -- cgit v1.2.3 From 76e8eaea958029fa11849624c0eb9edcfe248529 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 01:48:23 +0000 Subject: Make channel comparison against None consistent --- bot/cogs/slowmode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 93ddf4b19..ecbc235a0 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -44,7 +44,7 @@ class Slowmode(Cog): async def set_slowmode(self, ctx: Context, channel: Optional[TextChannel], delay: DurationDelta) -> None: """Set the slowmode delay for a given text channel.""" # Use the channel this command was invoked in if one was not given - if not channel: + if channel is None: channel = ctx.channel # Convert `dateutil.relativedelta.relativedelta` to `datetime.timedelta` -- cgit v1.2.3 From 7c4f6db3f7291612862f6f16cddc73f7add72fd0 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 01:50:08 +0000 Subject: Remove unneeded kwargs for `typing.Optional` to keep consistency --- bot/cogs/slowmode.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index ecbc235a0..1e83065ab 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -29,7 +29,7 @@ class Slowmode(Cog): await ctx.send_help(ctx.command) @slowmode_group.command(name='get', aliases=['g']) - async def get_slowmode(self, ctx: Context, channel: Optional[TextChannel] = None) -> None: + async def get_slowmode(self, ctx: Context, channel: Optional[TextChannel]) -> None: """Get the slowmode delay for a given text channel.""" # Use the channel this command was invoked in if one was not given if channel is None: @@ -74,7 +74,7 @@ class Slowmode(Cog): ) @slowmode_group.command(name='reset', aliases=['r']) - async def reset_slowmode(self, ctx: Context, channel: Optional[TextChannel] = None) -> None: + async def reset_slowmode(self, ctx: Context, channel: Optional[TextChannel]) -> None: """Reset the slowmode delay for a given text channel to 0 seconds.""" # Use the channel this command was invoked in if one was not given if channel is None: -- cgit v1.2.3 From f31babf54ef1e4d2d2966bf8b695b1e4a01848e0 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 02:04:30 +0000 Subject: Update the docstrings to account for optional channel parameter --- bot/cogs/slowmode.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index 1e83065ab..1d055afac 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -25,12 +25,12 @@ class Slowmode(Cog): @group(name='slowmode', aliases=['sm'], invoke_without_command=True) async def slowmode_group(self, ctx: Context) -> None: - """Get and set the slowmode delay for a given text channel.""" + """Get or set the slowmode delay for the text channel this was invoked in or a given text channel.""" await ctx.send_help(ctx.command) @slowmode_group.command(name='get', aliases=['g']) async def get_slowmode(self, ctx: Context, channel: Optional[TextChannel]) -> None: - """Get the slowmode delay for a given text channel.""" + """Get the slowmode delay for a text channel.""" # Use the channel this command was invoked in if one was not given if channel is None: channel = ctx.channel @@ -42,7 +42,7 @@ class Slowmode(Cog): @slowmode_group.command(name='set', aliases=['s']) async def set_slowmode(self, ctx: Context, channel: Optional[TextChannel], delay: DurationDelta) -> None: - """Set the slowmode delay for a given text channel.""" + """Set the slowmode delay for a text channel.""" # Use the channel this command was invoked in if one was not given if channel is None: channel = ctx.channel @@ -75,7 +75,7 @@ class Slowmode(Cog): @slowmode_group.command(name='reset', aliases=['r']) async def reset_slowmode(self, ctx: Context, channel: Optional[TextChannel]) -> None: - """Reset the slowmode delay for a given text channel to 0 seconds.""" + """Reset the slowmode delay for a text channel to 0 seconds.""" # Use the channel this command was invoked in if one was not given if channel is None: channel = ctx.channel -- cgit v1.2.3 From 40719793f9c0d8a2c5761d3730b5920a146709c3 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 04:08:27 +0000 Subject: Add tests for cog_check and get_slowmode --- tests/bot/cogs/test_slowmode.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/bot/cogs/test_slowmode.py diff --git a/tests/bot/cogs/test_slowmode.py b/tests/bot/cogs/test_slowmode.py new file mode 100644 index 000000000..fb9f3c9ad --- /dev/null +++ b/tests/bot/cogs/test_slowmode.py @@ -0,0 +1,37 @@ +import unittest +from unittest import mock + +from bot.cogs.slowmode import Slowmode +from tests.helpers import MockBot, MockContext, MockTextChannel + + +class SlowmodeTests(unittest.IsolatedAsyncioTestCase): + + def setUp(self) -> None: + self.bot = MockBot() + self.cog = Slowmode(self.bot) + self.text_channel = MockTextChannel() + self.ctx = MockContext(channel=self.text_channel) + + async def test_get_slowmode_no_channel(self) -> None: + """Get slowmode without a given channel""" + self.text_channel.mention = '#python-general' + self.text_channel.slowmode_delay = 5 + + await self.cog.get_slowmode(self.cog, self.ctx, None) + self.ctx.send.assert_called_once_with("The slowmode delay for #python-general is 5 seconds.") + + async def test_get_slowmode_with_channel(self) -> None: + """Get slowmode without a given channel""" + self.text_channel.mention = '#python-language' + self.text_channel.slowmode_delay = 2 + + await self.cog.get_slowmode(self.cog, self.ctx, self.text_channel) + self.ctx.send.assert_called_once_with("The slowmode delay for #python-language is 2 seconds.") + + @mock.patch("bot.cogs.slowmode.with_role_check") + @mock.patch("bot.cogs.slowmode.MODERATION_ROLES", new=(1, 2, 3)) + def test_cog_check(self, role_check): + """Role check is called with `MODERATION_ROLES`""" + self.cog.cog_check(self.ctx) + role_check.assert_called_once_with(self.ctx, *(1, 2, 3)) -- cgit v1.2.3 From e760b4312a5264fe9442cb1d53c9e357dbeb2b81 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 04:55:42 +0000 Subject: Add tests for reset_slowmode --- tests/bot/cogs/test_slowmode.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/bot/cogs/test_slowmode.py b/tests/bot/cogs/test_slowmode.py index fb9f3c9ad..a2e5ad346 100644 --- a/tests/bot/cogs/test_slowmode.py +++ b/tests/bot/cogs/test_slowmode.py @@ -2,6 +2,7 @@ import unittest from unittest import mock from bot.cogs.slowmode import Slowmode +from bot.constants import Emojis from tests.helpers import MockBot, MockContext, MockTextChannel @@ -14,7 +15,7 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase): self.ctx = MockContext(channel=self.text_channel) async def test_get_slowmode_no_channel(self) -> None: - """Get slowmode without a given channel""" + """Get slowmode without a given channel.""" self.text_channel.mention = '#python-general' self.text_channel.slowmode_delay = 5 @@ -22,12 +23,30 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase): self.ctx.send.assert_called_once_with("The slowmode delay for #python-general is 5 seconds.") async def test_get_slowmode_with_channel(self) -> None: - """Get slowmode without a given channel""" + """Get slowmode with a given channel.""" self.text_channel.mention = '#python-language' self.text_channel.slowmode_delay = 2 await self.cog.get_slowmode(self.cog, self.ctx, self.text_channel) - self.ctx.send.assert_called_once_with("The slowmode delay for #python-language is 2 seconds.") + self.ctx.send.assert_called_once_with('The slowmode delay for #python-language is 2 seconds.') + + async def test_reset_slowmode_no_channel(self) -> None: + """Reset slowmode without a given channel.""" + self.text_channel.mention = '#careers' + + await self.cog.reset_slowmode(self.cog, self.ctx, None) + self.ctx.send.assert_called_once_with( + f'{Emojis.check_mark} The slowmode delay for #careers has been reset to 0 seconds.' + ) + + async def test_reset_slowmode_with_channel(self) -> None: + """Reset slowmode with a given channel.""" + self.text_channel.mention = '#meta' + + await self.cog.reset_slowmode(self.cog, self.ctx, self.text_channel) + self.ctx.send.assert_called_once_with( + f'{Emojis.check_mark} The slowmode delay for #meta has been reset to 0 seconds.' + ) @mock.patch("bot.cogs.slowmode.with_role_check") @mock.patch("bot.cogs.slowmode.MODERATION_ROLES", new=(1, 2, 3)) -- cgit v1.2.3 From 8613659cb191bedca925dc798c89623b49c9a90a Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 05:45:04 +0000 Subject: Add tests for set_slowmode --- tests/bot/cogs/test_slowmode.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/bot/cogs/test_slowmode.py b/tests/bot/cogs/test_slowmode.py index a2e5ad346..5262ce34a 100644 --- a/tests/bot/cogs/test_slowmode.py +++ b/tests/bot/cogs/test_slowmode.py @@ -1,6 +1,8 @@ import unittest from unittest import mock +from dateutil.relativedelta import relativedelta + from bot.cogs.slowmode import Slowmode from bot.constants import Emojis from tests.helpers import MockBot, MockContext, MockTextChannel @@ -30,6 +32,24 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase): await self.cog.get_slowmode(self.cog, self.ctx, self.text_channel) self.ctx.send.assert_called_once_with('The slowmode delay for #python-language is 2 seconds.') + async def test_set_slowmode_no_channel(self) -> None: + """Set slowmode without a given channel.""" + self.text_channel.mention = '#careers' + + await self.cog.set_slowmode(self.cog, self.ctx, None, relativedelta(seconds=3)) + self.ctx.send.assert_called_once_with( + f'{Emojis.check_mark} The slowmode delay for #careers is now 3 seconds.' + ) + + async def test_set_slowmode_with_channel(self) -> None: + """Set slowmode with a given channel.""" + self.text_channel.mention = '#meta' + + await self.cog.set_slowmode(self.cog, self.ctx, self.text_channel, relativedelta(seconds=4)) + self.ctx.send.assert_called_once_with( + f'{Emojis.check_mark} The slowmode delay for #meta is now 4 seconds.' + ) + async def test_reset_slowmode_no_channel(self) -> None: """Reset slowmode without a given channel.""" self.text_channel.mention = '#careers' -- cgit v1.2.3 From 4935ed5ae632f5887bcff23ac67c781eab8527e9 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 06:05:32 +0000 Subject: Use local text_channel instead of instance attribute --- tests/bot/cogs/test_slowmode.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/tests/bot/cogs/test_slowmode.py b/tests/bot/cogs/test_slowmode.py index 5262ce34a..663c9fd43 100644 --- a/tests/bot/cogs/test_slowmode.py +++ b/tests/bot/cogs/test_slowmode.py @@ -13,28 +13,25 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase): def setUp(self) -> None: self.bot = MockBot() self.cog = Slowmode(self.bot) - self.text_channel = MockTextChannel() - self.ctx = MockContext(channel=self.text_channel) + self.ctx = MockContext() async def test_get_slowmode_no_channel(self) -> None: """Get slowmode without a given channel.""" - self.text_channel.mention = '#python-general' - self.text_channel.slowmode_delay = 5 + self.ctx.channel = MockTextChannel(name='python-general', slowmode_delay=5) await self.cog.get_slowmode(self.cog, self.ctx, None) self.ctx.send.assert_called_once_with("The slowmode delay for #python-general is 5 seconds.") async def test_get_slowmode_with_channel(self) -> None: """Get slowmode with a given channel.""" - self.text_channel.mention = '#python-language' - self.text_channel.slowmode_delay = 2 + text_channel = MockTextChannel(name='python-language', slowmode_delay=2) - await self.cog.get_slowmode(self.cog, self.ctx, self.text_channel) + await self.cog.get_slowmode(self.cog, self.ctx, text_channel) self.ctx.send.assert_called_once_with('The slowmode delay for #python-language is 2 seconds.') async def test_set_slowmode_no_channel(self) -> None: """Set slowmode without a given channel.""" - self.text_channel.mention = '#careers' + self.ctx.channel = MockTextChannel(name='careers') await self.cog.set_slowmode(self.cog, self.ctx, None, relativedelta(seconds=3)) self.ctx.send.assert_called_once_with( @@ -43,16 +40,16 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase): async def test_set_slowmode_with_channel(self) -> None: """Set slowmode with a given channel.""" - self.text_channel.mention = '#meta' + text_channel = MockTextChannel(name='meta') - await self.cog.set_slowmode(self.cog, self.ctx, self.text_channel, relativedelta(seconds=4)) + await self.cog.set_slowmode(self.cog, self.ctx, text_channel, relativedelta(seconds=4)) self.ctx.send.assert_called_once_with( f'{Emojis.check_mark} The slowmode delay for #meta is now 4 seconds.' ) async def test_reset_slowmode_no_channel(self) -> None: """Reset slowmode without a given channel.""" - self.text_channel.mention = '#careers' + self.ctx.channel = MockTextChannel(name='careers', slowmode_delay=6) await self.cog.reset_slowmode(self.cog, self.ctx, None) self.ctx.send.assert_called_once_with( @@ -61,9 +58,9 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase): async def test_reset_slowmode_with_channel(self) -> None: """Reset slowmode with a given channel.""" - self.text_channel.mention = '#meta' + text_channel = MockTextChannel(name='meta', slowmode_delay=1) - await self.cog.reset_slowmode(self.cog, self.ctx, self.text_channel) + await self.cog.reset_slowmode(self.cog, self.ctx, text_channel) self.ctx.send.assert_called_once_with( f'{Emojis.check_mark} The slowmode delay for #meta has been reset to 0 seconds.' ) -- cgit v1.2.3 From 77a2e514dd2e200e23ccf45760677c2e7c40b9ff Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 06:11:00 +0000 Subject: Add multiple test cases for set_slowmode tests --- tests/bot/cogs/test_slowmode.py | 44 +++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/tests/bot/cogs/test_slowmode.py b/tests/bot/cogs/test_slowmode.py index 663c9fd43..e9835b8bd 100644 --- a/tests/bot/cogs/test_slowmode.py +++ b/tests/bot/cogs/test_slowmode.py @@ -31,22 +31,46 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase): async def test_set_slowmode_no_channel(self) -> None: """Set slowmode without a given channel.""" - self.ctx.channel = MockTextChannel(name='careers') - - await self.cog.set_slowmode(self.cog, self.ctx, None, relativedelta(seconds=3)) - self.ctx.send.assert_called_once_with( - f'{Emojis.check_mark} The slowmode delay for #careers is now 3 seconds.' + test_cases = ( + ('helpers', 23, f'{Emojis.check_mark} The slowmode delay for #helpers is now 23 seconds.'), + ('mods', 76526, f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.'), + ('admins', 97, f'{Emojis.check_mark} The slowmode delay for #admins is now 1 minute and 37 seconds.') ) + for channel_name, seconds, result_msg in test_cases: + with self.subTest( + channel_mention=channel_name, + seconds=seconds, + result_msg=result_msg + ): + self.ctx.channel = MockTextChannel(name=channel_name) + + await self.cog.set_slowmode(self.cog, self.ctx, None, relativedelta(seconds=seconds)) + self.ctx.send.assert_called_once_with(result_msg) + + self.ctx.reset_mock() + async def test_set_slowmode_with_channel(self) -> None: """Set slowmode with a given channel.""" - text_channel = MockTextChannel(name='meta') - - await self.cog.set_slowmode(self.cog, self.ctx, text_channel, relativedelta(seconds=4)) - self.ctx.send.assert_called_once_with( - f'{Emojis.check_mark} The slowmode delay for #meta is now 4 seconds.' + test_cases = ( + ('bot-commands', 12, f'{Emojis.check_mark} The slowmode delay for #bot-commands is now 12 seconds.'), + ('mod-spam', 21, f'{Emojis.check_mark} The slowmode delay for #mod-spam is now 21 seconds.'), + ('admin-spam', 4323598, f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.') ) + for channel_name, seconds, result_msg in test_cases: + with self.subTest( + channel_mention=channel_name, + seconds=seconds, + result_msg=result_msg + ): + text_channel = MockTextChannel(name=channel_name) + + await self.cog.set_slowmode(self.cog, self.ctx, text_channel, relativedelta(seconds=seconds)) + self.ctx.send.assert_called_once_with(result_msg) + + self.ctx.reset_mock() + async def test_reset_slowmode_no_channel(self) -> None: """Reset slowmode without a given channel.""" self.ctx.channel = MockTextChannel(name='careers', slowmode_delay=6) -- cgit v1.2.3 From 2d170b8af92c77bedea4d77fbdeedc515d3f2c59 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 17:08:24 +0000 Subject: Improve set_slowmode tests by checking whether the channel was edited --- tests/bot/cogs/test_slowmode.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/bot/cogs/test_slowmode.py b/tests/bot/cogs/test_slowmode.py index e9835b8bd..65b1534cb 100644 --- a/tests/bot/cogs/test_slowmode.py +++ b/tests/bot/cogs/test_slowmode.py @@ -32,20 +32,27 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase): async def test_set_slowmode_no_channel(self) -> None: """Set slowmode without a given channel.""" test_cases = ( - ('helpers', 23, f'{Emojis.check_mark} The slowmode delay for #helpers is now 23 seconds.'), - ('mods', 76526, f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.'), - ('admins', 97, f'{Emojis.check_mark} The slowmode delay for #admins is now 1 minute and 37 seconds.') + ('helpers', 23, True, f'{Emojis.check_mark} The slowmode delay for #helpers is now 23 seconds.'), + ('mods', 76526, False, f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.'), + ('admins', 97, True, f'{Emojis.check_mark} The slowmode delay for #admins is now 1 minute and 37 seconds.') ) - for channel_name, seconds, result_msg in test_cases: + for channel_name, seconds, edited, result_msg in test_cases: with self.subTest( channel_mention=channel_name, seconds=seconds, + edited=edited, result_msg=result_msg ): self.ctx.channel = MockTextChannel(name=channel_name) await self.cog.set_slowmode(self.cog, self.ctx, None, relativedelta(seconds=seconds)) + + if edited: + self.ctx.channel.edit.assert_awaited_once_with(slowmode_delay=float(seconds)) + else: + self.ctx.channel.edit.assert_not_called() + self.ctx.send.assert_called_once_with(result_msg) self.ctx.reset_mock() @@ -53,20 +60,27 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase): async def test_set_slowmode_with_channel(self) -> None: """Set slowmode with a given channel.""" test_cases = ( - ('bot-commands', 12, f'{Emojis.check_mark} The slowmode delay for #bot-commands is now 12 seconds.'), - ('mod-spam', 21, f'{Emojis.check_mark} The slowmode delay for #mod-spam is now 21 seconds.'), - ('admin-spam', 4323598, f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.') + ('bot-commands', 12, True, f'{Emojis.check_mark} The slowmode delay for #bot-commands is now 12 seconds.'), + ('mod-spam', 21, True, f'{Emojis.check_mark} The slowmode delay for #mod-spam is now 21 seconds.'), + ('admin-spam', 4323598, False, f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.') ) - for channel_name, seconds, result_msg in test_cases: + for channel_name, seconds, edited, result_msg in test_cases: with self.subTest( channel_mention=channel_name, seconds=seconds, + edited=edited, result_msg=result_msg ): text_channel = MockTextChannel(name=channel_name) await self.cog.set_slowmode(self.cog, self.ctx, text_channel, relativedelta(seconds=seconds)) + + if edited: + text_channel.edit.assert_awaited_once_with(slowmode_delay=float(seconds)) + else: + text_channel.edit.assert_not_called() + self.ctx.send.assert_called_once_with(result_msg) self.ctx.reset_mock() -- cgit v1.2.3 From 14cfd1e9dd4d149fb554b84969fed27f85ad5361 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 6 Jul 2020 10:09:03 -0700 Subject: Scheduler: assert the coroutine hasn't been awaited yet It'd fail to schedule the coroutine otherwise anyway. There is also the potential to close the coroutine, which may be unexpected to see for a coroutine that was already running (despite being documented). --- bot/utils/scheduling.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 9fc519393..fddb0c2fe 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -43,6 +43,9 @@ class Scheduler: """ self._log.trace(f"Scheduling task #{task_id}...") + msg = f"Cannot schedule an already started coroutine for #{task_id}" + assert inspect.getcoroutinestate(coroutine) == "CORO_CREATED", msg + if task_id in self._scheduled_tasks: self._log.debug(f"Did not schedule task #{task_id}; task was already scheduled.") coroutine.close() -- cgit v1.2.3 From 420171bc5d472868f5fb96c8960731eea4d67c5d Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 17:15:45 +0000 Subject: Move slowmode cog to the moderation subpackage --- bot/__main__.py | 1 - bot/cogs/moderation/__init__.py | 4 +- bot/cogs/moderation/slowmode.py | 97 +++++++++++++++++++++++++++++++++++++++++ bot/cogs/slowmode.py | 97 ----------------------------------------- 4 files changed, 100 insertions(+), 99 deletions(-) create mode 100644 bot/cogs/moderation/slowmode.py delete mode 100644 bot/cogs/slowmode.py diff --git a/bot/__main__.py b/bot/__main__.py index bbd9c9144..4e0d4a111 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -62,7 +62,6 @@ bot.load_extension("bot.cogs.off_topic_names") bot.load_extension("bot.cogs.reddit") bot.load_extension("bot.cogs.reminders") bot.load_extension("bot.cogs.site") -bot.load_extension("bot.cogs.slowmode") bot.load_extension("bot.cogs.snekbox") bot.load_extension("bot.cogs.stats") bot.load_extension("bot.cogs.sync") diff --git a/bot/cogs/moderation/__init__.py b/bot/cogs/moderation/__init__.py index 6880ca1bd..a5c1ef362 100644 --- a/bot/cogs/moderation/__init__.py +++ b/bot/cogs/moderation/__init__.py @@ -3,13 +3,15 @@ from .infractions import Infractions from .management import ModManagement from .modlog import ModLog from .silence import Silence +from .slowmode import Slowmode from .superstarify import Superstarify def setup(bot: Bot) -> None: - """Load the Infractions, ModManagement, ModLog, Silence, and Superstarify cogs.""" + """Load the Infractions, ModManagement, ModLog, Silence, Slowmode, and Superstarify cogs.""" bot.add_cog(Infractions(bot)) bot.add_cog(ModLog(bot)) bot.add_cog(ModManagement(bot)) bot.add_cog(Silence(bot)) + bot.add_cog(Slowmode(bot)) bot.add_cog(Superstarify(bot)) diff --git a/bot/cogs/moderation/slowmode.py b/bot/cogs/moderation/slowmode.py new file mode 100644 index 000000000..1d055afac --- /dev/null +++ b/bot/cogs/moderation/slowmode.py @@ -0,0 +1,97 @@ +import logging +from datetime import datetime +from typing import Optional + +from dateutil.relativedelta import relativedelta +from discord import TextChannel +from discord.ext.commands import Cog, Context, group + +from bot.bot import Bot +from bot.constants import Emojis, MODERATION_ROLES +from bot.converters import DurationDelta +from bot.decorators import with_role_check +from bot.utils import time + +log = logging.getLogger(__name__) + +SLOWMODE_MAX_DELAY = 21600 # seconds + + +class Slowmode(Cog): + """Commands for getting and setting slowmode delays of text channels.""" + + def __init__(self, bot: Bot) -> None: + self.bot = bot + + @group(name='slowmode', aliases=['sm'], invoke_without_command=True) + async def slowmode_group(self, ctx: Context) -> None: + """Get or set the slowmode delay for the text channel this was invoked in or a given text channel.""" + await ctx.send_help(ctx.command) + + @slowmode_group.command(name='get', aliases=['g']) + async def get_slowmode(self, ctx: Context, channel: Optional[TextChannel]) -> None: + """Get the slowmode delay for a text channel.""" + # Use the channel this command was invoked in if one was not given + if channel is None: + channel = ctx.channel + + delay = relativedelta(seconds=channel.slowmode_delay) + humanized_delay = time.humanize_delta(delay) + + await ctx.send(f'The slowmode delay for {channel.mention} is {humanized_delay}.') + + @slowmode_group.command(name='set', aliases=['s']) + async def set_slowmode(self, ctx: Context, channel: Optional[TextChannel], delay: DurationDelta) -> None: + """Set the slowmode delay for a text channel.""" + # Use the channel this command was invoked in if one was not given + if channel is None: + channel = ctx.channel + + # Convert `dateutil.relativedelta.relativedelta` to `datetime.timedelta` + # Must do this to get the delta in a particular unit of time + utcnow = datetime.utcnow() + slowmode_delay = (utcnow + delay - utcnow).total_seconds() + + humanized_delay = time.humanize_delta(delay) + + # Ensure the delay is within discord's limits + if slowmode_delay <= SLOWMODE_MAX_DELAY: + log.info(f'{ctx.author} set the slowmode delay for #{channel} to {humanized_delay}.') + + await channel.edit(slowmode_delay=slowmode_delay) + await ctx.send( + f'{Emojis.check_mark} The slowmode delay for {channel.mention} is now {humanized_delay}.' + ) + + else: + log.info( + f'{ctx.author} tried to set the slowmode delay of #{channel} to {humanized_delay}, ' + 'which is not between 0 and 6 hours.' + ) + + await ctx.send( + f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.' + ) + + @slowmode_group.command(name='reset', aliases=['r']) + async def reset_slowmode(self, ctx: Context, channel: Optional[TextChannel]) -> None: + """Reset the slowmode delay for a text channel to 0 seconds.""" + # Use the channel this command was invoked in if one was not given + if channel is None: + channel = ctx.channel + + log.info(f'{ctx.author} reset the slowmode delay for #{channel} to 0 seconds.') + + await channel.edit(slowmode_delay=0) + await ctx.send( + f'{Emojis.check_mark} The slowmode delay for {channel.mention} has been reset to 0 seconds.' + ) + + def cog_check(self, ctx: Context) -> bool: + """Only allow moderators to invoke the commands in this cog.""" + return with_role_check(ctx, *MODERATION_ROLES) + + +def setup(bot: Bot) -> None: + """Load the Slowmode cog.""" + bot.add_cog(Slowmode(bot)) diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py deleted file mode 100644 index 1d055afac..000000000 --- a/bot/cogs/slowmode.py +++ /dev/null @@ -1,97 +0,0 @@ -import logging -from datetime import datetime -from typing import Optional - -from dateutil.relativedelta import relativedelta -from discord import TextChannel -from discord.ext.commands import Cog, Context, group - -from bot.bot import Bot -from bot.constants import Emojis, MODERATION_ROLES -from bot.converters import DurationDelta -from bot.decorators import with_role_check -from bot.utils import time - -log = logging.getLogger(__name__) - -SLOWMODE_MAX_DELAY = 21600 # seconds - - -class Slowmode(Cog): - """Commands for getting and setting slowmode delays of text channels.""" - - def __init__(self, bot: Bot) -> None: - self.bot = bot - - @group(name='slowmode', aliases=['sm'], invoke_without_command=True) - async def slowmode_group(self, ctx: Context) -> None: - """Get or set the slowmode delay for the text channel this was invoked in or a given text channel.""" - await ctx.send_help(ctx.command) - - @slowmode_group.command(name='get', aliases=['g']) - async def get_slowmode(self, ctx: Context, channel: Optional[TextChannel]) -> None: - """Get the slowmode delay for a text channel.""" - # Use the channel this command was invoked in if one was not given - if channel is None: - channel = ctx.channel - - delay = relativedelta(seconds=channel.slowmode_delay) - humanized_delay = time.humanize_delta(delay) - - await ctx.send(f'The slowmode delay for {channel.mention} is {humanized_delay}.') - - @slowmode_group.command(name='set', aliases=['s']) - async def set_slowmode(self, ctx: Context, channel: Optional[TextChannel], delay: DurationDelta) -> None: - """Set the slowmode delay for a text channel.""" - # Use the channel this command was invoked in if one was not given - if channel is None: - channel = ctx.channel - - # Convert `dateutil.relativedelta.relativedelta` to `datetime.timedelta` - # Must do this to get the delta in a particular unit of time - utcnow = datetime.utcnow() - slowmode_delay = (utcnow + delay - utcnow).total_seconds() - - humanized_delay = time.humanize_delta(delay) - - # Ensure the delay is within discord's limits - if slowmode_delay <= SLOWMODE_MAX_DELAY: - log.info(f'{ctx.author} set the slowmode delay for #{channel} to {humanized_delay}.') - - await channel.edit(slowmode_delay=slowmode_delay) - await ctx.send( - f'{Emojis.check_mark} The slowmode delay for {channel.mention} is now {humanized_delay}.' - ) - - else: - log.info( - f'{ctx.author} tried to set the slowmode delay of #{channel} to {humanized_delay}, ' - 'which is not between 0 and 6 hours.' - ) - - await ctx.send( - f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.' - ) - - @slowmode_group.command(name='reset', aliases=['r']) - async def reset_slowmode(self, ctx: Context, channel: Optional[TextChannel]) -> None: - """Reset the slowmode delay for a text channel to 0 seconds.""" - # Use the channel this command was invoked in if one was not given - if channel is None: - channel = ctx.channel - - log.info(f'{ctx.author} reset the slowmode delay for #{channel} to 0 seconds.') - - await channel.edit(slowmode_delay=0) - await ctx.send( - f'{Emojis.check_mark} The slowmode delay for {channel.mention} has been reset to 0 seconds.' - ) - - def cog_check(self, ctx: Context) -> bool: - """Only allow moderators to invoke the commands in this cog.""" - return with_role_check(ctx, *MODERATION_ROLES) - - -def setup(bot: Bot) -> None: - """Load the Slowmode cog.""" - bot.add_cog(Slowmode(bot)) -- cgit v1.2.3 From 30114ac8c118220b743d4a91f737f8ad973eeb9c Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 6 Jul 2020 10:10:47 -0700 Subject: Scheduler: document coroutine closing elsewhere --- bot/utils/scheduling.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index fddb0c2fe..03f31d78f 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -36,10 +36,10 @@ class Scheduler: def schedule(self, task_id: t.Hashable, coroutine: t.Coroutine) -> None: """ - Schedule the execution of a coroutine. + Schedule the execution of a `coroutine`. - If a task with `task_id` already exists, close `coroutine` instead of scheduling it. - This prevents unawaited coroutine warnings. + If a task with `task_id` already exists, close `coroutine` instead of scheduling it. This + prevents unawaited coroutine warnings. Don't pass a coroutine that'll be re-used elsewhere. """ self._log.trace(f"Scheduling task #{task_id}...") @@ -62,6 +62,9 @@ class Scheduler: Schedule `coroutine` to be executed at the given naïve UTC `time`. If `time` is in the past, schedule `coroutine` immediately. + + If a task with `task_id` already exists, close `coroutine` instead of scheduling it. This + prevents unawaited coroutine warnings. Don't pass a coroutine that'll be re-used elsewhere. """ delay = (time - datetime.utcnow()).total_seconds() if delay > 0: @@ -70,7 +73,12 @@ class Scheduler: self.schedule(task_id, coroutine) def schedule_later(self, delay: t.Union[int, float], task_id: t.Hashable, coroutine: t.Coroutine) -> None: - """Schedule `coroutine` to be executed after the given `delay` number of seconds.""" + """ + Schedule `coroutine` to be executed after the given `delay` number of seconds. + + If a task with `task_id` already exists, close `coroutine` instead of scheduling it. This + prevents unawaited coroutine warnings. Don't pass a coroutine that'll be re-used elsewhere. + """ self.schedule(task_id, self._await_later(delay, task_id, coroutine)) def cancel(self, task_id: t.Hashable) -> None: -- cgit v1.2.3 From cdeb41bfd283cb6cb1285993737e8e3abd5aea9f Mon Sep 17 00:00:00 2001 From: Den4200 Date: Mon, 6 Jul 2020 17:30:44 +0000 Subject: Fix imports in slowmode tests --- tests/bot/cogs/test_slowmode.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/bot/cogs/test_slowmode.py b/tests/bot/cogs/test_slowmode.py index 65b1534cb..f442814c8 100644 --- a/tests/bot/cogs/test_slowmode.py +++ b/tests/bot/cogs/test_slowmode.py @@ -3,7 +3,7 @@ from unittest import mock from dateutil.relativedelta import relativedelta -from bot.cogs.slowmode import Slowmode +from bot.cogs.moderation.slowmode import Slowmode from bot.constants import Emojis from tests.helpers import MockBot, MockContext, MockTextChannel @@ -103,8 +103,8 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase): f'{Emojis.check_mark} The slowmode delay for #meta has been reset to 0 seconds.' ) - @mock.patch("bot.cogs.slowmode.with_role_check") - @mock.patch("bot.cogs.slowmode.MODERATION_ROLES", new=(1, 2, 3)) + @mock.patch("bot.cogs.moderation.slowmode.with_role_check") + @mock.patch("bot.cogs.moderation.slowmode.MODERATION_ROLES", new=(1, 2, 3)) def test_cog_check(self, role_check): """Role check is called with `MODERATION_ROLES`""" self.cog.cog_check(self.ctx) -- cgit v1.2.3 From b1c017741318ff0e96e4a46d0390054541a215d1 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 7 Jul 2020 12:01:50 -0700 Subject: Prevent bot from mentioning roles This was open to abuse when the bot relayed user input. --- Pipfile | 2 +- Pipfile.lock | 220 ++++++++++++++++++++++++++++++++------------------------ bot/__main__.py | 1 + 3 files changed, 127 insertions(+), 96 deletions(-) diff --git a/Pipfile b/Pipfile index 33be99587..e25e7b1e1 100644 --- a/Pipfile +++ b/Pipfile @@ -12,7 +12,7 @@ beautifulsoup4 = "~=4.9" colorama = {version = "~=0.4.3",sys_platform = "== 'win32'"} coloredlogs = "~=14.0" deepdiff = "~=4.0" -discord.py = "~=1.3.2" +discord-py = {git = "https://github.com/Rapptz/discord.py.git",ref = "e971e2f16cba22decd25db6b44e9cc84adf08555",editable = true} fakeredis = "~=1.4" feedparser = "~=5.2" fuzzywuzzy = "~=0.17" diff --git a/Pipfile.lock b/Pipfile.lock index 0e591710c..12325f2a7 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "0297accc3d614d3da8080b89d56ef7fe489c28a0ada8102df396a604af7ee330" + "sha256": "f6fac6e59e6579ea4cc0e2b49a5fa59785137d02e6c6a7df47ef502375313703" }, "pipfile-spec": 6, "requires": { @@ -63,6 +63,7 @@ "sha256:41a9d4eb17db805f30ed172f3f609fe0c2b16657fb15b1b67df19d251dd93c0d", "sha256:7c19477a9450824cb79f9949fd238f4148e2c0dca67756a2868863c387209f04" ], + "markers": "python_version >= '3.6'", "version": "==3.2.2" }, "alabaster": { @@ -77,6 +78,7 @@ "sha256:0c3c816a028d47f659d6ff5c745cb2acf1f966da1fe5c19c77a70282b25f4c5f", "sha256:4291ca197d287d274d0b6cb5d6f8f8f82d434ed288f962539ff18cc9012f9ea3" ], + "markers": "python_full_version >= '3.5.3'", "version": "==3.0.1" }, "attrs": { @@ -84,6 +86,7 @@ "sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c", "sha256:f7b7ce16570fe9965acd6d30101a28f62fb4a7f9e926b3bbc9b61f8b04247e72" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==19.3.0" }, "babel": { @@ -91,6 +94,7 @@ "sha256:1aac2ae2d0d8ea368fa90906567f5c08463d98ade155c0c4bfedd6a0f7160e38", "sha256:d670ea0b10f8b723672d3a6abeb87b565b244da220d76b4dba1b66269ec152d4" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==2.8.0" }, "beautifulsoup4": { @@ -104,10 +108,10 @@ }, "certifi": { "hashes": [ - "sha256:1d987a998c75633c40847cc966fcf5904906c920a7f17ef374f5aa4282abd304", - "sha256:51fcb31174be6e6664c5f69e3e1691a2d72a1a12e90f872cbdb1567eb47b6519" + "sha256:5930595817496dd21bb8dc35dad090f1c2cd0adfaf21204bf6732ca5d8ee34d3", + "sha256:8fc0819f1f30ba15bdb34cceffb9ef04d99f420f68eb75d901e9560b8749fc41" ], - "version": "==2020.4.5.1" + "version": "==2020.6.20" }, "cffi": { "hashes": [ @@ -154,7 +158,6 @@ "sha256:7d73d2a99753107a36ac6b455ee49046802e59d9d076ef8e47b61499fa29afff", "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1" ], - "index": "pypi", "markers": "sys_platform == 'win32'", "version": "==0.4.3" }, @@ -174,26 +177,17 @@ "index": "pypi", "version": "==4.3.2" }, - "discord": { - "hashes": [ - "sha256:9d4debb4a37845543bd4b92cb195bc53a302797333e768e70344222857ff1559", - "sha256:ff6653655e342e7721dfb3f10421345fd852c2a33f2cca912b1c39b3778a9429" - ], - "index": "pypi", - "version": "==1.0.1" - }, - "discord.py": { - "hashes": [ - "sha256:406871b06d86c3dc49fba63238519f28628dac946fef8a0e22988ff58ec05580", - "sha256:ad00e34c72d2faa8db2157b651d05f3c415d7d05078e7e41dc9e8dc240051beb" - ], - "version": "==1.3.3" + "discord-py": { + "editable": true, + "git": "https://github.com/Rapptz/discord.py.git", + "ref": "e971e2f16cba22decd25db6b44e9cc84adf08555" }, "docutils": { "hashes": [ "sha256:0c5b78adfbf7762415433f5515cd5c9e762339e23369dbe8000d84a4bf4ab3af", "sha256:c2de3a60e9e7d07be26b7f2b00ca0309c207e06c100f9cc2a94931fc75a478fc" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", "version": "==0.16" }, "fakeredis": { @@ -264,6 +258,7 @@ "sha256:fa2dc05b87d97acc1c6ae63f3e0f39eae5246565232484b08db6bf2dc1580678", "sha256:fe7d6ce9f6a5fbe24f09d95ea93e9c7271abc4e1565da511e1449b107b4d7848" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==1.0.1" }, "humanfriendly": { @@ -271,20 +266,23 @@ "sha256:bf52ec91244819c780341a3438d5d7b09f431d3f113a475147ac9b7b167a3d12", "sha256:e78960b31198511f45fd455534ae7645a6207d33e512d2e842c766d15d9c8080" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", "version": "==8.2" }, "idna": { "hashes": [ - "sha256:7588d1c14ae4c77d74036e8c22ff447b26d0fde8f007354fd48a7814db15b7cb", - "sha256:a068a21ceac8a4d63dbfd964670474107f541babbd2250d61922f029858365fa" + "sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6", + "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0" ], - "version": "==2.9" + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==2.10" }, "imagesize": { "hashes": [ "sha256:6965f19a6a2039c7d48bca7dba2473069ff854c36ae6f19d2cde309d998228a1", "sha256:b1f6b5a4eab1f73479a50fb79fcf729514a900c341d8503d62a62dbc4127a2b1" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==1.2.0" }, "jinja2": { @@ -292,6 +290,7 @@ "sha256:89aab215427ef59c34ad58735269eb58b1a5808103067f7bb9d5836c651b3bb0", "sha256:f0a4641d3cf955324a89c04f3d94663aa4d638abe8f733ecd3582848e1c37035" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", "version": "==2.11.2" }, "lxml": { @@ -370,15 +369,16 @@ "sha256:e249096428b3ae81b08327a63a485ad0878de3fb939049038579ac0ef61e17e7", "sha256:e8313f01ba26fbbe36c7be1966a7b7424942f670f38e666995b88d012765b9be" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==1.1.1" }, "more-itertools": { "hashes": [ - "sha256:558bb897a2232f5e4f8e2399089e35aecb746e1f9191b6584a151647e89267be", - "sha256:7818f596b1e87be009031c7653d01acc46ed422e6656b394b0f765ce66ed4982" + "sha256:68c70cc7167bdf5c7c9d8f6954a7837089c6a36bf565383919bb595efb8a17e5", + "sha256:b78134b2063dd214000685165d81c154522c3ee0a1c0d4d113c80361c234c5a2" ], "index": "pypi", - "version": "==8.3.0" + "version": "==8.4.0" }, "multidict": { "hashes": [ @@ -400,19 +400,22 @@ "sha256:fcfbb44c59af3f8ea984de67ec7c306f618a3ec771c2843804069917a8f2e255", "sha256:feed85993dbdb1dbc29102f50bca65bdc68f2c0c8d352468c25b54874f23c39d" ], + "markers": "python_version >= '3.5'", "version": "==4.7.6" }, "ordered-set": { "hashes": [ - "sha256:a31008c57f9c9776b12eb8841b1f61d1e4d70dfbbe8875ccfa2403c54af3d51b" + "sha256:ba93b2df055bca202116ec44b9bead3df33ea63a7d5827ff8e16738b97f33a95" ], - "version": "==4.0.1" + "markers": "python_version >= '3.5'", + "version": "==4.0.2" }, "packaging": { "hashes": [ "sha256:4357f74f47b9c12db93624a82154e9b120fa8293699949152b22065d556079f8", "sha256:998416ba6962ae7fbd6596850b80e17859a5753ba17c32284f67bfff33784181" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==20.4" }, "pamqp": { @@ -461,6 +464,7 @@ "sha256:2d475327684562c3a96cc71adf7dc8c4f0565175cf86b6d7a404ff4c771f15f0", "sha256:7582ad22678f0fcd81102833f60ef8d0e57288b6b5fb00323d101be910e35705" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==2.20" }, "pygments": { @@ -468,6 +472,7 @@ "sha256:647344a061c249a3b74e230c739f434d7ea4d8b1d5f3721bc0f3558049b38f44", "sha256:ff7a40b4860b727ab48fad6360eb351cc1b33cbf9b15a0f689ca5353e9463324" ], + "markers": "python_version >= '3.5'", "version": "==2.6.1" }, "pyparsing": { @@ -475,6 +480,7 @@ "sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1", "sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b" ], + "markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==2.4.7" }, "python-dateutil": { @@ -511,32 +517,34 @@ }, "redis": { "hashes": [ - "sha256:2ef11f489003f151777c064c5dbc6653dfb9f3eade159bcadc524619fddc2242", - "sha256:6d65e84bc58091140081ee9d9c187aab0480097750fac44239307a3bdf0b1251" + "sha256:0e7e0cfca8660dea8b7d5cd8c4f6c5e29e11f31158c0b0ae91a397f00e5a05a2", + "sha256:432b788c4530cfe16d8d943a09d40ca6c16149727e4afe8c2c9d5580c59d9f24" ], - "version": "==3.5.2" + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", + "version": "==3.5.3" }, "requests": { "hashes": [ - "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee", - "sha256:b3f43d496c6daba4493e7c431722aeb7dbc6288f52a6e04e7b6023b0247817e6" + "sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b", + "sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898" ], "index": "pypi", - "version": "==2.23.0" + "version": "==2.24.0" }, "sentry-sdk": { "hashes": [ - "sha256:0e5e947d0f7a969314aa23669a94a9712be5a688ff069ff7b9fc36c66adc160c", - "sha256:799a8bf76b012e3030a881be00e97bc0b922ce35dde699c6537122b751d80e2c" + "sha256:da06bc3641e81ec2c942f87a0676cd9180044fa3d1697524a0005345997542e2", + "sha256:e80d61af85d99a1222c1a3e2a24023618374cd50a99673aa7fa3cf920e7d813b" ], "index": "pypi", - "version": "==0.14.4" + "version": "==0.16.0" }, "six": { "hashes": [ "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259", "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==1.15.0" }, "snowballstemmer": { @@ -548,16 +556,17 @@ }, "sortedcontainers": { "hashes": [ - "sha256:974e9a32f56b17c1bac2aebd9dcf197f3eb9cd30553c5852a3187ad162e1a03a", - "sha256:d9e96492dd51fae31e60837736b38fe42a187b5404c16606ff7ee7cd582d4c60" + "sha256:4e73a757831fc3ca4de2859c422564239a31d8213d09a2a666e375807034d2ba", + "sha256:c633ebde8580f241f274c1f8994a665c0e54a17724fecd0cae2f079e09c36d3f" ], - "version": "==2.1.0" + "version": "==2.2.2" }, "soupsieve": { "hashes": [ "sha256:1634eea42ab371d3d346309b93df7870a88610f0725d47528be902a0d95ecc55", "sha256:a59dc181727e95d25f781f0eb4fd1825ff45590ec8ff49eadfd7f1a537cc0232" ], + "markers": "python_version >= '3.5'", "version": "==2.0.1" }, "sphinx": { @@ -573,6 +582,7 @@ "sha256:806111e5e962be97c29ec4c1e7fe277bfd19e9652fb1a4392105b43e01af885a", "sha256:a072735ec80e7675e3f432fcae8610ecf509c5f1869d17e2eecff44389cdbc58" ], + "markers": "python_version >= '3.5'", "version": "==1.0.2" }, "sphinxcontrib-devhelp": { @@ -580,6 +590,7 @@ "sha256:8165223f9a335cc1af7ffe1ed31d2871f325254c0423bc0c4c7cd1c1e4734a2e", "sha256:ff7f1afa7b9642e7060379360a67e9c41e8f3121f2ce9164266f61b9f4b338e4" ], + "markers": "python_version >= '3.5'", "version": "==1.0.2" }, "sphinxcontrib-htmlhelp": { @@ -587,6 +598,7 @@ "sha256:3c0bc24a2c41e340ac37c85ced6dafc879ab485c095b1d65d2461ac2f7cca86f", "sha256:e8f5bb7e31b2dbb25b9cc435c8ab7a79787ebf7f906155729338f3156d93659b" ], + "markers": "python_version >= '3.5'", "version": "==1.0.3" }, "sphinxcontrib-jsmath": { @@ -594,6 +606,7 @@ "sha256:2ec2eaebfb78f3f2078e73666b1415417a116cc848b72e5172e596c871103178", "sha256:a9925e4a4587247ed2191a22df5f6970656cb8ca2bd6284309578f2153e0c4b8" ], + "markers": "python_version >= '3.5'", "version": "==1.0.1" }, "sphinxcontrib-qthelp": { @@ -601,6 +614,7 @@ "sha256:4c33767ee058b70dba89a6fc5c1892c0d57a54be67ddd3e7875a18d14cba5a72", "sha256:bd9fc24bcb748a8d51fd4ecaade681350aa63009a347a8c14e637895444dfab6" ], + "markers": "python_version >= '3.5'", "version": "==1.0.3" }, "sphinxcontrib-serializinghtml": { @@ -608,6 +622,7 @@ "sha256:eaa0eccc86e982a9b939b2b82d12cc5d013385ba5eadcc7e4fed23f4405f77bc", "sha256:f242a81d423f59617a8e5cf16f5d4d74e28ee9a66f9e5b637a18082991db5a9a" ], + "markers": "python_version >= '3.5'", "version": "==1.1.4" }, "statsd": { @@ -623,6 +638,7 @@ "sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527", "sha256:88206b0eb87e6d677d424843ac5209e3fb9d0190d0ee169599165ec25e9d9115" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'", "version": "==1.25.9" }, "websockets": { @@ -650,6 +666,7 @@ "sha256:e898a0863421650f0bebac8ba40840fc02258ef4714cb7e1fd76b6a6354bda36", "sha256:f8a7bff6e8664afc4e6c28b983845c5bc14965030e3fb98789734d416af77c4b" ], + "markers": "python_full_version >= '3.6.1'", "version": "==8.1" }, "yarl": { @@ -672,6 +689,7 @@ "sha256:d8cdee92bc930d8b09d8bd2043cedd544d9c8bd7436a77678dd602467a993080", "sha256:e15199cdb423316e15f108f51249e44eb156ae5dba232cb73be555324a1d49c2" ], + "markers": "python_version >= '3.5'", "version": "==1.4.2" } }, @@ -688,6 +706,7 @@ "sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c", "sha256:f7b7ce16570fe9965acd6d30101a28f62fb4a7f9e926b3bbc9b61f8b04247e72" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==19.3.0" }, "cfgv": { @@ -695,50 +714,55 @@ "sha256:1ccf53320421aeeb915275a196e23b3b8ae87dea8ac6698b1638001d4a486d53", "sha256:c8e8f552ffcc6194f4e18dd4f68d9aef0c0d58ae7e7be8c82bee3c5e9edfa513" ], + "markers": "python_full_version >= '3.6.1'", "version": "==3.1.0" }, "coverage": { "hashes": [ - "sha256:00f1d23f4336efc3b311ed0d807feb45098fc86dee1ca13b3d6768cdab187c8a", - "sha256:01333e1bd22c59713ba8a79f088b3955946e293114479bbfc2e37d522be03355", - "sha256:0cb4be7e784dcdc050fc58ef05b71aa8e89b7e6636b99967fadbdba694cf2b65", - "sha256:0e61d9803d5851849c24f78227939c701ced6704f337cad0a91e0972c51c1ee7", - "sha256:1601e480b9b99697a570cea7ef749e88123c04b92d84cedaa01e117436b4a0a9", - "sha256:2742c7515b9eb368718cd091bad1a1b44135cc72468c731302b3d641895b83d1", - "sha256:2d27a3f742c98e5c6b461ee6ef7287400a1956c11421eb574d843d9ec1f772f0", - "sha256:402e1744733df483b93abbf209283898e9f0d67470707e3c7516d84f48524f55", - "sha256:5c542d1e62eece33c306d66fe0a5c4f7f7b3c08fecc46ead86d7916684b36d6c", - "sha256:5f2294dbf7875b991c381e3d5af2bcc3494d836affa52b809c91697449d0eda6", - "sha256:6402bd2fdedabbdb63a316308142597534ea8e1895f4e7d8bf7476c5e8751fef", - "sha256:66460ab1599d3cf894bb6baee8c684788819b71a5dc1e8fa2ecc152e5d752019", - "sha256:782caea581a6e9ff75eccda79287daefd1d2631cc09d642b6ee2d6da21fc0a4e", - "sha256:79a3cfd6346ce6c13145731d39db47b7a7b859c0272f02cdb89a3bdcbae233a0", - "sha256:7a5bdad4edec57b5fb8dae7d3ee58622d626fd3a0be0dfceda162a7035885ecf", - "sha256:8fa0cbc7ecad630e5b0f4f35b0f6ad419246b02bc750de7ac66db92667996d24", - "sha256:a027ef0492ede1e03a8054e3c37b8def89a1e3c471482e9f046906ba4f2aafd2", - "sha256:a3f3654d5734a3ece152636aad89f58afc9213c6520062db3978239db122f03c", - "sha256:a82b92b04a23d3c8a581fc049228bafde988abacba397d57ce95fe95e0338ab4", - "sha256:acf3763ed01af8410fc36afea23707d4ea58ba7e86a8ee915dfb9ceff9ef69d0", - "sha256:adeb4c5b608574a3d647011af36f7586811a2c1197c861aedb548dd2453b41cd", - "sha256:b83835506dfc185a319031cf853fa4bb1b3974b1f913f5bb1a0f3d98bdcded04", - "sha256:bb28a7245de68bf29f6fb199545d072d1036a1917dca17a1e75bbb919e14ee8e", - "sha256:bf9cb9a9fd8891e7efd2d44deb24b86d647394b9705b744ff6f8261e6f29a730", - "sha256:c317eaf5ff46a34305b202e73404f55f7389ef834b8dbf4da09b9b9b37f76dd2", - "sha256:dbe8c6ae7534b5b024296464f387d57c13caa942f6d8e6e0346f27e509f0f768", - "sha256:de807ae933cfb7f0c7d9d981a053772452217df2bf38e7e6267c9cbf9545a796", - "sha256:dead2ddede4c7ba6cb3a721870f5141c97dc7d85a079edb4bd8d88c3ad5b20c7", - "sha256:dec5202bfe6f672d4511086e125db035a52b00f1648d6407cc8e526912c0353a", - "sha256:e1ea316102ea1e1770724db01998d1603ed921c54a86a2efcb03428d5417e489", - "sha256:f90bfc4ad18450c80b024036eaf91e4a246ae287701aaa88eaebebf150868052" - ], - "index": "pypi", - "version": "==5.1" + "sha256:0fc4e0d91350d6f43ef6a61f64a48e917637e1dcfcba4b4b7d543c628ef82c2d", + "sha256:10f2a618a6e75adf64329f828a6a5b40244c1c50f5ef4ce4109e904e69c71bd2", + "sha256:12eaccd86d9a373aea59869bc9cfa0ab6ba8b1477752110cb4c10d165474f703", + "sha256:1874bdc943654ba46d28f179c1846f5710eda3aeb265ff029e0ac2b52daae404", + "sha256:1dcebae667b73fd4aa69237e6afb39abc2f27520f2358590c1b13dd90e32abe7", + "sha256:1e58fca3d9ec1a423f1b7f2aa34af4f733cbfa9020c8fe39ca451b6071237405", + "sha256:214eb2110217f2636a9329bc766507ab71a3a06a8ea30cdeebb47c24dce5972d", + "sha256:25fe74b5b2f1b4abb11e103bb7984daca8f8292683957d0738cd692f6a7cc64c", + "sha256:32ecee61a43be509b91a526819717d5e5650e009a8d5eda8631a59c721d5f3b6", + "sha256:3740b796015b889e46c260ff18b84683fa2e30f0f75a171fb10d2bf9fb91fc70", + "sha256:3b2c34690f613525672697910894b60d15800ac7e779fbd0fccf532486c1ba40", + "sha256:41d88736c42f4a22c494c32cc48a05828236e37c991bd9760f8923415e3169e4", + "sha256:42fa45a29f1059eda4d3c7b509589cc0343cd6bbf083d6118216830cd1a51613", + "sha256:4bb385a747e6ae8a65290b3df60d6c8a692a5599dc66c9fa3520e667886f2e10", + "sha256:509294f3e76d3f26b35083973fbc952e01e1727656d979b11182f273f08aa80b", + "sha256:5c74c5b6045969b07c9fb36b665c9cac84d6c174a809fc1b21bdc06c7836d9a0", + "sha256:60a3d36297b65c7f78329b80120f72947140f45b5c7a017ea730f9112b40f2ec", + "sha256:6f91b4492c5cde83bfe462f5b2b997cdf96a138f7c58b1140f05de5751623cf1", + "sha256:7403675df5e27745571aba1c957c7da2dacb537c21e14007ec3a417bf31f7f3d", + "sha256:87bdc8135b8ee739840eee19b184804e5d57f518578ffc797f5afa2c3c297913", + "sha256:8a3decd12e7934d0254939e2bf434bf04a5890c5bf91a982685021786a08087e", + "sha256:9702e2cb1c6dec01fb8e1a64c015817c0800a6eca287552c47a5ee0ebddccf62", + "sha256:a4d511012beb967a39580ba7d2549edf1e6865a33e5fe51e4dce550522b3ac0e", + "sha256:bbb387811f7a18bdc61a2ea3d102be0c7e239b0db9c83be7bfa50f095db5b92a", + "sha256:bfcc811883699ed49afc58b1ed9f80428a18eb9166422bce3c31a53dba00fd1d", + "sha256:c32aa13cc3fe86b0f744dfe35a7f879ee33ac0a560684fef0f3e1580352b818f", + "sha256:ca63dae130a2e788f2b249200f01d7fa240f24da0596501d387a50e57aa7075e", + "sha256:d54d7ea74cc00482a2410d63bf10aa34ebe1c49ac50779652106c867f9986d6b", + "sha256:d67599521dff98ec8c34cd9652cbcfe16ed076a2209625fca9dc7419b6370e5c", + "sha256:d82db1b9a92cb5c67661ca6616bdca6ff931deceebb98eecbd328812dab52032", + "sha256:d9ad0a988ae20face62520785ec3595a5e64f35a21762a57d115dae0b8fb894a", + "sha256:ebf2431b2d457ae5217f3a1179533c456f3272ded16f8ed0b32961a6d90e38ee", + "sha256:ed9a21502e9223f563e071759f769c3d6a2e1ba5328c31e86830368e8d78bc9c", + "sha256:f50632ef2d749f541ca8e6c07c9928a37f87505ce3a9f20c8446ad310f1aa87b" + ], + "index": "pypi", + "version": "==5.2" }, "distlib": { "hashes": [ - "sha256:2e166e231a26b36d6dfe35a48c4464346620f8645ed0ace01ee31822b288de21" + "sha256:8c09de2c67b3e7deef7184574fc060ab8a793e7adbb183d942c389c8b13c52fb", + "sha256:edf6116872c863e1aa9d5bb7cb5e05a022c519a4594dc703843343a9ddd9bff1" ], - "version": "==0.3.0" + "version": "==0.3.1" }, "filelock": { "hashes": [ @@ -749,19 +773,19 @@ }, "flake8": { "hashes": [ - "sha256:c69ac1668e434d37a2d2880b3ca9aafd54b3a10a3ac1ab101d22f29e29cf8634", - "sha256:ccaa799ef9893cebe69fdfefed76865aeaefbb94cb8545617b2298786a4de9a5" + "sha256:15e351d19611c887e482fb960eae4d44845013cc142d42896e9862f775d8cf5c", + "sha256:f04b9fcbac03b0a3e58c0ab3a0ecc462e023a9faf046d57794184028123aa208" ], "index": "pypi", - "version": "==3.8.2" + "version": "==3.8.3" }, "flake8-annotations": { "hashes": [ - "sha256:9091d920406a7ff10e401e0dd1baa396d1d7d2e3d101a9beecf815f5894ad554", - "sha256:f59fdceb8c8f380a20aed20e1ba8a57bde05935958166c52be2249f113f7ab75" + "sha256:babc81a17a5f1a63464195917e20d3e8663fb712b3633d4522dbfc407cff31b3", + "sha256:fcd833b415726a7a374922c95a5c47a7a4d8ea71cb4a586369c665e7476146e1" ], "index": "pypi", - "version": "==2.1.0" + "version": "==2.2.0" }, "flake8-bugbear": { "hashes": [ @@ -819,10 +843,11 @@ }, "identify": { "hashes": [ - "sha256:0f3c3aac62b51b86fea6ff52fe8ff9e06f57f10411502443809064d23e16f1c2", - "sha256:f9ad3d41f01e98eb066b6e05c5b184fd1e925fadec48eb165b4e01c72a1ef3a7" + "sha256:c4d07f2b979e3931894170a9e0d4b8281e6905ea6d018c326f7ffefaf20db680", + "sha256:dac33eff90d57164e289fb20bf4e131baef080947ee9bf45efcd0da8d19064bf" ], - "version": "==1.4.16" + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==1.4.21" }, "mccabe": { "hashes": [ @@ -833,31 +858,32 @@ }, "nodeenv": { "hashes": [ - "sha256:5b2438f2e42af54ca968dd1b374d14a1194848955187b0e5e4be1f73813a5212" + "sha256:4b0b77afa3ba9b54f4b6396e60b0c83f59eaeb2d63dc3cc7a70f7f4af96c82bc" ], - "version": "==1.3.5" + "version": "==1.4.0" }, "pep8-naming": { "hashes": [ - "sha256:5d9f1056cb9427ce344e98d1a7f5665710e2f20f748438e308995852cfa24164", - "sha256:f3b4a5f9dd72b991bf7d8e2a341d2e1aa3a884a769b5aaac4f56825c1763bf3a" + "sha256:a1dd47dd243adfe8a83616e27cf03164960b507530f155db94e10b36a6cd6724", + "sha256:f43bfe3eea7e0d73e8b5d07d6407ab47f2476ccaeff6937c84275cd30b016738" ], "index": "pypi", - "version": "==0.10.0" + "version": "==0.11.1" }, "pre-commit": { "hashes": [ - "sha256:5559e09afcac7808933951ffaf4ff9aac524f31efbc3f24d021540b6c579813c", - "sha256:703e2e34cbe0eedb0d319eff9f7b83e2022bb5a3ab5289a6a8841441076514d0" + "sha256:1657663fdd63a321a4a739915d7d03baedd555b25054449090f97bb0cb30a915", + "sha256:e8b1315c585052e729ab7e99dcca5698266bedce9067d21dc909c23e3ceed626" ], "index": "pypi", - "version": "==2.4.0" + "version": "==2.6.0" }, "pycodestyle": { "hashes": [ "sha256:2295e7b2f6b5bd100585ebcb1f616591b652db8a741695b3d8f5d28bdc934367", "sha256:c58a7d2815e0e8d7972bf1803331fb0152f867bd89adf8a01dfd55085434192e" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==2.6.0" }, "pydocstyle": { @@ -865,6 +891,7 @@ "sha256:da7831660b7355307b32778c4a0dbfb137d89254ef31a2b2978f50fc0b4d7586", "sha256:f4f5d210610c2d153fae39093d44224c17429e2ad7da12a8b419aba5c2f614b5" ], + "markers": "python_version >= '3.5'", "version": "==5.0.2" }, "pyflakes": { @@ -872,6 +899,7 @@ "sha256:0d94e0e05a19e57a99444b6ddcf9a6eb2e5c68d3ca1e98e90707af8152c90a92", "sha256:35b2d75ee967ea93b55750aa9edbbf72813e06a66ba54438df2cfac9e3c27fc8" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==2.2.0" }, "pyyaml": { @@ -896,6 +924,7 @@ "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259", "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==1.15.0" }, "snowballstemmer": { @@ -922,10 +951,11 @@ }, "virtualenv": { "hashes": [ - "sha256:a116629d4e7f4d03433b8afa27f43deba09d48bc48f5ecefa4f015a178efb6cf", - "sha256:a730548b27366c5e6cbdf6f97406d861cccece2e22275e8e1a757aeff5e00c70" + "sha256:c11a475400e98450403c0364eb3a2d25d42f71cf1493da64390487b666de4324", + "sha256:e10cc66f40cbda459720dfe1d334c4dc15add0d80f09108224f171006a97a172" ], - "version": "==20.0.21" + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==20.0.26" } } } diff --git a/bot/__main__.py b/bot/__main__.py index 4e0d4a111..7e92d1a25 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -29,6 +29,7 @@ bot = Bot( activity=discord.Game(name="Commands: !help"), case_insensitive=True, max_messages=10_000, + allowed_mentions=discord.AllowedMentions(everyone=False, roles=False) ) # Internal/debug -- cgit v1.2.3 From 36330b1e386f1d3964eb34f5c5cc4afdf988358f Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 7 Jul 2020 12:15:24 -0700 Subject: Allow owners, admins, and mods roles to be pinged --- bot/__main__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/__main__.py b/bot/__main__.py index 7e92d1a25..37e62c2f1 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -24,12 +24,13 @@ sentry_sdk.init( ] ) +allowed_roles = [discord.Object(id_) for id_ in constants.MODERATION_ROLES] bot = Bot( command_prefix=when_mentioned_or(constants.Bot.prefix), activity=discord.Game(name="Commands: !help"), case_insensitive=True, max_messages=10_000, - allowed_mentions=discord.AllowedMentions(everyone=False, roles=False) + allowed_mentions=discord.AllowedMentions(everyone=False, roles=allowed_roles) ) # Internal/debug -- cgit v1.2.3 From 8f36817fb4a8c995e92986db3199763b7110aa9e Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Tue, 7 Jul 2020 20:24:04 +0100 Subject: Add git to Docker image --- Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Dockerfile b/Dockerfile index 06a538b2a..c51b9dff6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,9 @@ ENV PIP_NO_CACHE_DIR=false \ PIPENV_HIDE_EMOJIS=1 \ PIPENV_IGNORE_VIRTUALENVS=1 \ PIPENV_NOSPIN=1 + +RUN apt-get update +RUN apt-get install -y git # Install pipenv RUN pip install -U pipenv -- cgit v1.2.3 From baf10b6327ba8ca6f3b2b644613170ee5f937e95 Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Tue, 7 Jul 2020 20:28:17 +0100 Subject: Fix git install in Dockerfile --- Dockerfile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index c51b9dff6..0b1674e7a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,9 +5,11 @@ ENV PIP_NO_CACHE_DIR=false \ PIPENV_HIDE_EMOJIS=1 \ PIPENV_IGNORE_VIRTUALENVS=1 \ PIPENV_NOSPIN=1 - -RUN apt-get update -RUN apt-get install -y git + +RUN apt-get -y update \ + && apt-get install -y \ + git \ + && rm -rf /var/lib/apt/lists/* # Install pipenv RUN pip install -U pipenv -- cgit v1.2.3 From ba1b9081cfbc245b7a8fd8d41f7ab7173b097a31 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 7 Jul 2020 12:35:06 -0700 Subject: Don't install discord.py as editable It may be causing it to not be cached in Azure. --- Pipfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pipfile b/Pipfile index e25e7b1e1..29aa1a08f 100644 --- a/Pipfile +++ b/Pipfile @@ -12,7 +12,7 @@ beautifulsoup4 = "~=4.9" colorama = {version = "~=0.4.3",sys_platform = "== 'win32'"} coloredlogs = "~=14.0" deepdiff = "~=4.0" -discord-py = {git = "https://github.com/Rapptz/discord.py.git",ref = "e971e2f16cba22decd25db6b44e9cc84adf08555",editable = true} +discord-py = {git = "https://github.com/Rapptz/discord.py.git",ref = "e971e2f16cba22decd25db6b44e9cc84adf08555"} fakeredis = "~=1.4" feedparser = "~=5.2" fuzzywuzzy = "~=0.17" -- cgit v1.2.3 From a1a44be2b57e35fbaee8cac024fdd74c218c72b1 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 7 Jul 2020 12:42:24 -0700 Subject: Re-lock Pipfile Forgot to do this after removing editable. --- Pipfile.lock | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Pipfile.lock b/Pipfile.lock index 12325f2a7..a522e20d3 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "f6fac6e59e6579ea4cc0e2b49a5fa59785137d02e6c6a7df47ef502375313703" + "sha256": "6404ca2550369b6416801688b4382d22fdba178d9319c4a68bd207d1e5aaeaab" }, "pipfile-spec": 6, "requires": { @@ -178,7 +178,6 @@ "version": "==4.3.2" }, "discord-py": { - "editable": true, "git": "https://github.com/Rapptz/discord.py.git", "ref": "e971e2f16cba22decd25db6b44e9cc84adf08555" }, -- cgit v1.2.3 From a201e76c805fe69e70e39bbd8a24f81ee5d0fe9b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 8 Jul 2020 21:39:17 +0300 Subject: Help Channels: Simplify unpinning Remove complex None checking message fetching and replace it with `bot.http.unpin_message` and catch exception when message don't exist. --- bot/cogs/help_channels.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index bb97759ee..9313efc67 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -554,19 +554,12 @@ class HelpChannels(Scheduler, commands.Cog): """ msg_id = await self.question_messages.pop(channel.id) - # When message ID exist in cache, try to get it from cache first. When this fail, use API request. - # When this return 404, this mean that message is deleted and can't be unpinned. - if msg_id: - msg = discord.utils.get(self.bot.cached_messages, id=msg_id) - if msg is None: - try: - msg = await channel.fetch_message(msg_id) - except discord.NotFound: - log.debug(f"Can't unpin message {msg_id} because this is deleted.") - - # When we got message, then unpin it - if msg: - await msg.unpin() + try: + await self.bot.http.unpin_message(channel.id, msg_id) + except discord.HTTPException: + log.trace(f"Message {msg_id} don't exist, can't unpin.") + else: + log.trace(f"Unpinned message {msg_id}.") log.info(f"Moving #{channel} ({channel.id}) to the Dormant category.") -- cgit v1.2.3 From d6c775bc96d8b913677a87c9025a6194831d4b3b Mon Sep 17 00:00:00 2001 From: swfarnsworth Date: Wed, 8 Jul 2020 15:56:49 -0400 Subject: Initial commit for proposed range-len command --- bot/resources/tags/range-len.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 bot/resources/tags/range-len.md diff --git a/bot/resources/tags/range-len.md b/bot/resources/tags/range-len.md new file mode 100644 index 000000000..b1c973647 --- /dev/null +++ b/bot/resources/tags/range-len.md @@ -0,0 +1,19 @@ +Iterating over `range(len(...))` is a common approach to accessing each item +in an ordered collection. + +```py +for i in range(len(my_list)): + do_something(my_list[i]) +``` + +The pythonic syntax is much simpler, and is +guaranteed to produce elements in the same order: + +```py +for item in my_list: + do_something(item) +``` + +Python has other solutions for cases when the index itself might be needed. +To get the element at the same index from two or more lists, use [zip](https://docs.python.org/3/library/functions.html#zip). +To get both the index and the element at that index, use [enumerate](https://docs.python.org/3/library/functions.html#enumerate). -- cgit v1.2.3 From 4775b174597e72100641b97ea6ef2c9e63622d60 Mon Sep 17 00:00:00 2001 From: Slushie Date: Wed, 8 Jul 2020 21:28:17 +0100 Subject: Edit BadArgument error message --- bot/cogs/error_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index 5de961116..a7f8074e2 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -170,7 +170,7 @@ class ErrorHandler(Cog): await prepared_help_command self.bot.stats.incr("errors.too_many_arguments") elif isinstance(e, errors.BadArgument): - await ctx.send(f"Bad argument: {e}\n") + await ctx.send("Bad argument: Please double check your input arguments and try again.\n") await prepared_help_command self.bot.stats.incr("errors.bad_argument") elif isinstance(e, errors.BadUnionArgument): -- cgit v1.2.3 From 9060c909f6816eb2fff97a41d709a1c67b034af1 Mon Sep 17 00:00:00 2001 From: Slushie Date: Wed, 8 Jul 2020 21:29:14 +0100 Subject: Create a filtering function to filter eval results --- bot/cogs/filtering.py | 172 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 120 insertions(+), 52 deletions(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 76ea68660..ae77ad7f0 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -2,7 +2,7 @@ import asyncio import logging import re from datetime import datetime, timedelta -from typing import List, Mapping, Optional, Union +from typing import List, Mapping, Optional, Tuple, Union import dateutil import discord.errors @@ -200,24 +200,66 @@ class Filtering(Cog, Scheduler): # Update time when alert sent await self.name_alerts.set(member.id, datetime.utcnow().timestamp()) + async def _filter_eval(self, result: str, msg: Message) -> bool: + """ + Filter the result of an !eval to see if it violates any of our rules, and then respond accordingly. + + Also requires the original message, to check whether to filter and for mod logs. + Returns whether a filter was triggered or not. + """ + # Should we filter this message? + if self._check_filter(msg): + for filter_name, _filter in self.filters.items(): + # Is this specific filter enabled in the config? + # We also do not need to worry about filters that take the full message, + # since all we have is an arbitrary string. + if _filter["enabled"] and _filter["content_only"]: + match = await _filter["function"](result) + + if match: + # If this is a filter (not a watchlist), we set the variable so we know + # that it has been triggered + if _filter["type"] == "filter": + filter_triggered = True + + # We do not have to check against DM channels since !eval cannot be used there. + channel_str = f"in {msg.channel.mention}" + + message_content, additional_embeds, additional_embeds_msg = self._add_stats( + filter_name, match, result + ) + + message = ( + f"The {filter_name} {_filter['type']} was triggered " + f"by **{msg.author}** " + f"(`{msg.author.id}`) {channel_str} using !eval with " + f"[the following message]({msg.jump_url}):\n\n" + f"{message_content}" + ) + + log.debug(message) + + # Send pretty mod log embed to mod-alerts + await self.mod_log.send_log_message( + icon_url=Icons.filtering, + colour=Colour(Colours.soft_red), + title=f"{_filter['type'].title()} triggered!", + text=message, + thumbnail=msg.author.avatar_url_as(static_format="png"), + channel_id=Channels.mod_alerts, + ping_everyone=Filter.ping_everyone, + additional_embeds=additional_embeds, + additional_embeds_msg=additional_embeds_msg + ) + + break # We don't want multiple filters to trigger + + return filter_triggered + async def _filter_message(self, msg: Message, delta: Optional[int] = None) -> None: """Filter the input message to see if it violates any of our rules, and then respond accordingly.""" # Should we filter this message? - role_whitelisted = False - - if type(msg.author) is Member: # Only Member has roles, not User. - for role in msg.author.roles: - if role.id in Filter.role_whitelist: - role_whitelisted = True - - filter_message = ( - msg.channel.id not in Filter.channel_whitelist # Channel not in whitelist - and not role_whitelisted # Role not in whitelist - and not msg.author.bot # Author not a bot - ) - - # If none of the above, we can start filtering. - if filter_message: + if self._check_filter(msg): for filter_name, _filter in self.filters.items(): # Is this specific filter enabled in the config? if _filter["enabled"]: @@ -276,16 +318,9 @@ class Filtering(Cog, Scheduler): else: channel_str = f"in {msg.channel.mention}" - # Word and match stats for watch_regex - if filter_name == "watch_regex": - surroundings = match.string[max(match.start() - 10, 0): match.end() + 10] - message_content = ( - f"**Match:** '{match[0]}'\n" - f"**Location:** '...{escape_markdown(surroundings)}...'\n" - f"\n**Original Message:**\n{escape_markdown(msg.content)}" - ) - else: # Use content of discord Message - message_content = msg.content + message_content, additional_embeds, additional_embeds_msg = self._add_stats( + filter_name, match, msg.content + ) message = ( f"The {filter_name} {_filter['type']} was triggered " @@ -297,30 +332,6 @@ class Filtering(Cog, Scheduler): log.debug(message) - self.bot.stats.incr(f"filters.{filter_name}") - - additional_embeds = None - additional_embeds_msg = None - - # The function returns True for invalid invites. - # They have no data so additional embeds can't be created for them. - if filter_name == "filter_invites" and match is not True: - additional_embeds = [] - for invite, data in match.items(): - embed = discord.Embed(description=( - f"**Members:**\n{data['members']}\n" - f"**Active:**\n{data['active']}" - )) - embed.set_author(name=data["name"]) - embed.set_thumbnail(url=data["icon"]) - embed.set_footer(text=f"Guild Invite Code: {invite}") - additional_embeds.append(embed) - additional_embeds_msg = "For the following guild(s):" - - elif filter_name == "watch_rich_embeds": - additional_embeds = msg.embeds - additional_embeds_msg = "With the following embed(s):" - # Send pretty mod log embed to mod-alerts await self.mod_log.send_log_message( icon_url=Icons.filtering, @@ -336,6 +347,63 @@ class Filtering(Cog, Scheduler): break # We don't want multiple filters to trigger + def _add_stats(self, name: str, match: Union[re.Match, dict, bool, List[discord.Embed]], content: str) -> Tuple[ + str, Optional[List[discord.Embed]], Optional[str] + ]: + """Adds relevant statistical information to the relevant filter and increments the bot's stats.""" + # Word and match stats for watch_regex + if name == "watch_regex": + surroundings = match.string[max(match.start() - 10, 0): match.end() + 10] + message_content = ( + f"**Match:** '{match[0]}'\n" + f"**Location:** '...{escape_markdown(surroundings)}...'\n" + f"\n**Original Message:**\n{escape_markdown(content)}" + ) + else: # Use original content + message_content = content + + additional_embeds = None + additional_embeds_msg = None + + self.bot.stats.incr(f"filters.{name}") + + # The function returns True for invalid invites. + # They have no data so additional embeds can't be created for them. + if name == "filter_invites" and match is not True: + additional_embeds = [] + for invite, data in match.items(): + embed = discord.Embed(description=( + f"**Members:**\n{data['members']}\n" + f"**Active:**\n{data['active']}" + )) + embed.set_author(name=data["name"]) + embed.set_thumbnail(url=data["icon"]) + embed.set_footer(text=f"Guild Invite Code: {invite}") + additional_embeds.append(embed) + additional_embeds_msg = "For the following guild(s):" + + elif name == "watch_rich_embeds": + additional_embeds = match + additional_embeds_msg = "With the following embed(s):" + + return message_content, additional_embeds, additional_embeds_msg + + @staticmethod + def _check_filter(msg: Message) -> bool: + """Check whitelists to see if we should filter this message.""" + role_whitelisted = False + + if type(msg.author) is Member: # Only Member has roles, not User. + for role in msg.author.roles: + if role.id in Filter.role_whitelist: + role_whitelisted = True + + return ( + msg.channel.id not in Filter.channel_whitelist # Channel not in whitelist + and not role_whitelisted # Role not in whitelist + and not msg.author.bot # Author not a bot + ) + @staticmethod async def _has_watch_regex_match(text: str) -> Union[bool, re.Match]: """ @@ -428,7 +496,7 @@ class Filtering(Cog, Scheduler): return invite_data if invite_data else False @staticmethod - async def _has_rich_embed(msg: Message) -> bool: + async def _has_rich_embed(msg: Message) -> Union[bool, List[discord.Embed]]: """Determines if `msg` contains any rich embeds not auto-generated from a URL.""" if msg.embeds: for embed in msg.embeds: @@ -437,7 +505,7 @@ class Filtering(Cog, Scheduler): if not embed.url or embed.url not in urls: # If `embed.url` does not exist or if `embed.url` is not part of the content # of the message, it's unlikely to be an auto-generated embed by Discord. - return True + return msg.embeds else: log.trace( "Found a rich embed sent by a regular user account, " -- cgit v1.2.3 From 63846d17a851c97fe073e5c1e27cd65719d2c854 Mon Sep 17 00:00:00 2001 From: Slushie Date: Wed, 8 Jul 2020 21:35:04 +0100 Subject: Call the filter eval command after receiving an eval result --- bot/cogs/snekbox.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bot/cogs/snekbox.py b/bot/cogs/snekbox.py index a2a7574d4..649bab492 100644 --- a/bot/cogs/snekbox.py +++ b/bot/cogs/snekbox.py @@ -212,7 +212,12 @@ class Snekbox(Cog): else: self.bot.stats.incr("snekbox.python.success") - response = await ctx.send(msg) + filter_cog = self.bot.get_cog("Filtering") + filter_triggered = await filter_cog._filter_eval(msg, ctx.message) + if filter_triggered: + response = await ctx.send("Attempt to circumvent filter detected. Moderator team has been alerted.") + else: + response = await ctx.send(msg) self.bot.loop.create_task( wait_for_deletion(response, user_ids=(ctx.author.id,), client=ctx.bot) ) -- cgit v1.2.3 From 9174125a41793d4703a81dc6783f4244f1634d27 Mon Sep 17 00:00:00 2001 From: swfarnsworth Date: Wed, 8 Jul 2020 17:51:04 -0400 Subject: Removed hard line breaks --- bot/resources/tags/range-len.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/bot/resources/tags/range-len.md b/bot/resources/tags/range-len.md index b1c973647..9b88aab47 100644 --- a/bot/resources/tags/range-len.md +++ b/bot/resources/tags/range-len.md @@ -1,19 +1,15 @@ -Iterating over `range(len(...))` is a common approach to accessing each item -in an ordered collection. +Iterating over `range(len(...))` is a common approach to accessing each item in an ordered collection. ```py for i in range(len(my_list)): do_something(my_list[i]) ``` -The pythonic syntax is much simpler, and is -guaranteed to produce elements in the same order: +The pythonic syntax is much simpler, and is guaranteed to produce elements in the same order: ```py for item in my_list: do_something(item) ``` -Python has other solutions for cases when the index itself might be needed. -To get the element at the same index from two or more lists, use [zip](https://docs.python.org/3/library/functions.html#zip). -To get both the index and the element at that index, use [enumerate](https://docs.python.org/3/library/functions.html#enumerate). +Python has other solutions for cases when the index itself might be needed. To get the element at the same index from two or more lists, use [zip](https://docs.python.org/3/library/functions.html#zip). To get both the index and the element at that index, use [enumerate](https://docs.python.org/3/library/functions.html#enumerate). -- cgit v1.2.3 From 918e1b9ca628abd7867812b32096d05dcf69f32f Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 9 Jul 2020 12:07:30 +0200 Subject: Incidents: use `moderation_roles` constant Better than building the set manually. Tested against regression by comparing the two sets for equality. Suggested by vivax. Co-authored-by: vivax3794 <51753506+vivax3794@users.noreply.github.com> --- bot/cogs/moderation/incidents.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 1a12c8bbd..be46c8202 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -8,7 +8,7 @@ import discord from discord.ext.commands import Cog from bot.bot import Bot -from bot.constants import Channels, Colours, Emojis, Roles, Webhooks +from bot.constants import Channels, Colours, Emojis, Guild, Webhooks from bot.utils.messages import sub_clyde log = logging.getLogger(__name__) @@ -35,8 +35,8 @@ class Signal(Enum): INVESTIGATING = Emojis.incident_investigating -# Reactions from roles not listed here will be removed -ALLOWED_ROLES: t.Set[int] = {Roles.moderators, Roles.admins, Roles.owners} +# Reactions from non-mod roles will be removed +ALLOWED_ROLES: t.Set[int] = set(Guild.moderation_roles) # Message must have all of these emoji to pass the `has_signals` check ALL_SIGNALS: t.Set[str] = {signal.value for signal in Signal} -- cgit v1.2.3 From ddb1f556ace346a97b8639f278fae8915078e78d Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 9 Jul 2020 12:11:16 +0200 Subject: Incidents tests: improve in-line comment wording Co-authored-by: MarkKoz --- tests/bot/cogs/moderation/test_incidents.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index f8d479cef..789a37cd4 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -318,7 +318,7 @@ class TestArchive(TestIncidents): webhook = MockAsyncWebhook() self.cog_instance.bot.fetch_webhook = AsyncMock(return_value=webhook) # Patch in our webhook - # Define our own `incident` for archivation + # Define our own `incident` to be archived incident = MockMessage( content="this is an incident", author=MockUser(name="author_name", avatar_url="author_avatar"), -- cgit v1.2.3 From d10a61d3ef21cbf511304a97a5e2871bd1fcb2dd Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 9 Jul 2020 12:18:21 +0200 Subject: Config: refactor #incidents constants to lexicographical sorting Co-authored-by: MarkKoz --- bot/constants.py | 6 +++--- config-default.yml | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bot/constants.py b/bot/constants.py index b3ef1660f..cd660acee 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -427,12 +427,12 @@ class Webhooks(metaclass=YAMLGetter): section = "guild" subsection = "webhooks" - talent_pool: int big_brother: int - reddit: int - duck_pond: int dev_log: int + duck_pond: int incidents_archive: int + reddit: int + talent_pool: int class Roles(metaclass=YAMLGetter): diff --git a/config-default.yml b/config-default.yml index 4c0196dc5..7fcc27d64 100644 --- a/config-default.yml +++ b/config-default.yml @@ -171,13 +171,13 @@ guild: admin_spam: &ADMIN_SPAM 563594791770914816 defcon: &DEFCON 464469101889454091 helpers: &HELPERS 385474242440986624 + incidents: 714214212200562749 + incidents_archive: 720668923636351037 mods: &MODS 305126844661760000 mod_alerts: &MOD_ALERTS 473092532147060736 mod_spam: &MOD_SPAM 620607373828030464 organisation: &ORGANISATION 551789653284356126 staff_lounge: &STAFF_LOUNGE 464905259261755392 - incidents: 714214212200562749 - incidents_archive: 720668923636351037 # Voice admins_voice: &ADMINS_VOICE 500734494840717332 @@ -250,13 +250,13 @@ guild: - *HELPERS_ROLE webhooks: - talent_pool: 569145364800602132 - big_brother: 569133704568373283 - reddit: 635408384794951680 - duck_pond: 637821475327311927 - dev_log: 680501655111729222 - python_news: &PYNEWS_WEBHOOK 704381182279942324 - incidents_archive: 720671599790915702 + big_brother: 569133704568373283 + dev_log: 680501655111729222 + duck_pond: 637821475327311927 + incidents_archive: 720671599790915702 + python_news: &PYNEWS_WEBHOOK 704381182279942324 + reddit: 635408384794951680 + talent_pool: 569145364800602132 filter: -- cgit v1.2.3 From 5f73f40eeb025e6694443a8bc4535df894b83e4f Mon Sep 17 00:00:00 2001 From: slushiegoose <38522108+slushiegoose@users.noreply.github.com> Date: Thu, 9 Jul 2020 15:29:03 +0100 Subject: Fix missing hypen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Leon Sandøy --- bot/cogs/error_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index a7f8074e2..233851e41 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -170,7 +170,7 @@ class ErrorHandler(Cog): await prepared_help_command self.bot.stats.incr("errors.too_many_arguments") elif isinstance(e, errors.BadArgument): - await ctx.send("Bad argument: Please double check your input arguments and try again.\n") + await ctx.send("Bad argument: Please double-check your input arguments and try again.\n") await prepared_help_command self.bot.stats.incr("errors.bad_argument") elif isinstance(e, errors.BadUnionArgument): -- cgit v1.2.3 From 288c6526e03388bf7ff5b3b1e8b861ad1a7f6e63 Mon Sep 17 00:00:00 2001 From: Slushie Date: Thu, 9 Jul 2020 15:32:27 +0100 Subject: Add missing variable assignment to stop NameErrors occurring --- bot/cogs/filtering.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index ae77ad7f0..4c97073c3 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -207,6 +207,7 @@ class Filtering(Cog, Scheduler): Also requires the original message, to check whether to filter and for mod logs. Returns whether a filter was triggered or not. """ + filter_triggered = False # Should we filter this message? if self._check_filter(msg): for filter_name, _filter in self.filters.items(): -- cgit v1.2.3 From 8cba21c353a728d2c09ad82a425c46ce3f03abf0 Mon Sep 17 00:00:00 2001 From: Steele Farnsworth <32915757+swfarnsworth@users.noreply.github.com> Date: Thu, 9 Jul 2020 11:20:01 -0400 Subject: Update range-len.md Removed all blank lines to improve how it's rendered on Discord; thanks @kwzrd for rendering this! --- bot/resources/tags/range-len.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bot/resources/tags/range-len.md b/bot/resources/tags/range-len.md index 9b88aab47..65665eccf 100644 --- a/bot/resources/tags/range-len.md +++ b/bot/resources/tags/range-len.md @@ -1,15 +1,11 @@ Iterating over `range(len(...))` is a common approach to accessing each item in an ordered collection. - ```py for i in range(len(my_list)): do_something(my_list[i]) ``` - The pythonic syntax is much simpler, and is guaranteed to produce elements in the same order: - ```py for item in my_list: do_something(item) ``` - Python has other solutions for cases when the index itself might be needed. To get the element at the same index from two or more lists, use [zip](https://docs.python.org/3/library/functions.html#zip). To get both the index and the element at that index, use [enumerate](https://docs.python.org/3/library/functions.html#enumerate). -- cgit v1.2.3 From 39651d0410ed292a5f761d9595ba79833dfa167c Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Thu, 9 Jul 2020 10:40:47 -0700 Subject: Update discord.py to fix issue with overwrites Fixes BOT-6T --- Pipfile | 2 +- Pipfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Pipfile b/Pipfile index 29aa1a08f..2d6b45aa9 100644 --- a/Pipfile +++ b/Pipfile @@ -12,7 +12,7 @@ beautifulsoup4 = "~=4.9" colorama = {version = "~=0.4.3",sys_platform = "== 'win32'"} coloredlogs = "~=14.0" deepdiff = "~=4.0" -discord-py = {git = "https://github.com/Rapptz/discord.py.git",ref = "e971e2f16cba22decd25db6b44e9cc84adf08555"} +discord-py = {git = "https://github.com/Rapptz/discord.py.git",ref = "0bc15fa130b8f01fe2d67446a2184d474b0d0ba7"} fakeredis = "~=1.4" feedparser = "~=5.2" fuzzywuzzy = "~=0.17" diff --git a/Pipfile.lock b/Pipfile.lock index a522e20d3..4b9d092d4 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "6404ca2550369b6416801688b4382d22fdba178d9319c4a68bd207d1e5aaeaab" + "sha256": "8a53baefbbd2a0f3fbaf831f028b23d257a5e28b5efa1260661d74604f4113b8" }, "pipfile-spec": 6, "requires": { @@ -179,7 +179,7 @@ }, "discord-py": { "git": "https://github.com/Rapptz/discord.py.git", - "ref": "e971e2f16cba22decd25db6b44e9cc84adf08555" + "ref": "0bc15fa130b8f01fe2d67446a2184d474b0d0ba7" }, "docutils": { "hashes": [ -- cgit v1.2.3 From de924691e4967b85424fe6e802d7f92846bb0850 Mon Sep 17 00:00:00 2001 From: Dennis Pham Date: Sat, 11 Jul 2020 21:46:12 -0400 Subject: Fix comment --- bot/cogs/help_channels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 9313efc67..b06934eff 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -701,7 +701,7 @@ class HelpChannels(Scheduler, commands.Cog): log.info(f"Channel #{channel} was claimed by `{message.author.id}`.") await self.move_to_in_use(channel) await self.revoke_send_permissions(message.author) - # Pin message for better access and storage this to cache + # Pin message for better access and store this to cache try: await message.pin() except discord.NotFound: -- cgit v1.2.3 From 210c0a09b1bced80d03ed9ac81845f5f94c8b687 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 13:23:22 +0200 Subject: Ping @Moderators in ModLog Instead of pinging @everyone, let's just ping the people who actually need to see the mod alerts or the modlogs, which would be the mods. `@everyone` is currently not permitted by our allowed_mentions setting, so this also restores pings to those channels. GitHub #1038 https://github.com/python-discord/bot/issues/1038 --- bot/cogs/antispam.py | 4 ++-- bot/cogs/filtering.py | 2 +- bot/cogs/moderation/modlog.py | 10 +++++----- bot/cogs/watchchannels/watchchannel.py | 4 ++-- bot/constants.py | 4 ++-- config-default.yml | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bot/cogs/antispam.py b/bot/cogs/antispam.py index 0bcca578d..71382bba9 100644 --- a/bot/cogs/antispam.py +++ b/bot/cogs/antispam.py @@ -98,7 +98,7 @@ class DeletionContext: text=mod_alert_message, thumbnail=last_message.author.avatar_url_as(static_format="png"), channel_id=Channels.mod_alerts, - ping_everyone=AntiSpamConfig.ping_everyone + ping_moderators=AntiSpamConfig.ping_moderators ) @@ -132,7 +132,7 @@ class AntiSpam(Cog): await self.mod_log.send_log_message( title="Error: AntiSpam configuration validation failed!", text=body, - ping_everyone=True, + ping_moderators=True, icon_url=Icons.token_removed, colour=Colour.red() ) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 76ea68660..a5d59085f 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -329,7 +329,7 @@ class Filtering(Cog, Scheduler): text=message, thumbnail=msg.author.avatar_url_as(static_format="png"), channel_id=Channels.mod_alerts, - ping_everyone=Filter.ping_everyone, + ping_moderators=Filter.ping_moderators, additional_embeds=additional_embeds, additional_embeds_msg=additional_embeds_msg ) diff --git a/bot/cogs/moderation/modlog.py b/bot/cogs/moderation/modlog.py index ffbb87bbe..a37a9faf5 100644 --- a/bot/cogs/moderation/modlog.py +++ b/bot/cogs/moderation/modlog.py @@ -15,7 +15,7 @@ from discord.ext.commands import Cog, Context from discord.utils import escape_markdown from bot.bot import Bot -from bot.constants import Categories, Channels, Colours, Emojis, Event, Guild as GuildConstant, Icons, URLs +from bot.constants import Categories, Channels, Colours, Emojis, Event, Guild as GuildConstant, Icons, Roles, URLs from bot.utils.time import humanize_delta log = logging.getLogger(__name__) @@ -88,7 +88,7 @@ class ModLog(Cog, name="ModLog"): text: str, thumbnail: t.Optional[t.Union[str, discord.Asset]] = None, channel_id: int = Channels.mod_log, - ping_everyone: bool = False, + ping_moderators: bool = False, files: t.Optional[t.List[discord.File]] = None, content: t.Optional[str] = None, additional_embeds: t.Optional[t.List[discord.Embed]] = None, @@ -114,11 +114,11 @@ class ModLog(Cog, name="ModLog"): if thumbnail: embed.set_thumbnail(url=thumbnail) - if ping_everyone: + if ping_moderators: if content: - content = f"@everyone\n{content}" + content = f"<@&{Roles.moderators}>\n{content}" else: - content = "@everyone" + content = f"<@&{Roles.moderators}>" channel = self.bot.get_channel(channel_id) log_message = await channel.send(content=content, embed=embed, files=files) diff --git a/bot/cogs/watchchannels/watchchannel.py b/bot/cogs/watchchannels/watchchannel.py index 7c58a0fb5..8c4af4581 100644 --- a/bot/cogs/watchchannels/watchchannel.py +++ b/bot/cogs/watchchannels/watchchannel.py @@ -120,7 +120,7 @@ class WatchChannel(metaclass=CogABCMeta): await self.modlog.send_log_message( title=f"Error: Failed to initialize the {self.__class__.__name__} watch channel", text=message, - ping_everyone=True, + ping_moderators=True, icon_url=Icons.token_removed, colour=Color.red() ) @@ -132,7 +132,7 @@ class WatchChannel(metaclass=CogABCMeta): await self.modlog.send_log_message( title=f"Warning: Failed to retrieve user cache for the {self.__class__.__name__} watch channel", text="Could not retrieve the list of watched users from the API and messages will not be relayed.", - ping_everyone=True, + ping_moderators=True, icon_url=Icons.token_removed, colour=Color.red() ) diff --git a/bot/constants.py b/bot/constants.py index a1b392c82..34b312d2d 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -225,7 +225,7 @@ class Filter(metaclass=YAMLGetter): notify_user_invites: bool notify_user_domains: bool - ping_everyone: bool + ping_moderators: bool offensive_msg_delete_days: int guild_invite_whitelist: List[int] domain_blacklist: List[str] @@ -522,7 +522,7 @@ class AntiSpam(metaclass=YAMLGetter): section = 'anti_spam' clean_offending: bool - ping_everyone: bool + ping_moderators: bool punishment: Dict[str, Dict[str, int]] rules: Dict[str, Dict[str, int]] diff --git a/config-default.yml b/config-default.yml index 64c4e715b..5dd96d67a 100644 --- a/config-default.yml +++ b/config-default.yml @@ -269,7 +269,7 @@ filter: notify_user_domains: false # Filter configuration - ping_everyone: true # Ping @everyone when we send a mod-alert? + ping_moderators: true # Ping @everyone when we send a mod-alert? offensive_msg_delete_days: 7 # How many days before deleting an offensive message? guild_invite_whitelist: @@ -428,7 +428,7 @@ urls: anti_spam: # Clean messages that violate a rule. clean_offending: true - ping_everyone: true + ping_moderators: true punishment: role_id: *MUTED_ROLE -- cgit v1.2.3 From 57e210ccfcc91132182029f1d931118e715439b2 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 13:38:02 +0200 Subject: Allow role pings in Syncers and help_channels.py Now that we're running Discord 1.4.0a, we need to explicitely allow all the role mentions for sends that don't use ping one of the globally whitelisted role pings, which are Moderators, Admins and Owners. We were pinging roles other than Mods+ in exactly two cases: - Inside the Syncers, whenever we ask for sync confirmation (if the number of roles or users to sync is unusually high) - In the help_channels.py system, whenever we max out help channels and are unable to create more. This commit addresses both of these. GitHub #1038 https://github.com/python-discord/bot/issues/1038 --- bot/cogs/help_channels.py | 4 +++- bot/cogs/sync/syncers.py | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 187adfe51..fd1a449c1 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -624,11 +624,13 @@ class HelpChannels(Scheduler, commands.Cog): channel = self.bot.get_channel(constants.HelpChannels.notify_channel) mentions = " ".join(f"<@&{role}>" for role in constants.HelpChannels.notify_roles) + allowed_roles = [discord.Object(id_) for id_ in constants.HelpChannels.notify_roles] message = await channel.send( f"{mentions} A new available help channel is needed but there " f"are no more dormant ones. Consider freeing up some in-use channels manually by " - f"using the `{constants.Bot.prefix}dormant` command within the channels." + f"using the `{constants.Bot.prefix}dormant` command within the channels.", + allowed_mentions=discord.AllowedMentions(everyone=False, roles=allowed_roles) ) self.bot.stats.incr("help.out_of_channel_alerts") diff --git a/bot/cogs/sync/syncers.py b/bot/cogs/sync/syncers.py index 536455668..f7ba811bc 100644 --- a/bot/cogs/sync/syncers.py +++ b/bot/cogs/sync/syncers.py @@ -5,6 +5,7 @@ import typing as t from collections import namedtuple from functools import partial +import discord from discord import Guild, HTTPException, Member, Message, Reaction, User from discord.ext.commands import Context @@ -68,7 +69,11 @@ class Syncer(abc.ABC): ) return None - message = await channel.send(f"{self._CORE_DEV_MENTION}{msg_content}") + allowed_roles = [discord.Object(constants.Roles.core_developers)] + message = await channel.send( + f"{self._CORE_DEV_MENTION}{msg_content}", + allowed_mentions=discord.AllowedMentions(everyone=False, roles=allowed_roles) + ) else: await message.edit(content=msg_content) -- cgit v1.2.3 From cb5e361d04cd9c430bca4fb3496284e469d35c98 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 14:40:26 +0200 Subject: Add the #dm_log ID to constants. https://github.com/python-discord/bot/issues/667 --- bot/constants.py | 1 + config-default.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/bot/constants.py b/bot/constants.py index a1b392c82..074699025 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -416,6 +416,7 @@ class Channels(metaclass=YAMLGetter): user_log: int verification: int voice_log: int + dm_log: int class Webhooks(metaclass=YAMLGetter): diff --git a/config-default.yml b/config-default.yml index 64c4e715b..d3ba45f88 100644 --- a/config-default.yml +++ b/config-default.yml @@ -150,6 +150,7 @@ guild: mod_log: &MOD_LOG 282638479504965634 user_log: 528976905546760203 voice_log: 640292421988646961 + dm_log: 653713721625018428 # Off-topic off_topic_0: 291284109232308226 -- cgit v1.2.3 From 9042325f06523e04a2c51b39fd20436cd6eaa3fc Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 14:57:15 +0200 Subject: Refactor Duck Pond embed sender to be a util. https://github.com/python-discord/bot/issues/667 --- bot/cogs/duck_pond.py | 30 ++++++++---------------------- bot/utils/webhooks.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 bot/utils/webhooks.py diff --git a/bot/cogs/duck_pond.py b/bot/cogs/duck_pond.py index 5b6a7fd62..89b4ad0e4 100644 --- a/bot/cogs/duck_pond.py +++ b/bot/cogs/duck_pond.py @@ -1,5 +1,5 @@ import logging -from typing import Optional, Union +from typing import Union import discord from discord import Color, Embed, Member, Message, RawReactionActionEvent, User, errors @@ -7,7 +7,8 @@ from discord.ext.commands import Cog from bot import constants from bot.bot import Bot -from bot.utils.messages import send_attachments, sub_clyde +from bot.utils.messages import send_attachments +from bot.utils.webhooks import send_webhook log = logging.getLogger(__name__) @@ -18,6 +19,7 @@ class DuckPond(Cog): def __init__(self, bot: Bot): self.bot = bot self.webhook_id = constants.Webhooks.duck_pond + self.webhook = None self.bot.loop.create_task(self.fetch_webhook()) async def fetch_webhook(self) -> None: @@ -47,24 +49,6 @@ class DuckPond(Cog): return True return False - async def send_webhook( - self, - content: Optional[str] = None, - username: Optional[str] = None, - avatar_url: Optional[str] = None, - embed: Optional[Embed] = None, - ) -> None: - """Send a webhook to the duck_pond channel.""" - try: - await self.webhook.send( - content=content, - username=sub_clyde(username), - avatar_url=avatar_url, - embed=embed - ) - except discord.HTTPException: - log.exception("Failed to send a message to the Duck Pool webhook") - async def count_ducks(self, message: Message) -> int: """ Count the number of ducks in the reactions of a specific message. @@ -97,7 +81,8 @@ class DuckPond(Cog): clean_content = message.clean_content if clean_content: - await self.send_webhook( + await send_webhook( + webhook=self.webhook, content=message.clean_content, username=message.author.display_name, avatar_url=message.author.avatar_url @@ -111,7 +96,8 @@ class DuckPond(Cog): description=":x: **This message contained an attachment, but it could not be retrieved**", color=Color.red() ) - await self.send_webhook( + await send_webhook( + webhook=self.webhook, embed=e, username=message.author.display_name, avatar_url=message.author.avatar_url diff --git a/bot/utils/webhooks.py b/bot/utils/webhooks.py new file mode 100644 index 000000000..37fdfe907 --- /dev/null +++ b/bot/utils/webhooks.py @@ -0,0 +1,34 @@ +import logging +from typing import Optional + +import discord +from discord import Embed + +from bot.utils.messages import sub_clyde + +log = logging.getLogger(__name__) + + +async def send_webhook( + webhook: discord.Webhook, + content: Optional[str] = None, + username: Optional[str] = None, + avatar_url: Optional[str] = None, + embed: Optional[Embed] = None, + wait: Optional[bool] = False +) -> None: + """ + Send a message using the provided webhook. + + This uses sub_clyde() and tries for an HTTPException to ensure it doesn't crash. + """ + try: + await webhook.send( + content=content, + username=sub_clyde(username), + avatar_url=avatar_url, + embed=embed, + wait=wait, + ) + except discord.HTTPException: + log.exception("Failed to send a message to the webhook!") -- cgit v1.2.3 From 3fd89d59081f2c906fa43265471d235f4f5b4749 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 15:08:54 +0200 Subject: Remove pointless comment This comment violates the DRY principle. Co-authored-by: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> --- config-default.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config-default.yml b/config-default.yml index 5dd96d67a..0f6a25ef2 100644 --- a/config-default.yml +++ b/config-default.yml @@ -269,7 +269,7 @@ filter: notify_user_domains: false # Filter configuration - ping_moderators: true # Ping @everyone when we send a mod-alert? + ping_moderators: true offensive_msg_delete_days: 7 # How many days before deleting an offensive message? guild_invite_whitelist: -- cgit v1.2.3 From ef65033eaed01a2459561dd9fe37133b595f3d3a Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 15:10:00 +0200 Subject: Refactor python_news.py to use webhook util. https://github.com/python-discord/bot/issues/667 --- bot/cogs/python_news.py | 70 ++++++++++++++++++++----------------------------- bot/utils/webhooks.py | 4 +-- 2 files changed, 31 insertions(+), 43 deletions(-) diff --git a/bot/cogs/python_news.py b/bot/cogs/python_news.py index adefd5c7c..1d8f2aeb0 100644 --- a/bot/cogs/python_news.py +++ b/bot/cogs/python_news.py @@ -10,7 +10,7 @@ from discord.ext.tasks import loop from bot import constants from bot.bot import Bot -from bot.utils.messages import sub_clyde +from bot.utils.webhooks import send_webhook PEPS_RSS_URL = "https://www.python.org/dev/peps/peps.rss/" @@ -100,13 +100,20 @@ class PythonNews(Cog): ): continue - msg = await self.send_webhook( + # Build an embed and send a webhook + embed = discord.Embed( title=new["title"], description=new["summary"], timestamp=new_datetime, url=new["link"], - webhook_profile_name=data["feed"]["title"], - footer=data["feed"]["title"] + colour=constants.Colours.soft_green + ) + embed.set_footer(text=data["feed"]["title"], icon_url=AVATAR_URL) + msg = await send_webhook( + webhook=self.webhook, + username=data["feed"]["title"], + embed=embed, + wait=True, ) payload["data"]["pep"].append(pep_nr) @@ -161,15 +168,28 @@ class PythonNews(Cog): content = email_information["content"] link = THREAD_URL.format(id=thread["href"].split("/")[-2], list=maillist) - msg = await self.send_webhook( + + # Build an embed and send a message to the webhook + embed = discord.Embed( title=thread_information["subject"], description=content[:500] + f"... [continue reading]({link})" if len(content) > 500 else content, timestamp=new_date, url=link, - author=f"{email_information['sender_name']} ({email_information['sender']['address']})", - author_url=MAILMAN_PROFILE_URL.format(id=email_information["sender"]["mailman_id"]), - webhook_profile_name=self.webhook_names[maillist], - footer=f"Posted to {self.webhook_names[maillist]}" + colour=constants.Colours.soft_green + ) + embed.set_author( + name=f"{email_information['sender_name']} ({email_information['sender']['address']})", + url=MAILMAN_PROFILE_URL.format(id=email_information["sender"]["mailman_id"]), + ) + embed.set_footer( + text=f"Posted to {self.webhook_names[maillist]}", + icon_url=AVATAR_URL, + ) + msg = await send_webhook( + webhook=self.webhook, + username=self.webhook_names[maillist], + embed=embed, + wait=True, ) payload["data"][maillist].append(thread_information["thread_id"]) @@ -182,38 +202,6 @@ class PythonNews(Cog): await self.bot.api_client.put("bot/bot-settings/news", json=payload) - async def send_webhook(self, - title: str, - description: str, - timestamp: datetime, - url: str, - webhook_profile_name: str, - footer: str, - author: t.Optional[str] = None, - author_url: t.Optional[str] = None, - ) -> discord.Message: - """Send webhook entry and return sent message.""" - embed = discord.Embed( - title=title, - description=description, - timestamp=timestamp, - url=url, - colour=constants.Colours.soft_green - ) - if author and author_url: - embed.set_author( - name=author, - url=author_url - ) - embed.set_footer(text=footer, icon_url=AVATAR_URL) - - return await self.webhook.send( - embed=embed, - username=sub_clyde(webhook_profile_name), - avatar_url=AVATAR_URL, - wait=True - ) - async def get_thread_and_first_mail(self, maillist: str, thread_identifier: str) -> t.Tuple[t.Any, t.Any]: """Get mail thread and first mail from mail.python.org based on `maillist` and `thread_identifier`.""" async with self.bot.http_session.get( diff --git a/bot/utils/webhooks.py b/bot/utils/webhooks.py index 37fdfe907..66f82ec66 100644 --- a/bot/utils/webhooks.py +++ b/bot/utils/webhooks.py @@ -16,14 +16,14 @@ async def send_webhook( avatar_url: Optional[str] = None, embed: Optional[Embed] = None, wait: Optional[bool] = False -) -> None: +) -> discord.Message: """ Send a message using the provided webhook. This uses sub_clyde() and tries for an HTTPException to ensure it doesn't crash. """ try: - await webhook.send( + return await webhook.send( content=content, username=sub_clyde(username), avatar_url=avatar_url, -- cgit v1.2.3 From 3fce243e15996eb81157c198544fcc705e46e1e6 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 15:27:23 +0200 Subject: Relay all DMs and embeds to #dm-log. https://github.com/python-discord/bot/issues/667 --- bot/__main__.py | 1 + bot/cogs/dm_relay.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 bot/cogs/dm_relay.py diff --git a/bot/__main__.py b/bot/__main__.py index 37e62c2f1..49388455a 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -54,6 +54,7 @@ bot.load_extension("bot.cogs.verification") # Feature cogs bot.load_extension("bot.cogs.alias") bot.load_extension("bot.cogs.defcon") +bot.load_extension("bot.cogs.dm_relay") bot.load_extension("bot.cogs.duck_pond") bot.load_extension("bot.cogs.eval") bot.load_extension("bot.cogs.information") diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py new file mode 100644 index 000000000..32ac0e4ee --- /dev/null +++ b/bot/cogs/dm_relay.py @@ -0,0 +1,66 @@ +import logging + +import discord +from discord import Color +from discord.ext.commands import Cog + +from bot import constants +from bot.bot import Bot +from bot.utils.messages import send_attachments +from bot.utils.webhooks import send_webhook + +log = logging.getLogger(__name__) + + +class DMRelay(Cog): + """Debug logging module.""" + + def __init__(self, bot: Bot): + self.bot = bot + self.webhook_id = constants.Webhooks.dm_log + self.webhook = None + self.bot.loop.create_task(self.fetch_webhook()) + + async def fetch_webhook(self) -> None: + """Fetches the webhook object, so we can post to it.""" + await self.bot.wait_until_guild_available() + + try: + self.webhook = await self.bot.fetch_webhook(self.webhook_id) + except discord.HTTPException: + log.exception(f"Failed to fetch webhook with id `{self.webhook_id}`") + + @Cog.listener() + async def on_message(self, message: discord.Message) -> None: + """Relays the message's content and attachments to the dm_log channel.""" + clean_content = message.clean_content + if clean_content: + await send_webhook( + webhook=self.webhook, + content=message.clean_content, + username=message.author.display_name, + avatar_url=message.author.avatar_url + ) + + # Handle any attachments + if message.attachments: + try: + await send_attachments(message, self.webhook) + except (discord.errors.Forbidden, discord.errors.NotFound): + e = discord.Embed( + description=":x: **This message contained an attachment, but it could not be retrieved**", + color=Color.red() + ) + await send_webhook( + webhook=self.webhook, + embed=e, + username=message.author.display_name, + avatar_url=message.author.avatar_url + ) + except discord.HTTPException: + log.exception("Failed to send an attachment to the webhook") + + +def setup(bot: Bot) -> None: + """Load the DMRelay cog.""" + bot.add_cog(DMRelay(bot)) -- cgit v1.2.3 From 5007e736b93017003f02a75d12ce1ef8bae9fd69 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 15:34:39 +0200 Subject: Replace channel ID with webhook ID for dm_log. https://github.com/python-discord/bot/issues/667 --- bot/constants.py | 2 +- config-default.yml | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/bot/constants.py b/bot/constants.py index 074699025..3f44003a8 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -416,7 +416,6 @@ class Channels(metaclass=YAMLGetter): user_log: int verification: int voice_log: int - dm_log: int class Webhooks(metaclass=YAMLGetter): @@ -428,6 +427,7 @@ class Webhooks(metaclass=YAMLGetter): reddit: int duck_pond: int dev_log: int + dm_log: int class Roles(metaclass=YAMLGetter): diff --git a/config-default.yml b/config-default.yml index d3ba45f88..c09902a5d 100644 --- a/config-default.yml +++ b/config-default.yml @@ -150,7 +150,6 @@ guild: mod_log: &MOD_LOG 282638479504965634 user_log: 528976905546760203 voice_log: 640292421988646961 - dm_log: 653713721625018428 # Off-topic off_topic_0: 291284109232308226 @@ -252,10 +251,9 @@ guild: duck_pond: 637821475327311927 dev_log: 680501655111729222 python_news: &PYNEWS_WEBHOOK 704381182279942324 - + dm_log: 654567640664244225 filter: - # What do we filter? filter_zalgo: false filter_invites: true -- cgit v1.2.3 From 4349fdedaae43f35f9821aa61c91a1e76908b0b5 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 15:39:40 +0200 Subject: Only relay DMs, and only from humans. https://github.com/python-discord/bot/issues/667 --- bot/cogs/dm_relay.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index 32ac0e4ee..bb060fe90 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -33,6 +33,10 @@ class DMRelay(Cog): @Cog.listener() async def on_message(self, message: discord.Message) -> None: """Relays the message's content and attachments to the dm_log channel.""" + # Only relay DMs from humans + if message.author.bot or message.guild: + return + clean_content = message.clean_content if clean_content: await send_webhook( -- cgit v1.2.3 From df1730ef5d51223fe1d5a2cfe8c027e5177ae9c7 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 16:30:03 +0200 Subject: Fix DuckPond tests now that send_webhook is gone. Some of the tests were failing because they were expecting send_webhook to be a method of the DuckPond cog, other tests simply were no longer applicable, and have been removed. https://github.com/python-discord/bot/issues/667 --- tests/bot/cogs/test_duck_pond.py | 51 ++++++++++------------------------------ 1 file changed, 12 insertions(+), 39 deletions(-) diff --git a/tests/bot/cogs/test_duck_pond.py b/tests/bot/cogs/test_duck_pond.py index a8c0107c6..cfe10aebf 100644 --- a/tests/bot/cogs/test_duck_pond.py +++ b/tests/bot/cogs/test_duck_pond.py @@ -129,38 +129,6 @@ class DuckPondTests(base.LoggingTestsMixin, unittest.IsolatedAsyncioTestCase): ): self.assertEqual(expected_return, actual_return) - def test_send_webhook_correctly_passes_on_arguments(self): - """The `send_webhook` method should pass the arguments to the webhook correctly.""" - self.cog.webhook = helpers.MockAsyncWebhook() - - content = "fake content" - username = "fake username" - avatar_url = "fake avatar_url" - embed = "fake embed" - - asyncio.run(self.cog.send_webhook(content, username, avatar_url, embed)) - - self.cog.webhook.send.assert_called_once_with( - content=content, - username=username, - avatar_url=avatar_url, - embed=embed - ) - - def test_send_webhook_logs_when_sending_message_fails(self): - """The `send_webhook` method should catch a `discord.HTTPException` and log accordingly.""" - self.cog.webhook = helpers.MockAsyncWebhook() - self.cog.webhook.send.side_effect = discord.HTTPException(response=MagicMock(), message="Something failed.") - - log = logging.getLogger('bot.cogs.duck_pond') - with self.assertLogs(logger=log, level=logging.ERROR) as log_watcher: - asyncio.run(self.cog.send_webhook()) - - self.assertEqual(len(log_watcher.records), 1) - - record = log_watcher.records[0] - self.assertEqual(record.levelno, logging.ERROR) - def _get_reaction( self, emoji: typing.Union[str, helpers.MockEmoji], @@ -280,16 +248,20 @@ class DuckPondTests(base.LoggingTestsMixin, unittest.IsolatedAsyncioTestCase): async def test_relay_message_correctly_relays_content_and_attachments(self): """The `relay_message` method should correctly relay message content and attachments.""" - send_webhook_path = f"{MODULE_PATH}.DuckPond.send_webhook" + send_webhook_path = f"{MODULE_PATH}.send_webhook" send_attachments_path = f"{MODULE_PATH}.send_attachments" + author = MagicMock( + display_name="x", + avatar_url="https://" + ) self.cog.webhook = helpers.MockAsyncWebhook() test_values = ( - (helpers.MockMessage(clean_content="", attachments=[]), False, False), - (helpers.MockMessage(clean_content="message", attachments=[]), True, False), - (helpers.MockMessage(clean_content="", attachments=["attachment"]), False, True), - (helpers.MockMessage(clean_content="message", attachments=["attachment"]), True, True), + (helpers.MockMessage(author=author, clean_content="", attachments=[]), False, False), + (helpers.MockMessage(author=author, clean_content="message", attachments=[]), True, False), + (helpers.MockMessage(author=author, clean_content="", attachments=["attachment"]), False, True), + (helpers.MockMessage(author=author, clean_content="message", attachments=["attachment"]), True, True), ) for message, expect_webhook_call, expect_attachment_call in test_values: @@ -314,14 +286,14 @@ class DuckPondTests(base.LoggingTestsMixin, unittest.IsolatedAsyncioTestCase): for side_effect in side_effects: # pragma: no cover send_attachments.side_effect = side_effect - with patch(f"{MODULE_PATH}.DuckPond.send_webhook", new_callable=AsyncMock) as send_webhook: + with patch(f"{MODULE_PATH}.send_webhook", new_callable=AsyncMock) as send_webhook: with self.subTest(side_effect=type(side_effect).__name__): with self.assertNotLogs(logger=log, level=logging.ERROR): await self.cog.relay_message(message) self.assertEqual(send_webhook.call_count, 2) - @patch(f"{MODULE_PATH}.DuckPond.send_webhook", new_callable=AsyncMock) + @patch(f"{MODULE_PATH}.send_webhook", new_callable=AsyncMock) @patch(f"{MODULE_PATH}.send_attachments", new_callable=AsyncMock) async def test_relay_message_handles_attachment_http_error(self, send_attachments, send_webhook): """The `relay_message` method should handle irretrievable attachments.""" @@ -337,6 +309,7 @@ class DuckPondTests(base.LoggingTestsMixin, unittest.IsolatedAsyncioTestCase): await self.cog.relay_message(message) send_webhook.assert_called_once_with( + webhook=self.cog.webhook, content=message.clean_content, username=message.author.display_name, avatar_url=message.author.avatar_url -- cgit v1.2.3 From aaf8db7550e8b95354d6f079c99ef2beb400cac8 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 12 Jul 2020 20:19:54 +0200 Subject: Add a way to respond to DMs. This shouldn't be used as a replacement for ModMail, but I think it makes sense to have the feature just in case #dm-log provides an interesting use-case where responding as the bot makes sense. It's a bit of a curiosity, and Ves hates it, but I included it anyway. https://github.com/python-discord/bot/issues/667 --- bot/cogs/dm_relay.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index bb060fe90..df19000fe 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -1,7 +1,9 @@ import logging +from typing import Optional import discord from discord import Color +from discord.ext import commands from discord.ext.commands import Cog from bot import constants @@ -20,6 +22,32 @@ class DMRelay(Cog): self.webhook_id = constants.Webhooks.dm_log self.webhook = None self.bot.loop.create_task(self.fetch_webhook()) + self.last_dm_user = None + + @commands.command(aliases=("reply",)) + async def send_dm(self, ctx: commands.Context, member: Optional[discord.Member], *, message: str) -> None: + """ + Allows you to send a DM to a user from the bot. + + If `member` is not provided, it will send to the last user who DM'd the bot. + + This feature should be used extremely sparingly. Use ModMail if you need to have a serious + conversation with a user. This is just for responding to extraordinary DMs, having a little + fun with users, and telling people they are DMing the wrong bot. + + NOTE: This feature will be removed if it is overused. + """ + if member: + await member.send(message) + await ctx.message.add_reaction("✅") + return + elif self.last_dm_user: + await self.last_dm_user.send(message) + await ctx.message.add_reaction("✅") + return + else: + log.debug("Unable to send a DM to the user.") + await ctx.message.add_reaction("❌") async def fetch_webhook(self) -> None: """Fetches the webhook object, so we can post to it.""" @@ -45,6 +73,7 @@ class DMRelay(Cog): username=message.author.display_name, avatar_url=message.author.avatar_url ) + self.last_dm_user = message.author # Handle any attachments if message.attachments: -- cgit v1.2.3 From 4527c038d21149d4d3fab73c54b9a1ad31e671c0 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 13 Jul 2020 00:17:37 +0200 Subject: Revert "Ping @Moderators in ModLog" Let's continue to use "@everyone" for now, and add an explicit allow for it so that it successfully pings people. There's a full justification for this in the pull request. https://github.com/python-discord/bot/issues/1038 --- bot/cogs/antispam.py | 4 ++-- bot/cogs/filtering.py | 2 +- bot/cogs/moderation/modlog.py | 17 +++++++++++------ bot/cogs/watchchannels/watchchannel.py | 4 ++-- bot/constants.py | 4 ++-- config-default.yml | 4 ++-- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/bot/cogs/antispam.py b/bot/cogs/antispam.py index 71382bba9..0bcca578d 100644 --- a/bot/cogs/antispam.py +++ b/bot/cogs/antispam.py @@ -98,7 +98,7 @@ class DeletionContext: text=mod_alert_message, thumbnail=last_message.author.avatar_url_as(static_format="png"), channel_id=Channels.mod_alerts, - ping_moderators=AntiSpamConfig.ping_moderators + ping_everyone=AntiSpamConfig.ping_everyone ) @@ -132,7 +132,7 @@ class AntiSpam(Cog): await self.mod_log.send_log_message( title="Error: AntiSpam configuration validation failed!", text=body, - ping_moderators=True, + ping_everyone=True, icon_url=Icons.token_removed, colour=Colour.red() ) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index a5d59085f..76ea68660 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -329,7 +329,7 @@ class Filtering(Cog, Scheduler): text=message, thumbnail=msg.author.avatar_url_as(static_format="png"), channel_id=Channels.mod_alerts, - ping_moderators=Filter.ping_moderators, + ping_everyone=Filter.ping_everyone, additional_embeds=additional_embeds, additional_embeds_msg=additional_embeds_msg ) diff --git a/bot/cogs/moderation/modlog.py b/bot/cogs/moderation/modlog.py index a37a9faf5..0a63f57b8 100644 --- a/bot/cogs/moderation/modlog.py +++ b/bot/cogs/moderation/modlog.py @@ -15,7 +15,7 @@ from discord.ext.commands import Cog, Context from discord.utils import escape_markdown from bot.bot import Bot -from bot.constants import Categories, Channels, Colours, Emojis, Event, Guild as GuildConstant, Icons, Roles, URLs +from bot.constants import Categories, Channels, Colours, Emojis, Event, Guild as GuildConstant, Icons, URLs from bot.utils.time import humanize_delta log = logging.getLogger(__name__) @@ -88,7 +88,7 @@ class ModLog(Cog, name="ModLog"): text: str, thumbnail: t.Optional[t.Union[str, discord.Asset]] = None, channel_id: int = Channels.mod_log, - ping_moderators: bool = False, + ping_everyone: bool = False, files: t.Optional[t.List[discord.File]] = None, content: t.Optional[str] = None, additional_embeds: t.Optional[t.List[discord.Embed]] = None, @@ -114,14 +114,19 @@ class ModLog(Cog, name="ModLog"): if thumbnail: embed.set_thumbnail(url=thumbnail) - if ping_moderators: + if ping_everyone: if content: - content = f"<@&{Roles.moderators}>\n{content}" + content = f"@everyone\n{content}" else: - content = f"<@&{Roles.moderators}>" + content = "@everyone" channel = self.bot.get_channel(channel_id) - log_message = await channel.send(content=content, embed=embed, files=files) + log_message = await channel.send( + content=content, + embed=embed, + files=files, + allowed_mentions=discord.AllowedMentions(everyone=True) + ) if additional_embeds: if additional_embeds_msg: diff --git a/bot/cogs/watchchannels/watchchannel.py b/bot/cogs/watchchannels/watchchannel.py index 8c4af4581..7c58a0fb5 100644 --- a/bot/cogs/watchchannels/watchchannel.py +++ b/bot/cogs/watchchannels/watchchannel.py @@ -120,7 +120,7 @@ class WatchChannel(metaclass=CogABCMeta): await self.modlog.send_log_message( title=f"Error: Failed to initialize the {self.__class__.__name__} watch channel", text=message, - ping_moderators=True, + ping_everyone=True, icon_url=Icons.token_removed, colour=Color.red() ) @@ -132,7 +132,7 @@ class WatchChannel(metaclass=CogABCMeta): await self.modlog.send_log_message( title=f"Warning: Failed to retrieve user cache for the {self.__class__.__name__} watch channel", text="Could not retrieve the list of watched users from the API and messages will not be relayed.", - ping_moderators=True, + ping_everyone=True, icon_url=Icons.token_removed, colour=Color.red() ) diff --git a/bot/constants.py b/bot/constants.py index 34b312d2d..a1b392c82 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -225,7 +225,7 @@ class Filter(metaclass=YAMLGetter): notify_user_invites: bool notify_user_domains: bool - ping_moderators: bool + ping_everyone: bool offensive_msg_delete_days: int guild_invite_whitelist: List[int] domain_blacklist: List[str] @@ -522,7 +522,7 @@ class AntiSpam(metaclass=YAMLGetter): section = 'anti_spam' clean_offending: bool - ping_moderators: bool + ping_everyone: bool punishment: Dict[str, Dict[str, int]] rules: Dict[str, Dict[str, int]] diff --git a/config-default.yml b/config-default.yml index 0f6a25ef2..636b9db37 100644 --- a/config-default.yml +++ b/config-default.yml @@ -269,7 +269,7 @@ filter: notify_user_domains: false # Filter configuration - ping_moderators: true + ping_everyone: true offensive_msg_delete_days: 7 # How many days before deleting an offensive message? guild_invite_whitelist: @@ -428,7 +428,7 @@ urls: anti_spam: # Clean messages that violate a rule. clean_offending: true - ping_moderators: true + ping_everyone: true punishment: role_id: *MUTED_ROLE -- cgit v1.2.3 From e1c3b66f5f4d1f421d6469bd4f0964166262832c Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sun, 12 Jul 2020 23:49:46 -0700 Subject: Fix rescheduling of edited infractions It was attempting to schedule a dictionary instead of a coroutine. Fixes #1043 Fixes BOT-6Y --- bot/cogs/moderation/management.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/moderation/management.py b/bot/cogs/moderation/management.py index 4ef9d4209..672bb0e9c 100644 --- a/bot/cogs/moderation/management.py +++ b/bot/cogs/moderation/management.py @@ -139,7 +139,7 @@ class ModManagement(commands.Cog): # If the infraction was not marked as permanent, schedule a new expiration task if request_data['expires_at']: - self.infractions_cog.scheduler.schedule(new_infraction['id'], new_infraction) + self.infractions_cog.schedule_expiration(new_infraction) log_text += f""" Previous expiry: {old_infraction['expires_at'] or "Permanent"} -- cgit v1.2.3 From c4e9060a76a901c7d2e6035e6ca19d51770a4ab3 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Mon, 13 Jul 2020 15:04:40 +0200 Subject: Incidents: add `download_file` helper & tests Co-authored-by: MarkKoz --- bot/cogs/moderation/incidents.py | 13 +++++++++++++ tests/bot/cogs/moderation/test_incidents.py | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index be46c8202..65b0e458e 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -42,6 +42,19 @@ ALLOWED_ROLES: t.Set[int] = set(Guild.moderation_roles) ALL_SIGNALS: t.Set[str] = {signal.value for signal in Signal} +async def download_file(attachment: discord.Attachment) -> t.Optional[discord.File]: + """ + Download & return `attachment` file. + + If the download fails, the reason is logged and None will be returned. + """ + log.debug(f"Attempting to download attachment: {attachment.filename}") + try: + return await attachment.to_file() + except Exception: + log.exception("Failed to download attachment") + + def make_embed(incident: discord.Message, outcome: Signal, actioned_by: discord.Member) -> discord.Embed: """ Create an embed representation of `incident` for the #incidents-archive channel. diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 789a37cd4..273916199 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -12,6 +12,7 @@ from bot.cogs.moderation import Incidents, incidents from bot.constants import Colours from tests.helpers import ( MockAsyncWebhook, + MockAttachment, MockBot, MockMember, MockMessage, @@ -69,6 +70,25 @@ mock_404 = discord.NotFound( ) +class TestDownloadFile(unittest.IsolatedAsyncioTestCase): + """Collection of tests for the `download_file` helper function.""" + + async def test_download_file_success(self): + """If `to_file` succeeds, function returns the acquired `discord.File`.""" + file = MagicMock(discord.File, filename="bigbadlemon.jpg") + attachment = MockAttachment(to_file=AsyncMock(return_value=file)) + + acquired_file = await incidents.download_file(attachment) + self.assertIs(file, acquired_file) + + async def test_download_file_fail(self): + """If `to_file` fails, function handles the exception & returns None.""" + attachment = MockAttachment(to_file=AsyncMock(side_effect=mock_404)) + + acquired_file = await incidents.download_file(attachment) + self.assertIsNone(acquired_file) + + class TestMakeEmbed(unittest.TestCase): """Collection of tests for the `make_embed` helper function.""" -- cgit v1.2.3 From ed2368791870bd0b464391d9da7b13de15b322a3 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 13 Jul 2020 15:13:39 +0200 Subject: Better docstring for DMRelay cog. https://github.com/python-discord/bot/issues/667 --- bot/cogs/dm_relay.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index df19000fe..c6206629e 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -15,7 +15,7 @@ log = logging.getLogger(__name__) class DMRelay(Cog): - """Debug logging module.""" + """Relay direct messages to and from the bot.""" def __init__(self, bot: Bot): self.bot = bot -- cgit v1.2.3 From ab1546611a9952ddb45f211901ad129c2e8c5007 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 13 Jul 2020 15:14:31 +0200 Subject: Add avatar_url in python_news.py https://github.com/python-discord/bot/issues/667 --- bot/cogs/python_news.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bot/cogs/python_news.py b/bot/cogs/python_news.py index 1d8f2aeb0..0ab5738a4 100644 --- a/bot/cogs/python_news.py +++ b/bot/cogs/python_news.py @@ -113,6 +113,7 @@ class PythonNews(Cog): webhook=self.webhook, username=data["feed"]["title"], embed=embed, + avatar_url=AVATAR_URL, wait=True, ) payload["data"]["pep"].append(pep_nr) @@ -189,6 +190,7 @@ class PythonNews(Cog): webhook=self.webhook, username=self.webhook_names[maillist], embed=embed, + avatar_url=AVATAR_URL, wait=True, ) payload["data"][maillist].append(thread_information["thread_id"]) -- cgit v1.2.3 From 87c2ef7610a42207b0289820458285648f5dd41e Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 13 Jul 2020 15:20:59 +0200 Subject: Only mods+ may use the commands in this cog. https://github.com/python-discord/bot/issues/667 --- bot/cogs/dm_relay.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index c6206629e..67411f57b 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -8,6 +8,8 @@ from discord.ext.commands import Cog from bot import constants from bot.bot import Bot +from bot.constants import MODERATION_ROLES +from bot.utils.checks import with_role_check from bot.utils.messages import send_attachments from bot.utils.webhooks import send_webhook @@ -93,6 +95,10 @@ class DMRelay(Cog): except discord.HTTPException: log.exception("Failed to send an attachment to the webhook") + def cog_check(self, ctx: commands.Context) -> bool: + """Only allow moderators to invoke the commands in this cog.""" + return with_role_check(ctx, *MODERATION_ROLES) + def setup(bot: Bot) -> None: """Load the DMRelay cog.""" -- cgit v1.2.3 From 311936991d5543e35dbe4a5a5a13261fb44c27f4 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 13 Jul 2020 15:24:58 +0200 Subject: Don't run on_message if self.webhook is None. https://github.com/python-discord/bot/issues/667 --- bot/cogs/dm_relay.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index 67411f57b..3d16db8a0 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -64,7 +64,7 @@ class DMRelay(Cog): async def on_message(self, message: discord.Message) -> None: """Relays the message's content and attachments to the dm_log channel.""" # Only relay DMs from humans - if message.author.bot or message.guild: + if message.author.bot or message.guild or self.webhook is None: return clean_content = message.clean_content -- cgit v1.2.3 From d98a67f36444a7732f4527d8c343e2fb8fad6f93 Mon Sep 17 00:00:00 2001 From: Slushie Date: Mon, 13 Jul 2020 14:25:07 +0100 Subject: rename the `_filter_eval` function to be a public function --- bot/cogs/filtering.py | 2 +- bot/cogs/snekbox.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 4c97073c3..ec6769f68 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -200,7 +200,7 @@ class Filtering(Cog, Scheduler): # Update time when alert sent await self.name_alerts.set(member.id, datetime.utcnow().timestamp()) - async def _filter_eval(self, result: str, msg: Message) -> bool: + async def filter_eval(self, result: str, msg: Message) -> bool: """ Filter the result of an !eval to see if it violates any of our rules, and then respond accordingly. diff --git a/bot/cogs/snekbox.py b/bot/cogs/snekbox.py index 649bab492..4f73690da 100644 --- a/bot/cogs/snekbox.py +++ b/bot/cogs/snekbox.py @@ -213,7 +213,7 @@ class Snekbox(Cog): self.bot.stats.incr("snekbox.python.success") filter_cog = self.bot.get_cog("Filtering") - filter_triggered = await filter_cog._filter_eval(msg, ctx.message) + filter_triggered = await filter_cog.filter_eval(msg, ctx.message) if filter_triggered: response = await ctx.send("Attempt to circumvent filter detected. Moderator team has been alerted.") else: -- cgit v1.2.3 From aa0b20bdd780bc75cadde781981d063287bfe5ce Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 13 Jul 2020 15:27:41 +0200 Subject: Remove redundant clean_content variable. https://github.com/python-discord/bot/issues/667 --- bot/cogs/dm_relay.py | 3 +-- bot/cogs/duck_pond.py | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index 3d16db8a0..494c71066 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -67,8 +67,7 @@ class DMRelay(Cog): if message.author.bot or message.guild or self.webhook is None: return - clean_content = message.clean_content - if clean_content: + if message.clean_content: await send_webhook( webhook=self.webhook, content=message.clean_content, diff --git a/bot/cogs/duck_pond.py b/bot/cogs/duck_pond.py index 89b4ad0e4..7021069fa 100644 --- a/bot/cogs/duck_pond.py +++ b/bot/cogs/duck_pond.py @@ -78,9 +78,7 @@ class DuckPond(Cog): async def relay_message(self, message: Message) -> None: """Relays the message's content and attachments to the duck pond channel.""" - clean_content = message.clean_content - - if clean_content: + if message.clean_content: await send_webhook( webhook=self.webhook, content=message.clean_content, -- cgit v1.2.3 From b40a5f0de6758eb9dfb79ac3f34fbc0bf90d8a1e Mon Sep 17 00:00:00 2001 From: Slushie Date: Mon, 13 Jul 2020 16:08:40 +0100 Subject: check for the filter_cog in case it is unloaded --- bot/cogs/snekbox.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bot/cogs/snekbox.py b/bot/cogs/snekbox.py index 4f73690da..662f90869 100644 --- a/bot/cogs/snekbox.py +++ b/bot/cogs/snekbox.py @@ -213,7 +213,9 @@ class Snekbox(Cog): self.bot.stats.incr("snekbox.python.success") filter_cog = self.bot.get_cog("Filtering") - filter_triggered = await filter_cog.filter_eval(msg, ctx.message) + filter_triggered = False + if filter_cog: + filter_triggered = await filter_cog.filter_eval(msg, ctx.message) if filter_triggered: response = await ctx.send("Attempt to circumvent filter detected. Moderator team has been alerted.") else: -- cgit v1.2.3 From f1b1d0cb723abbbf7d4b49ac4b42fe0b7f266692 Mon Sep 17 00:00:00 2001 From: Slushie Date: Mon, 13 Jul 2020 16:09:08 +0100 Subject: edit snekbox tests to work with filtering --- tests/bot/cogs/test_snekbox.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/bot/cogs/test_snekbox.py b/tests/bot/cogs/test_snekbox.py index cf9adbee0..98dee7a1b 100644 --- a/tests/bot/cogs/test_snekbox.py +++ b/tests/bot/cogs/test_snekbox.py @@ -233,6 +233,10 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): self.cog.get_status_emoji = MagicMock(return_value=':yay!:') self.cog.format_output = AsyncMock(return_value=('[No output]', None)) + mocked_filter_cog = MagicMock() + mocked_filter_cog.filter_eval = AsyncMock(return_value=False) + self.bot.get_cog.return_value = mocked_filter_cog + await self.cog.send_eval(ctx, 'MyAwesomeCode') ctx.send.assert_called_once_with( '@LemonLemonishBeard#0042 :yay!: Return code 0.\n\n```py\n[No output]\n```' @@ -254,6 +258,10 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): self.cog.get_status_emoji = MagicMock(return_value=':yay!:') self.cog.format_output = AsyncMock(return_value=('Way too long beard', 'lookatmybeard.com')) + mocked_filter_cog = MagicMock() + mocked_filter_cog.filter_eval = AsyncMock(return_value=False) + self.bot.get_cog.return_value = mocked_filter_cog + await self.cog.send_eval(ctx, 'MyAwesomeCode') ctx.send.assert_called_once_with( '@LemonLemonishBeard#0042 :yay!: Return code 0.' @@ -275,6 +283,10 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): self.cog.get_status_emoji = MagicMock(return_value=':nope!:') self.cog.format_output = AsyncMock() # This function isn't called + mocked_filter_cog = MagicMock() + mocked_filter_cog.filter_eval = AsyncMock(return_value=False) + self.bot.get_cog.return_value = mocked_filter_cog + await self.cog.send_eval(ctx, 'MyAwesomeCode') ctx.send.assert_called_once_with( '@LemonLemonishBeard#0042 :nope!: Return code 127.\n\n```py\nBeard got stuck in the eval\n```' -- cgit v1.2.3 From ea62b6bae85113be913101a41053c91497b23c9a Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 13 Jul 2020 21:09:41 +0200 Subject: Store last DM user in RedisCache. Also now catches the exception if a user has disabled DMs, and adds a red cross reaction. https://github.com/python-discord/bot/issues/667 --- bot/cogs/dm_relay.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index 494c71066..3fce52b93 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -9,6 +9,7 @@ from discord.ext.commands import Cog from bot import constants from bot.bot import Bot from bot.constants import MODERATION_ROLES +from bot.utils import RedisCache from bot.utils.checks import with_role_check from bot.utils.messages import send_attachments from bot.utils.webhooks import send_webhook @@ -19,12 +20,14 @@ log = logging.getLogger(__name__) class DMRelay(Cog): """Relay direct messages to and from the bot.""" + # RedisCache[str, t.Union[discord.User.id, discord.Member.id]] + dm_cache = RedisCache() + def __init__(self, bot: Bot): self.bot = bot self.webhook_id = constants.Webhooks.dm_log self.webhook = None self.bot.loop.create_task(self.fetch_webhook()) - self.last_dm_user = None @commands.command(aliases=("reply",)) async def send_dm(self, ctx: commands.Context, member: Optional[discord.Member], *, message: str) -> None: @@ -39,16 +42,23 @@ class DMRelay(Cog): NOTE: This feature will be removed if it is overused. """ - if member: - await member.send(message) - await ctx.message.add_reaction("✅") - return - elif self.last_dm_user: - await self.last_dm_user.send(message) - await ctx.message.add_reaction("✅") - return - else: - log.debug("Unable to send a DM to the user.") + user_id = await self.dm_cache.get("last_user") + last_dm_user = ctx.guild.get_member(user_id) if user_id else None + + try: + if member: + await member.send(message) + await ctx.message.add_reaction("✅") + return + elif last_dm_user: + await last_dm_user.send(message) + await ctx.message.add_reaction("✅") + return + else: + log.debug("This bot has never gotten a DM, or the RedisCache has been cleared.") + await ctx.message.add_reaction("❌") + except discord.errors.Forbidden: + log.debug("User has disabled DMs.") await ctx.message.add_reaction("❌") async def fetch_webhook(self) -> None: @@ -74,7 +84,7 @@ class DMRelay(Cog): username=message.author.display_name, avatar_url=message.author.avatar_url ) - self.last_dm_user = message.author + await self.dm_cache.set("last_user", message.author.id) # Handle any attachments if message.attachments: -- cgit v1.2.3 From c91ad4b74d4aea220ef564af3b1c044ab81a01d8 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 13 Jul 2020 21:19:30 +0200 Subject: Whitelisting some popular communities The following communities are whitelisted by this commit: - Django - Programming Discussions - JetBrains Community - Raspberry Pi - Programmers Hangout - SpeakJS - DevCord - Unity - Programmer Humor - Microsoft Community Most of these are partners, or otherwise friendly communities that aren't worth pinging mods over. --- config-default.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/config-default.yml b/config-default.yml index 636b9db37..19d79fa76 100644 --- a/config-default.yml +++ b/config-default.yml @@ -295,12 +295,22 @@ filter: - 172018499005317120 # The Coding Den - 666560367173828639 # PyWeek - 702724176489873509 # Microsoft Python + - 150662382874525696 # Microsoft Community - 81384788765712384 # Discord API - 613425648685547541 # Discord Developers - 185590609631903755 # Blender Hub - 420324994703163402 # /r/FlutterDev - 488751051629920277 # Python Atlanta - 143867839282020352 # C# + - 159039020565790721 # Django + - 238666723824238602 # Programming Discussions + - 433980600391696384 # JetBrains Community + - 204621105720328193 # Raspberry Pi + - 244230771232079873 # Programmers Hangout + - 239433591950540801 # SpeakJS + - 174075418410876928 # DevCord + - 489222168727519232 # Unity + - 494558898880118785 # Programmer Humor domain_blacklist: - pornhub.com -- cgit v1.2.3 From 1fb9bdb0deb3609f426a3bca555c67d0a7dc52a7 Mon Sep 17 00:00:00 2001 From: Slushie Date: Tue, 14 Jul 2020 00:39:31 +0100 Subject: fix misaligned indentation --- bot/cogs/filtering.py | 74 +++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index ec6769f68..2de00f3a1 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -217,43 +217,43 @@ class Filtering(Cog, Scheduler): if _filter["enabled"] and _filter["content_only"]: match = await _filter["function"](result) - if match: - # If this is a filter (not a watchlist), we set the variable so we know - # that it has been triggered - if _filter["type"] == "filter": - filter_triggered = True - - # We do not have to check against DM channels since !eval cannot be used there. - channel_str = f"in {msg.channel.mention}" - - message_content, additional_embeds, additional_embeds_msg = self._add_stats( - filter_name, match, result - ) - - message = ( - f"The {filter_name} {_filter['type']} was triggered " - f"by **{msg.author}** " - f"(`{msg.author.id}`) {channel_str} using !eval with " - f"[the following message]({msg.jump_url}):\n\n" - f"{message_content}" - ) - - log.debug(message) - - # Send pretty mod log embed to mod-alerts - await self.mod_log.send_log_message( - icon_url=Icons.filtering, - colour=Colour(Colours.soft_red), - title=f"{_filter['type'].title()} triggered!", - text=message, - thumbnail=msg.author.avatar_url_as(static_format="png"), - channel_id=Channels.mod_alerts, - ping_everyone=Filter.ping_everyone, - additional_embeds=additional_embeds, - additional_embeds_msg=additional_embeds_msg - ) - - break # We don't want multiple filters to trigger + if match: + # If this is a filter (not a watchlist), we set the variable so we know + # that it has been triggered + if _filter["type"] == "filter": + filter_triggered = True + + # We do not have to check against DM channels since !eval cannot be used there. + channel_str = f"in {msg.channel.mention}" + + message_content, additional_embeds, additional_embeds_msg = self._add_stats( + filter_name, match, result + ) + + message = ( + f"The {filter_name} {_filter['type']} was triggered " + f"by **{msg.author}** " + f"(`{msg.author.id}`) {channel_str} using !eval with " + f"[the following message]({msg.jump_url}):\n\n" + f"{message_content}" + ) + + log.debug(message) + + # Send pretty mod log embed to mod-alerts + await self.mod_log.send_log_message( + icon_url=Icons.filtering, + colour=Colour(Colours.soft_red), + title=f"{_filter['type'].title()} triggered!", + text=message, + thumbnail=msg.author.avatar_url_as(static_format="png"), + channel_id=Channels.mod_alerts, + ping_everyone=Filter.ping_everyone, + additional_embeds=additional_embeds, + additional_embeds_msg=additional_embeds_msg + ) + + break # We don't want multiple filters to trigger return filter_triggered -- cgit v1.2.3 From 7ff8b2c9ebc27194835c25258bc90c623bdbec6b Mon Sep 17 00:00:00 2001 From: kosayoda Date: Tue, 14 Jul 2020 10:51:56 +0800 Subject: Allow ordering watched users by oldest first --- bot/cogs/watchchannels/watchchannel.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bot/cogs/watchchannels/watchchannel.py b/bot/cogs/watchchannels/watchchannel.py index 7c58a0fb5..2992a3085 100644 --- a/bot/cogs/watchchannels/watchchannel.py +++ b/bot/cogs/watchchannels/watchchannel.py @@ -287,7 +287,9 @@ class WatchChannel(metaclass=CogABCMeta): await self.webhook_send(embed=embed, username=msg.author.display_name, avatar_url=msg.author.avatar_url) - async def list_watched_users(self, ctx: Context, update_cache: bool = True) -> None: + async def list_watched_users( + self, ctx: Context, oldest_first: bool = False, update_cache: bool = True + ) -> None: """ Gives an overview of the watched user list for this channel. @@ -305,7 +307,11 @@ class WatchChannel(metaclass=CogABCMeta): time_delta = self._get_time_delta(inserted_at) lines.append(f"• <@{user_id}> (added {time_delta})") + if oldest_first: + lines.reverse() + lines = lines or ("There's nothing here yet.",) + embed = Embed( title=f"{self.__class__.__name__} watched users ({'updated' if update_cache else 'cached'})", color=Color.blue() -- cgit v1.2.3 From a6f66611b17aab8fbc3c54377c3971faeac5073b Mon Sep 17 00:00:00 2001 From: kosayoda Date: Tue, 14 Jul 2020 10:52:33 +0800 Subject: Pass argument as kwarg to preserve functionality --- bot/cogs/watchchannels/bigbrother.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/watchchannels/bigbrother.py b/bot/cogs/watchchannels/bigbrother.py index 702d371f4..fc899281b 100644 --- a/bot/cogs/watchchannels/bigbrother.py +++ b/bot/cogs/watchchannels/bigbrother.py @@ -42,7 +42,7 @@ class BigBrother(WatchChannel, Cog, name="Big Brother"): The optional kwarg `update_cache` can be used to update the user cache using the API before listing the users. """ - await self.list_watched_users(ctx, update_cache) + await self.list_watched_users(ctx, update_cache=update_cache) @bigbrother_group.command(name='watch', aliases=('w',)) @with_role(*MODERATION_ROLES) -- cgit v1.2.3 From ac7ed93eb64884a622c8718c8594aa74a3b2b201 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Tue, 14 Jul 2020 10:58:02 +0800 Subject: Accept argument to order nominees by oldest first --- bot/cogs/watchchannels/talentpool.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bot/cogs/watchchannels/talentpool.py b/bot/cogs/watchchannels/talentpool.py index 33550f68e..1f5989f23 100644 --- a/bot/cogs/watchchannels/talentpool.py +++ b/bot/cogs/watchchannels/talentpool.py @@ -38,14 +38,18 @@ class TalentPool(WatchChannel, Cog, name="Talentpool"): @nomination_group.command(name='watched', aliases=('all', 'list')) @with_role(*MODERATION_ROLES) - async def watched_command(self, ctx: Context, update_cache: bool = True) -> None: + async def watched_command( + self, ctx: Context, oldest_first: bool = False, update_cache: bool = True + ) -> None: """ Shows the users that are currently being monitored in the talent pool. + The optional kwarg `oldest_first` can be used to order the list by oldest nomination. + The optional kwarg `update_cache` can be used to update the user cache using the API before listing the users. """ - await self.list_watched_users(ctx, update_cache) + await self.list_watched_users(ctx, oldest_first=oldest_first, update_cache=update_cache) @nomination_group.command(name='watch', aliases=('w', 'add', 'a')) @with_role(*STAFF_ROLES) -- cgit v1.2.3 From 01d2803b608407330959ef880bd562456921d0fd Mon Sep 17 00:00:00 2001 From: kosayoda Date: Tue, 14 Jul 2020 10:58:56 +0800 Subject: Add command to list nominees by oldest first --- bot/cogs/watchchannels/talentpool.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bot/cogs/watchchannels/talentpool.py b/bot/cogs/watchchannels/talentpool.py index 1f5989f23..89256e92e 100644 --- a/bot/cogs/watchchannels/talentpool.py +++ b/bot/cogs/watchchannels/talentpool.py @@ -51,6 +51,17 @@ class TalentPool(WatchChannel, Cog, name="Talentpool"): """ await self.list_watched_users(ctx, oldest_first=oldest_first, update_cache=update_cache) + @nomination_group.command(name='oldest') + @with_role(*MODERATION_ROLES) + async def oldest_command(self, ctx: Context, update_cache: bool = True) -> None: + """ + Shows talent pool monitored users ordered by oldest nomination. + + The optional kwarg `update_cache` can be used to update the user + cache using the API before listing the users. + """ + await ctx.invoke(self.watched_command, oldest_first=True, update_cache=update_cache) + @nomination_group.command(name='watch', aliases=('w', 'add', 'a')) @with_role(*STAFF_ROLES) async def watch_command(self, ctx: Context, user: FetchedMember, *, reason: str) -> None: -- cgit v1.2.3 From 28fe47d5d2404cdc70eaabe3e6c41567b9fd7c3d Mon Sep 17 00:00:00 2001 From: kosayoda Date: Tue, 14 Jul 2020 11:55:00 +0800 Subject: Achieve feature parity with talentpool --- bot/cogs/watchchannels/bigbrother.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/bot/cogs/watchchannels/bigbrother.py b/bot/cogs/watchchannels/bigbrother.py index fc899281b..4d27a6333 100644 --- a/bot/cogs/watchchannels/bigbrother.py +++ b/bot/cogs/watchchannels/bigbrother.py @@ -35,14 +35,29 @@ class BigBrother(WatchChannel, Cog, name="Big Brother"): @bigbrother_group.command(name='watched', aliases=('all', 'list')) @with_role(*MODERATION_ROLES) - async def watched_command(self, ctx: Context, update_cache: bool = True) -> None: + async def watched_command( + self, ctx: Context, oldest_first: bool = False, update_cache: bool = True + ) -> None: """ Shows the users that are currently being monitored by Big Brother. + The optional kwarg `oldest_first` can be used to order the list by oldest watched. + + The optional kwarg `update_cache` can be used to update the user + cache using the API before listing the users. + """ + await self.list_watched_users(ctx, oldest_first=oldest_first, update_cache=update_cache) + + @bigbrother_group.command(name='oldest') + @with_role(*MODERATION_ROLES) + async def oldest_command(self, ctx: Context, update_cache: bool = True) -> None: + """ + Shows Big Brother monitored users ordered by oldest watched. + The optional kwarg `update_cache` can be used to update the user cache using the API before listing the users. """ - await self.list_watched_users(ctx, update_cache=update_cache) + await ctx.invoke(self.watched_command, oldest_first=True, update_cache=update_cache) @bigbrother_group.command(name='watch', aliases=('w',)) @with_role(*MODERATION_ROLES) -- cgit v1.2.3 From 8d62214b0b009d1cc9b343c9589a5a1fe8f4692b Mon Sep 17 00:00:00 2001 From: kosayoda Date: Tue, 14 Jul 2020 12:49:39 +0800 Subject: Invoke fuzzywuzzy's processor before matching Trying to match a string with only non-alphanumeric characters results in a warning by fuzzywuzzy. Processing the string before matching lets us avoid the warning, which which uses the root logger and thus isn't supressible. --- bot/cogs/help.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bot/cogs/help.py b/bot/cogs/help.py index 832f6ea6b..198e88b55 100644 --- a/bot/cogs/help.py +++ b/bot/cogs/help.py @@ -8,6 +8,7 @@ from typing import List, Union from discord import Colour, Embed, Member, Message, NotFound, Reaction, User from discord.ext.commands import Bot, Cog, Command, Context, Group, HelpCommand from fuzzywuzzy import fuzz, process +from fuzzywuzzy.utils import full_process from bot import constants from bot.constants import Channels, Emojis, STAFF_ROLES @@ -146,7 +147,13 @@ class CustomHelpCommand(HelpCommand): Will return an instance of the `HelpQueryNotFound` exception with the error message and possible matches. """ choices = await self.get_all_help_choices() - result = process.extractBests(string, choices, scorer=fuzz.ratio, score_cutoff=60) + + # Run fuzzywuzzy's processor beforehand, and avoid matching if processed string is empty + # This avoids fuzzywuzzy from raising a warning on inputs with only non-alphanumeric characters + if full_process(string): + result = process.extractBests(string, choices, scorer=fuzz.ratio, score_cutoff=60, processor=None) + else: + result = [] return HelpQueryNotFound(f'Query "{string}" not found.', dict(result)) -- cgit v1.2.3 From 3c1d43dc83b4a3d3a02492a1d045c7b9f1735feb Mon Sep 17 00:00:00 2001 From: kosayoda Date: Tue, 14 Jul 2020 13:22:08 +0800 Subject: Remove redundant kwarg in !kick and !shadow_kick The kwarg `active=False` is already being passed in `apply_kick`, therefore passing it in the parent callers result in a TypeError. Fixes #976 Fixes BOT-5P --- bot/cogs/moderation/infractions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/moderation/infractions.py b/bot/cogs/moderation/infractions.py index 3b28526b2..8df642428 100644 --- a/bot/cogs/moderation/infractions.py +++ b/bot/cogs/moderation/infractions.py @@ -64,7 +64,7 @@ class Infractions(InfractionScheduler, commands.Cog): @command() async def kick(self, ctx: Context, user: Member, *, reason: t.Optional[str] = None) -> None: """Kick a user for the given reason.""" - await self.apply_kick(ctx, user, reason, active=False) + await self.apply_kick(ctx, user, reason) @command() async def ban(self, ctx: Context, user: FetchedMember, *, reason: t.Optional[str] = None) -> None: @@ -134,7 +134,7 @@ class Infractions(InfractionScheduler, commands.Cog): @command(hidden=True, aliases=['shadowkick', 'skick']) async def shadow_kick(self, ctx: Context, user: Member, *, reason: t.Optional[str] = None) -> None: """Kick a user for the given reason without notifying the user.""" - await self.apply_kick(ctx, user, reason, hidden=True, active=False) + await self.apply_kick(ctx, user, reason, hidden=True) @command(hidden=True, aliases=['shadowban', 'sban']) async def shadow_ban(self, ctx: Context, user: FetchedMember, *, reason: t.Optional[str] = None) -> None: -- cgit v1.2.3 From b35fad987b2b6de8da2c36bb02f4e0c6777b9737 Mon Sep 17 00:00:00 2001 From: ItsCinnabar <50111163+ItsCinnabar@users.noreply.github.com> Date: Tue, 14 Jul 2020 09:48:31 -0400 Subject: Update or-gotcha.md Adjust description and include link to docs --- bot/resources/tags/or-gotcha.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/resources/tags/or-gotcha.md b/bot/resources/tags/or-gotcha.md index 00c2db1f8..2dc1410ad 100644 --- a/bot/resources/tags/or-gotcha.md +++ b/bot/resources/tags/or-gotcha.md @@ -3,7 +3,7 @@ When checking if something is equal to one thing or another, you might think tha if favorite_fruit == 'grapefruit' or 'lemon': print("That's a weird favorite fruit to have.") ``` -After all, that's how you would normally phrase it in plain English. In Python, however, you have to have _complete instructions on both sides of the logical operator_. +While this makes sense in English, it may not behave the way you would expect. In Python, you should have _complete instructions on both sides of the logical operator_. So, if you want to check if something is equal to one thing or another, there are two common ways: ```py @@ -15,3 +15,4 @@ if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon': if favorite_fruit in ('grapefruit', 'lemon'): print("That's a weird favorite fruit to have.") ``` +For more info, see here: [Python Docs - Boolean Operations](https://docs.python.org/3/reference/expressions.html#boolean-operations) -- cgit v1.2.3 From 18e58ad1e040f3997a23308d916eed7d474a5dd6 Mon Sep 17 00:00:00 2001 From: ItsCinnabar <50111163+ItsCinnabar@users.noreply.github.com> Date: Tue, 14 Jul 2020 10:03:07 -0400 Subject: Update or-gotcha.md --- bot/resources/tags/or-gotcha.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/resources/tags/or-gotcha.md b/bot/resources/tags/or-gotcha.md index 2dc1410ad..cbb64c276 100644 --- a/bot/resources/tags/or-gotcha.md +++ b/bot/resources/tags/or-gotcha.md @@ -3,7 +3,7 @@ When checking if something is equal to one thing or another, you might think tha if favorite_fruit == 'grapefruit' or 'lemon': print("That's a weird favorite fruit to have.") ``` -While this makes sense in English, it may not behave the way you would expect. In Python, you should have _complete instructions on both sides of the logical operator_. +While this makes sense in English, it may not behave the way you would expect. [In Python, you should have _complete instructions on both sides of the logical operator_.](https://docs.python.org/3/reference/expressions.html#boolean-operations) So, if you want to check if something is equal to one thing or another, there are two common ways: ```py -- cgit v1.2.3 From 6d064912e5c2caf29de609955f78eb60014a5b63 Mon Sep 17 00:00:00 2001 From: ItsCinnabar <50111163+ItsCinnabar@users.noreply.github.com> Date: Tue, 14 Jul 2020 10:06:00 -0400 Subject: Update or-gotcha.md --- bot/resources/tags/or-gotcha.md | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/resources/tags/or-gotcha.md b/bot/resources/tags/or-gotcha.md index cbb64c276..00c8a5645 100644 --- a/bot/resources/tags/or-gotcha.md +++ b/bot/resources/tags/or-gotcha.md @@ -15,4 +15,3 @@ if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon': if favorite_fruit in ('grapefruit', 'lemon'): print("That's a weird favorite fruit to have.") ``` -For more info, see here: [Python Docs - Boolean Operations](https://docs.python.org/3/reference/expressions.html#boolean-operations) -- cgit v1.2.3 From 6e48d666b31d13d801c394b527ce545b039b478f Mon Sep 17 00:00:00 2001 From: kwzrd Date: Tue, 14 Jul 2020 17:28:53 +0200 Subject: Incidents: link `proxy_url` if attachment fails to download Suggested by Mark during review. If the download fails, we fallback on showing an informative message, which will link the attachment cdn link. The attachment-handling logic was moved from the `archive` coroutine into `make_embed`, which now also returns the file, if available. In the end, this appears to be the smoothest approach. Co-authored-by: MarkKoz --- bot/cogs/moderation/incidents.py | 36 +++++++++----- tests/bot/cogs/moderation/test_incidents.py | 73 ++++++++++++++--------------- 2 files changed, 59 insertions(+), 50 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 65b0e458e..018538040 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -41,6 +41,10 @@ ALLOWED_ROLES: t.Set[int] = set(Guild.moderation_roles) # Message must have all of these emoji to pass the `has_signals` check ALL_SIGNALS: t.Set[str] = {signal.value for signal in Signal} +# An embed coupled with an optional file to be dispatched +# If the file is not None, the embed attempts to show it in its body +FileEmbed = t.Tuple[discord.Embed, t.Optional[discord.File]] + async def download_file(attachment: discord.Attachment) -> t.Optional[discord.File]: """ @@ -55,7 +59,7 @@ async def download_file(attachment: discord.Attachment) -> t.Optional[discord.Fi log.exception("Failed to download attachment") -def make_embed(incident: discord.Message, outcome: Signal, actioned_by: discord.Member) -> discord.Embed: +async def make_embed(incident: discord.Message, outcome: Signal, actioned_by: discord.Member) -> FileEmbed: """ Create an embed representation of `incident` for the #incidents-archive channel. @@ -66,6 +70,11 @@ def make_embed(incident: discord.Message, outcome: Signal, actioned_by: discord. of information will be relayed in other ways, e.g. webhook username. As mentions in embeds do not ping, we do not need to use `incident.clean_content`. + + If `incident` contains attachments, the first attachment will be downloaded and + returned alongside the embed. The embed attempts to display the attachment. + Should the download fail, we fallback on linking the `proxy_url`, which should + remain functional for some time after the original message is deleted. """ log.trace(f"Creating embed for {incident.id=}") @@ -83,7 +92,18 @@ def make_embed(incident: discord.Message, outcome: Signal, actioned_by: discord. ) embed.set_footer(text=footer, icon_url=actioned_by.avatar_url) - return embed + if incident.attachments: + attachment = incident.attachments[0] # User-sent messages can only contain one attachment + file = await download_file(attachment) + + if file is not None: + embed.set_image(url=f"attachment://{attachment.filename}") # Embed displays the attached file + else: + embed.set_author(name="[Failed to relay attachment]", url=attachment.proxy_url) # Embed links the file + else: + file = None + + return embed, file def is_incident(message: discord.Message) -> bool: @@ -215,17 +235,7 @@ class Incidents(Cog): message is not safe to be deleted, as we will lose some information. """ log.debug(f"Archiving incident: {incident.id} (outcome: {outcome}, actioned by: {actioned_by})") - embed = make_embed(incident, outcome, actioned_by) - - # If the incident had an attachment, we will try to relay it - if incident.attachments: - attachment = incident.attachments[0] # User-sent messages can only contain one attachment - log.debug(f"Attempting to archive incident attachment: {attachment.filename}") - - attachment_file = await attachment.to_file() # The file will be sent with the webhook - embed.set_image(url=f"attachment://{attachment.filename}") # Embed displays the attached file - else: - attachment_file = None + embed, attachment_file = await make_embed(incident, outcome, actioned_by) try: webhook = await self.bot.fetch_webhook(Webhooks.incidents_archive) diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 273916199..9b6054f55 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -89,30 +89,58 @@ class TestDownloadFile(unittest.IsolatedAsyncioTestCase): self.assertIsNone(acquired_file) -class TestMakeEmbed(unittest.TestCase): +class TestMakeEmbed(unittest.IsolatedAsyncioTestCase): """Collection of tests for the `make_embed` helper function.""" - def test_make_embed_actioned(self): + async def test_make_embed_actioned(self): """Embed is coloured green and footer contains 'Actioned' when `outcome=Signal.ACTIONED`.""" - embed = incidents.make_embed(MockMessage(), incidents.Signal.ACTIONED, MockMember()) + embed, file = await incidents.make_embed(MockMessage(), incidents.Signal.ACTIONED, MockMember()) self.assertEqual(embed.colour.value, Colours.soft_green) self.assertIn("Actioned", embed.footer.text) - def test_make_embed_not_actioned(self): + async def test_make_embed_not_actioned(self): """Embed is coloured red and footer contains 'Rejected' when `outcome=Signal.NOT_ACTIONED`.""" - embed = incidents.make_embed(MockMessage(), incidents.Signal.NOT_ACTIONED, MockMember()) + embed, file = await incidents.make_embed(MockMessage(), incidents.Signal.NOT_ACTIONED, MockMember()) self.assertEqual(embed.colour.value, Colours.soft_red) self.assertIn("Rejected", embed.footer.text) - def test_make_embed_content(self): + async def test_make_embed_content(self): """Incident content appears as embed description.""" incident = MockMessage(content="this is an incident") - embed = incidents.make_embed(incident, incidents.Signal.ACTIONED, MockMember()) + embed, file = await incidents.make_embed(incident, incidents.Signal.ACTIONED, MockMember()) self.assertEqual(incident.content, embed.description) + async def test_make_embed_with_attachment_succeeds(self): + """Incident's attachment is downloaded and displayed in the embed's image field.""" + file = MagicMock(discord.File, filename="bigbadjoe.jpg") + attachment = MockAttachment(filename="bigbadjoe.jpg") + incident = MockMessage(content="this is an incident", attachments=[attachment]) + + # Patch `download_file` to return our `file` + with patch("bot.cogs.moderation.incidents.download_file", AsyncMock(return_value=file)): + embed, returned_file = await incidents.make_embed(incident, incidents.Signal.ACTIONED, MockMember()) + + self.assertIs(file, returned_file) + self.assertEqual("attachment://bigbadjoe.jpg", embed.image.url) + + async def test_make_embed_with_attachment_fails(self): + """Incident's attachment fails to download, proxy url is linked instead.""" + attachment = MockAttachment(proxy_url="discord.com/bigbadjoe.jpg") + incident = MockMessage(content="this is an incident", attachments=[attachment]) + + # Patch `download_file` to return None as if the download failed + with patch("bot.cogs.moderation.incidents.download_file", AsyncMock(return_value=None)): + embed, returned_file = await incidents.make_embed(incident, incidents.Signal.ACTIONED, MockMember()) + + self.assertIsNone(returned_file) + + # The author name field is simply expected to have something in it, we do not assert the message + self.assertGreater(len(embed.author.name), 0) + self.assertEqual(embed.author.url, "discord.com/bigbadjoe.jpg") # However, it should link the exact url + @patch("bot.constants.Channels.incidents", 123) class TestIsIncident(unittest.TestCase): @@ -343,11 +371,10 @@ class TestArchive(TestIncidents): content="this is an incident", author=MockUser(name="author_name", avatar_url="author_avatar"), id=123, - attachments=[], # This incident has no attachments ) built_embed = MagicMock(discord.Embed, id=123) # We patch `make_embed` to return this - with patch("bot.cogs.moderation.incidents.make_embed", MagicMock(return_value=built_embed)): + with patch("bot.cogs.moderation.incidents.make_embed", AsyncMock(return_value=(built_embed, None))): archive_return = await self.cog_instance.archive(incident, MagicMock(value="A"), MockMember()) # Now we check that the webhook was given the correct args, and that `archive` returned True @@ -359,34 +386,6 @@ class TestArchive(TestIncidents): ) self.assertTrue(archive_return) - async def test_archive_relays_incident_with_attachments(self): - """ - Incident attachments are relayed and displayed in the embed. - - This test asserts the two things that need to happen in order to relay the attachment. - The embed returned by `make_embed` must have the `set_image` method called with the - attachment's filename, and the file must be passed to the webhook's send method. - """ - attachment_file = MagicMock(discord.File) - attachment = MagicMock( - discord.Attachment, - filename="abc.png", - to_file=AsyncMock(return_value=attachment_file), - ) - incident = MockMessage( - attachments=[attachment], - ) - built_embed = MagicMock(discord.Embed) - - with patch("bot.cogs.moderation.incidents.make_embed", MagicMock(return_value=built_embed)): - await self.cog_instance.archive(incident, incidents.Signal.ACTIONED, actioned_by=MockMember()) - - built_embed.set_image.assert_called_once_with(url="attachment://abc.png") - - send_kwargs = self.cog_instance.bot.fetch_webhook.return_value.send.call_args.kwargs - self.assertIn("file", send_kwargs) - self.assertIs(send_kwargs["file"], attachment_file) - async def test_archive_clyde_username(self): """ The archive webhook username is cleansed using `sub_clyde`. -- cgit v1.2.3 From 4fc0971dfff6a72c322a8f434a4b656cbea8fb66 Mon Sep 17 00:00:00 2001 From: ItsCinnabar <50111163+ItsCinnabar@users.noreply.github.com> Date: Tue, 14 Jul 2020 16:59:19 +0000 Subject: Update bot/resources/tags/or-gotcha.md Co-authored-by: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> --- bot/resources/tags/or-gotcha.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/resources/tags/or-gotcha.md b/bot/resources/tags/or-gotcha.md index 00c8a5645..d75a73d78 100644 --- a/bot/resources/tags/or-gotcha.md +++ b/bot/resources/tags/or-gotcha.md @@ -3,7 +3,7 @@ When checking if something is equal to one thing or another, you might think tha if favorite_fruit == 'grapefruit' or 'lemon': print("That's a weird favorite fruit to have.") ``` -While this makes sense in English, it may not behave the way you would expect. [In Python, you should have _complete instructions on both sides of the logical operator_.](https://docs.python.org/3/reference/expressions.html#boolean-operations) +While this makes sense in English, it may not behave the way you would expect. In Python, you should have _[complete instructions on both sides of the logical operator](https://docs.python.org/3/reference/expressions.html#boolean-operations)_. So, if you want to check if something is equal to one thing or another, there are two common ways: ```py -- cgit v1.2.3 From 042f472ac3207ad685a5acb659a5a69f22c72282 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 15 Jul 2020 01:09:35 +0200 Subject: Remove caching of last_dm_user. If you're typing up a reply and the bot gets another DM while you're typing, you might accidentally send your reply to the wrong person. This could happen even if you're very attentive, because it might be a matter of milliseconds. The complexity to prevent this isn't worth the convenience of the feature, and it's nice to get rid of the caching as well, so I've decided to just make .reply require a user for every reply. https://github.com/python-discord/bot/issues/1041 --- bot/cogs/dm_relay.py | 42 +++++++++++++++++------------------------- bot/constants.py | 2 ++ config-default.yml | 1 + 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index 3fce52b93..f62d6105e 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -1,5 +1,4 @@ import logging -from typing import Optional import discord from discord import Color @@ -8,9 +7,7 @@ from discord.ext.commands import Cog from bot import constants from bot.bot import Bot -from bot.constants import MODERATION_ROLES -from bot.utils import RedisCache -from bot.utils.checks import with_role_check +from bot.utils.checks import in_whitelist_check, with_role_check from bot.utils.messages import send_attachments from bot.utils.webhooks import send_webhook @@ -20,9 +17,6 @@ log = logging.getLogger(__name__) class DMRelay(Cog): """Relay direct messages to and from the bot.""" - # RedisCache[str, t.Union[discord.User.id, discord.Member.id]] - dm_cache = RedisCache() - def __init__(self, bot: Bot): self.bot = bot self.webhook_id = constants.Webhooks.dm_log @@ -30,11 +24,11 @@ class DMRelay(Cog): self.bot.loop.create_task(self.fetch_webhook()) @commands.command(aliases=("reply",)) - async def send_dm(self, ctx: commands.Context, member: Optional[discord.Member], *, message: str) -> None: + async def send_dm(self, ctx: commands.Context, member: discord.Member, *, message: str) -> None: """ Allows you to send a DM to a user from the bot. - If `member` is not provided, it will send to the last user who DM'd the bot. + A `member` must be provided. This feature should be used extremely sparingly. Use ModMail if you need to have a serious conversation with a user. This is just for responding to extraordinary DMs, having a little @@ -42,21 +36,11 @@ class DMRelay(Cog): NOTE: This feature will be removed if it is overused. """ - user_id = await self.dm_cache.get("last_user") - last_dm_user = ctx.guild.get_member(user_id) if user_id else None - try: - if member: - await member.send(message) - await ctx.message.add_reaction("✅") - return - elif last_dm_user: - await last_dm_user.send(message) - await ctx.message.add_reaction("✅") - return - else: - log.debug("This bot has never gotten a DM, or the RedisCache has been cleared.") - await ctx.message.add_reaction("❌") + await member.send(message) + await ctx.message.add_reaction("✅") + return + except discord.errors.Forbidden: log.debug("User has disabled DMs.") await ctx.message.add_reaction("❌") @@ -84,7 +68,6 @@ class DMRelay(Cog): username=message.author.display_name, avatar_url=message.author.avatar_url ) - await self.dm_cache.set("last_user", message.author.id) # Handle any attachments if message.attachments: @@ -106,7 +89,16 @@ class DMRelay(Cog): def cog_check(self, ctx: commands.Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" - return with_role_check(ctx, *MODERATION_ROLES) + checks = [ + with_role_check(ctx, *constants.MODERATION_ROLES), + in_whitelist_check( + ctx, + channels=[constants.Channels.dm_log], + redirect=None, + fail_silently=True, + ) + ] + return all(checks) def setup(bot: Bot) -> None: diff --git a/bot/constants.py b/bot/constants.py index 3f44003a8..778bc093c 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -395,6 +395,7 @@ class Channels(metaclass=YAMLGetter): dev_contrib: int dev_core: int dev_log: int + dm_log: int esoteric: int helpers: int how_to_get_help: int @@ -461,6 +462,7 @@ class Guild(metaclass=YAMLGetter): staff_channels: List[int] staff_roles: List[int] + class Keys(metaclass=YAMLGetter): section = "keys" diff --git a/config-default.yml b/config-default.yml index d12b9be27..8061e5e16 100644 --- a/config-default.yml +++ b/config-default.yml @@ -150,6 +150,7 @@ guild: mod_log: &MOD_LOG 282638479504965634 user_log: 528976905546760203 voice_log: 640292421988646961 + dm_log: 653713721625018428 # Off-topic off_topic_0: 291284109232308226 -- cgit v1.2.3 From 867b561a6ce4aff85451d00794c22e02793c8dac Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 14 Jul 2020 17:07:35 -0700 Subject: Suppress NotFound when removing help cmd reactions The message may be deleted somehow before the wait_for times out. Fixes #1050 Fixes BOT-6X --- bot/cogs/help.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/bot/cogs/help.py b/bot/cogs/help.py index 832f6ea6b..70e62d590 100644 --- a/bot/cogs/help.py +++ b/bot/cogs/help.py @@ -36,13 +36,12 @@ async def help_cleanup(bot: Bot, author: Member, message: Message) -> None: await message.add_reaction(DELETE_EMOJI) - try: - await bot.wait_for("reaction_add", check=check, timeout=300) - await message.delete() - except TimeoutError: - await message.remove_reaction(DELETE_EMOJI, bot.user) - except NotFound: - pass + with suppress(NotFound): + try: + await bot.wait_for("reaction_add", check=check, timeout=300) + await message.delete() + except TimeoutError: + await message.remove_reaction(DELETE_EMOJI, bot.user) class HelpQueryNotFound(ValueError): -- cgit v1.2.3 From 992f3c47d328821bcf647df7683fd5ca8bd780aa Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 14 Jul 2020 18:20:52 -0700 Subject: HelpChannels: remove cooldown info from available message Users can no longer see available channels if they're on cooldown. They will instead see a special "cooldown" channel which will explain what's going on. --- bot/cogs/help_channels.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 4d0c534b0..0c8cbb417 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -34,9 +34,6 @@ and will be yours until it has been inactive for {constants.HelpChannels.idle_mi is closed manually with `!close`. When that happens, it will be set to **dormant** and moved into \ the **Help: Dormant** category. -You may claim a new channel once every {constants.HelpChannels.claim_minutes} minutes. If you \ -currently cannot send a message in this channel, it means you are on cooldown and need to wait. - Try to write the best question you can by providing a detailed description and telling us what \ you've tried already. For more information on asking a good question, \ check out our guide on [asking good questions]({ASKING_GUIDE_URL}). -- cgit v1.2.3 From e46385d656129e06dd267764811d10ef5e8cd5a2 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Wed, 15 Jul 2020 11:06:03 +0800 Subject: Document new kwarg in docstring --- bot/cogs/watchchannels/watchchannel.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bot/cogs/watchchannels/watchchannel.py b/bot/cogs/watchchannels/watchchannel.py index 2992a3085..044077350 100644 --- a/bot/cogs/watchchannels/watchchannel.py +++ b/bot/cogs/watchchannels/watchchannel.py @@ -293,6 +293,8 @@ class WatchChannel(metaclass=CogABCMeta): """ Gives an overview of the watched user list for this channel. + The optional kwarg `oldest_first` orders the list by oldest entry. + The optional kwarg `update_cache` specifies whether the cache should be refreshed by polling the API. """ -- cgit v1.2.3 From eec57c86999bb2e9486dd6443b44cfd29026c823 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Wed, 15 Jul 2020 11:39:17 +0800 Subject: Pass processed string to `extractBests` Fixes a regression where the string to be matched was not processed beforehand. --- bot/cogs/help.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/help.py b/bot/cogs/help.py index 198e88b55..5f3fc4750 100644 --- a/bot/cogs/help.py +++ b/bot/cogs/help.py @@ -150,8 +150,8 @@ class CustomHelpCommand(HelpCommand): # Run fuzzywuzzy's processor beforehand, and avoid matching if processed string is empty # This avoids fuzzywuzzy from raising a warning on inputs with only non-alphanumeric characters - if full_process(string): - result = process.extractBests(string, choices, scorer=fuzz.ratio, score_cutoff=60, processor=None) + if (processed := full_process(string)): + result = process.extractBests(processed, choices, scorer=fuzz.ratio, score_cutoff=60, processor=None) else: result = [] -- cgit v1.2.3 From 6ccbb944a50058cea74bfdfe855a538b09ab67b7 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 15 Jul 2020 07:22:59 +0200 Subject: Restore DM user caching. This reverts commit 042f472a --- bot/cogs/dm_relay.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index f62d6105e..9a68b5341 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -1,4 +1,5 @@ import logging +from typing import Optional import discord from discord import Color @@ -7,6 +8,7 @@ from discord.ext.commands import Cog from bot import constants from bot.bot import Bot +from bot.utils import RedisCache from bot.utils.checks import in_whitelist_check, with_role_check from bot.utils.messages import send_attachments from bot.utils.webhooks import send_webhook @@ -17,6 +19,9 @@ log = logging.getLogger(__name__) class DMRelay(Cog): """Relay direct messages to and from the bot.""" + # RedisCache[str, t.Union[discord.User.id, discord.Member.id]] + dm_cache = RedisCache() + def __init__(self, bot: Bot): self.bot = bot self.webhook_id = constants.Webhooks.dm_log @@ -24,11 +29,11 @@ class DMRelay(Cog): self.bot.loop.create_task(self.fetch_webhook()) @commands.command(aliases=("reply",)) - async def send_dm(self, ctx: commands.Context, member: discord.Member, *, message: str) -> None: + async def send_dm(self, ctx: commands.Context, member: Optional[discord.Member], *, message: str) -> None: """ Allows you to send a DM to a user from the bot. - A `member` must be provided. + If `member` is not provided, it will send to the last user who DM'd the bot. This feature should be used extremely sparingly. Use ModMail if you need to have a serious conversation with a user. This is just for responding to extraordinary DMs, having a little @@ -36,11 +41,21 @@ class DMRelay(Cog): NOTE: This feature will be removed if it is overused. """ - try: - await member.send(message) - await ctx.message.add_reaction("✅") - return + user_id = await self.dm_cache.get("last_user") + last_dm_user = ctx.guild.get_member(user_id) if user_id else None + try: + if member: + await member.send(message) + await ctx.message.add_reaction("✅") + return + elif last_dm_user: + await last_dm_user.send(message) + await ctx.message.add_reaction("✅") + return + else: + log.debug("This bot has never gotten a DM, or the RedisCache has been cleared.") + await ctx.message.add_reaction("❌") except discord.errors.Forbidden: log.debug("User has disabled DMs.") await ctx.message.add_reaction("❌") @@ -68,6 +83,7 @@ class DMRelay(Cog): username=message.author.display_name, avatar_url=message.author.avatar_url ) + await self.dm_cache.set("last_user", message.author.id) # Handle any attachments if message.attachments: -- cgit v1.2.3 From 226eb68a4d397c14c68566f60a2de4a3704cf696 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 15 Jul 2020 07:30:02 +0200 Subject: Add the user ID to the username in dm relays. Without this, it is difficult to know precisely who the user that is DMing us is, which might be useful to us. https://github.com/python-discord/bot/issues/1041 --- bot/cogs/dm_relay.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index 9a68b5341..d3637d34b 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -80,7 +80,7 @@ class DMRelay(Cog): await send_webhook( webhook=self.webhook, content=message.clean_content, - username=message.author.display_name, + username=f"{message.author.display_name} ({message.author.id})", avatar_url=message.author.avatar_url ) await self.dm_cache.set("last_user", message.author.id) -- cgit v1.2.3 From ed6d848e3cd5cf355b9702e9c8df08c063c11a47 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 15 Jul 2020 07:33:48 +0200 Subject: Add some stats for DMs sent and received. https://github.com/python-discord/bot/issues/1041 --- bot/cogs/dm_relay.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index d3637d34b..edfcccf6d 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -48,10 +48,12 @@ class DMRelay(Cog): if member: await member.send(message) await ctx.message.add_reaction("✅") + self.bot.stats.incr("dm_relay.dm_sent") return elif last_dm_user: await last_dm_user.send(message) await ctx.message.add_reaction("✅") + self.bot.stats.incr("dm_relay.dm_sent") return else: log.debug("This bot has never gotten a DM, or the RedisCache has been cleared.") @@ -84,6 +86,7 @@ class DMRelay(Cog): avatar_url=message.author.avatar_url ) await self.dm_cache.set("last_user", message.author.id) + self.bot.stats.incr("dm_relay.dm_received") # Handle any attachments if message.attachments: -- cgit v1.2.3 From 90d2a77becb39d6b3c0056ea7c05b4a9e4d16f50 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 15 Jul 2020 10:15:08 +0200 Subject: Ves' refactor Co-authored-by: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> --- bot/cogs/dm_relay.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index edfcccf6d..0c3eddf42 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -41,23 +41,24 @@ class DMRelay(Cog): NOTE: This feature will be removed if it is overused. """ - user_id = await self.dm_cache.get("last_user") - last_dm_user = ctx.guild.get_member(user_id) if user_id else None + if not member: + user_id = await self.dm_cache.get("last_user") + member = ctx.guild.get_member(user_id) if user_id else None + + # If we still don't have a Member at this point, give up + if not member: + log.debug("This bot has never gotten a DM, or the RedisCache has been cleared.") + await ctx.message.add_reaction("❌") + return try: - if member: - await member.send(message) - await ctx.message.add_reaction("✅") - self.bot.stats.incr("dm_relay.dm_sent") - return - elif last_dm_user: - await last_dm_user.send(message) - await ctx.message.add_reaction("✅") - self.bot.stats.incr("dm_relay.dm_sent") - return - else: - log.debug("This bot has never gotten a DM, or the RedisCache has been cleared.") - await ctx.message.add_reaction("❌") + await member.send(message) + except discord.errors.Forbidden: + log.debug("User has disabled DMs.") + await ctx.message.add_reaction("❌") + else: + await ctx.message.add_reaction("✅") + self.bot.stats.incr("dm_relay.dm_sent") except discord.errors.Forbidden: log.debug("User has disabled DMs.") await ctx.message.add_reaction("❌") -- cgit v1.2.3 From 403572b83cf3faea9068a25cb09e809d993c1514 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 15 Jul 2020 10:39:07 +0200 Subject: Create a UserMentionOrID converter. When we're using the !reply command, using a regular UserConverter is somewhat problematic. For example, if I wanted to send the message "lemon loves you", then I'd try to write `!reply lemon loves you` - however, the optional User converter would then try to convert `lemon` into a User, which it would successfully do since there's like 60 lemons on our server. As a result, the message "loves you" would be sent to a user called lemon.. god knows which one. To solve this bit of ambiguity, I introduce a new converter which only converts user mentions or user IDs into User, not strings that may be intended as part of the message you are sending. https://github.com/python-discord/bot/issues/1041 --- bot/cogs/dm_relay.py | 3 ++- bot/converters.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index 0c3eddf42..c5a3dba22 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -8,6 +8,7 @@ from discord.ext.commands import Cog from bot import constants from bot.bot import Bot +from bot.converters import UserMentionOrID from bot.utils import RedisCache from bot.utils.checks import in_whitelist_check, with_role_check from bot.utils.messages import send_attachments @@ -29,7 +30,7 @@ class DMRelay(Cog): self.bot.loop.create_task(self.fetch_webhook()) @commands.command(aliases=("reply",)) - async def send_dm(self, ctx: commands.Context, member: Optional[discord.Member], *, message: str) -> None: + async def send_dm(self, ctx: commands.Context, member: Optional[UserMentionOrID], *, message: str) -> None: """ Allows you to send a DM to a user from the bot. diff --git a/bot/converters.py b/bot/converters.py index 898822165..7c62f92dd 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -330,6 +330,28 @@ def proxy_user(user_id: str) -> discord.Object: return user +class UserMentionOrID(UserConverter): + """ + Converts to a `discord.User`, but only if a mention or userID is provided. + + Unlike the default `UserConverter`, it does allow conversion from name, or name#descrim. + + This is useful in cases where that lookup strategy would lead to ambiguity. + """ + + async def convert(self, ctx: Context, argument: str) -> discord.User: + """Convert the `arg` to a `discord.User`.""" + print(argument) + match = self._get_id_match(argument) or re.match(r'<@!?([0-9]+)>$', argument) + + print(match) + + if match is not None: + return await super().convert(ctx, argument) + else: + raise BadArgument(f"`{argument}` is not a User mention or a User ID.") + + class FetchedUser(UserConverter): """ Converts to a `discord.User` or, if it fails, a `discord.Object`. -- cgit v1.2.3 From 80c1dbb240b744ceed5d1ea56c44c91d0014c304 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 15 Jul 2020 10:44:11 +0200 Subject: How did that except except block get in? Weird. https://github.com/python-discord/bot/issues/1041 --- bot/cogs/dm_relay.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index c5a3dba22..0dc15d4b1 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -60,9 +60,6 @@ class DMRelay(Cog): else: await ctx.message.add_reaction("✅") self.bot.stats.incr("dm_relay.dm_sent") - except discord.errors.Forbidden: - log.debug("User has disabled DMs.") - await ctx.message.add_reaction("❌") async def fetch_webhook(self) -> None: """Fetches the webhook object, so we can post to it.""" -- cgit v1.2.3 From 14141a25bb87c298afae89886cb0ca3df65b9dee Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 15 Jul 2020 12:59:48 +0200 Subject: Oops, these prints shouldn't be here. https://github.com/python-discord/bot/issues/1041 --- bot/converters.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/bot/converters.py b/bot/converters.py index 7c62f92dd..4a0633951 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -341,11 +341,8 @@ class UserMentionOrID(UserConverter): async def convert(self, ctx: Context, argument: str) -> discord.User: """Convert the `arg` to a `discord.User`.""" - print(argument) match = self._get_id_match(argument) or re.match(r'<@!?([0-9]+)>$', argument) - print(match) - if match is not None: return await super().convert(ctx, argument) else: -- cgit v1.2.3 From 281ffdfdcaa900cb82c4f0a9f0b0ae5c859a4de4 Mon Sep 17 00:00:00 2001 From: Senjan21 <53477086+Senjan21@users.noreply.github.com> Date: Wed, 15 Jul 2020 18:28:03 +0200 Subject: Added command&system to purge all messages up to given message --- bot/cogs/clean.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/bot/cogs/clean.py b/bot/cogs/clean.py index 368d91c85..7b2b83a02 100644 --- a/bot/cogs/clean.py +++ b/bot/cogs/clean.py @@ -45,6 +45,7 @@ class Clean(Cog): bots_only: bool = False, user: User = None, regex: Optional[str] = None, + until_message: Optional[Message] = None, ) -> None: """A helper function that does the actual message cleaning.""" def predicate_bots_only(message: Message) -> bool: @@ -129,6 +130,25 @@ class Clean(Cog): if not self.cleaning: return + # If we are looking for specific message. + if until_message: + # Since we will be using `delete_messages` method + # of a TextChannel + # and we need message objects to use it + # as well as to send logs + # we will start appending messages here + # instead adding them from purge. + messages.append(message) + # we could use ID's here however + # in case if the message we are looking for + # gets deleted, we won't have a way to figure that out + # thus checking for datetime should be more reliable + if message.created_at <= until_message.created_at: + # means we have found the message until which + # we were supposed to be deleting. + message_ids.append(message.id) + break + # If the message passes predicate, let's save it. if predicate is None or predicate(message): message_ids.append(message.id) @@ -138,7 +158,14 @@ class Clean(Cog): # Now let's delete the actual messages with purge. self.mod_log.ignore(Event.message_delete, *message_ids) for channel in channels: - messages += await channel.purge(limit=amount, check=predicate) + if until_message: + for i in range(0, len(messages), 100): + # while purge automatically handles the amount of messages + # delete_messages only allows for up to 100 messages at once + # thus we need to paginate the amount to always be <= 100 + await channel.delete_messages(messages[i:i + 100]) + else: + messages += await channel.purge(limit=amount, check=predicate) # Reverse the list to restore chronological order if messages: @@ -221,6 +248,17 @@ class Clean(Cog): """Delete all messages that match a certain regex, stop cleaning after traversing `amount` messages.""" await self._clean_messages(amount, ctx, regex=regex, channels=channels) + @clean_group.command(name="message", aliases=["messages"]) + @with_role(*MODERATION_ROLES) + async def clean_message(self, ctx: Context, message: Message) -> None: + """Delete all messages until certain message, stop cleaning after hitting the `message`""" + await self._clean_messages( + CleanMessages.message_limit, + ctx, + channels=[message.channel], + until_message=message + ) + @clean_group.command(name="stop", aliases=["cancel", "abort"]) @with_role(*MODERATION_ROLES) async def clean_cancel(self, ctx: Context) -> None: -- cgit v1.2.3 From ace60c4776ee02104390f0c782543118290e53c8 Mon Sep 17 00:00:00 2001 From: Senjan21 <53477086+Senjan21@users.noreply.github.com> Date: Wed, 15 Jul 2020 19:04:15 +0200 Subject: Fix docstring and comments --- bot/cogs/clean.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/bot/cogs/clean.py b/bot/cogs/clean.py index 7b2b83a02..aee7fa055 100644 --- a/bot/cogs/clean.py +++ b/bot/cogs/clean.py @@ -132,20 +132,14 @@ class Clean(Cog): # If we are looking for specific message. if until_message: - # Since we will be using `delete_messages` method - # of a TextChannel - # and we need message objects to use it - # as well as to send logs - # we will start appending messages here - # instead adding them from purge. + # Since we will be using `delete_messages` method of a TextChannel and we need message objects to + # use it as well as to send logs we will start appending messages here instead adding them from + # purge. messages.append(message) - # we could use ID's here however - # in case if the message we are looking for - # gets deleted, we won't have a way to figure that out - # thus checking for datetime should be more reliable + # we could use ID's here however in case if the message we are looking for gets deleted, + # we won't have a way to figure that out thus checking for datetime should be more reliable if message.created_at <= until_message.created_at: - # means we have found the message until which - # we were supposed to be deleting. + # means we have found the message until which we were supposed to be deleting. message_ids.append(message.id) break @@ -251,7 +245,7 @@ class Clean(Cog): @clean_group.command(name="message", aliases=["messages"]) @with_role(*MODERATION_ROLES) async def clean_message(self, ctx: Context, message: Message) -> None: - """Delete all messages until certain message, stop cleaning after hitting the `message`""" + """Delete all messages until certain message, stop cleaning after hitting the `message`.""" await self._clean_messages( CleanMessages.message_limit, ctx, -- cgit v1.2.3 From 776b4379c478284803a4a526b5f14fe63d8e7c01 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 16 Jul 2020 11:45:15 +0800 Subject: Remove duplicate reminder deletion. The function `_delete_reminder` was called twice, once in `schedule_reminder`, which calls `send_reminder`, then another in `send_reminder` itself. This led to a 404 response from the site api, as the reminder was already deleted the first time. Fixes BOT-6W --- bot/cogs/reminders.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index 0d20bdb2b..4f2ab1781 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -55,6 +55,7 @@ class Reminders(Cog): if remind_at < now: late = relativedelta(now, remind_at) await self.send_reminder(reminder, late) + await self._delete_reminder(reminder["id"]) else: self.schedule_reminder(reminder) @@ -157,7 +158,6 @@ class Reminders(Cog): content=user.mention, embed=embed ) - await self._delete_reminder(reminder["id"]) @group(name="remind", aliases=("reminder", "reminders", "remindme"), invoke_without_command=True) async def remind_group(self, ctx: Context, expiration: Duration, *, content: str) -> None: -- cgit v1.2.3 From 9389543fe89f623301842b3f850cf767d1bf45ea Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 16 Jul 2020 12:29:20 +0800 Subject: Extract sending error embed to a separate method. --- bot/cogs/reminders.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index 4f2ab1781..ebf85cc4d 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -100,6 +100,16 @@ class Reminders(Cog): await ctx.send(embed=embed) + @staticmethod + async def _send_denial(ctx: Context, reason: str) -> None: + """Send an embed denying the user from creating a reminder.""" + embed = discord.Embed() + embed.colour = discord.Colour.red() + embed.title = random.choice(NEGATIVE_REPLIES) + embed.description = reason + + await ctx.send(embed=embed) + def schedule_reminder(self, reminder: dict) -> None: """A coroutine which sends the reminder once the time is reached, and cancels the running task.""" reminder_id = reminder["id"] @@ -171,18 +181,12 @@ class Reminders(Cog): Expiration is parsed per: http://strftime.org/ """ - embed = discord.Embed() - # If the user is not staff, we need to verify whether or not to make a reminder at all. if without_role_check(ctx, *STAFF_ROLES): # If they don't have permission to set a reminder in this channel if ctx.channel.id not in WHITELISTED_CHANNELS: - embed.colour = discord.Colour.red() - embed.title = random.choice(NEGATIVE_REPLIES) - embed.description = "Sorry, you can't do that here!" - - return await ctx.send(embed=embed) + return await self._send_denial(ctx, "Sorry, you can't do that here!") # Get their current active reminders active_reminders = await self.bot.api_client.get( @@ -195,12 +199,7 @@ class Reminders(Cog): # Let's limit this, so we don't get 10 000 # reminders from kip or something like that :P if len(active_reminders) > MAXIMUM_REMINDERS: - embed.colour = discord.Colour.red() - embed.title = random.choice(NEGATIVE_REPLIES) - embed.description = "You have too many active reminders!" - - return await ctx.send(embed=embed) - + return await self._send_denial(ctx, "You have too many active reminders!") # Now we can attempt to actually set the reminder. reminder = await self.bot.api_client.post( 'bot/reminders', -- cgit v1.2.3 From 61459ed1fd40b10eb9c61d2b2d3ae1cea3547ea8 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 16 Jul 2020 13:01:54 +0800 Subject: Add method to check if user is allowed to mention in reminders. --- bot/cogs/reminders.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index ebf85cc4d..aefc4a359 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -9,10 +9,11 @@ from operator import itemgetter import discord from dateutil.parser import isoparse from dateutil.relativedelta import relativedelta +from discord import Member, Role from discord.ext.commands import Cog, Context, group from bot.bot import Bot -from bot.constants import Guild, Icons, NEGATIVE_REPLIES, POSITIVE_REPLIES, STAFF_ROLES +from bot.constants import Guild, Icons, MODERATION_ROLES, NEGATIVE_REPLIES, POSITIVE_REPLIES, STAFF_ROLES from bot.converters import Duration from bot.pagination import LinePaginator from bot.utils.checks import without_role_check @@ -24,6 +25,8 @@ log = logging.getLogger(__name__) WHITELISTED_CHANNELS = Guild.reminder_whitelist MAXIMUM_REMINDERS = 5 +Mentionable = t.Union[Member, Role] + class Reminders(Cog): """Provide in-channel reminder functionality.""" @@ -110,6 +113,23 @@ class Reminders(Cog): await ctx.send(embed=embed) + @staticmethod + async def allow_mentions(ctx: Context, mentions: t.List[Mentionable]) -> t.Tuple[bool, str]: + """ + Returns whether or not the list of mentions is allowed. + + Conditions: + - Role reminders are Mods+ + - Reminders for other users are Helpers+ + If mentions aren't allowed, also return the type of mention(s) disallowed. + """ + if without_role_check(ctx, *STAFF_ROLES): + return False, "members/roles" + elif without_role_check(ctx, *MODERATION_ROLES): + return all(isinstance(mention, Member) for mention in mentions), "roles" + else: + return True, "" + def schedule_reminder(self, reminder: dict) -> None: """A coroutine which sends the reminder once the time is reached, and cancels the running task.""" reminder_id = reminder["id"] -- cgit v1.2.3 From 76b8e4625e853bcc96c946fa408c2267e78dbc72 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 16 Jul 2020 13:10:28 +0800 Subject: Add generator that converts IDs to Role or Member objects. --- bot/cogs/reminders.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index aefc4a359..ab47f3b11 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -130,6 +130,13 @@ class Reminders(Cog): else: return True, "" + def get_mentionables_from_ids(self, mention_ids: t.List[str]) -> t.Iterator[Mentionable]: + """Converts Role and Member ids to their corresponding objects if possible.""" + guild = self.bot.get_guild(Guild.id) + for mention_id in mention_ids: + if (mentionable := (guild.get_member(mention_id) or guild.get_role(mention_id))): + yield mentionable + def schedule_reminder(self, reminder: dict) -> None: """A coroutine which sends the reminder once the time is reached, and cancels the running task.""" reminder_id = reminder["id"] -- cgit v1.2.3 From da2849a4fbbc5b2180cc042be66f1511017b2488 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 16 Jul 2020 13:12:12 +0800 Subject: Allow mentioning other users and roles in reminders. --- bot/cogs/reminders.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index ab47f3b11..5ef35602c 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -10,7 +10,7 @@ import discord from dateutil.parser import isoparse from dateutil.relativedelta import relativedelta from discord import Member, Role -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 Guild, Icons, MODERATION_ROLES, NEGATIVE_REPLIES, POSITIVE_REPLIES, STAFF_ROLES @@ -197,12 +197,16 @@ class Reminders(Cog): ) @group(name="remind", aliases=("reminder", "reminders", "remindme"), invoke_without_command=True) - async def remind_group(self, ctx: Context, expiration: Duration, *, content: str) -> None: + async def remind_group( + self, ctx: Context, mentions: Greedy[Mentionable], expiration: Duration, *, content: str + ) -> None: """Commands for managing your reminders.""" - await ctx.invoke(self.new_reminder, expiration=expiration, content=content) + await ctx.invoke(self.new_reminder, mentions=mentions, expiration=expiration, content=content) @remind_group.command(name="new", aliases=("add", "create")) - async def new_reminder(self, ctx: Context, expiration: Duration, *, content: str) -> t.Optional[discord.Message]: + async def new_reminder( + self, ctx: Context, mentions: Greedy[Mentionable], expiration: Duration, *, content: str + ) -> t.Optional[discord.Message]: """ Set yourself a simple reminder. @@ -227,6 +231,17 @@ class Reminders(Cog): # reminders from kip or something like that :P if len(active_reminders) > MAXIMUM_REMINDERS: return await self._send_denial(ctx, "You have too many active reminders!") + + # Filter mentions to see if the user can mention members/roles + if mentions: + mentions_allowed, disallowed_mentions = await self.allow_mentions(ctx, mentions) + if not mentions_allowed: + return await self._send_denial( + ctx, f"You can't mention other {disallowed_mentions} in your reminder!" + ) + + mention_ids = [mention.id for mention in mentions] + # Now we can attempt to actually set the reminder. reminder = await self.bot.api_client.post( 'bot/reminders', @@ -235,17 +250,22 @@ class Reminders(Cog): 'channel_id': ctx.message.channel.id, 'jump_url': ctx.message.jump_url, 'content': content, - 'expiration': expiration.isoformat() + 'expiration': expiration.isoformat(), + 'mentions': mention_ids, } ) now = datetime.utcnow() - timedelta(seconds=1) humanized_delta = humanize_delta(relativedelta(expiration, now)) + mention_string = ( + f"Your reminder will arrive in {humanized_delta} " + f"and will mention {len(mentions)} other(s)!" + ) # Confirm to the user that it worked. await self._send_confirmation( ctx, - on_success=f"Your reminder will arrive in {humanized_delta}!", + on_success=mention_string, reminder_id=reminder["id"], delivery_dt=expiration, ) -- cgit v1.2.3 From 1ee3febc398deafa4d87b6db93b4e3af6976b0e7 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 16 Jul 2020 13:13:05 +0800 Subject: Send additional mentions in reminders. --- bot/cogs/reminders.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index 5ef35602c..a004902c2 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -191,8 +191,12 @@ class Reminders(Cog): name=f"Sorry it arrived {humanize_delta(late, max_units=2)} late!" ) + additional_mentions = ' '.join( + mentionable.mention for mentionable in self.get_mentionables_from_ids(reminder["mentions"]) + ) + await channel.send( - content=user.mention, + content=f"{user.mention} {additional_mentions}", embed=embed ) -- cgit v1.2.3 From cf9a350934a099ae71fcc0c46fbf57d7fb82dd86 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 16 Jul 2020 13:14:10 +0800 Subject: List additional mentions in `!reminder list`. --- bot/cogs/reminders.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index a004902c2..fd3c6efa2 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -290,7 +290,7 @@ class Reminders(Cog): # Make a list of tuples so it can be sorted by time. reminders = sorted( ( - (rem['content'], rem['expiration'], rem['id']) + (rem['content'], rem['expiration'], rem['id'], rem['mentions']) for rem in data ), key=itemgetter(1) @@ -298,13 +298,19 @@ class Reminders(Cog): lines = [] - for content, remind_at, id_ in reminders: + for content, remind_at, id_, mentions in reminders: # Parse and humanize the time, make it pretty :D remind_datetime = isoparse(remind_at).replace(tzinfo=None) time = humanize_delta(relativedelta(remind_datetime, now)) + mentions = ", ".join( + # Both Role and User objects have the `name` attribute + mention.name for mention in self.get_mentionables_from_ids(mentions) + ) + mention_string = f"\n**Mentions:** {mentions}" if mentions else "" + text = textwrap.dedent(f""" - **Reminder #{id_}:** *expires in {time}* (ID: {id_}) + **Reminder #{id_}:** *expires in {time}* (ID: {id_}) {mention_string} {content} """).strip() -- cgit v1.2.3 From 3f319488f479cd38e719201b4c926ace68ef9102 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 16 Jul 2020 13:14:38 +0800 Subject: Allow editing additional mentions for reminders. --- bot/cogs/reminders.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index fd3c6efa2..9eddd283b 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -384,6 +384,34 @@ class Reminders(Cog): ) await self._reschedule_reminder(reminder) + @edit_reminder_group.command(name="mentions", aliases=("pings",)) + async def edit_reminder_mentions(self, ctx: Context, id_: int, mentions: Greedy[Mentionable]) -> None: + """Edit one of your reminder's mentions.""" + # Filter mentions to see if the user can mention members/roles + mentions_allowed, disallowed_mentions = await self.allow_mentions(ctx, mentions) + if not mentions_allowed: + return await self._send_denial( + ctx, f"You can't mention other {disallowed_mentions} in your reminder!" + ) + + mention_ids = [mention.id for mention in mentions] + reminder = await self.bot.api_client.patch( + 'bot/reminders/' + str(id_), + json={"mentions": mention_ids} + ) + + # Parse the reminder expiration back into a datetime for the confirmation message + expiration = isoparse(reminder['expiration']).replace(tzinfo=None) + + # Send a confirmation message to the channel + await self._send_confirmation( + ctx, + on_success="That reminder has been edited successfully!", + reminder_id=id_, + delivery_dt=expiration, + ) + await self._reschedule_reminder(reminder) + @remind_group.command("delete", aliases=("remove", "cancel")) async def delete_reminder(self, ctx: Context, id_: int) -> None: """Delete one of your active reminders.""" -- cgit v1.2.3 From b6abe9cbb2e63f562bb44e14d51ea87f19da32ac Mon Sep 17 00:00:00 2001 From: Senjan21 <53477086+Senjan21@users.noreply.github.com> Date: Thu, 16 Jul 2020 10:41:54 +0200 Subject: Prevent deleting messages above the desired message. --- bot/cogs/clean.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bot/cogs/clean.py b/bot/cogs/clean.py index aee7fa055..f436e531a 100644 --- a/bot/cogs/clean.py +++ b/bot/cogs/clean.py @@ -132,17 +132,18 @@ class Clean(Cog): # If we are looking for specific message. if until_message: - # Since we will be using `delete_messages` method of a TextChannel and we need message objects to - # use it as well as to send logs we will start appending messages here instead adding them from - # purge. - messages.append(message) + # we could use ID's here however in case if the message we are looking for gets deleted, # we won't have a way to figure that out thus checking for datetime should be more reliable - if message.created_at <= until_message.created_at: + if message.created_at < until_message.created_at: # means we have found the message until which we were supposed to be deleting. - message_ids.append(message.id) break + # Since we will be using `delete_messages` method of a TextChannel and we need message objects to + # use it as well as to send logs we will start appending messages here instead adding them from + # purge. + messages.append(message) + # If the message passes predicate, let's save it. if predicate is None or predicate(message): message_ids.append(message.id) -- cgit v1.2.3 From 6f5fb205bcc3f9b468ef585f83e123e5b19d7340 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 16 Jul 2020 17:03:02 +0200 Subject: Incidents: reduce log level of 404 exception Co-authored-by: MarkKoz --- bot/cogs/moderation/incidents.py | 2 ++ tests/bot/cogs/moderation/test_incidents.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 018538040..2d5f26f20 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -55,6 +55,8 @@ async def download_file(attachment: discord.Attachment) -> t.Optional[discord.Fi log.debug(f"Attempting to download attachment: {attachment.filename}") try: return await attachment.to_file() + except discord.NotFound as not_found: + log.debug(f"Failed to download attachment: {not_found}") except Exception: log.exception("Failed to download attachment") diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py index 9b6054f55..435a1cd51 100644 --- a/tests/bot/cogs/moderation/test_incidents.py +++ b/tests/bot/cogs/moderation/test_incidents.py @@ -81,13 +81,23 @@ class TestDownloadFile(unittest.IsolatedAsyncioTestCase): acquired_file = await incidents.download_file(attachment) self.assertIs(file, acquired_file) - async def test_download_file_fail(self): - """If `to_file` fails, function handles the exception & returns None.""" + async def test_download_file_404(self): + """If `to_file` encounters a 404, function handles the exception & returns None.""" attachment = MockAttachment(to_file=AsyncMock(side_effect=mock_404)) acquired_file = await incidents.download_file(attachment) self.assertIsNone(acquired_file) + async def test_download_file_fail(self): + """If `to_file` fails on a non-404 error, function logs the exception & returns None.""" + arbitrary_error = discord.HTTPException(MagicMock(aiohttp.ClientResponse), "Arbitrary API error") + attachment = MockAttachment(to_file=AsyncMock(side_effect=arbitrary_error)) + + with self.assertLogs(logger=incidents.log, level=logging.ERROR): + acquired_file = await incidents.download_file(attachment) + + self.assertIsNone(acquired_file) + class TestMakeEmbed(unittest.IsolatedAsyncioTestCase): """Collection of tests for the `make_embed` helper function.""" -- cgit v1.2.3 From 4fb188da6af5a1751e7a996693001d464232b10c Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Thu, 16 Jul 2020 19:54:06 +0200 Subject: Bugfix: Show ID for embed DM relays, too. --- bot/cogs/dm_relay.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index 0dc15d4b1..0d8f340b4 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -99,7 +99,7 @@ class DMRelay(Cog): await send_webhook( webhook=self.webhook, embed=e, - username=message.author.display_name, + username=f"{message.author.display_name} ({message.author.id})", avatar_url=message.author.avatar_url ) except discord.HTTPException: -- cgit v1.2.3 From 476d4070940830d14859f7cc8970a14409d142a6 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 16 Jul 2020 20:27:32 +0200 Subject: Incidents: reduce log level of 403 exception In addition to 404, this shouldn't send Sentry notifs. Co-authored-by: MarkKoz --- bot/cogs/moderation/incidents.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py index 2d5f26f20..3605ab1d2 100644 --- a/bot/cogs/moderation/incidents.py +++ b/bot/cogs/moderation/incidents.py @@ -51,12 +51,13 @@ async def download_file(attachment: discord.Attachment) -> t.Optional[discord.Fi Download & return `attachment` file. If the download fails, the reason is logged and None will be returned. + 404 and 403 errors are only logged at debug level. """ log.debug(f"Attempting to download attachment: {attachment.filename}") try: return await attachment.to_file() - except discord.NotFound as not_found: - log.debug(f"Failed to download attachment: {not_found}") + except (discord.NotFound, discord.Forbidden) as exc: + log.debug(f"Failed to download attachment: {exc}") except Exception: log.exception("Failed to download attachment") -- cgit v1.2.3 From db79b6acb8c4204ef2dad7053d94f0ddcec3c283 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 17 Jul 2020 21:30:39 +0200 Subject: Kaizen: Move OffTopicName to converters.py. --- bot/cogs/off_topic_names.py | 31 ++----------------------------- bot/converters.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index 201579a0b..ce95450e0 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -4,46 +4,19 @@ import logging from datetime import datetime, timedelta from discord import Colour, Embed -from discord.ext.commands import BadArgument, Cog, Context, Converter, group +from discord.ext.commands import Cog, Context, group from bot.api import ResponseCodeError from bot.bot import Bot from bot.constants import Channels, MODERATION_ROLES +from bot.converters import OffTopicName from bot.decorators import with_role from bot.pagination import LinePaginator - CHANNELS = (Channels.off_topic_0, Channels.off_topic_1, Channels.off_topic_2) log = logging.getLogger(__name__) -class OffTopicName(Converter): - """A converter that ensures an added off-topic name is valid.""" - - @staticmethod - async def convert(ctx: Context, argument: str) -> str: - """Attempt to replace any invalid characters with their approximate Unicode equivalent.""" - allowed_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!?'`-" - - # Chain multiple words to a single one - argument = "-".join(argument.split()) - - if not (2 <= len(argument) <= 96): - raise BadArgument("Channel name must be between 2 and 96 chars long") - - elif not all(c.isalnum() or c in allowed_characters for c in argument): - raise BadArgument( - "Channel name must only consist of " - "alphanumeric characters, minus signs or apostrophes." - ) - - # Replace invalid characters with unicode alternatives. - table = str.maketrans( - allowed_characters, '𝖠𝖡𝖢𝖣𝖤𝖥𝖦𝖧𝖨𝖩𝖪𝖫𝖬𝖭𝖮𝖯𝖰𝖱𝖲𝖳𝖴𝖵𝖶𝖷𝖸𝖹ǃ?’’-' - ) - return argument.translate(table) - - async def update_names(bot: Bot) -> None: """Background updater task that performs the daily channel name update.""" while True: diff --git a/bot/converters.py b/bot/converters.py index 4a0633951..406fd0d68 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -237,6 +237,32 @@ class Duration(DurationDelta): raise BadArgument(f"`{duration}` results in a datetime outside the supported range.") +class OffTopicName(Converter): + """A converter that ensures an added off-topic name is valid.""" + + async def convert(self, ctx: Context, argument: str) -> str: + """Attempt to replace any invalid characters with their approximate Unicode equivalent.""" + allowed_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!?'`-" + + # Chain multiple words to a single one + argument = "-".join(argument.split()) + + if not (2 <= len(argument) <= 96): + raise BadArgument("Channel name must be between 2 and 96 chars long") + + elif not all(c.isalnum() or c in allowed_characters for c in argument): + raise BadArgument( + "Channel name must only consist of " + "alphanumeric characters, minus signs or apostrophes." + ) + + # Replace invalid characters with unicode alternatives. + table = str.maketrans( + allowed_characters, '𝖠𝖡𝖢𝖣𝖤𝖥𝖦𝖧𝖨𝖩𝖪𝖫𝖬𝖭𝖮𝖯𝖰𝖱𝖲𝖳𝖴𝖵𝖶𝖷𝖸𝖹ǃ?’’-' + ) + return argument.translate(table) + + class ISODateTime(Converter): """Converts an ISO-8601 datetime string into a datetime.datetime.""" -- cgit v1.2.3 From 98c325f316038536270c87d0f767e2c18c215df7 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 17 Jul 2020 21:33:14 +0200 Subject: Cache AllowDenyList data at bot startup. We shouldn't be making an API call for every single message posted, so what we're gonna do is cache the data in the Bot, and then update the cache whenever we make changes to it via our new AllowDenyList cog. Since this cog will be the only way to make changes to this, this level of lazy caching should be enough to always keep the cache up to date. --- bot/bot.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bot/bot.py b/bot/bot.py index 313652d11..b170be6d3 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -49,6 +49,10 @@ class Bot(commands.Bot): self.stats = AsyncStatsClient(self.loop, statsd_url, 8125, prefix="bot") + async def _cache_allow_deny_list_data(self) -> None: + """Cache all the data in the AllowDenyList on the site.""" + self.allow_deny_list_cache = await self.api_client.get('bot/allow_deny_lists') + async def _create_redis_session(self) -> None: """ Create the Redis connection pool, and then open the redis event gate. @@ -159,6 +163,9 @@ class Bot(commands.Bot): self.http_session = aiohttp.ClientSession(connector=self._connector) self.api_client.recreate(force=True, connector=self._connector) + # Build the AllowDenyList cache + self.loop.create_task(self._cache_allow_deny_list_data()) + async def on_guild_available(self, guild: discord.Guild) -> None: """ Set the internal guild available event when constants.Guild.id becomes available. -- cgit v1.2.3 From d83417432324019b16d0450cdb0c71db9452c52f Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 17 Jul 2020 21:34:40 +0200 Subject: Add ValidAllowDenyListType converter. We'll use this to ensure the input is valid when people try to whitelist or blacklist stuff. It will fetch its data from an Enum maintained on the site, so that the types of lists we support will only need to be maintained in a single place, instead of duplicating that data in the bot and the site. --- bot/converters.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/bot/converters.py b/bot/converters.py index 406fd0d68..4d2acb910 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -7,7 +7,7 @@ from ssl import CertificateError import dateutil.parser import dateutil.tz import discord -from aiohttp import ClientConnectorError +from aiohttp import ClientConnectorError, ContentTypeError from dateutil.relativedelta import relativedelta from discord.ext.commands import BadArgument, Context, Converter, UserConverter @@ -34,6 +34,32 @@ def allowed_strings(*values, preserve_case: bool = False) -> t.Callable[[str], s return converter +class ValidAllowDenyListType(Converter): + """ + A converter that checks whether the given string is a valid AllowDenyList type. + + Raises `BadArgument` if the argument is not a valid AllowDenyList type, and simply + passes through the given argument otherwise. + """ + + async def convert(self, ctx: Context, list_type: str) -> str: + """Checks whether the given string is a valid AllowDenyList type.""" + try: + valid_types = await ctx.bot.api_client.get('bot/allow_deny_lists/get_types') + except ContentTypeError: + raise BadArgument("Cannot validate list_type: Unable to fetch valid types from API.") + + valid_types = [enum for enum, classname in valid_types] + list_type = list_type.upper() + + if list_type not in valid_types: + raise BadArgument( + f"You have provided an invalid AllowDenyList type!\n\n" + f"Please provide one of the following: \n{', '.join(valid_types)}." + ) + return list_type + + class ValidPythonIdentifier(Converter): """ A converter that checks whether the given string is a valid Python identifier. -- cgit v1.2.3 From 0d22a0483e619788f59b6dfe2f8e6f64ec76e326 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 17 Jul 2020 21:40:16 +0200 Subject: Kaizen: Make error_handler.py more embeddy. Currently, some types of errors are returning plain strings that repeat the input (which can be exploited to deliver stuff like mentions), and others are returning generic messages that don't give any exception information. This commit unifies our approach around putting as much information as we can (including the exception message), but always putting it inside an embed, so that stuff like pings will not fire. This, combined with the 1.4.0a `allowed_mentions` functionality, seems like a reasonable compromise between security and usability. --- bot/cogs/error_handler.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index 233851e41..f9d4de638 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -2,12 +2,13 @@ import contextlib import logging import typing as t +from discord import Embed from discord.ext.commands import Cog, Context, errors from sentry_sdk import push_scope from bot.api import ResponseCodeError from bot.bot import Bot -from bot.constants import Channels +from bot.constants import Channels, Colours from bot.converters import TagNameConverter from bot.utils.checks import InWhitelistCheckFailure @@ -20,6 +21,14 @@ class ErrorHandler(Cog): def __init__(self, bot: Bot): self.bot = bot + def _get_error_embed(self, title: str, body: str) -> Embed: + """Return an embed that contains the exception.""" + return Embed( + title=title, + colour=Colours.soft_red, + description=body + ) + @Cog.listener() async def on_command_error(self, ctx: Context, e: errors.CommandError) -> None: """ @@ -162,25 +171,34 @@ class ErrorHandler(Cog): prepared_help_command = self.get_help_command(ctx) if isinstance(e, errors.MissingRequiredArgument): - await ctx.send(f"Missing required argument `{e.param.name}`.") + embed = self._get_error_embed("Missing required argument", e.param.name) + await ctx.send(embed=embed) await prepared_help_command self.bot.stats.incr("errors.missing_required_argument") elif isinstance(e, errors.TooManyArguments): - await ctx.send("Too many arguments provided.") + embed = self._get_error_embed("Too many arguments", str(e)) + await ctx.send(embed=embed) await prepared_help_command self.bot.stats.incr("errors.too_many_arguments") elif isinstance(e, errors.BadArgument): - await ctx.send("Bad argument: Please double-check your input arguments and try again.\n") + embed = self._get_error_embed("Bad argument", str(e)) + await ctx.send(embed=embed) await prepared_help_command self.bot.stats.incr("errors.bad_argument") elif isinstance(e, errors.BadUnionArgument): - await ctx.send(f"Bad argument: {e}\n```{e.errors[-1]}```") + embed = self._get_error_embed("Bad argument", f"{e}\n{e.errors[-1]}") + await ctx.send(embed=embed) self.bot.stats.incr("errors.bad_union_argument") elif isinstance(e, errors.ArgumentParsingError): - await ctx.send(f"Argument parsing error: {e}") + embed = self._get_error_embed("Argument parsing error", str(e)) + await ctx.send(embed=embed) self.bot.stats.incr("errors.argument_parsing_error") else: - await ctx.send("Something about your input seems off. Check the arguments:") + embed = self._get_error_embed( + "Input error", + "Something about your input seems off. Check the arguments and try again." + ) + await ctx.send(embed=embed) await prepared_help_command self.bot.stats.incr("errors.other_user_input_error") -- cgit v1.2.3 From ccc2e7abe8762dd394a0e548a47d881dbffdc917 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 18 Jul 2020 12:50:04 +0200 Subject: Better BadArgument exception text. --- bot/converters.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bot/converters.py b/bot/converters.py index 4d2acb910..429546ba2 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -50,12 +50,13 @@ class ValidAllowDenyListType(Converter): raise BadArgument("Cannot validate list_type: Unable to fetch valid types from API.") valid_types = [enum for enum, classname in valid_types] + valid_types_lower = [type_.lower() for type_ in valid_types] list_type = list_type.upper() if list_type not in valid_types: raise BadArgument( - f"You have provided an invalid AllowDenyList type!\n\n" - f"Please provide one of the following: \n{', '.join(valid_types)}." + f"You have provided an invalid list type!\n\n" + f"Please provide one of the following: \n{', '.join(valid_types_lower)}." ) return list_type -- cgit v1.2.3 From 7d7fdd7bd27aba48edf65cb8f9da1974ea0aac0b Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 18 Jul 2020 13:44:59 +0200 Subject: Bulletlist with valid file types in converter. --- bot/converters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/converters.py b/bot/converters.py index 429546ba2..edac67be2 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -50,13 +50,13 @@ class ValidAllowDenyListType(Converter): raise BadArgument("Cannot validate list_type: Unable to fetch valid types from API.") valid_types = [enum for enum, classname in valid_types] - valid_types_lower = [type_.lower() for type_ in valid_types] + valid_types_list = '\n'.join([f"• {type_.lower()}" for type_ in valid_types]) list_type = list_type.upper() if list_type not in valid_types: raise BadArgument( f"You have provided an invalid list type!\n\n" - f"Please provide one of the following: \n{', '.join(valid_types_lower)}." + f"Please provide one of the following: \n{valid_types_list}" ) return list_type -- cgit v1.2.3 From b1311ea71adbc3c4c5568363aa971a08f21b2522 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 18 Jul 2020 13:46:10 +0200 Subject: Make the cache more convenient to access. Instead of just dumping the JSON response from the site, we'll build a data structure that it will be convenient to access from our new cog, and from the Filtering cog. --- bot/bot.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bot/bot.py b/bot/bot.py index b170be6d3..6c02e72a7 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -51,7 +51,19 @@ class Bot(commands.Bot): async def _cache_allow_deny_list_data(self) -> None: """Cache all the data in the AllowDenyList on the site.""" - self.allow_deny_list_cache = await self.api_client.get('bot/allow_deny_lists') + full_cache = await self.api_client.get('bot/allow_deny_lists') + self.allow_deny_list_cache = {} + + for item in full_cache: + type_ = item.get("type") + allowed = item.get("allowed") + metadata = { + "content": item.get("content"), + "id": item.get("id"), + "created_at": item.get("created_at"), + "updated_at": item.get("updated_at"), + } + self.allow_deny_list_cache.setdefault(f"{type_}.{allowed}", []).append(metadata) async def _create_redis_session(self) -> None: """ -- cgit v1.2.3 From 4d1b6a3abee00d9729ce333a25a2440d00d509f1 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 18 Jul 2020 13:47:40 +0200 Subject: Add AllowDenyLists cog. This includes commands to add, remove and show the items in the whitelists and blacklists for the different list types. Commands are limited to Moderators+. --- bot/__main__.py | 1 + bot/cogs/allow_deny_lists.py | 144 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 bot/cogs/allow_deny_lists.py diff --git a/bot/__main__.py b/bot/__main__.py index 49388455a..932aa705c 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -53,6 +53,7 @@ bot.load_extension("bot.cogs.verification") # Feature cogs bot.load_extension("bot.cogs.alias") +bot.load_extension("bot.cogs.allow_deny_lists") bot.load_extension("bot.cogs.defcon") bot.load_extension("bot.cogs.dm_relay") bot.load_extension("bot.cogs.duck_pond") diff --git a/bot/cogs/allow_deny_lists.py b/bot/cogs/allow_deny_lists.py new file mode 100644 index 000000000..d03c774ec --- /dev/null +++ b/bot/cogs/allow_deny_lists.py @@ -0,0 +1,144 @@ +import logging + +from discord import Colour, Embed +from discord.ext.commands import BadArgument, Cog, Context, group + +from bot import constants +from bot.api import ResponseCodeError +from bot.bot import Bot +from bot.converters import ValidAllowDenyListType +from bot.pagination import LinePaginator +from bot.utils.checks import with_role_check + +log = logging.getLogger(__name__) + + +class AllowDenyLists(Cog): + """Commands for blacklisting and whitelisting things.""" + + def __init__(self, bot: Bot) -> None: + self.bot = bot + + async def _add_data(self, ctx: Context, allowed: bool, list_type: ValidAllowDenyListType, content: str) -> None: + """Add an item to an allow or denylist.""" + payload = { + 'allowed': allowed, + 'type': list_type, + 'content': content, + } + allow_type = "whitelist" if allowed else "blacklist" + + # Try to add the item to the database + try: + item = await self.bot.api_client.post( + "bot/allow_deny_lists", + json=payload + ) + except ResponseCodeError as e: + if e.status == 500: + await ctx.message.add_reaction("❌") + raise BadArgument( + f"Unable to add the item to the {allow_type}. " + "The item probably already exists. Keep in mind that a " + "blacklist and a whitelist for the same item cannot co-exist, " + "and we do not permit any duplicates." + ) + raise + + # Insert the item into the cache + type_ = item.get("type") + allowed = item.get("allowed") + metadata = { + "content": item.get("content"), + "id": item.get("id"), + "created_at": item.get("created_at"), + "updated_at": item.get("updated_at"), + } + self.bot.allow_deny_list_cache.setdefault(f"{type_}.{allowed}", []).append(metadata) + await ctx.message.add_reaction("✅") + + async def _delete_data(self, ctx: Context, allowed: bool, list_type: ValidAllowDenyListType, content: str) -> None: + """Remove an item from an allow or denylist.""" + item = None + + for allow_list in self.bot.allow_deny_list_cache.get(f"{list_type}.{allowed}", []): + if content == allow_list.get("content"): + item = allow_list + break + + if item is not None: + await self.bot.api_client.delete( + f"bot/allow_deny_lists/{item.get('id')}" + ) + self.bot.allow_deny_list_cache[f"{list_type}.{allowed}"].remove(item) + await ctx.message.add_reaction("✅") + + async def _list_all_data(self, ctx: Context, allowed: bool, list_type: ValidAllowDenyListType) -> None: + """Paginate and display all items in an allow or denylist.""" + result = self.bot.allow_deny_list_cache.get(f"{list_type}.{allowed}", []) + lines = sorted(f"• {item.get('content')}" for item in result) + allowed_string = "Whitelisted" if allowed else "Blacklisted" + embed = Embed( + title=f"{allowed_string} {list_type.lower()} items ({len(result)} total)", + colour=Colour.blue() + ) + + if result: + await LinePaginator.paginate(lines, ctx, embed, max_lines=15, empty=False) + else: + embed.description = "Hmmm, seems like there's nothing here yet." + await ctx.send(embed=embed) + + @group(aliases=("allowlist", "allow", "al", "wl")) + async def whitelist(self, ctx: Context) -> None: + """Group for whitelisting commands.""" + if not ctx.invoked_subcommand: + await ctx.send_help(ctx.command) + + @group(aliases=("denylist", "deny", "bl", "dl")) + async def blacklist(self, ctx: Context) -> None: + """Group for blacklisting commands.""" + if not ctx.invoked_subcommand: + await ctx.send_help(ctx.command) + + @whitelist.command(name="add", aliases=("a", "set")) + async def allow_add(self, ctx: Context, list_type: ValidAllowDenyListType, content: str) -> None: + """Add an item to the specified allowlist.""" + await self._add_data(ctx, True, list_type, content) + + @blacklist.command(name="add", aliases=("a", "set")) + async def deny_add(self, ctx: Context, list_type: ValidAllowDenyListType, content: str) -> None: + """Add an item to the specified denylist.""" + await self._add_data(ctx, False, list_type, content) + + @whitelist.command(name="remove", aliases=("delete", "rm",)) + async def allow_delete(self, ctx: Context, list_type: ValidAllowDenyListType, content: str) -> None: + """Remove an item from the specified allowlist.""" + await self._delete_data(ctx, True, list_type, content) + + @blacklist.command(name="remove", aliases=("delete", "rm",)) + async def deny_delete(self, ctx: Context, list_type: ValidAllowDenyListType, content: str) -> None: + """Remove an item from the specified denylist.""" + await self._delete_data(ctx, False, list_type, content) + + @whitelist.command(name="get", aliases=("list", "ls", "fetch", "show")) + async def allow_get(self, ctx: Context, list_type: ValidAllowDenyListType) -> None: + """Get the contents of a specified allowlist.""" + await self._list_all_data(ctx, True, list_type) + + @blacklist.command(name="get", aliases=("list", "ls", "fetch", "show")) + async def deny_get(self, ctx: Context, list_type: ValidAllowDenyListType) -> None: + """Get the contents of a specified denylist.""" + await self._list_all_data(ctx, False, list_type) + + def cog_check(self, ctx: Context) -> bool: + """Only allow moderators to invoke the commands in this cog.""" + checks = [ + with_role_check(ctx, *constants.MODERATION_ROLES), + ] + return all(checks) + + +def setup(bot: Bot) -> None: + """Load the AllowDenyLists cog.""" + bot.add_cog(AllowDenyLists(bot)) -- cgit v1.2.3 From 2228b4229aa2c866616e2452af2c6a2f85c21fef Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 18 Jul 2020 14:20:24 +0200 Subject: Add more logging to AllowDenyLists cog. --- bot/cogs/allow_deny_lists.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/bot/cogs/allow_deny_lists.py b/bot/cogs/allow_deny_lists.py index d03c774ec..6558990a7 100644 --- a/bot/cogs/allow_deny_lists.py +++ b/bot/cogs/allow_deny_lists.py @@ -29,6 +29,7 @@ class AllowDenyLists(Cog): allow_type = "whitelist" if allowed else "blacklist" # Try to add the item to the database + log.trace(f"Trying to add the {content} item to the {list_type} {allow_type}") try: item = await self.bot.api_client.post( "bot/allow_deny_lists", @@ -37,6 +38,10 @@ class AllowDenyLists(Cog): except ResponseCodeError as e: if e.status == 500: await ctx.message.add_reaction("❌") + log.debug( + f"{ctx.author} tried to add data to a {allow_type}, but the API returned 500, " + "probably because the request violated the UniqueConstraint." + ) raise BadArgument( f"Unable to add the item to the {allow_type}. " "The item probably already exists. Keep in mind that a " @@ -60,6 +65,9 @@ class AllowDenyLists(Cog): async def _delete_data(self, ctx: Context, allowed: bool, list_type: ValidAllowDenyListType, content: str) -> None: """Remove an item from an allow or denylist.""" item = None + allow_type = "whitelist" if allowed else "blacklist" + + log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}") for allow_list in self.bot.allow_deny_list_cache.get(f"{list_type}.{allowed}", []): if content == allow_list.get("content"): @@ -77,11 +85,12 @@ class AllowDenyLists(Cog): """Paginate and display all items in an allow or denylist.""" result = self.bot.allow_deny_list_cache.get(f"{list_type}.{allowed}", []) lines = sorted(f"• {item.get('content')}" for item in result) - allowed_string = "Whitelisted" if allowed else "Blacklisted" + allow_type = "whitelist" if allowed else "blacklist" embed = Embed( - title=f"{allowed_string} {list_type.lower()} items ({len(result)} total)", + title=f"{allow_type.title()}ed {list_type.lower()} items ({len(result)} total)", colour=Colour.blue() ) + log.trace(f"Trying to list {len(result)} items from the {list_type.lower()} {allow_type}") if result: await LinePaginator.paginate(lines, ctx, embed, max_lines=15, empty=False) -- cgit v1.2.3 From d07b1af634787f53ee381d31a4c125498af52beb Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 18 Jul 2020 15:55:56 +0200 Subject: Remove Filtering constants, use cache data. Instead of fetching the guild invite IDs from config-default.yml, we will now be using the AllowDenyList cache to check these. --- bot/cogs/filtering.py | 62 ++++++++++++++++--------------- bot/constants.py | 4 -- config-default.yml | 101 -------------------------------------------------- 3 files changed, 32 insertions(+), 135 deletions(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index bd665f424..9e35a83d1 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -22,6 +22,7 @@ from bot.utils.scheduling import Scheduler log = logging.getLogger(__name__) +# Regular expressions INVITE_RE = re.compile( r"(?:discord(?:[\.,]|dot)gg|" # Could be discord.gg/ r"discord(?:[\.,]|dot)com(?:\/|slash)invite|" # or discord.com/invite/ @@ -37,25 +38,8 @@ SPOILER_RE = re.compile(r"(\|\|.+?\|\|)", re.DOTALL) URL_RE = re.compile(r"(https?://[^\s]+)", flags=re.IGNORECASE) ZALGO_RE = re.compile(r"[\u0300-\u036F\u0489]") -WORD_WATCHLIST_PATTERNS = [ - re.compile(fr'\b{expression}\b', flags=re.IGNORECASE) for expression in Filter.word_watchlist -] -TOKEN_WATCHLIST_PATTERNS = [ - re.compile(fr'{expression}', flags=re.IGNORECASE) for expression in Filter.token_watchlist -] -WATCHLIST_PATTERNS = WORD_WATCHLIST_PATTERNS + TOKEN_WATCHLIST_PATTERNS - +# Other constants. DAYS_BETWEEN_ALERTS = 3 - - -def expand_spoilers(text: str) -> str: - """Return a string containing all interpretations of a spoilered message.""" - split_text = SPOILER_RE.split(text) - return ''.join( - split_text[0::2] + split_text[1::2] + split_text - ) - - OFFENSIVE_MSG_DELETE_TIME = timedelta(days=Filter.offensive_msg_delete_days) @@ -125,6 +109,23 @@ class Filtering(Cog): self.bot.loop.create_task(self.reschedule_offensive_msg_deletion()) + def _get_allowlist_items(self, allow: bool, list_type: str, compiled: Optional[bool] = False) -> list: + """Fetch items from the allow_deny_list_cache.""" + items = self.bot.allow_deny_list_cache[f"{list_type}.{allow}"] + + if compiled: + return [re.compile(fr'{item.get("content")}', flags=re.IGNORECASE) for item in items] + else: + return [item.get("content") for item in items] + + @staticmethod + def _expand_spoilers(text: str) -> str: + """Return a string containing all interpretations of a spoilered message.""" + split_text = SPOILER_RE.split(text) + return ''.join( + split_text[0::2] + split_text[1::2] + split_text + ) + @property def mod_log(self) -> ModLog: """Get currently loaded ModLog cog instance.""" @@ -149,11 +150,11 @@ class Filtering(Cog): delta = relativedelta(after.edited_at, before.edited_at).microseconds await self._filter_message(after, delta) - @staticmethod - def get_name_matches(name: str) -> List[re.Match]: + def get_name_matches(self, name: str) -> List[re.Match]: """Check bad words from passed string (name). Return list of matches.""" matches = [] - for pattern in WATCHLIST_PATTERNS: + watchlist_patterns = self._get_allowlist_items(False, 'word_watchlist', compiled=True) + for pattern in watchlist_patterns: if match := pattern.search(name): matches.append(match) return matches @@ -403,8 +404,7 @@ class Filtering(Cog): and not msg.author.bot # Author not a bot ) - @staticmethod - async def _has_watch_regex_match(text: str) -> Union[bool, re.Match]: + async def _has_watch_regex_match(self, text: str) -> Union[bool, re.Match]: """ Return True if `text` matches any regex from `word_watchlist` or `token_watchlist` configs. @@ -412,26 +412,27 @@ class Filtering(Cog): matched as-is. Spoilers are expanded, if any, and URLs are ignored. """ if SPOILER_RE.search(text): - text = expand_spoilers(text) + text = self._expand_spoilers(text) # Make sure it's not a URL if URL_RE.search(text): return False - for pattern in WATCHLIST_PATTERNS: + watchlist_patterns = self._get_allowlist_items(False, 'word_watchlist', compiled=True) + for pattern in watchlist_patterns: match = pattern.search(text) if match: return match - @staticmethod - async def _has_urls(text: str) -> bool: + async def _has_urls(self, text: str) -> bool: """Returns True if the text contains one of the blacklisted URLs from the config file.""" if not URL_RE.search(text): return False text = text.lower() + domain_blacklist = self._get_allowlist_items(False, "domain_name") - for url in Filter.domain_blacklist: + for url in domain_blacklist: if url.lower() in text: return True @@ -476,9 +477,10 @@ class Filtering(Cog): # between invalid and expired invites return True - guild_id = int(guild.get("id")) + guild_id = guild.get("id") + guild_invite_whitelist = self._get_allowlist_items(True, "guild_invite_id") - if guild_id not in Filter.guild_invite_whitelist: + if guild_id not in guild_invite_whitelist: guild_icon_hash = guild["icon"] guild_icon = ( "https://cdn.discordapp.com/icons/" diff --git a/bot/constants.py b/bot/constants.py index 778bc093c..f5245ca50 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -227,10 +227,6 @@ class Filter(metaclass=YAMLGetter): ping_everyone: bool offensive_msg_delete_days: int - guild_invite_whitelist: List[int] - domain_blacklist: List[str] - word_watchlist: List[str] - token_watchlist: List[str] channel_whitelist: List[int] role_whitelist: List[int] diff --git a/config-default.yml b/config-default.yml index f2eb17b89..81c8c40d5 100644 --- a/config-default.yml +++ b/config-default.yml @@ -272,107 +272,6 @@ filter: ping_everyone: true offensive_msg_delete_days: 7 # How many days before deleting an offensive message? - guild_invite_whitelist: - - 280033776820813825 # Functional Programming - - 267624335836053506 # Python Discord - - 440186186024222721 # Python Discord: Emojis 1 - - 578587418123304970 # Python Discord: Emojis 2 - - 273944235143593984 # STEM - - 348658686962696195 # RLBot - - 531221516914917387 # Pallets - - 249111029668249601 # Gentoo - - 327254708534116352 # Adafruit - - 544525886180032552 # kennethreitz.org - - 590806733924859943 # Discord Hack Week - - 423249981340778496 # Kivy - - 197038439483310086 # Discord Testers - - 286633898581164032 # Ren'Py - - 349505959032389632 # PyGame - - 438622377094414346 # Pyglet - - 524691714909274162 # Panda3D - - 336642139381301249 # discord.py - - 405403391410438165 # Sentdex - - 172018499005317120 # The Coding Den - - 666560367173828639 # PyWeek - - 702724176489873509 # Microsoft Python - - 150662382874525696 # Microsoft Community - - 81384788765712384 # Discord API - - 613425648685547541 # Discord Developers - - 185590609631903755 # Blender Hub - - 420324994703163402 # /r/FlutterDev - - 488751051629920277 # Python Atlanta - - 143867839282020352 # C# - - 159039020565790721 # Django - - 238666723824238602 # Programming Discussions - - 433980600391696384 # JetBrains Community - - 204621105720328193 # Raspberry Pi - - 244230771232079873 # Programmers Hangout - - 239433591950540801 # SpeakJS - - 174075418410876928 # DevCord - - 489222168727519232 # Unity - - 494558898880118785 # Programmer Humor - - domain_blacklist: - - pornhub.com - - liveleak.com - - grabify.link - - bmwforum.co - - leancoding.co - - spottyfly.com - - stopify.co - - yoütu.be - - discörd.com - - minecräft.com - - freegiftcards.co - - disçordapp.com - - fortnight.space - - fortnitechat.site - - joinmy.site - - curiouscat.club - - catsnthings.fun - - yourtube.site - - youtubeshort.watch - - catsnthing.com - - youtubeshort.pro - - canadianlumberjacks.online - - poweredbydialup.club - - poweredbydialup.online - - poweredbysecurity.org - - poweredbysecurity.online - - ssteam.site - - steamwalletgift.com - - discord.gift - - lmgtfy.com - - word_watchlist: - - goo+ks* - - ky+s+ - - ki+ke+s* - - beaner+s? - - coo+ns* - - nig+lets* - - slant-eyes* - - towe?l-?head+s* - - chi*n+k+s* - - spick*s* - - kill* +(?:yo)?urself+ - - jew+s* - - suicide - - rape - - (re+)tar+(d+|t+)(ed)? - - ta+r+d+ - - cunts* - - trann*y - - shemale - - token_watchlist: - - fa+g+s* - - 卐 - - 卍 - - cuck(?!oo+) - - nigg+(?:e*r+|a+h*?|u+h+)s? - - fag+o+t+s* - # Censor doesn't apply to these channel_whitelist: - *ADMINS -- cgit v1.2.3 From 1c569f2f38fe18d6210deec001046cf9ee68ea53 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 18 Jul 2020 16:54:01 +0200 Subject: Remove AntiMalWare constants, use cache data. Also updates the tests for this cog. --- bot/bot.py | 2 +- bot/cogs/antimalware.py | 24 ++++++++++++++---------- bot/constants.py | 6 ------ config-default.yml | 29 ----------------------------- tests/bot/cogs/test_antimalware.py | 24 +++++++++++++++--------- 5 files changed, 30 insertions(+), 55 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 6c02e72a7..962c8dd93 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -34,6 +34,7 @@ class Bot(commands.Bot): self.redis_ready = asyncio.Event() self.redis_closed = False self.api_client = api.APIClient(loop=self.loop) + self.allow_deny_list_cache = {} self._connector = None self._resolver = None @@ -52,7 +53,6 @@ class Bot(commands.Bot): async def _cache_allow_deny_list_data(self) -> None: """Cache all the data in the AllowDenyList on the site.""" full_cache = await self.api_client.get('bot/allow_deny_lists') - self.allow_deny_list_cache = {} for item in full_cache: type_ = item.get("type") diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index ea257442e..38ff1133d 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -6,7 +6,7 @@ from discord import Embed, Message, NotFound from discord.ext.commands import Cog from bot.bot import Bot -from bot.constants import AntiMalware as AntiMalwareConfig, Channels, STAFF_ROLES, URLs +from bot.constants import Channels, STAFF_ROLES, URLs log = logging.getLogger(__name__) @@ -27,7 +27,7 @@ TXT_EMBED_DESCRIPTION = ( DISALLOWED_EMBED_DESCRIPTION = ( "It looks like you tried to attach file type(s) that we do not allow ({blocked_extensions_str}). " - f"We currently allow the following file types: **{', '.join(AntiMalwareConfig.whitelist)}**.\n\n" + "We currently allow the following file types: **{joined_whitelist}**.\n\n" "Feel free to ask in {meta_channel_mention} if you think this is a mistake." ) @@ -38,6 +38,16 @@ class AntiMalware(Cog): def __init__(self, bot: Bot): self.bot = bot + def _get_whitelisted_file_formats(self) -> list: + """Get the file formats currently on the whitelist.""" + return [item.get('content') for item in self.bot.allow_deny_list_cache['file_format.True']] + + def _get_disallowed_extensions(self, message: Message) -> t.Iterable[str]: + """Get an iterable containing all the disallowed extensions of attachments.""" + file_extensions = {splitext(attachment.filename.lower())[1] for attachment in message.attachments} + extensions_blocked = file_extensions - set(self._get_whitelisted_file_formats()) + return extensions_blocked + @Cog.listener() async def on_message(self, message: Message) -> None: """Identify messages with prohibited attachments.""" @@ -51,7 +61,7 @@ class AntiMalware(Cog): return embed = Embed() - extensions_blocked = self.get_disallowed_extensions(message) + extensions_blocked = self._get_disallowed_extensions(message) blocked_extensions_str = ', '.join(extensions_blocked) if ".py" in extensions_blocked: # Short-circuit on *.py files to provide a pastebin link @@ -63,6 +73,7 @@ class AntiMalware(Cog): elif extensions_blocked: meta_channel = self.bot.get_channel(Channels.meta) embed.description = DISALLOWED_EMBED_DESCRIPTION.format( + joined_whitelist=', '.join(self._get_whitelisted_file_formats()), blocked_extensions_str=blocked_extensions_str, meta_channel_mention=meta_channel.mention, ) @@ -81,13 +92,6 @@ class AntiMalware(Cog): except NotFound: log.info(f"Tried to delete message `{message.id}`, but message could not be found.") - @classmethod - def get_disallowed_extensions(cls, message: Message) -> t.Iterable[str]: - """Get an iterable containing all the disallowed extensions of attachments.""" - file_extensions = {splitext(attachment.filename.lower())[1] for attachment in message.attachments} - extensions_blocked = file_extensions - set(AntiMalwareConfig.whitelist) - return extensions_blocked - def setup(bot: Bot) -> None: """Load the AntiMalware cog.""" diff --git a/bot/constants.py b/bot/constants.py index f5245ca50..857e6c4f0 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -527,12 +527,6 @@ class AntiSpam(metaclass=YAMLGetter): rules: Dict[str, Dict[str, int]] -class AntiMalware(metaclass=YAMLGetter): - section = "anti_malware" - - whitelist: list - - class BigBrother(metaclass=YAMLGetter): section = 'big_brother' diff --git a/config-default.yml b/config-default.yml index 81c8c40d5..503cc2b52 100644 --- a/config-default.yml +++ b/config-default.yml @@ -386,35 +386,6 @@ anti_spam: max: 3 -anti_malware: - whitelist: - - '.3gp' - - '.3g2' - - '.avi' - - '.bmp' - - '.gif' - - '.h264' - - '.jpg' - - '.jpeg' - - '.m4v' - - '.mkv' - - '.mov' - - '.mp4' - - '.mpeg' - - '.mpg' - - '.png' - - '.tiff' - - '.wmv' - - '.svg' - - '.psd' # Photoshop - - '.ai' # Illustrator - - '.aep' # After Effects - - '.xcf' # GIMP - - '.mp3' - - '.wav' - - '.ogg' - - reddit: subreddits: - 'r/Python' diff --git a/tests/bot/cogs/test_antimalware.py b/tests/bot/cogs/test_antimalware.py index f219fc1ba..1e010d2ce 100644 --- a/tests/bot/cogs/test_antimalware.py +++ b/tests/bot/cogs/test_antimalware.py @@ -1,28 +1,33 @@ import unittest -from unittest.mock import AsyncMock, Mock, patch +from unittest.mock import AsyncMock, Mock from discord import NotFound from bot.cogs import antimalware -from bot.constants import AntiMalware as AntiMalwareConfig, Channels, STAFF_ROLES +from bot.constants import Channels, STAFF_ROLES from tests.helpers import MockAttachment, MockBot, MockMessage, MockRole -MODULE = "bot.cogs.antimalware" - -@patch(f"{MODULE}.AntiMalwareConfig.whitelist", new=[".first", ".second", ".third"]) class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase): """Test the AntiMalware cog.""" def setUp(self): """Sets up fresh objects for each test.""" self.bot = MockBot() + self.bot.allow_deny_list_cache = { + "file_format.True": [ + {"content": ".first"}, + {"content": ".second"}, + {"content": ".third"} + ] + } self.cog = antimalware.AntiMalware(self.bot) self.message = MockMessage() + self.whitelist = [".first", ".second", ".third"] async def test_message_with_allowed_attachment(self): """Messages with allowed extensions should not be deleted""" - attachment = MockAttachment(filename=f"python{AntiMalwareConfig.whitelist[0]}") + attachment = MockAttachment(filename="python.first") self.message.attachments = [attachment] await self.cog.on_message(self.message) @@ -93,7 +98,7 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase): self.assertEqual(embed.description, antimalware.TXT_EMBED_DESCRIPTION.format.return_value) antimalware.TXT_EMBED_DESCRIPTION.format.assert_called_with(cmd_channel_mention=cmd_channel.mention) - async def test_other_disallowed_extention_embed_description(self): + async def test_other_disallowed_extension_embed_description(self): """Test the description for a non .py/.txt disallowed extension.""" attachment = MockAttachment(filename="python.disallowed") self.message.attachments = [attachment] @@ -109,6 +114,7 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase): self.assertEqual(embed.description, antimalware.DISALLOWED_EMBED_DESCRIPTION.format.return_value) antimalware.DISALLOWED_EMBED_DESCRIPTION.format.assert_called_with( + joined_whitelist=", ".join(self.whitelist), blocked_extensions_str=".disallowed", meta_channel_mention=meta_channel.mention ) @@ -135,7 +141,7 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase): """The return value should include all non-whitelisted extensions.""" test_values = ( ([], []), - (AntiMalwareConfig.whitelist, []), + (self.whitelist, []), ([".first"], []), ([".first", ".disallowed"], [".disallowed"]), ([".disallowed"], [".disallowed"]), @@ -145,7 +151,7 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase): for extensions, expected_disallowed_extensions in test_values: with self.subTest(extensions=extensions, expected_disallowed_extensions=expected_disallowed_extensions): self.message.attachments = [MockAttachment(filename=f"filename{extension}") for extension in extensions] - disallowed_extensions = self.cog.get_disallowed_extensions(self.message) + disallowed_extensions = self.cog._get_disallowed_extensions(self.message) self.assertCountEqual(disallowed_extensions, expected_disallowed_extensions) -- cgit v1.2.3 From cdf4e2595b8321158bcb514936b7c2a23a88cd0d Mon Sep 17 00:00:00 2001 From: Kieran Siek Date: Sun, 19 Jul 2020 13:10:14 +0800 Subject: Add whitespace to improve readability Co-authored-by: Mark --- bot/cogs/reminders.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index 9eddd283b..5f76164cd 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -121,6 +121,7 @@ class Reminders(Cog): Conditions: - Role reminders are Mods+ - Reminders for other users are Helpers+ + If mentions aren't allowed, also return the type of mention(s) disallowed. """ if without_role_check(ctx, *STAFF_ROLES): -- cgit v1.2.3 From 1d9efd32278688adebd539b15c4d16d4dd88e74c Mon Sep 17 00:00:00 2001 From: kosayoda Date: Sun, 19 Jul 2020 13:17:45 +0800 Subject: Namespace Member and Role to avoid extra imports --- bot/cogs/reminders.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index 9eddd283b..ae387f09a 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -9,7 +9,6 @@ from operator import itemgetter import discord from dateutil.parser import isoparse from dateutil.relativedelta import relativedelta -from discord import Member, Role from discord.ext.commands import Cog, Context, Greedy, group from bot.bot import Bot @@ -25,7 +24,7 @@ log = logging.getLogger(__name__) WHITELISTED_CHANNELS = Guild.reminder_whitelist MAXIMUM_REMINDERS = 5 -Mentionable = t.Union[Member, Role] +Mentionable = t.Union[discord.Member, discord.Role] class Reminders(Cog): @@ -126,7 +125,7 @@ class Reminders(Cog): if without_role_check(ctx, *STAFF_ROLES): return False, "members/roles" elif without_role_check(ctx, *MODERATION_ROLES): - return all(isinstance(mention, Member) for mention in mentions), "roles" + return all(isinstance(mention, discord.Member) for mention in mentions), "roles" else: return True, "" -- cgit v1.2.3 From ced9117e848a9eb1e003576d4f355ba7aa220cd8 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Sun, 19 Jul 2020 13:31:00 +0800 Subject: Extract `send_denial` to a utility function --- bot/cogs/reminders.py | 21 ++++++--------------- bot/utils/messages.py | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index ae387f09a..f36b67f5a 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -12,10 +12,11 @@ from dateutil.relativedelta import relativedelta from discord.ext.commands import Cog, Context, Greedy, group from bot.bot import Bot -from bot.constants import Guild, Icons, MODERATION_ROLES, NEGATIVE_REPLIES, POSITIVE_REPLIES, STAFF_ROLES +from bot.constants import Guild, Icons, MODERATION_ROLES, POSITIVE_REPLIES, STAFF_ROLES from bot.converters import Duration from bot.pagination import LinePaginator from bot.utils.checks import without_role_check +from bot.utils.messages import send_denial from bot.utils.scheduling import Scheduler from bot.utils.time import humanize_delta @@ -102,16 +103,6 @@ class Reminders(Cog): await ctx.send(embed=embed) - @staticmethod - async def _send_denial(ctx: Context, reason: str) -> None: - """Send an embed denying the user from creating a reminder.""" - embed = discord.Embed() - embed.colour = discord.Colour.red() - embed.title = random.choice(NEGATIVE_REPLIES) - embed.description = reason - - await ctx.send(embed=embed) - @staticmethod async def allow_mentions(ctx: Context, mentions: t.List[Mentionable]) -> t.Tuple[bool, str]: """ @@ -220,7 +211,7 @@ class Reminders(Cog): # If they don't have permission to set a reminder in this channel if ctx.channel.id not in WHITELISTED_CHANNELS: - return await self._send_denial(ctx, "Sorry, you can't do that here!") + return await send_denial(ctx, "Sorry, you can't do that here!") # Get their current active reminders active_reminders = await self.bot.api_client.get( @@ -233,13 +224,13 @@ class Reminders(Cog): # Let's limit this, so we don't get 10 000 # reminders from kip or something like that :P if len(active_reminders) > MAXIMUM_REMINDERS: - return await self._send_denial(ctx, "You have too many active reminders!") + return await send_denial(ctx, "You have too many active reminders!") # Filter mentions to see if the user can mention members/roles if mentions: mentions_allowed, disallowed_mentions = await self.allow_mentions(ctx, mentions) if not mentions_allowed: - return await self._send_denial( + return await send_denial( ctx, f"You can't mention other {disallowed_mentions} in your reminder!" ) @@ -389,7 +380,7 @@ class Reminders(Cog): # Filter mentions to see if the user can mention members/roles mentions_allowed, disallowed_mentions = await self.allow_mentions(ctx, mentions) if not mentions_allowed: - return await self._send_denial( + return await send_denial( ctx, f"You can't mention other {disallowed_mentions} in your reminder!" ) diff --git a/bot/utils/messages.py b/bot/utils/messages.py index a40a12e98..670289941 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -1,15 +1,17 @@ import asyncio import contextlib import logging +import random import re from io import BytesIO from typing import List, Optional, Sequence, Union -from discord import Client, Embed, File, Member, Message, Reaction, TextChannel, Webhook +from discord import Client, Colour, Embed, File, Member, Message, Reaction, TextChannel, Webhook from discord.abc import Snowflake from discord.errors import HTTPException +from discord.ext.commands import Context -from bot.constants import Emojis +from bot.constants import Emojis, NEGATIVE_REPLIES log = logging.getLogger(__name__) @@ -132,3 +134,13 @@ def sub_clyde(username: Optional[str]) -> Optional[str]: return re.sub(r"(clyd)(e)", replace_e, username, flags=re.I) else: return username # Empty string or None + + +async def send_denial(ctx: Context, reason: str) -> None: + """Send an embed denying the user with the given reason.""" + embed = Embed() + embed.colour = Colour.red() + embed.title = random.choice(NEGATIVE_REPLIES) + embed.description = reason + + await ctx.send(embed=embed) -- cgit v1.2.3 From 010373673700f821b5860bd749f40fdf8d59d134 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Sun, 19 Jul 2020 13:39:24 +0800 Subject: Fix incorrect typehint and shorten method name --- bot/cogs/reminders.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index f36b67f5a..d36494a69 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -120,7 +120,7 @@ class Reminders(Cog): else: return True, "" - def get_mentionables_from_ids(self, mention_ids: t.List[str]) -> t.Iterator[Mentionable]: + def get_mentionables(self, mention_ids: t.List[int]) -> t.Iterator[Mentionable]: """Converts Role and Member ids to their corresponding objects if possible.""" guild = self.bot.get_guild(Guild.id) for mention_id in mention_ids: @@ -182,7 +182,7 @@ class Reminders(Cog): ) additional_mentions = ' '.join( - mentionable.mention for mentionable in self.get_mentionables_from_ids(reminder["mentions"]) + mentionable.mention for mentionable in self.get_mentionables(reminder["mentions"]) ) await channel.send( @@ -295,7 +295,7 @@ class Reminders(Cog): mentions = ", ".join( # Both Role and User objects have the `name` attribute - mention.name for mention in self.get_mentionables_from_ids(mentions) + mention.name for mention in self.get_mentionables(mentions) ) mention_string = f"\n**Mentions:** {mentions}" if mentions else "" -- cgit v1.2.3 From 0d51d357a5a9f192c8ed71d40726838b7fb5136e Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 19 Jul 2020 10:41:49 +0200 Subject: Fix an absolutely terrible comment. --- bot/cogs/filtering.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 9e35a83d1..d94c19471 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -456,7 +456,7 @@ class Filtering(Cog): Attempts to catch some of common ways to try to cheat the system. """ - # Remove backslashes to prevent escape character around fuckery like + # Remove backslashes to prevent escape character aroundfuckery like # discord\.gg/gdudes-pony-farm text = text.replace("\\", "") -- cgit v1.2.3 From da260365d3a6d9b92a630b16e32397b52d64e6c3 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 19 Jul 2020 12:19:34 +0200 Subject: Include the guild ID in mod-log embed. This gives easier access to the Guild ID in the place where you're most likely to want to use the whitelist command. --- bot/cogs/filtering.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index d94c19471..4d51bba2e 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -111,7 +111,7 @@ class Filtering(Cog): def _get_allowlist_items(self, allow: bool, list_type: str, compiled: Optional[bool] = False) -> list: """Fetch items from the allow_deny_list_cache.""" - items = self.bot.allow_deny_list_cache[f"{list_type}.{allow}"] + items = self.bot.allow_deny_list_cache.get(f"{list_type.upper()}.{allow}", []) if compiled: return [re.compile(fr'{item.get("content")}', flags=re.IGNORECASE) for item in items] @@ -371,14 +371,14 @@ class Filtering(Cog): # They have no data so additional embeds can't be created for them. if name == "filter_invites" and match is not True: additional_embeds = [] - for invite, data in match.items(): + for _, data in match.items(): embed = discord.Embed(description=( f"**Members:**\n{data['members']}\n" f"**Active:**\n{data['active']}" )) embed.set_author(name=data["name"]) embed.set_thumbnail(url=data["icon"]) - embed.set_footer(text=f"Guild Invite Code: {invite}") + embed.set_footer(text=f"Guild ID: {data['id']}") additional_embeds.append(embed) additional_embeds_msg = "For the following guild(s):" @@ -489,6 +489,7 @@ class Filtering(Cog): invite_data[invite] = { "name": guild["name"], + "id": guild['id'], "icon": guild_icon, "members": response["approximate_member_count"], "active": response["approximate_presence_count"] -- cgit v1.2.3 From 064130f7838647ab7bb63824446d93ba50833126 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 19 Jul 2020 12:31:04 +0200 Subject: Support the new AllowDenyList field, 'comment'. --- bot/bot.py | 1 + bot/cogs/allow_deny_lists.py | 55 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 962c8dd93..d834c151b 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -59,6 +59,7 @@ class Bot(commands.Bot): allowed = item.get("allowed") metadata = { "content": item.get("content"), + "comment": item.get("comment"), "id": item.get("id"), "created_at": item.get("created_at"), "updated_at": item.get("updated_at"), diff --git a/bot/cogs/allow_deny_lists.py b/bot/cogs/allow_deny_lists.py index 6558990a7..8b3c892f5 100644 --- a/bot/cogs/allow_deny_lists.py +++ b/bot/cogs/allow_deny_lists.py @@ -1,4 +1,5 @@ import logging +from typing import Optional from discord import Colour, Embed from discord.ext.commands import BadArgument, Cog, Context, group @@ -19,17 +20,26 @@ class AllowDenyLists(Cog): def __init__(self, bot: Bot) -> None: self.bot = bot - async def _add_data(self, ctx: Context, allowed: bool, list_type: ValidAllowDenyListType, content: str) -> None: + async def _add_data( + self, + ctx: Context, + allowed: bool, + list_type: ValidAllowDenyListType, + content: str, + comment: Optional[str] = None, + ) -> None: """Add an item to an allow or denylist.""" + allow_type = "whitelist" if allowed else "blacklist" + + # Try to add the item to the database + log.trace(f"Trying to add the {content} item to the {list_type} {allow_type}") payload = { 'allowed': allowed, 'type': list_type, 'content': content, + 'comment': comment, } - allow_type = "whitelist" if allowed else "blacklist" - # Try to add the item to the database - log.trace(f"Trying to add the {content} item to the {list_type} {allow_type}") try: item = await self.bot.api_client.post( "bot/allow_deny_lists", @@ -55,6 +65,7 @@ class AllowDenyLists(Cog): allowed = item.get("allowed") metadata = { "content": item.get("content"), + "comment": item.get("comment"), "id": item.get("id"), "created_at": item.get("created_at"), "updated_at": item.get("updated_at"), @@ -83,9 +94,21 @@ class AllowDenyLists(Cog): async def _list_all_data(self, ctx: Context, allowed: bool, list_type: ValidAllowDenyListType) -> None: """Paginate and display all items in an allow or denylist.""" - result = self.bot.allow_deny_list_cache.get(f"{list_type}.{allowed}", []) - lines = sorted(f"• {item.get('content')}" for item in result) allow_type = "whitelist" if allowed else "blacklist" + result = self.bot.allow_deny_list_cache.get(f"{list_type}.{allowed}", []) + + # Build a list of lines we want to show in the paginator + lines = [] + for item in result: + line = f"• {item.get('content')}" + + if item.get("comment"): + line += f" ({item.get('comment')})" + + lines.append(line) + lines = sorted(lines) + + # Build the embed embed = Embed( title=f"{allow_type.title()}ed {list_type.lower()} items ({len(result)} total)", colour=Colour.blue() @@ -111,14 +134,26 @@ class AllowDenyLists(Cog): await ctx.send_help(ctx.command) @whitelist.command(name="add", aliases=("a", "set")) - async def allow_add(self, ctx: Context, list_type: ValidAllowDenyListType, content: str) -> None: + async def allow_add( + self, + ctx: Context, + list_type: ValidAllowDenyListType, + content: str, + comment: Optional[str] = None, + ) -> None: """Add an item to the specified allowlist.""" - await self._add_data(ctx, True, list_type, content) + await self._add_data(ctx, True, list_type, content, comment) @blacklist.command(name="add", aliases=("a", "set")) - async def deny_add(self, ctx: Context, list_type: ValidAllowDenyListType, content: str) -> None: + async def deny_add( + self, + ctx: Context, + list_type: ValidAllowDenyListType, + content: str, + comment: Optional[str] = None, + ) -> None: """Add an item to the specified denylist.""" - await self._add_data(ctx, False, list_type, content) + await self._add_data(ctx, False, list_type, content, comment) @whitelist.command(name="remove", aliases=("delete", "rm",)) async def allow_delete(self, ctx: Context, list_type: ValidAllowDenyListType, content: str) -> None: -- cgit v1.2.3 From 4ef9770ed26b46bc5916f4147acce1cf57f4f9af Mon Sep 17 00:00:00 2001 From: kosayoda Date: Sun, 19 Jul 2020 21:13:08 +0800 Subject: Rename method to improve readability --- bot/cogs/reminders.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index d36494a69..6755993a0 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -104,7 +104,7 @@ class Reminders(Cog): await ctx.send(embed=embed) @staticmethod - async def allow_mentions(ctx: Context, mentions: t.List[Mentionable]) -> t.Tuple[bool, str]: + async def check_mentions(ctx: Context, mentions: t.List[Mentionable]) -> t.Tuple[bool, str]: """ Returns whether or not the list of mentions is allowed. @@ -228,7 +228,7 @@ class Reminders(Cog): # Filter mentions to see if the user can mention members/roles if mentions: - mentions_allowed, disallowed_mentions = await self.allow_mentions(ctx, mentions) + mentions_allowed, disallowed_mentions = await self.check_mentions(ctx, mentions) if not mentions_allowed: return await send_denial( ctx, f"You can't mention other {disallowed_mentions} in your reminder!" @@ -378,7 +378,7 @@ class Reminders(Cog): async def edit_reminder_mentions(self, ctx: Context, id_: int, mentions: Greedy[Mentionable]) -> None: """Edit one of your reminder's mentions.""" # Filter mentions to see if the user can mention members/roles - mentions_allowed, disallowed_mentions = await self.allow_mentions(ctx, mentions) + mentions_allowed, disallowed_mentions = await self.check_mentions(ctx, mentions) if not mentions_allowed: return await send_denial( ctx, f"You can't mention other {disallowed_mentions} in your reminder!" -- cgit v1.2.3 From ff7707f8306c26ef9d14944e2826671bbcfcf113 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Sun, 19 Jul 2020 22:17:48 +0800 Subject: Refactor reminder edits to reduce code duplication The reminder expiration returnedfrom the API call is also now parsed again even when the edit is to the duration since it does not matter and trying to keep it DRY while still doing that check is a pain. --- bot/cogs/reminders.py | 65 ++++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index 6755993a0..d99979ace 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -148,6 +148,19 @@ class Reminders(Cog): # Now we can remove it from the schedule list self.scheduler.cancel(reminder_id) + async def _edit_reminder(self, reminder_id: int, payload: dict) -> dict: + """ + Edits a reminder in the database given the ID and payload. + + Returns the edited reminder. + """ + # Send the request to update the reminder in the database + reminder = await self.bot.api_client.patch( + 'bot/reminders/' + str(reminder_id), + json=payload + ) + return reminder + async def _reschedule_reminder(self, reminder: dict) -> None: """Reschedule a reminder object.""" log.trace(f"Cancelling old task #{reminder['id']}") @@ -300,7 +313,7 @@ class Reminders(Cog): mention_string = f"\n**Mentions:** {mentions}" if mentions else "" text = textwrap.dedent(f""" - **Reminder #{id_}:** *expires in {time}* (ID: {id_}) {mention_string} + **Reminder #{id_}:** *expires in {time}* (ID: {id_}){mention_string} {content} """).strip() @@ -333,46 +346,16 @@ class Reminders(Cog): @edit_reminder_group.command(name="duration", aliases=("time",)) async def edit_reminder_duration(self, ctx: Context, id_: int, expiration: Duration) -> None: """ - Edit one of your reminder's expiration. + Edit one of your reminder's expiration. Expiration is parsed per: http://strftime.org/ """ - # Send the request to update the reminder in the database - reminder = await self.bot.api_client.patch( - 'bot/reminders/' + str(id_), - json={'expiration': expiration.isoformat()} - ) - - # Send a confirmation message to the channel - await self._send_confirmation( - ctx, - on_success="That reminder has been edited successfully!", - reminder_id=id_, - delivery_dt=expiration, - ) - - await self._reschedule_reminder(reminder) + await self.edit_reminder(ctx, id_, {'expiration': expiration.isoformat()}) @edit_reminder_group.command(name="content", aliases=("reason",)) async def edit_reminder_content(self, ctx: Context, id_: int, *, content: str) -> None: """Edit one of your reminder's content.""" - # Send the request to update the reminder in the database - reminder = await self.bot.api_client.patch( - 'bot/reminders/' + str(id_), - json={'content': content} - ) - - # Parse the reminder expiration back into a datetime for the confirmation message - expiration = isoparse(reminder['expiration']).replace(tzinfo=None) - - # Send a confirmation message to the channel - await self._send_confirmation( - ctx, - on_success="That reminder has been edited successfully!", - reminder_id=id_, - delivery_dt=expiration, - ) - await self._reschedule_reminder(reminder) + await self.edit_reminder(ctx, id_, {"content": content}) @edit_reminder_group.command(name="mentions", aliases=("pings",)) async def edit_reminder_mentions(self, ctx: Context, id_: int, mentions: Greedy[Mentionable]) -> None: @@ -385,13 +368,15 @@ class Reminders(Cog): ) mention_ids = [mention.id for mention in mentions] - reminder = await self.bot.api_client.patch( - 'bot/reminders/' + str(id_), - json={"mentions": mention_ids} - ) - # Parse the reminder expiration back into a datetime for the confirmation message - expiration = isoparse(reminder['expiration']).replace(tzinfo=None) + await self.edit_reminder(ctx, id_, {"mentions": mention_ids}) + + async def edit_reminder(self, ctx: Context, id_: int, payload: dict) -> None: + """Edits a reminder with the given payload, then sends a confirmation message.""" + reminder = await self._edit_reminder(id_, payload) + + # Parse the reminder expiration back into a datetime + expiration = isoparse(reminder["expiration"]).replace(tzinfo=None) # Send a confirmation message to the channel await self._send_confirmation( -- cgit v1.2.3 From c491d054daaa9f3e2ff3ecffba626b9991f93005 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Sun, 19 Jul 2020 22:37:08 +0800 Subject: Move mentions validation to another method --- bot/cogs/reminders.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index d99979ace..cc20897e0 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -104,7 +104,7 @@ class Reminders(Cog): await ctx.send(embed=embed) @staticmethod - async def check_mentions(ctx: Context, mentions: t.List[Mentionable]) -> t.Tuple[bool, str]: + async def _check_mentions(ctx: Context, mentions: t.List[Mentionable]) -> t.Tuple[bool, str]: """ Returns whether or not the list of mentions is allowed. @@ -120,6 +120,21 @@ class Reminders(Cog): else: return True, "" + @staticmethod + async def validate_mentions(ctx: Context, mentions: t.List[Mentionable]) -> bool: + """ + Filter mentions to see if the user can mention, and sends a denial if not allowed. + + Returns whether or not the validation is successful. + """ + mentions_allowed, disallowed_mentions = await Reminders._check_mentions(ctx, mentions) + + if not mentions or mentions_allowed: + return True + else: + await send_denial(ctx, f"You can't mention other {disallowed_mentions} in your reminder!") + return False + def get_mentionables(self, mention_ids: t.List[int]) -> t.Iterator[Mentionable]: """Converts Role and Member ids to their corresponding objects if possible.""" guild = self.bot.get_guild(Guild.id) @@ -240,12 +255,8 @@ class Reminders(Cog): return await send_denial(ctx, "You have too many active reminders!") # Filter mentions to see if the user can mention members/roles - if mentions: - mentions_allowed, disallowed_mentions = await self.check_mentions(ctx, mentions) - if not mentions_allowed: - return await send_denial( - ctx, f"You can't mention other {disallowed_mentions} in your reminder!" - ) + if not await self.validate_mentions(ctx, mentions): + return mention_ids = [mention.id for mention in mentions] @@ -361,14 +372,10 @@ class Reminders(Cog): async def edit_reminder_mentions(self, ctx: Context, id_: int, mentions: Greedy[Mentionable]) -> None: """Edit one of your reminder's mentions.""" # Filter mentions to see if the user can mention members/roles - mentions_allowed, disallowed_mentions = await self.check_mentions(ctx, mentions) - if not mentions_allowed: - return await send_denial( - ctx, f"You can't mention other {disallowed_mentions} in your reminder!" - ) + if not await self.validate_mentions(ctx, mentions): + return mention_ids = [mention.id for mention in mentions] - await self.edit_reminder(ctx, id_, {"mentions": mention_ids}) async def edit_reminder(self, ctx: Context, id_: int, payload: dict) -> None: -- cgit v1.2.3 From daff90ef6ff5de8c9f08b8394979da758e484001 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Sun, 19 Jul 2020 22:39:12 +0800 Subject: Refactor commands return type --- bot/cogs/reminders.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index cc20897e0..1410bfea6 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -228,7 +228,7 @@ class Reminders(Cog): @remind_group.command(name="new", aliases=("add", "create")) async def new_reminder( self, ctx: Context, mentions: Greedy[Mentionable], expiration: Duration, *, content: str - ) -> t.Optional[discord.Message]: + ) -> None: """ Set yourself a simple reminder. @@ -239,7 +239,8 @@ class Reminders(Cog): # If they don't have permission to set a reminder in this channel if ctx.channel.id not in WHITELISTED_CHANNELS: - return await send_denial(ctx, "Sorry, you can't do that here!") + await send_denial(ctx, "Sorry, you can't do that here!") + return # Get their current active reminders active_reminders = await self.bot.api_client.get( @@ -252,7 +253,8 @@ class Reminders(Cog): # Let's limit this, so we don't get 10 000 # reminders from kip or something like that :P if len(active_reminders) > MAXIMUM_REMINDERS: - return await send_denial(ctx, "You have too many active reminders!") + await send_denial(ctx, "You have too many active reminders!") + return # Filter mentions to see if the user can mention members/roles if not await self.validate_mentions(ctx, mentions): @@ -291,7 +293,7 @@ class Reminders(Cog): self.schedule_reminder(reminder) @remind_group.command(name="list") - async def list_reminders(self, ctx: Context) -> t.Optional[discord.Message]: + async def list_reminders(self, ctx: Context) -> None: """View a paginated embed of all reminders for your user.""" # Get all the user's reminders from the database. data = await self.bot.api_client.get( @@ -337,7 +339,8 @@ class Reminders(Cog): # Remind the user that they have no reminders :^) if not lines: embed.description = "No active reminders could be found." - return await ctx.send(embed=embed) + await ctx.send(embed=embed) + return # Construct the embed and paginate it. embed.colour = discord.Colour.blurple() -- cgit v1.2.3 From d658fabaa1238eda9d26175af751e2e8b7f1fc13 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Sun, 19 Jul 2020 22:51:51 +0800 Subject: Remove duplicate mentions from reminder arguments This also accounts for the author passing themselves to mention, and therefore avoids the user from being told they're not allowed to mention themselves even though they could. --- bot/cogs/reminders.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index 1410bfea6..60fa70d74 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -104,7 +104,7 @@ class Reminders(Cog): await ctx.send(embed=embed) @staticmethod - async def _check_mentions(ctx: Context, mentions: t.List[Mentionable]) -> t.Tuple[bool, str]: + async def _check_mentions(ctx: Context, mentions: t.Iterable[Mentionable]) -> t.Tuple[bool, str]: """ Returns whether or not the list of mentions is allowed. @@ -121,7 +121,7 @@ class Reminders(Cog): return True, "" @staticmethod - async def validate_mentions(ctx: Context, mentions: t.List[Mentionable]) -> bool: + async def validate_mentions(ctx: Context, mentions: t.Iterable[Mentionable]) -> bool: """ Filter mentions to see if the user can mention, and sends a denial if not allowed. @@ -256,6 +256,10 @@ class Reminders(Cog): await send_denial(ctx, "You have too many active reminders!") return + # Remove duplicate mentions + mentions = set(mentions) + mentions.discard(ctx.author) + # Filter mentions to see if the user can mention members/roles if not await self.validate_mentions(ctx, mentions): return @@ -374,6 +378,10 @@ class Reminders(Cog): @edit_reminder_group.command(name="mentions", aliases=("pings",)) async def edit_reminder_mentions(self, ctx: Context, id_: int, mentions: Greedy[Mentionable]) -> None: """Edit one of your reminder's mentions.""" + # Remove duplicate mentions + mentions = set(mentions) + mentions.discard(ctx.author) + # Filter mentions to see if the user can mention members/roles if not await self.validate_mentions(ctx, mentions): return -- cgit v1.2.3 From 69e3a2e31e4c91c1932efe5a584708a3a370bb35 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Sun, 19 Jul 2020 22:54:10 +0800 Subject: Revert "Remove duplicate reminder deletion." This reverts commit 776b4379c478284803a4a526b5f14fe63d8e7c01. This is already being fixed in #835, and therefore is no longer required. --- bot/cogs/reminders.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index 60fa70d74..219c52659 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -58,7 +58,6 @@ class Reminders(Cog): if remind_at < now: late = relativedelta(now, remind_at) await self.send_reminder(reminder, late) - await self._delete_reminder(reminder["id"]) else: self.schedule_reminder(reminder) @@ -217,6 +216,7 @@ class Reminders(Cog): content=f"{user.mention} {additional_mentions}", embed=embed ) + await self._delete_reminder(reminder["id"]) @group(name="remind", aliases=("reminder", "reminders", "remindme"), invoke_without_command=True) async def remind_group( -- cgit v1.2.3 From f99d27074e8088f7ea8abe4957321490875aa249 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 19 Jul 2020 19:39:20 +0200 Subject: Validation of guild invites. We will now validate and convert any standard discord server invite to a guild ID, and automatically add the name of the server as a comment. This will ensure that the list of whitelisted guild IDs will be readable and nice. This also makes minor changes to list output aesthetics. --- bot/cogs/allow_deny_lists.py | 29 +++++++++++++++++++++++++---- bot/cogs/filtering.py | 14 ++------------ bot/converters.py | 40 +++++++++++++++++++++++++++++++++++++++- bot/utils/regex.py | 12 ++++++++++++ 4 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 bot/utils/regex.py diff --git a/bot/cogs/allow_deny_lists.py b/bot/cogs/allow_deny_lists.py index 8b3c892f5..d82d175cf 100644 --- a/bot/cogs/allow_deny_lists.py +++ b/bot/cogs/allow_deny_lists.py @@ -7,7 +7,7 @@ from discord.ext.commands import BadArgument, Cog, Context, group from bot import constants from bot.api import ResponseCodeError from bot.bot import Bot -from bot.converters import ValidAllowDenyListType +from bot.converters import ValidAllowDenyListType, ValidDiscordServerInvite from bot.pagination import LinePaginator from bot.utils.checks import with_role_check @@ -31,6 +31,24 @@ class AllowDenyLists(Cog): """Add an item to an allow or denylist.""" allow_type = "whitelist" if allowed else "blacklist" + # If this is a server invite, we gotta validate it. + if list_type == "GUILD_INVITE": + log.trace(f"{content} is a guild invite, attempting to validate.") + validator = ValidDiscordServerInvite() + guild_data = await validator.convert(ctx, content) + + # If we make it this far without raising a BadArgument, the invite is + # valid. Let's convert the content to an ID. + log.trace(f"{content} validated as server invite. Converting to ID.") + content = guild_data.get("id") + + # Unless the user has specified another comment, let's + # use the server name as the comment so that the list + # of guild IDs will be more easily readable when we + # display it. + if not comment: + comment = guild_data.get("name") + # Try to add the item to the database log.trace(f"Trying to add the {content} item to the {list_type} {allow_type}") payload = { @@ -100,17 +118,18 @@ class AllowDenyLists(Cog): # Build a list of lines we want to show in the paginator lines = [] for item in result: - line = f"• {item.get('content')}" + line = f"• `{item.get('content')}`" if item.get("comment"): - line += f" ({item.get('comment')})" + line += f" - {item.get('comment')}" lines.append(line) lines = sorted(lines) # Build the embed + list_type_plural = list_type.lower().replace("_", " ").title() + "s" embed = Embed( - title=f"{allow_type.title()}ed {list_type.lower()} items ({len(result)} total)", + title=f"{allow_type.title()}ed {list_type_plural} ({len(result)} total)", colour=Colour.blue() ) log.trace(f"Trying to list {len(result)} items from the {list_type.lower()} {allow_type}") @@ -139,6 +158,7 @@ class AllowDenyLists(Cog): ctx: Context, list_type: ValidAllowDenyListType, content: str, + *, comment: Optional[str] = None, ) -> None: """Add an item to the specified allowlist.""" @@ -150,6 +170,7 @@ class AllowDenyLists(Cog): ctx: Context, list_type: ValidAllowDenyListType, content: str, + *, comment: Optional[str] = None, ) -> None: """Add an item to the specified denylist.""" diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 4d51bba2e..3ebb47a0f 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -18,22 +18,12 @@ from bot.constants import ( Filter, Icons, URLs ) from bot.utils.redis_cache import RedisCache +from bot.utils.regex import INVITE_RE from bot.utils.scheduling import Scheduler log = logging.getLogger(__name__) # Regular expressions -INVITE_RE = re.compile( - r"(?:discord(?:[\.,]|dot)gg|" # Could be discord.gg/ - r"discord(?:[\.,]|dot)com(?:\/|slash)invite|" # or discord.com/invite/ - r"discordapp(?:[\.,]|dot)com(?:\/|slash)invite|" # or discordapp.com/invite/ - r"discord(?:[\.,]|dot)me|" # or discord.me - r"discord(?:[\.,]|dot)io" # or discord.io. - r")(?:[\/]|slash)" # / or 'slash' - r"([a-zA-Z0-9]+)", # the invite code itself - flags=re.IGNORECASE -) - SPOILER_RE = re.compile(r"(\|\|.+?\|\|)", re.DOTALL) URL_RE = re.compile(r"(https?://[^\s]+)", flags=re.IGNORECASE) ZALGO_RE = re.compile(r"[\u0300-\u036F\u0489]") @@ -478,7 +468,7 @@ class Filtering(Cog): return True guild_id = guild.get("id") - guild_invite_whitelist = self._get_allowlist_items(True, "guild_invite_id") + guild_invite_whitelist = self._get_allowlist_items(True, "guild_invite") if guild_id not in guild_invite_whitelist: guild_icon_hash = guild["icon"] diff --git a/bot/converters.py b/bot/converters.py index edac67be2..7e21c1542 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -9,8 +9,10 @@ import dateutil.tz import discord from aiohttp import ClientConnectorError, ContentTypeError from dateutil.relativedelta import relativedelta -from discord.ext.commands import BadArgument, Context, Converter, UserConverter +from discord.ext.commands import BadArgument, Context, Converter, IDConverter, UserConverter +from bot.constants import URLs +from bot.utils.regex import INVITE_RE log = logging.getLogger(__name__) @@ -34,6 +36,42 @@ def allowed_strings(*values, preserve_case: bool = False) -> t.Callable[[str], s return converter +class ValidDiscordServerInvite(Converter): + """ + A converter that validates whether a given string is a valid Discord server invite. + + Raises 'BadArgument' if: + - The string is not a valid Discord server invite. + - The string is valid, but is an invite for a group DM. + - The string is valid, but is expired. + + Returns a (partial) guild object if: + - The string is a valid vanity + - The string is a full invite URI + - The string contains the invite code (the stuff after discord.gg/) + + See the Discord API docs for documentation on the guild object: + https://discord.com/developers/docs/resources/guild#guild-object + """ + + async def convert(self, ctx: Context, server_invite: str) -> dict: + """Check whether the string is a valid Discord server invite.""" + invite_code = INVITE_RE.match(server_invite) + if invite_code: + response = await ctx.bot.http_session.get( + f"{URLs.discord_invite_api}/{invite_code[1]}" + ) + if response.status != 404: + invite_data = await response.json() + return invite_data.get("guild") + + id_converter = IDConverter() + if id_converter._get_id_match(server_invite): + raise BadArgument("Guild IDs are not supported, only invites.") + + raise BadArgument("This does not appear to be a valid Discord server invite.") + + class ValidAllowDenyListType(Converter): """ A converter that checks whether the given string is a valid AllowDenyList type. diff --git a/bot/utils/regex.py b/bot/utils/regex.py new file mode 100644 index 000000000..d194f93cb --- /dev/null +++ b/bot/utils/regex.py @@ -0,0 +1,12 @@ +import re + +INVITE_RE = re.compile( + r"(?:discord(?:[\.,]|dot)gg|" # Could be discord.gg/ + r"discord(?:[\.,]|dot)com(?:\/|slash)invite|" # or discord.com/invite/ + r"discordapp(?:[\.,]|dot)com(?:\/|slash)invite|" # or discordapp.com/invite/ + r"discord(?:[\.,]|dot)me|" # or discord.me + r"discord(?:[\.,]|dot)io" # or discord.io. + r")(?:[\/]|slash)" # / or 'slash' + r"([a-zA-Z0-9]+)", # the invite code itself + flags=re.IGNORECASE +) -- cgit v1.2.3 From f771222df165d90aff0f2d9d44bd9ba86b265574 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 19 Jul 2020 19:57:40 +0200 Subject: Validation of guild invites for delete. We want to support deletion of both IDs and guild invites, so we need a bit of special handling for that. --- bot/cogs/allow_deny_lists.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/bot/cogs/allow_deny_lists.py b/bot/cogs/allow_deny_lists.py index d82d175cf..71a032ea5 100644 --- a/bot/cogs/allow_deny_lists.py +++ b/bot/cogs/allow_deny_lists.py @@ -2,7 +2,7 @@ import logging from typing import Optional from discord import Colour, Embed -from discord.ext.commands import BadArgument, Cog, Context, group +from discord.ext.commands import BadArgument, Cog, Context, IDConverter, group from bot import constants from bot.api import ResponseCodeError @@ -95,9 +95,21 @@ class AllowDenyLists(Cog): """Remove an item from an allow or denylist.""" item = None allow_type = "whitelist" if allowed else "blacklist" + id_converter = IDConverter() - log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}") + # If this is a server invite, we need to convert it. + if list_type == "GUILD_INVITE" and not id_converter._get_id_match(content): + log.trace(f"{content} is a guild invite, attempting to validate.") + validator = ValidDiscordServerInvite() + guild_data = await validator.convert(ctx, content) + + # If we make it this far without raising a BadArgument, the invite is + # valid. Let's convert the content to an ID. + log.trace(f"{content} validated as server invite. Converting to ID.") + content = guild_data.get("id") + # Find the content and delete it. + log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}") for allow_list in self.bot.allow_deny_list_cache.get(f"{list_type}.{allowed}", []): if content == allow_list.get("content"): item = allow_list -- cgit v1.2.3 From 73b12fa63877a26bfe324e968f00337969f1f6cf Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sun, 19 Jul 2020 20:44:51 +0200 Subject: Implement new guild invite filtering logic. We now filter guild invites the following way: - Whitelisted invites are always permitted. - Blacklisted invites are never permitted. - If the invite is not blacklisted, it is permitted only if it is a Verified or a Partnered server, otherwise not. This strategy was decided on during the June 7th staff meeting, see https://github.com/python-discord/organisation/issues/261 --- bot/cogs/filtering.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 3ebb47a0f..b5b1c823a 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -99,9 +99,9 @@ class Filtering(Cog): self.bot.loop.create_task(self.reschedule_offensive_msg_deletion()) - def _get_allowlist_items(self, allow: bool, list_type: str, compiled: Optional[bool] = False) -> list: + def _get_allowlist_items(self, list_type: str, *, allowed: bool, compiled: Optional[bool] = False) -> list: """Fetch items from the allow_deny_list_cache.""" - items = self.bot.allow_deny_list_cache.get(f"{list_type.upper()}.{allow}", []) + items = self.bot.allow_deny_list_cache.get(f"{list_type.upper()}.{allowed}", []) if compiled: return [re.compile(fr'{item.get("content")}', flags=re.IGNORECASE) for item in items] @@ -143,7 +143,7 @@ class Filtering(Cog): def get_name_matches(self, name: str) -> List[re.Match]: """Check bad words from passed string (name). Return list of matches.""" matches = [] - watchlist_patterns = self._get_allowlist_items(False, 'word_watchlist', compiled=True) + watchlist_patterns = self._get_allowlist_items('word_watchlist', allowed=False, compiled=True) for pattern in watchlist_patterns: if match := pattern.search(name): matches.append(match) @@ -408,7 +408,7 @@ class Filtering(Cog): if URL_RE.search(text): return False - watchlist_patterns = self._get_allowlist_items(False, 'word_watchlist', compiled=True) + watchlist_patterns = self._get_allowlist_items('word_watchlist', allowed=False, compiled=True) for pattern in watchlist_patterns: match = pattern.search(text) if match: @@ -420,7 +420,7 @@ class Filtering(Cog): return False text = text.lower() - domain_blacklist = self._get_allowlist_items(False, "domain_name") + domain_blacklist = self._get_allowlist_items("domain_name", allowed=False) for url in domain_blacklist: if url.lower() in text: @@ -468,9 +468,21 @@ class Filtering(Cog): return True guild_id = guild.get("id") - guild_invite_whitelist = self._get_allowlist_items(True, "guild_invite") + guild_invite_whitelist = self._get_allowlist_items("guild_invite", allowed=True) + guild_invite_blacklist = self._get_allowlist_items("guild_invite", allowed=False) - if guild_id not in guild_invite_whitelist: + # Is this invite allowed? + guild_partnered_or_verified = ( + 'PARTNERED' in guild.get("features") + or 'VERIFIED' in guild.get("features") + ) + invite_not_allowed = ( + guild_id in guild_invite_blacklist # Blacklisted guilds are never permitted. + or guild_id not in guild_invite_whitelist # Whitelisted guilds are always permitted. + and not guild_partnered_or_verified # Otherwise guilds have to be Verified or Partnered. + ) + + if invite_not_allowed: guild_icon_hash = guild["icon"] guild_icon = ( "https://cdn.discordapp.com/icons/" -- cgit v1.2.3 From 10b17a5026489c6fa28ed93edef340e4cacdc8c3 Mon Sep 17 00:00:00 2001 From: wookie184 Date: Mon, 20 Jul 2020 14:58:00 +0100 Subject: Removed python formatting from returned codeblock --- bot/cogs/snekbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/snekbox.py b/bot/cogs/snekbox.py index 662f90869..52c8b6f88 100644 --- a/bot/cogs/snekbox.py +++ b/bot/cogs/snekbox.py @@ -202,7 +202,7 @@ class Snekbox(Cog): output, paste_link = await self.format_output(results["stdout"]) icon = self.get_status_emoji(results) - msg = f"{ctx.author.mention} {icon} {msg}.\n\n```py\n{output}\n```" + msg = f"{ctx.author.mention} {icon} {msg}.\n\n```\n{output}\n```" if paste_link: msg = f"{msg}\nFull output: {paste_link}" -- cgit v1.2.3 From e93fdaf57d1d35394b466a6bd1c84712e29415d7 Mon Sep 17 00:00:00 2001 From: wookie184 Date: Mon, 20 Jul 2020 16:05:32 +0100 Subject: Edited tests to reflect changes (removed py formatting) --- tests/bot/cogs/test_snekbox.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/bot/cogs/test_snekbox.py b/tests/bot/cogs/test_snekbox.py index 98dee7a1b..343e37db9 100644 --- a/tests/bot/cogs/test_snekbox.py +++ b/tests/bot/cogs/test_snekbox.py @@ -239,7 +239,7 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): await self.cog.send_eval(ctx, 'MyAwesomeCode') ctx.send.assert_called_once_with( - '@LemonLemonishBeard#0042 :yay!: Return code 0.\n\n```py\n[No output]\n```' + '@LemonLemonishBeard#0042 :yay!: Return code 0.\n\n```\n[No output]\n```' ) self.cog.post_eval.assert_called_once_with('MyAwesomeCode') self.cog.get_status_emoji.assert_called_once_with({'stdout': '', 'returncode': 0}) @@ -265,7 +265,7 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): await self.cog.send_eval(ctx, 'MyAwesomeCode') ctx.send.assert_called_once_with( '@LemonLemonishBeard#0042 :yay!: Return code 0.' - '\n\n```py\nWay too long beard\n```\nFull output: lookatmybeard.com' + '\n\n```\nWay too long beard\n```\nFull output: lookatmybeard.com' ) self.cog.post_eval.assert_called_once_with('MyAwesomeCode') self.cog.get_status_emoji.assert_called_once_with({'stdout': 'Way too long beard', 'returncode': 0}) @@ -289,7 +289,7 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): await self.cog.send_eval(ctx, 'MyAwesomeCode') ctx.send.assert_called_once_with( - '@LemonLemonishBeard#0042 :nope!: Return code 127.\n\n```py\nBeard got stuck in the eval\n```' + '@LemonLemonishBeard#0042 :nope!: Return code 127.\n\n```\nBeard got stuck in the eval\n```' ) self.cog.post_eval.assert_called_once_with('MyAwesomeCode') self.cog.get_status_emoji.assert_called_once_with({'stdout': 'ERROR', 'returncode': 127}) -- cgit v1.2.3 From b6f11f518acc29cf0bc025f2c89005556a06553a Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Tue, 21 Jul 2020 01:26:09 +0100 Subject: Use max_units for time since join in user command instead of precision --- bot/cogs/information.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/information.py b/bot/cogs/information.py index f0bd1afdb..d6090d481 100644 --- a/bot/cogs/information.py +++ b/bot/cogs/information.py @@ -226,7 +226,7 @@ class Information(Cog): if user.nick: name = f"{user.nick} ({name})" - joined = time_since(user.joined_at, precision="days") + joined = time_since(user.joined_at, max_units=3) roles = ", ".join(role.mention for role in user.roles[1:]) description = [ -- cgit v1.2.3 From bb774296cf613c66dc8d2863547c1147e8ad6520 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 21 Jul 2020 13:28:07 -0700 Subject: Charinfo: use send_denial helper --- bot/cogs/utils.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/bot/cogs/utils.py b/bot/cogs/utils.py index 697bf60ce..60e160ed0 100644 --- a/bot/cogs/utils.py +++ b/bot/cogs/utils.py @@ -12,6 +12,7 @@ from discord.ext.commands import BadArgument, Cog, Context, command from bot.bot import Bot from bot.constants import Channels, MODERATION_ROLES, STAFF_ROLES from bot.decorators import in_whitelist, with_role +from bot.utils import messages log = logging.getLogger(__name__) @@ -120,22 +121,15 @@ class Utils(Cog): """Shows you information on up to 25 unicode characters.""" match = re.match(r"<(a?):(\w+):(\d+)>", characters) if match: - embed = Embed( - title="Non-Character Detected", - description=( - "Only unicode characters can be processed, but a custom Discord emoji " - "was found. Please remove it and try again." - ) + return await messages.send_denial( + ctx, + "**Non-Character Detected**\n" + "Only unicode characters can be processed, but a custom Discord emoji " + "was found. Please remove it and try again." ) - embed.colour = Colour.red() - await ctx.send(embed=embed) - return if len(characters) > 25: - embed = Embed(title=f"Too many characters ({len(characters)}/25)") - embed.colour = Colour.red() - await ctx.send(embed=embed) - return + return await messages.send_denial(ctx, f"Too many characters ({len(characters)}/25)") def get_info(char: str) -> Tuple[str, str]: digit = f"{ord(char):x}" -- cgit v1.2.3 From 6c367269032b85fd60094228178209760aa8d282 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 21 Jul 2020 13:48:21 -0700 Subject: Charinfo: paginate the results Pagination ensures the results will never go over the char limit for an embed. Fixes #897 Fixes BOT-3D --- bot/cogs/utils.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bot/cogs/utils.py b/bot/cogs/utils.py index 60e160ed0..d70fb300d 100644 --- a/bot/cogs/utils.py +++ b/bot/cogs/utils.py @@ -12,6 +12,7 @@ from discord.ext.commands import BadArgument, Cog, Context, command from bot.bot import Bot from bot.constants import Channels, MODERATION_ROLES, STAFF_ROLES from bot.decorators import in_whitelist, with_role +from bot.pagination import LinePaginator from bot.utils import messages log = logging.getLogger(__name__) @@ -142,15 +143,14 @@ class Utils(Cog): info = f"`{u_code.ljust(10)}`: {name} - {utils.escape_markdown(char)}" return info, u_code - charlist, rawlist = zip(*(get_info(c) for c in characters)) - - embed = Embed(description="\n".join(charlist)) - embed.set_author(name="Character Info") + char_list, raw_list = zip(*(get_info(c) for c in characters)) + embed = Embed().set_author(name="Character Info") if len(characters) > 1: - embed.add_field(name='Raw', value=f"`{''.join(rawlist)}`", inline=False) + # Maximum length possible is 252 so no need to truncate. + embed.add_field(name='Raw', value=f"`{''.join(raw_list)}`", inline=False) - await ctx.send(embed=embed) + await LinePaginator.paginate(char_list, ctx, embed, max_size=2000, empty=False) @command() async def zen(self, ctx: Context, *, search_value: Union[int, str, None] = None) -> None: -- cgit v1.2.3 From 63c7827d9d9025c7505747904237b37eb46464df Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 22 Jul 2020 11:42:31 -0700 Subject: Jam Tests: fix utils patch stop needs to be called on the patcher, not the mock. Furthermore, using addCleanup is safer than tearDown because the latter may not be called if an exception is raised in setUp. --- tests/bot/cogs/test_jams.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 2f2cb4695..28eb1ab53 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -16,11 +16,12 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.guild = MockGuild([self.admin_role]) self.ctx = MockContext(bot=self.bot, author=self.command_user, guild=self.guild) self.cog = CodeJams(self.bot) - self.utils_mock = patch("bot.cogs.jams.utils").start() - self.default_args = [self.cog, self.ctx, "foo"] - def tearDown(self): - self.utils_mock.stop() + utils_patcher = patch("bot.cogs.jams.utils") + self.utils_mock = utils_patcher.start() + self.addCleanup(utils_patcher.stop) + + self.default_args = [self.cog, self.ctx, "foo"] async def test_too_small_amount_of_team_members_passed(self): """Should `ctx.send` and exit early when too small amount of members.""" -- cgit v1.2.3 From f7e177357e7a47d9a43b492aac7703961af72c19 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 22 Jul 2020 11:43:58 -0700 Subject: Jam Tests: re-arrange tests to follow definition order in the cog --- tests/bot/cogs/test_jams.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 28eb1ab53..e0018e006 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -48,6 +48,16 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.cog.create_channels.assert_not_awaited() self.cog.add_roles.assert_not_awaited() + async def test_result_sending(self): + """Should call `ctx.send` when everything goes right.""" + self.cog.create_channels = AsyncMock() + self.cog.add_roles = AsyncMock() + members = [MockMember() for _ in range(5)] + await self.cog.createteam(self.cog, self.ctx, "foo", members) + self.cog.create_channels.assert_awaited_once() + self.cog.add_roles.assert_awaited_once() + self.ctx.send.assert_awaited_once() + async def test_category_dont_exist(self): """Should create code jam category.""" self.utils_mock.get.return_value = None @@ -125,16 +135,6 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): for member in members: member.add_roles.assert_any_await(jam_role) - async def test_result_sending(self): - """Should call `ctx.send` when everything goes right.""" - self.cog.create_channels = AsyncMock() - self.cog.add_roles = AsyncMock() - members = [MockMember() for _ in range(5)] - await self.cog.createteam(self.cog, self.ctx, "foo", members) - self.cog.create_channels.assert_awaited_once() - self.cog.add_roles.assert_awaited_once() - self.ctx.send.assert_awaited_once() - class CodeJamSetup(unittest.TestCase): """Test for `setup` function of `CodeJam` cog.""" -- cgit v1.2.3 From b1d0f36356ecf4eee729bf276c8b0ed10653ad54 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 22 Jul 2020 11:47:49 -0700 Subject: Jam Tests: remove default_args attribute Kind of redundant since it's only used by two tests. --- tests/bot/cogs/test_jams.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index e0018e006..0fce2a67c 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -21,8 +21,6 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.utils_mock = utils_patcher.start() self.addCleanup(utils_patcher.stop) - self.default_args = [self.cog, self.ctx, "foo"] - async def test_too_small_amount_of_team_members_passed(self): """Should `ctx.send` and exit early when too small amount of members.""" for case in (1, 2): @@ -32,7 +30,8 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.ctx.reset_mock() self.utils_mock.reset_mock() - await self.cog.createteam(*self.default_args, (MockMember() for _ in range(case))) + members = (MockMember() for _ in range(case)) + await self.cog.createteam(self.cog, self.ctx, "foo", members) self.ctx.send.assert_awaited_once() self.cog.create_channels.assert_not_awaited() @@ -43,7 +42,7 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.cog.create_channels = AsyncMock() self.cog.add_roles = AsyncMock() member = MockMember() - await self.cog.createteam(*self.default_args, (member for _ in range(5))) + await self.cog.createteam(self.cog, self.ctx, "foo", (member for _ in range(5))) self.ctx.send.assert_awaited_once() self.cog.create_channels.assert_not_awaited() self.cog.add_roles.assert_not_awaited() -- cgit v1.2.3 From 44cd1d989d491d692d48324228ccc9593a545cd2 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 22 Jul 2020 11:51:08 -0700 Subject: Jam Tests: space out lines for readability --- tests/bot/cogs/test_jams.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 0fce2a67c..81fbcb798 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -41,8 +41,10 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Should `ctx.send` and exit early because duplicate members provided and total there is only 1 member.""" self.cog.create_channels = AsyncMock() self.cog.add_roles = AsyncMock() + member = MockMember() await self.cog.createteam(self.cog, self.ctx, "foo", (member for _ in range(5))) + self.ctx.send.assert_awaited_once() self.cog.create_channels.assert_not_awaited() self.cog.add_roles.assert_not_awaited() @@ -51,8 +53,10 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Should call `ctx.send` when everything goes right.""" self.cog.create_channels = AsyncMock() self.cog.add_roles = AsyncMock() + members = [MockMember() for _ in range(5)] await self.cog.createteam(self.cog, self.ctx, "foo", members) + self.cog.create_channels.assert_awaited_once() self.cog.add_roles.assert_awaited_once() self.ctx.send.assert_awaited_once() @@ -60,7 +64,9 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_category_dont_exist(self): """Should create code jam category.""" self.utils_mock.get.return_value = None + await self.cog.get_category(self.guild) + self.guild.create_category_channel.assert_awaited_once() category_overwrites = self.guild.create_category_channel.call_args[1]["overwrites"] -- cgit v1.2.3 From 1f0222129a3d9b01d97671360296d982629bd25d Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 22 Jul 2020 15:50:30 -0700 Subject: Jams: create a new category if others are full --- bot/cogs/jams.py | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/bot/cogs/jams.py b/bot/cogs/jams.py index a48dbc49a..b3102db2f 100644 --- a/bot/cogs/jams.py +++ b/bot/cogs/jams.py @@ -1,7 +1,7 @@ import logging import typing as t -from discord import CategoryChannel, Guild, Member, PermissionOverwrite, Role, utils +from discord import CategoryChannel, Guild, Member, PermissionOverwrite, Role from discord.ext import commands from more_itertools import unique_everseen @@ -11,6 +11,9 @@ from bot.decorators import with_role log = logging.getLogger(__name__) +MAX_CHANNELS = 50 +CATEGORY_NAME = "Code Jam" + class CodeJams(commands.Cog): """Manages the code-jam related parts of our server.""" @@ -50,30 +53,38 @@ class CodeJams(commands.Cog): f"**Team Members:** {' '.join(member.mention for member in members[1:])}" ) + async def get_category(self, guild: Guild) -> CategoryChannel: + """ + Return a code jam category. + + If all categories are full or none exist, create a new category. + """ + for category in guild.categories: + # Need 2 available spaces: one for the text channel and one for voice. + if category.name == CATEGORY_NAME and MAX_CHANNELS - len(category.channels) >= 2: + return category + + return await self.create_category(guild) + @staticmethod - async def get_category(guild: Guild) -> CategoryChannel: - """Create a Code Jam category if it doesn't exist and return it.""" - code_jam_category = utils.get(guild.categories, name="Code Jam") - - if code_jam_category is None: - log.info("Code Jam category not found, creating it.") - - category_overwrites = { - guild.default_role: PermissionOverwrite(read_messages=False), - guild.me: PermissionOverwrite(read_messages=True) - } - - code_jam_category = await guild.create_category_channel( - "Code Jam", - overwrites=category_overwrites, - reason="It's code jam time!" - ) + async def create_category(guild: Guild) -> CategoryChannel: + """Create a new code jam category and return it.""" + log.info("Creating a new code jam category.") - return code_jam_category + category_overwrites = { + guild.default_role: PermissionOverwrite(read_messages=False), + guild.me: PermissionOverwrite(read_messages=True) + } + + return await guild.create_category_channel( + CATEGORY_NAME, + overwrites=category_overwrites, + reason="It's code jam time!" + ) @staticmethod def get_overwrites(members: t.List[Member], guild: Guild) -> t.Dict[t.Union[Member, Role], PermissionOverwrite]: - """Get Code Jam team channels permission overwrites.""" + """Get code jam team channels permission overwrites.""" # First member is always the team leader team_channel_overwrites = { members[0]: PermissionOverwrite( -- cgit v1.2.3 From 12168766a153d9d1bd134ff64f74997eef8ff7b0 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 22 Jul 2020 16:11:32 -0700 Subject: Jam tests: fix category test --- tests/bot/cogs/test_jams.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 81fbcb798..54a096703 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -1,11 +1,22 @@ import unittest -from unittest.mock import AsyncMock, MagicMock, patch +from unittest.mock import AsyncMock, MagicMock, create_autospec -from bot.cogs.jams import CodeJams, setup +from discord import CategoryChannel + +from bot.cogs import jams from bot.constants import Roles from tests.helpers import MockBot, MockContext, MockGuild, MockMember, MockRole, MockTextChannel +def get_mock_category(channel_count: int, name: str) -> CategoryChannel: + """Return a mocked code jam category.""" + category = create_autospec(CategoryChannel, spec_set=True, instance=True) + category.name = name + category.channels = [MockTextChannel() for _ in range(channel_count)] + + return category + + class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): """Tests for `createteam` command.""" @@ -15,11 +26,7 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.command_user = MockMember([self.admin_role]) self.guild = MockGuild([self.admin_role]) self.ctx = MockContext(bot=self.bot, author=self.command_user, guild=self.guild) - self.cog = CodeJams(self.bot) - - utils_patcher = patch("bot.cogs.jams.utils") - self.utils_mock = utils_patcher.start() - self.addCleanup(utils_patcher.stop) + self.cog = jams.CodeJams(self.bot) async def test_too_small_amount_of_team_members_passed(self): """Should `ctx.send` and exit early when too small amount of members.""" @@ -29,7 +36,6 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.cog.add_roles = AsyncMock() self.ctx.reset_mock() - self.utils_mock.reset_mock() members = (MockMember() for _ in range(case)) await self.cog.createteam(self.cog, self.ctx, "foo", members) @@ -63,8 +69,6 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_category_dont_exist(self): """Should create code jam category.""" - self.utils_mock.get.return_value = None - await self.cog.get_category(self.guild) self.guild.create_category_channel.assert_awaited_once() @@ -75,8 +79,15 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_category_channel_exist(self): """Should not try to create category channel.""" - await self.cog.get_category(self.guild) - self.guild.create_category_channel.assert_not_awaited() + expected_category = get_mock_category(48, jams.CATEGORY_NAME) + self.guild.categories = [ + get_mock_category(48, "other"), + expected_category, + get_mock_category(6, jams.CATEGORY_NAME), + ] + + actual_category = await self.cog.get_category(self.guild) + self.assertEqual(expected_category, actual_category) async def test_channel_overwrites(self): """Should have correct permission overwrites for users and roles.""" @@ -103,7 +114,6 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_team_channels_creation(self): """Should create new voice and text channel for team.""" - self.utils_mock.get.return_value = "foo" members = [MockMember() for _ in range(5)] self.cog.get_overwrites = MagicMock() @@ -147,5 +157,5 @@ class CodeJamSetup(unittest.TestCase): def test_setup(self): """Should call `bot.add_cog`.""" bot = MockBot() - setup(bot) + jams.setup(bot) bot.add_cog.assert_called_once() -- cgit v1.2.3 From 92d3f88eb5c2348f3e4cb53a22a833bed61c6fb7 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 22 Jul 2020 16:21:38 -0700 Subject: Jam tests: add subtests to non-existent category test The test has to account for not only the name not matching, but also a lack of available spaces for new channels. --- tests/bot/cogs/test_jams.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index 54a096703..e6b2ac588 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -67,15 +67,26 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.cog.add_roles.assert_awaited_once() self.ctx.send.assert_awaited_once() - async def test_category_dont_exist(self): - """Should create code jam category.""" - await self.cog.get_category(self.guild) + async def test_category_doesnt_exist(self): + """Should create a new code jam category.""" + subtests = ( + [], + [get_mock_category(jams.MAX_CHANNELS - 1, jams.CATEGORY_NAME)], + [get_mock_category(48, "other")], + ) + + for categories in subtests: + self.guild.reset_mock() + self.guild.categories = categories + + with self.subTest(categories=categories): + await self.cog.get_category(self.guild) - self.guild.create_category_channel.assert_awaited_once() - category_overwrites = self.guild.create_category_channel.call_args[1]["overwrites"] + self.guild.create_category_channel.assert_awaited_once() + category_overwrites = self.guild.create_category_channel.call_args[1]["overwrites"] - self.assertFalse(category_overwrites[self.guild.default_role].read_messages) - self.assertTrue(category_overwrites[self.guild.me].read_messages) + self.assertFalse(category_overwrites[self.guild.default_role].read_messages) + self.assertTrue(category_overwrites[self.guild.me].read_messages) async def test_category_channel_exist(self): """Should not try to create category channel.""" -- cgit v1.2.3 From ddba3f5fcfbda0f72baa3f15055c8a92e94c6d88 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 22 Jul 2020 16:27:02 -0700 Subject: Jam tests: assert equality of new category --- tests/bot/cogs/test_jams.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index e6b2ac588..a76a8a051 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -80,13 +80,14 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): self.guild.categories = categories with self.subTest(categories=categories): - await self.cog.get_category(self.guild) + actual_category = await self.cog.get_category(self.guild) self.guild.create_category_channel.assert_awaited_once() category_overwrites = self.guild.create_category_channel.call_args[1]["overwrites"] self.assertFalse(category_overwrites[self.guild.default_role].read_messages) self.assertTrue(category_overwrites[self.guild.me].read_messages) + self.assertEqual(self.guild.create_category_channel.return_value, actual_category) async def test_category_channel_exist(self): """Should not try to create category channel.""" -- cgit v1.2.3 From 8e3c05210f057ab76d135afbe12035847c9029f4 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 22 Jul 2020 16:54:53 -0700 Subject: Jam tests: use the MAX_CHANNELS constant more It's clearer to write MAX_CHANNELS - 2 than a literal 48. --- tests/bot/cogs/test_jams.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/bot/cogs/test_jams.py b/tests/bot/cogs/test_jams.py index a76a8a051..b4ad8535f 100644 --- a/tests/bot/cogs/test_jams.py +++ b/tests/bot/cogs/test_jams.py @@ -72,7 +72,7 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): subtests = ( [], [get_mock_category(jams.MAX_CHANNELS - 1, jams.CATEGORY_NAME)], - [get_mock_category(48, "other")], + [get_mock_category(jams.MAX_CHANNELS - 2, "other")], ) for categories in subtests: @@ -91,11 +91,11 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase): async def test_category_channel_exist(self): """Should not try to create category channel.""" - expected_category = get_mock_category(48, jams.CATEGORY_NAME) + expected_category = get_mock_category(jams.MAX_CHANNELS - 2, jams.CATEGORY_NAME) self.guild.categories = [ - get_mock_category(48, "other"), + get_mock_category(jams.MAX_CHANNELS - 2, "other"), expected_category, - get_mock_category(6, jams.CATEGORY_NAME), + get_mock_category(0, jams.CATEGORY_NAME), ] actual_category = await self.cog.get_category(self.guild) -- cgit v1.2.3 From 14b00ad1fdc065f1f5412a875f31182d4ccfe7a2 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 22 Jul 2020 23:30:53 -0700 Subject: Charinfo: use more descriptive field name Since the raw field is displayed on every page, but pages are incomplete, it may be unclear whether the field's value is for the current page or for all pages. Co-authored-by: Kieran Siek --- bot/cogs/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/utils.py b/bot/cogs/utils.py index d70fb300d..8171706d0 100644 --- a/bot/cogs/utils.py +++ b/bot/cogs/utils.py @@ -148,7 +148,7 @@ class Utils(Cog): if len(characters) > 1: # Maximum length possible is 252 so no need to truncate. - embed.add_field(name='Raw', value=f"`{''.join(raw_list)}`", inline=False) + embed.add_field(name='Full Raw Text', value=f"`{''.join(raw_list)}`", inline=False) await LinePaginator.paginate(char_list, ctx, embed, max_size=2000, empty=False) -- cgit v1.2.3 From c35842b30d7bf8c58251ce780c4fe75eaf23a69f Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 22 Jul 2020 23:35:51 -0700 Subject: Charinfo: up char limit and reduce line limit Pagination means more characters can be supported without cluttering anything. It also means infinite lines, so there's no longer a need to squeeze out the most from a single page. Reducing the line limit leads to a smaller, tidier presentation. --- bot/cogs/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/utils.py b/bot/cogs/utils.py index 8171706d0..c0dc284e4 100644 --- a/bot/cogs/utils.py +++ b/bot/cogs/utils.py @@ -119,7 +119,7 @@ class Utils(Cog): @command() @in_whitelist(channels=(Channels.bot_commands,), roles=STAFF_ROLES) async def charinfo(self, ctx: Context, *, characters: str) -> None: - """Shows you information on up to 25 unicode characters.""" + """Shows you information on up to 50 unicode characters.""" match = re.match(r"<(a?):(\w+):(\d+)>", characters) if match: return await messages.send_denial( @@ -129,7 +129,7 @@ class Utils(Cog): "was found. Please remove it and try again." ) - if len(characters) > 25: + if len(characters) > 50: return await messages.send_denial(ctx, f"Too many characters ({len(characters)}/25)") def get_info(char: str) -> Tuple[str, str]: @@ -147,10 +147,10 @@ class Utils(Cog): embed = Embed().set_author(name="Character Info") if len(characters) > 1: - # Maximum length possible is 252 so no need to truncate. + # Maximum length possible is 502 out of 1024, so there's no need to truncate. embed.add_field(name='Full Raw Text', value=f"`{''.join(raw_list)}`", inline=False) - await LinePaginator.paginate(char_list, ctx, embed, max_size=2000, empty=False) + await LinePaginator.paginate(char_list, ctx, embed, max_lines=10, max_size=2000, empty=False) @command() async def zen(self, ctx: Context, *, search_value: Union[int, str, None] = None) -> None: -- cgit v1.2.3 From 53fe5d44f2a44a2102d00d29c73b4cde733f9f26 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 23 Jul 2020 14:39:01 +0800 Subject: Check that embed desc is not Empty before stripping. --- bot/cogs/help_channels.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 0c8cbb417..e0fd06654 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -428,8 +428,11 @@ class HelpChannels(commands.Cog): if not message or not message.embeds: return False - embed = message.embeds[0] - return message.author == self.bot.user and embed.description.strip() == description.strip() + bot_msg_desc = message.embeds[0].description + if bot_msg_desc is discord.Embed.Empty: + log.trace("Last message was a bot embed but it was empty.") + return False + return message.author == self.bot.user and bot_msg_desc.strip() == description.strip() @staticmethod def is_in_category(channel: discord.TextChannel, category_id: int) -> bool: -- cgit v1.2.3 From e15ceb83ac0ac081325ee47b1f6dddc581229197 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 22 Jul 2020 23:46:51 -0700 Subject: Charinfo: correct char limit used in error message Co-authored-by: Kieran Siek --- bot/cogs/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/utils.py b/bot/cogs/utils.py index c0dc284e4..017f3419e 100644 --- a/bot/cogs/utils.py +++ b/bot/cogs/utils.py @@ -130,7 +130,7 @@ class Utils(Cog): ) if len(characters) > 50: - return await messages.send_denial(ctx, f"Too many characters ({len(characters)}/25)") + return await messages.send_denial(ctx, f"Too many characters ({len(characters)}/50)") def get_info(char: str) -> Tuple[str, str]: digit = f"{ord(char):x}" -- cgit v1.2.3 From be14db91b1c70993773e67cfa663fef0cfa85666 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Thu, 23 Jul 2020 18:27:55 +0100 Subject: Disabled burst_shared filter temporarily --- config-default.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/config-default.yml b/config-default.yml index ad6149f6f..d0262be33 100644 --- a/config-default.yml +++ b/config-default.yml @@ -459,10 +459,6 @@ anti_spam: interval: 10 max: 7 - burst_shared: - interval: 10 - max: 20 - chars: interval: 5 max: 3_000 -- cgit v1.2.3 From 2723949b2fbc065bf870e7fee1275782071a180b Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 24 Jul 2020 09:47:49 +0200 Subject: Catch ResponseCodeError in the ValidAllowDenyListType converter. --- bot/converters.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bot/converters.py b/bot/converters.py index 7e21c1542..55cc630f7 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -1,16 +1,16 @@ +import dateutil.parser +import dateutil.tz +import discord import logging import re import typing as t +from aiohttp import ClientConnectorError from datetime import datetime -from ssl import CertificateError - -import dateutil.parser -import dateutil.tz -import discord -from aiohttp import ClientConnectorError, ContentTypeError from dateutil.relativedelta import relativedelta from discord.ext.commands import BadArgument, Context, Converter, IDConverter, UserConverter +from ssl import CertificateError +from bot.api import ResponseCodeError from bot.constants import URLs from bot.utils.regex import INVITE_RE @@ -84,7 +84,7 @@ class ValidAllowDenyListType(Converter): """Checks whether the given string is a valid AllowDenyList type.""" try: valid_types = await ctx.bot.api_client.get('bot/allow_deny_lists/get_types') - except ContentTypeError: + except ResponseCodeError: raise BadArgument("Cannot validate list_type: Unable to fetch valid types from API.") valid_types = [enum for enum, classname in valid_types] -- cgit v1.2.3 From 6f066fe2e18495425f6fbe90518d25b00b4276b4 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 24 Jul 2020 10:07:20 +0200 Subject: Put valid_types_list inside the conditional. --- bot/converters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/converters.py b/bot/converters.py index 55cc630f7..41cd3f3e5 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -88,10 +88,10 @@ class ValidAllowDenyListType(Converter): raise BadArgument("Cannot validate list_type: Unable to fetch valid types from API.") valid_types = [enum for enum, classname in valid_types] - valid_types_list = '\n'.join([f"• {type_.lower()}" for type_ in valid_types]) list_type = list_type.upper() if list_type not in valid_types: + valid_types_list = '\n'.join([f"• {type_.lower()}" for type_ in valid_types]) raise BadArgument( f"You have provided an invalid list type!\n\n" f"Please provide one of the following: \n{valid_types_list}" -- cgit v1.2.3 From be52d33e5466f83fbf86d0bec3553f788bc08c27 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 24 Jul 2020 10:26:41 +0200 Subject: No need for all() in cog_check for AllowDenyLists. --- bot/cogs/allow_deny_lists.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bot/cogs/allow_deny_lists.py b/bot/cogs/allow_deny_lists.py index 71a032ea5..e28e32bd6 100644 --- a/bot/cogs/allow_deny_lists.py +++ b/bot/cogs/allow_deny_lists.py @@ -210,10 +210,7 @@ class AllowDenyLists(Cog): def cog_check(self, ctx: Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" - checks = [ - with_role_check(ctx, *constants.MODERATION_ROLES), - ] - return all(checks) + return with_role_check(ctx, *constants.MODERATION_ROLES) def setup(bot: Bot) -> None: -- cgit v1.2.3 From 3aa33f3ec84a104ac13c0c60c21f4f149da5af78 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 24 Jul 2020 10:39:21 +0200 Subject: Add sanity to partner and verification check in filtering.py. --- bot/cogs/filtering.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index b5b1c823a..98a60f489 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -473,8 +473,8 @@ class Filtering(Cog): # Is this invite allowed? guild_partnered_or_verified = ( - 'PARTNERED' in guild.get("features") - or 'VERIFIED' in guild.get("features") + 'PARTNERED' in guild.get("features", []) + or 'VERIFIED' in guild.get("features", []) ) invite_not_allowed = ( guild_id in guild_invite_blacklist # Blacklisted guilds are never permitted. -- cgit v1.2.3 From 02e7672623dd1aea11a715e5187eaef7f8633d17 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 24 Jul 2020 10:42:09 +0200 Subject: More explicit dict indexing Addresses reviews from MarkKoz Co-authored-by: Mark --- bot/cogs/antimalware.py | 2 +- bot/cogs/filtering.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index 38ff1133d..5b56f937f 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -40,7 +40,7 @@ class AntiMalware(Cog): def _get_whitelisted_file_formats(self) -> list: """Get the file formats currently on the whitelist.""" - return [item.get('content') for item in self.bot.allow_deny_list_cache['file_format.True']] + return [item['content'] for item in self.bot.allow_deny_list_cache['file_format.True']] def _get_disallowed_extensions(self, message: Message) -> t.Iterable[str]: """Get an iterable containing all the disallowed extensions of attachments.""" diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 98a60f489..8897cbaf9 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -104,9 +104,9 @@ class Filtering(Cog): items = self.bot.allow_deny_list_cache.get(f"{list_type.upper()}.{allowed}", []) if compiled: - return [re.compile(fr'{item.get("content")}', flags=re.IGNORECASE) for item in items] + return [re.compile(fr'{item["content"]}', flags=re.IGNORECASE) for item in items] else: - return [item.get("content") for item in items] + return [item["content"] for item in items] @staticmethod def _expand_spoilers(text: str) -> str: -- cgit v1.2.3 From 6bab215b45b5ad2d40b68459a70e7731af2eb7a2 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Fri, 24 Jul 2020 17:42:42 +0800 Subject: Fix: Implicit string concatenation considered harmful Python joins two string adjacent string literals implicitly, which may cause unintended side effects when used with certain string methods. >>> 'A' ' '.join(['1', '2', '3']) '1A 2A 3' --- bot/cogs/information.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bot/cogs/information.py b/bot/cogs/information.py index d6090d481..8982196d1 100644 --- a/bot/cogs/information.py +++ b/bot/cogs/information.py @@ -116,10 +116,7 @@ class Information(Cog): parsed_roles.append(role) if failed_roles: - await ctx.send( - ":x: I could not convert the following role names to a role: \n- " - "\n- ".join(failed_roles) - ) + await ctx.send(f":x: Could not retrieve the following roles: {', '.join(failed_roles)}") for role in parsed_roles: h, s, v = colorsys.rgb_to_hsv(*role.colour.to_rgb()) -- cgit v1.2.3 From 3d5faa421756fadb42590db92e8fee64578390d4 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 27 Jul 2020 10:26:10 +0200 Subject: Rename AllowDenyList to FilterLists --- bot/__main__.py | 2 +- bot/bot.py | 14 +-- bot/cogs/allow_deny_lists.py | 218 ------------------------------------- bot/cogs/antimalware.py | 2 +- bot/cogs/filter_lists.py | 218 +++++++++++++++++++++++++++++++++++++ bot/cogs/filtering.py | 16 +-- bot/converters.py | 10 +- tests/bot/cogs/test_antimalware.py | 2 +- 8 files changed, 241 insertions(+), 241 deletions(-) delete mode 100644 bot/cogs/allow_deny_lists.py create mode 100644 bot/cogs/filter_lists.py diff --git a/bot/__main__.py b/bot/__main__.py index 932aa705c..c2271cd16 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -53,7 +53,7 @@ bot.load_extension("bot.cogs.verification") # Feature cogs bot.load_extension("bot.cogs.alias") -bot.load_extension("bot.cogs.allow_deny_lists") +bot.load_extension("bot.cogs.filter_lists") bot.load_extension("bot.cogs.defcon") bot.load_extension("bot.cogs.dm_relay") bot.load_extension("bot.cogs.duck_pond") diff --git a/bot/bot.py b/bot/bot.py index d834c151b..3dfb4e948 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -34,7 +34,7 @@ class Bot(commands.Bot): self.redis_ready = asyncio.Event() self.redis_closed = False self.api_client = api.APIClient(loop=self.loop) - self.allow_deny_list_cache = {} + self.filter_list_cache = {} self._connector = None self._resolver = None @@ -50,9 +50,9 @@ class Bot(commands.Bot): self.stats = AsyncStatsClient(self.loop, statsd_url, 8125, prefix="bot") - async def _cache_allow_deny_list_data(self) -> None: - """Cache all the data in the AllowDenyList on the site.""" - full_cache = await self.api_client.get('bot/allow_deny_lists') + async def _cache_filter_list_data(self) -> None: + """Cache all the data in the FilterList on the site.""" + full_cache = await self.api_client.get('bot/filter-lists') for item in full_cache: type_ = item.get("type") @@ -64,7 +64,7 @@ class Bot(commands.Bot): "created_at": item.get("created_at"), "updated_at": item.get("updated_at"), } - self.allow_deny_list_cache.setdefault(f"{type_}.{allowed}", []).append(metadata) + self.filter_list_cache.setdefault(f"{type_}.{allowed}", []).append(metadata) async def _create_redis_session(self) -> None: """ @@ -176,8 +176,8 @@ class Bot(commands.Bot): self.http_session = aiohttp.ClientSession(connector=self._connector) self.api_client.recreate(force=True, connector=self._connector) - # Build the AllowDenyList cache - self.loop.create_task(self._cache_allow_deny_list_data()) + # Build the FilterList cache + self.loop.create_task(self._cache_filter_list_data()) async def on_guild_available(self, guild: discord.Guild) -> None: """ diff --git a/bot/cogs/allow_deny_lists.py b/bot/cogs/allow_deny_lists.py deleted file mode 100644 index e28e32bd6..000000000 --- a/bot/cogs/allow_deny_lists.py +++ /dev/null @@ -1,218 +0,0 @@ -import logging -from typing import Optional - -from discord import Colour, Embed -from discord.ext.commands import BadArgument, Cog, Context, IDConverter, group - -from bot import constants -from bot.api import ResponseCodeError -from bot.bot import Bot -from bot.converters import ValidAllowDenyListType, ValidDiscordServerInvite -from bot.pagination import LinePaginator -from bot.utils.checks import with_role_check - -log = logging.getLogger(__name__) - - -class AllowDenyLists(Cog): - """Commands for blacklisting and whitelisting things.""" - - def __init__(self, bot: Bot) -> None: - self.bot = bot - - async def _add_data( - self, - ctx: Context, - allowed: bool, - list_type: ValidAllowDenyListType, - content: str, - comment: Optional[str] = None, - ) -> None: - """Add an item to an allow or denylist.""" - allow_type = "whitelist" if allowed else "blacklist" - - # If this is a server invite, we gotta validate it. - if list_type == "GUILD_INVITE": - log.trace(f"{content} is a guild invite, attempting to validate.") - validator = ValidDiscordServerInvite() - guild_data = await validator.convert(ctx, content) - - # If we make it this far without raising a BadArgument, the invite is - # valid. Let's convert the content to an ID. - log.trace(f"{content} validated as server invite. Converting to ID.") - content = guild_data.get("id") - - # Unless the user has specified another comment, let's - # use the server name as the comment so that the list - # of guild IDs will be more easily readable when we - # display it. - if not comment: - comment = guild_data.get("name") - - # Try to add the item to the database - log.trace(f"Trying to add the {content} item to the {list_type} {allow_type}") - payload = { - 'allowed': allowed, - 'type': list_type, - 'content': content, - 'comment': comment, - } - - try: - item = await self.bot.api_client.post( - "bot/allow_deny_lists", - json=payload - ) - except ResponseCodeError as e: - if e.status == 500: - await ctx.message.add_reaction("❌") - log.debug( - f"{ctx.author} tried to add data to a {allow_type}, but the API returned 500, " - "probably because the request violated the UniqueConstraint." - ) - raise BadArgument( - f"Unable to add the item to the {allow_type}. " - "The item probably already exists. Keep in mind that a " - "blacklist and a whitelist for the same item cannot co-exist, " - "and we do not permit any duplicates." - ) - raise - - # Insert the item into the cache - type_ = item.get("type") - allowed = item.get("allowed") - metadata = { - "content": item.get("content"), - "comment": item.get("comment"), - "id": item.get("id"), - "created_at": item.get("created_at"), - "updated_at": item.get("updated_at"), - } - self.bot.allow_deny_list_cache.setdefault(f"{type_}.{allowed}", []).append(metadata) - await ctx.message.add_reaction("✅") - - async def _delete_data(self, ctx: Context, allowed: bool, list_type: ValidAllowDenyListType, content: str) -> None: - """Remove an item from an allow or denylist.""" - item = None - allow_type = "whitelist" if allowed else "blacklist" - id_converter = IDConverter() - - # If this is a server invite, we need to convert it. - if list_type == "GUILD_INVITE" and not id_converter._get_id_match(content): - log.trace(f"{content} is a guild invite, attempting to validate.") - validator = ValidDiscordServerInvite() - guild_data = await validator.convert(ctx, content) - - # If we make it this far without raising a BadArgument, the invite is - # valid. Let's convert the content to an ID. - log.trace(f"{content} validated as server invite. Converting to ID.") - content = guild_data.get("id") - - # Find the content and delete it. - log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}") - for allow_list in self.bot.allow_deny_list_cache.get(f"{list_type}.{allowed}", []): - if content == allow_list.get("content"): - item = allow_list - break - - if item is not None: - await self.bot.api_client.delete( - f"bot/allow_deny_lists/{item.get('id')}" - ) - self.bot.allow_deny_list_cache[f"{list_type}.{allowed}"].remove(item) - await ctx.message.add_reaction("✅") - - async def _list_all_data(self, ctx: Context, allowed: bool, list_type: ValidAllowDenyListType) -> None: - """Paginate and display all items in an allow or denylist.""" - allow_type = "whitelist" if allowed else "blacklist" - result = self.bot.allow_deny_list_cache.get(f"{list_type}.{allowed}", []) - - # Build a list of lines we want to show in the paginator - lines = [] - for item in result: - line = f"• `{item.get('content')}`" - - if item.get("comment"): - line += f" - {item.get('comment')}" - - lines.append(line) - lines = sorted(lines) - - # Build the embed - list_type_plural = list_type.lower().replace("_", " ").title() + "s" - embed = Embed( - title=f"{allow_type.title()}ed {list_type_plural} ({len(result)} total)", - colour=Colour.blue() - ) - log.trace(f"Trying to list {len(result)} items from the {list_type.lower()} {allow_type}") - - if result: - await LinePaginator.paginate(lines, ctx, embed, max_lines=15, empty=False) - else: - embed.description = "Hmmm, seems like there's nothing here yet." - await ctx.send(embed=embed) - - @group(aliases=("allowlist", "allow", "al", "wl")) - async def whitelist(self, ctx: Context) -> None: - """Group for whitelisting commands.""" - if not ctx.invoked_subcommand: - await ctx.send_help(ctx.command) - - @group(aliases=("denylist", "deny", "bl", "dl")) - async def blacklist(self, ctx: Context) -> None: - """Group for blacklisting commands.""" - if not ctx.invoked_subcommand: - await ctx.send_help(ctx.command) - - @whitelist.command(name="add", aliases=("a", "set")) - async def allow_add( - self, - ctx: Context, - list_type: ValidAllowDenyListType, - content: str, - *, - comment: Optional[str] = None, - ) -> None: - """Add an item to the specified allowlist.""" - await self._add_data(ctx, True, list_type, content, comment) - - @blacklist.command(name="add", aliases=("a", "set")) - async def deny_add( - self, - ctx: Context, - list_type: ValidAllowDenyListType, - content: str, - *, - comment: Optional[str] = None, - ) -> None: - """Add an item to the specified denylist.""" - await self._add_data(ctx, False, list_type, content, comment) - - @whitelist.command(name="remove", aliases=("delete", "rm",)) - async def allow_delete(self, ctx: Context, list_type: ValidAllowDenyListType, content: str) -> None: - """Remove an item from the specified allowlist.""" - await self._delete_data(ctx, True, list_type, content) - - @blacklist.command(name="remove", aliases=("delete", "rm",)) - async def deny_delete(self, ctx: Context, list_type: ValidAllowDenyListType, content: str) -> None: - """Remove an item from the specified denylist.""" - await self._delete_data(ctx, False, list_type, content) - - @whitelist.command(name="get", aliases=("list", "ls", "fetch", "show")) - async def allow_get(self, ctx: Context, list_type: ValidAllowDenyListType) -> None: - """Get the contents of a specified allowlist.""" - await self._list_all_data(ctx, True, list_type) - - @blacklist.command(name="get", aliases=("list", "ls", "fetch", "show")) - async def deny_get(self, ctx: Context, list_type: ValidAllowDenyListType) -> None: - """Get the contents of a specified denylist.""" - await self._list_all_data(ctx, False, list_type) - - def cog_check(self, ctx: Context) -> bool: - """Only allow moderators to invoke the commands in this cog.""" - return with_role_check(ctx, *constants.MODERATION_ROLES) - - -def setup(bot: Bot) -> None: - """Load the AllowDenyLists cog.""" - bot.add_cog(AllowDenyLists(bot)) diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index 5b56f937f..9a100b3fc 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -40,7 +40,7 @@ class AntiMalware(Cog): def _get_whitelisted_file_formats(self) -> list: """Get the file formats currently on the whitelist.""" - return [item['content'] for item in self.bot.allow_deny_list_cache['file_format.True']] + return [item['content'] for item in self.bot.filter_list_cache['file_format.True']] def _get_disallowed_extensions(self, message: Message) -> t.Iterable[str]: """Get an iterable containing all the disallowed extensions of attachments.""" diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py new file mode 100644 index 000000000..d1db9830e --- /dev/null +++ b/bot/cogs/filter_lists.py @@ -0,0 +1,218 @@ +import logging +from typing import Optional + +from discord import Colour, Embed +from discord.ext.commands import BadArgument, Cog, Context, IDConverter, group + +from bot import constants +from bot.api import ResponseCodeError +from bot.bot import Bot +from bot.converters import ValidDiscordServerInvite, ValidFilterListType +from bot.pagination import LinePaginator +from bot.utils.checks import with_role_check + +log = logging.getLogger(__name__) + + +class FilterLists(Cog): + """Commands for blacklisting and whitelisting things.""" + + def __init__(self, bot: Bot) -> None: + self.bot = bot + + async def _add_data( + self, + ctx: Context, + allowed: bool, + list_type: ValidFilterListType, + content: str, + comment: Optional[str] = None, + ) -> None: + """Add an item to a filterlist.""" + allow_type = "whitelist" if allowed else "blacklist" + + # If this is a server invite, we gotta validate it. + if list_type == "GUILD_INVITE": + log.trace(f"{content} is a guild invite, attempting to validate.") + validator = ValidDiscordServerInvite() + guild_data = await validator.convert(ctx, content) + + # If we make it this far without raising a BadArgument, the invite is + # valid. Let's convert the content to an ID. + log.trace(f"{content} validated as server invite. Converting to ID.") + content = guild_data.get("id") + + # Unless the user has specified another comment, let's + # use the server name as the comment so that the list + # of guild IDs will be more easily readable when we + # display it. + if not comment: + comment = guild_data.get("name") + + # Try to add the item to the database + log.trace(f"Trying to add the {content} item to the {list_type} {allow_type}") + payload = { + 'allowed': allowed, + 'type': list_type, + 'content': content, + 'comment': comment, + } + + try: + item = await self.bot.api_client.post( + "bot/filter-lists", + json=payload + ) + except ResponseCodeError as e: + if e.status == 500: + await ctx.message.add_reaction("❌") + log.debug( + f"{ctx.author} tried to add data to a {allow_type}, but the API returned 500, " + "probably because the request violated the UniqueConstraint." + ) + raise BadArgument( + f"Unable to add the item to the {allow_type}. " + "The item probably already exists. Keep in mind that a " + "blacklist and a whitelist for the same item cannot co-exist, " + "and we do not permit any duplicates." + ) + raise + + # Insert the item into the cache + type_ = item.get("type") + allowed = item.get("allowed") + metadata = { + "content": item.get("content"), + "comment": item.get("comment"), + "id": item.get("id"), + "created_at": item.get("created_at"), + "updated_at": item.get("updated_at"), + } + self.bot.filter_list_cache.setdefault(f"{type_}.{allowed}", []).append(metadata) + await ctx.message.add_reaction("✅") + + async def _delete_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType, content: str) -> None: + """Remove an item from a filterlist.""" + item = None + allow_type = "whitelist" if allowed else "blacklist" + id_converter = IDConverter() + + # If this is a server invite, we need to convert it. + if list_type == "GUILD_INVITE" and not id_converter._get_id_match(content): + log.trace(f"{content} is a guild invite, attempting to validate.") + validator = ValidDiscordServerInvite() + guild_data = await validator.convert(ctx, content) + + # If we make it this far without raising a BadArgument, the invite is + # valid. Let's convert the content to an ID. + log.trace(f"{content} validated as server invite. Converting to ID.") + content = guild_data.get("id") + + # Find the content and delete it. + log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}") + for allow_list in self.bot.filter_list_cache.get(f"{list_type}.{allowed}", []): + if content == allow_list.get("content"): + item = allow_list + break + + if item is not None: + await self.bot.api_client.delete( + f"bot/filter-lists/{item.get('id')}" + ) + self.bot.filter_list_cache[f"{list_type}.{allowed}"].remove(item) + await ctx.message.add_reaction("✅") + + async def _list_all_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType) -> None: + """Paginate and display all items in a filterlist.""" + allow_type = "whitelist" if allowed else "blacklist" + result = self.bot.filter_list_cache.get(f"{list_type}.{allowed}", []) + + # Build a list of lines we want to show in the paginator + lines = [] + for item in result: + line = f"• `{item.get('content')}`" + + if item.get("comment"): + line += f" - {item.get('comment')}" + + lines.append(line) + lines = sorted(lines) + + # Build the embed + list_type_plural = list_type.lower().replace("_", " ").title() + "s" + embed = Embed( + title=f"{allow_type.title()}ed {list_type_plural} ({len(result)} total)", + colour=Colour.blue() + ) + log.trace(f"Trying to list {len(result)} items from the {list_type.lower()} {allow_type}") + + if result: + await LinePaginator.paginate(lines, ctx, embed, max_lines=15, empty=False) + else: + embed.description = "Hmmm, seems like there's nothing here yet." + await ctx.send(embed=embed) + + @group(aliases=("allowlist", "allow", "al", "wl")) + async def whitelist(self, ctx: Context) -> None: + """Group for whitelisting commands.""" + if not ctx.invoked_subcommand: + await ctx.send_help(ctx.command) + + @group(aliases=("denylist", "deny", "bl", "dl")) + async def blacklist(self, ctx: Context) -> None: + """Group for blacklisting commands.""" + if not ctx.invoked_subcommand: + await ctx.send_help(ctx.command) + + @whitelist.command(name="add", aliases=("a", "set")) + async def allow_add( + self, + ctx: Context, + list_type: ValidFilterListType, + content: str, + *, + comment: Optional[str] = None, + ) -> None: + """Add an item to the specified allowlist.""" + await self._add_data(ctx, True, list_type, content, comment) + + @blacklist.command(name="add", aliases=("a", "set")) + async def deny_add( + self, + ctx: Context, + list_type: ValidFilterListType, + content: str, + *, + comment: Optional[str] = None, + ) -> None: + """Add an item to the specified denylist.""" + await self._add_data(ctx, False, list_type, content, comment) + + @whitelist.command(name="remove", aliases=("delete", "rm",)) + async def allow_delete(self, ctx: Context, list_type: ValidFilterListType, content: str) -> None: + """Remove an item from the specified allowlist.""" + await self._delete_data(ctx, True, list_type, content) + + @blacklist.command(name="remove", aliases=("delete", "rm",)) + async def deny_delete(self, ctx: Context, list_type: ValidFilterListType, content: str) -> None: + """Remove an item from the specified denylist.""" + await self._delete_data(ctx, False, list_type, content) + + @whitelist.command(name="get", aliases=("list", "ls", "fetch", "show")) + async def allow_get(self, ctx: Context, list_type: ValidFilterListType) -> None: + """Get the contents of a specified allowlist.""" + await self._list_all_data(ctx, True, list_type) + + @blacklist.command(name="get", aliases=("list", "ls", "fetch", "show")) + async def deny_get(self, ctx: Context, list_type: ValidFilterListType) -> None: + """Get the contents of a specified denylist.""" + await self._list_all_data(ctx, False, list_type) + + def cog_check(self, ctx: Context) -> bool: + """Only allow moderators to invoke the commands in this cog.""" + return with_role_check(ctx, *constants.MODERATION_ROLES) + + +def setup(bot: Bot) -> None: + """Load the FilterLists cog.""" + bot.add_cog(FilterLists(bot)) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 8897cbaf9..652af5ff5 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -99,9 +99,9 @@ class Filtering(Cog): self.bot.loop.create_task(self.reschedule_offensive_msg_deletion()) - def _get_allowlist_items(self, list_type: str, *, allowed: bool, compiled: Optional[bool] = False) -> list: - """Fetch items from the allow_deny_list_cache.""" - items = self.bot.allow_deny_list_cache.get(f"{list_type.upper()}.{allowed}", []) + def _get_filterlist_items(self, list_type: str, *, allowed: bool, compiled: Optional[bool] = False) -> list: + """Fetch items from the filter_list_cache.""" + items = self.bot.filter_list_cache.get(f"{list_type.upper()}.{allowed}", []) if compiled: return [re.compile(fr'{item["content"]}', flags=re.IGNORECASE) for item in items] @@ -143,7 +143,7 @@ class Filtering(Cog): def get_name_matches(self, name: str) -> List[re.Match]: """Check bad words from passed string (name). Return list of matches.""" matches = [] - watchlist_patterns = self._get_allowlist_items('word_watchlist', allowed=False, compiled=True) + watchlist_patterns = self._get_filterlist_items('word_watchlist', allowed=False, compiled=True) for pattern in watchlist_patterns: if match := pattern.search(name): matches.append(match) @@ -408,7 +408,7 @@ class Filtering(Cog): if URL_RE.search(text): return False - watchlist_patterns = self._get_allowlist_items('word_watchlist', allowed=False, compiled=True) + watchlist_patterns = self._get_filterlist_items('word_watchlist', allowed=False, compiled=True) for pattern in watchlist_patterns: match = pattern.search(text) if match: @@ -420,7 +420,7 @@ class Filtering(Cog): return False text = text.lower() - domain_blacklist = self._get_allowlist_items("domain_name", allowed=False) + domain_blacklist = self._get_filterlist_items("domain_name", allowed=False) for url in domain_blacklist: if url.lower() in text: @@ -468,8 +468,8 @@ class Filtering(Cog): return True guild_id = guild.get("id") - guild_invite_whitelist = self._get_allowlist_items("guild_invite", allowed=True) - guild_invite_blacklist = self._get_allowlist_items("guild_invite", allowed=False) + guild_invite_whitelist = self._get_filterlist_items("guild_invite", allowed=True) + guild_invite_blacklist = self._get_filterlist_items("guild_invite", allowed=False) # Is this invite allowed? guild_partnered_or_verified = ( diff --git a/bot/converters.py b/bot/converters.py index 41cd3f3e5..158bf1a16 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -72,18 +72,18 @@ class ValidDiscordServerInvite(Converter): raise BadArgument("This does not appear to be a valid Discord server invite.") -class ValidAllowDenyListType(Converter): +class ValidFilterListType(Converter): """ - A converter that checks whether the given string is a valid AllowDenyList type. + A converter that checks whether the given string is a valid FilterList type. - Raises `BadArgument` if the argument is not a valid AllowDenyList type, and simply + Raises `BadArgument` if the argument is not a valid FilterList type, and simply passes through the given argument otherwise. """ async def convert(self, ctx: Context, list_type: str) -> str: - """Checks whether the given string is a valid AllowDenyList type.""" + """Checks whether the given string is a valid FilterList type.""" try: - valid_types = await ctx.bot.api_client.get('bot/allow_deny_lists/get_types') + valid_types = await ctx.bot.api_client.get('bot/filter-lists/get-types') except ResponseCodeError: raise BadArgument("Cannot validate list_type: Unable to fetch valid types from API.") diff --git a/tests/bot/cogs/test_antimalware.py b/tests/bot/cogs/test_antimalware.py index 1e010d2ce..664fa8f19 100644 --- a/tests/bot/cogs/test_antimalware.py +++ b/tests/bot/cogs/test_antimalware.py @@ -14,7 +14,7 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase): def setUp(self): """Sets up fresh objects for each test.""" self.bot = MockBot() - self.bot.allow_deny_list_cache = { + self.bot.filter_list_cache = { "file_format.True": [ {"content": ".first"}, {"content": ".second"}, -- cgit v1.2.3 From ba00d4f1525340141b0f6c85fbfb32793f5bdfdd Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 27 Jul 2020 10:26:49 +0200 Subject: Bump flake8 version to 3.8 This is necessary to support walrus operators. --- Pipfile | 2 +- Pipfile.lock | 362 +++++++++++++++++++++++++++++++---------------------------- 2 files changed, 191 insertions(+), 173 deletions(-) diff --git a/Pipfile b/Pipfile index 2d6b45aa9..4db8a238b 100644 --- a/Pipfile +++ b/Pipfile @@ -28,7 +28,7 @@ statsd = "~=3.3" [dev-packages] coverage = "~=5.0" -flake8 = "~=3.7" +flake8 = "~=3.8" flake8-annotations = "~=2.0" flake8-bugbear = "~=20.1" flake8-docstrings = "~=1.4" diff --git a/Pipfile.lock b/Pipfile.lock index 4b9d092d4..c8cd96d3d 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "8a53baefbbd2a0f3fbaf831f028b23d257a5e28b5efa1260661d74604f4113b8" + "sha256": "eab4852974d26bd2c10362540c3e01d34af62446cb4e1915ec9a0bf2bddf4d94" }, "pipfile-spec": 6, "requires": { @@ -115,36 +115,36 @@ }, "cffi": { "hashes": [ - "sha256:001bf3242a1bb04d985d63e138230802c6c8d4db3668fb545fb5005ddf5bb5ff", - "sha256:00789914be39dffba161cfc5be31b55775de5ba2235fe49aa28c148236c4e06b", - "sha256:028a579fc9aed3af38f4892bdcc7390508adabc30c6af4a6e4f611b0c680e6ac", - "sha256:14491a910663bf9f13ddf2bc8f60562d6bc5315c1f09c704937ef17293fb85b0", - "sha256:1cae98a7054b5c9391eb3249b86e0e99ab1e02bb0cc0575da191aedadbdf4384", - "sha256:2089ed025da3919d2e75a4d963d008330c96751127dd6f73c8dc0c65041b4c26", - "sha256:2d384f4a127a15ba701207f7639d94106693b6cd64173d6c8988e2c25f3ac2b6", - "sha256:337d448e5a725bba2d8293c48d9353fc68d0e9e4088d62a9571def317797522b", - "sha256:399aed636c7d3749bbed55bc907c3288cb43c65c4389964ad5ff849b6370603e", - "sha256:3b911c2dbd4f423b4c4fcca138cadde747abdb20d196c4a48708b8a2d32b16dd", - "sha256:3d311bcc4a41408cf5854f06ef2c5cab88f9fded37a3b95936c9879c1640d4c2", - "sha256:62ae9af2d069ea2698bf536dcfe1e4eed9090211dbaafeeedf5cb6c41b352f66", - "sha256:66e41db66b47d0d8672d8ed2708ba91b2f2524ece3dee48b5dfb36be8c2f21dc", - "sha256:675686925a9fb403edba0114db74e741d8181683dcf216be697d208857e04ca8", - "sha256:7e63cbcf2429a8dbfe48dcc2322d5f2220b77b2e17b7ba023d6166d84655da55", - "sha256:8a6c688fefb4e1cd56feb6c511984a6c4f7ec7d2a1ff31a10254f3c817054ae4", - "sha256:8c0ffc886aea5df6a1762d0019e9cb05f825d0eec1f520c51be9d198701daee5", - "sha256:95cd16d3dee553f882540c1ffe331d085c9e629499ceadfbda4d4fde635f4b7d", - "sha256:99f748a7e71ff382613b4e1acc0ac83bf7ad167fb3802e35e90d9763daba4d78", - "sha256:b8c78301cefcf5fd914aad35d3c04c2b21ce8629b5e4f4e45ae6812e461910fa", - "sha256:c420917b188a5582a56d8b93bdd8e0f6eca08c84ff623a4c16e809152cd35793", - "sha256:c43866529f2f06fe0edc6246eb4faa34f03fe88b64a0a9a942561c8e22f4b71f", - "sha256:cab50b8c2250b46fe738c77dbd25ce017d5e6fb35d3407606e7a4180656a5a6a", - "sha256:cef128cb4d5e0b3493f058f10ce32365972c554572ff821e175dbc6f8ff6924f", - "sha256:cf16e3cf6c0a5fdd9bc10c21687e19d29ad1fe863372b5543deaec1039581a30", - "sha256:e56c744aa6ff427a607763346e4170629caf7e48ead6921745986db3692f987f", - "sha256:e577934fc5f8779c554639376beeaa5657d54349096ef24abe8c74c5d9c117c3", - "sha256:f2b0fa0c01d8a0c7483afd9f31d7ecf2d71760ca24499c8697aeb5ca37dc090c" - ], - "version": "==1.14.0" + "sha256:267adcf6e68d77ba154334a3e4fc921b8e63cbb38ca00d33d40655d4228502bc", + "sha256:26f33e8f6a70c255767e3c3f957ccafc7f1f706b966e110b855bfe944511f1f9", + "sha256:3cd2c044517f38d1b577f05927fb9729d3396f1d44d0c659a445599e79519792", + "sha256:4a03416915b82b81af5502459a8a9dd62a3c299b295dcdf470877cb948d655f2", + "sha256:4ce1e995aeecf7cc32380bc11598bfdfa017d592259d5da00fc7ded11e61d022", + "sha256:4f53e4128c81ca3212ff4cf097c797ab44646a40b42ec02a891155cd7a2ba4d8", + "sha256:4fa72a52a906425416f41738728268072d5acfd48cbe7796af07a923236bcf96", + "sha256:66dd45eb9530e3dde8f7c009f84568bc7cac489b93d04ac86e3111fb46e470c2", + "sha256:6923d077d9ae9e8bacbdb1c07ae78405a9306c8fd1af13bfa06ca891095eb995", + "sha256:833401b15de1bb92791d7b6fb353d4af60dc688eaa521bd97203dcd2d124a7c1", + "sha256:8416ed88ddc057bab0526d4e4e9f3660f614ac2394b5e019a628cdfff3733849", + "sha256:892daa86384994fdf4856cb43c93f40cbe80f7f95bb5da94971b39c7f54b3a9c", + "sha256:98be759efdb5e5fa161e46d404f4e0ce388e72fbf7d9baf010aff16689e22abe", + "sha256:a6d28e7f14ecf3b2ad67c4f106841218c8ab12a0683b1528534a6c87d2307af3", + "sha256:b1d6ebc891607e71fd9da71688fcf332a6630b7f5b7f5549e6e631821c0e5d90", + "sha256:b2a2b0d276a136146e012154baefaea2758ef1f56ae9f4e01c612b0831e0bd2f", + "sha256:b87dfa9f10a470eee7f24234a37d1d5f51e5f5fa9eeffda7c282e2b8f5162eb1", + "sha256:bac0d6f7728a9cc3c1e06d4fcbac12aaa70e9379b3025b27ec1226f0e2d404cf", + "sha256:c991112622baee0ae4d55c008380c32ecfd0ad417bcd0417ba432e6ba7328caa", + "sha256:cda422d54ee7905bfc53ee6915ab68fe7b230cacf581110df4272ee10462aadc", + "sha256:d3148b6ba3923c5850ea197a91a42683f946dba7e8eb82dfa211ab7e708de939", + "sha256:d6033b4ffa34ef70f0b8086fd4c3df4bf801fee485a8a7d4519399818351aa8e", + "sha256:ddff0b2bd7edcc8c82d1adde6dbbf5e60d57ce985402541cd2985c27f7bec2a0", + "sha256:e23cb7f1d8e0f93addf0cae3c5b6f00324cccb4a7949ee558d7b6ca973ab8ae9", + "sha256:effd2ba52cee4ceff1a77f20d2a9f9bf8d50353c854a282b8760ac15b9833168", + "sha256:f90c2267101010de42f7273c94a1f026e56cbc043f9330acd8a80e64300aba33", + "sha256:f960375e9823ae6a07072ff7f8a85954e5a6434f97869f50d0e41649a1c8144f", + "sha256:fcf32bf76dc25e30ed793145a57426064520890d7c02866eb93d3e4abe516948" + ], + "version": "==1.14.1" }, "chardet": { "hashes": [ @@ -216,49 +216,55 @@ }, "hiredis": { "hashes": [ - "sha256:01b577f84c20ecc9c07fc4c184231b08e3c3942de096fa99978e053de231c423", - "sha256:01ff0900134166961c9e339df77c33b72f7edc5cb41739f0babcd9faa345926e", - "sha256:03ed34a13316d0c34213c4fd46e0fa3a5299073f4d4f08e93fed8c2108b399b3", - "sha256:040436e91df5143aff9e0debb49530d0b17a6bd52200ce568621c31ef581b10d", - "sha256:091eb38fbf968d1c5b703e412bbbd25f43a7967d8400842cee33a5a07b33c27b", - "sha256:102f9b9dc6ed57feb3a7c9bdf7e71cb7c278fe8df1edfcfe896bc3e0c2be9447", - "sha256:2b4b392c7e3082860c8371fab3ae762139090f9115819e12d9f56060f9ede05d", - "sha256:2c9cc0b986397b833073f466e6b9e9c70d1d4dc2c2c1b3e9cae3a23102ff296c", - "sha256:2fa65a9df683bca72073cd77709ddeb289ea2b114d3775d225fbbcc5faf808c5", - "sha256:38437a681f17c975fd22349e72c29bc643f8e7eb2d6dc5df419eac59afa4d7ce", - "sha256:3b3428fa3cf1ee178807b52c9bee8950ab94cd4eaa9bfae8c1bbae3c49501d34", - "sha256:3dd8c2fae7f5494978facb0e93297dd627b1a3f536f3b070cf0a7d9157a07dcb", - "sha256:4414a96c212e732723b5c3d7c04d386ebbb2ec359e1de646322cbc3f875cbd0d", - "sha256:48c627581ad4ef60adbac980981407939acf13a0e18f093502c7b542223c4f19", - "sha256:4a60e71625a2d78d8ab84dfb2fa2cfd9458c964b6e6c04fea76d9ade153fb371", - "sha256:585ace09f434e43d8a8dbeb366865b1a044d7c06319b3c7372a0a00e63b860f4", - "sha256:74b364b3f06c9cf0a53f7df611045bc9437ed972a283fa1f0b12537236d23ddc", - "sha256:75c65c3850e89e9daa68d1b9bedd5806f177d60aa5a7b0953b4829481cfc1f72", - "sha256:7f052de8bf744730a9120dbdc67bfeb7605a01f69fb8e7ba5c475af33c24e145", - "sha256:8113a7d5e87ecf57cd4ae263cc9e429adb9a3e59f5a7768da5d3312a8d0a051a", - "sha256:84857ce239eb8ed191ac78e77ff65d52902f00f30f4ee83bf80eb71da73b70e6", - "sha256:8644a48ddc4a40b3e3a6b9443f396c2ee353afb2d45656c4fc68d04a82e8e3f7", - "sha256:936aa565e673536e8a211e43ec43197406f24cd1f290138bd143765079c8ba00", - "sha256:9afeb88c67bbc663b9f27385c496da056d06ad87f55df6e393e1516cfecb0461", - "sha256:9d62cc7880110e4f83b0a51d218f465d3095e2751fbddd34e553dbd106a929ff", - "sha256:a1fadd062fc8d647ff39220c57ea2b48c99bb73f18223828ec97f88fc27e7898", - "sha256:a7754a783b1e5d6f627c19d099b178059c62f782ab62b4d8ba165b9fbc2ee34c", - "sha256:aa59dd63bb3f736de4fc2d080114429d5d369dfb3265f771778e8349d67a97a4", - "sha256:ae2ee0992f8de249715435942137843a93db204dd7db1e7cc9bdc5a8436443e8", - "sha256:b36842d7cf32929d568f37ec5b3173b72b2ec6572dec4d6be6ce774762215aee", - "sha256:bcbf9379c553b5facc6c04c1e5569b44b38ff16bcbf354676287698d61ee0c92", - "sha256:cbccbda6f1c62ab460449d9c85fdf24d0d32a6bf45176581151e53cc26a5d910", - "sha256:d0caf98dfb8af395d6732bd16561c0a2458851bea522e39f12f04802dbf6f502", - "sha256:d6456afeddba036def1a36d8a2758eca53202308d83db20ab5d0b66590919627", - "sha256:dbaef9a21a4f10bc281684ee4124f169e62bb533c2a92b55f8c06f64f9af7b8f", - "sha256:dce84916c09aaece006272b37234ae84a8ed13abb3a4d341a23933b8701abfb5", - "sha256:eb8c9c8b9869539d58d60ff4a28373a22514d40495911451343971cb4835b7a9", - "sha256:efc98b14ee3a8595e40b1425e8d42f5fd26f11a7b215a81ef9259068931754f4", - "sha256:fa2dc05b87d97acc1c6ae63f3e0f39eae5246565232484b08db6bf2dc1580678", - "sha256:fe7d6ce9f6a5fbe24f09d95ea93e9c7271abc4e1565da511e1449b107b4d7848" + "sha256:06a039208f83744a702279b894c8cf24c14fd63c59cd917dcde168b79eef0680", + "sha256:0a909bf501459062aa1552be1461456518f367379fdc9fdb1f2ca5e4a1fdd7c0", + "sha256:18402d9e54fb278cb9a8c638df6f1550aca36a009d47ecf5aa263a38600f35b0", + "sha256:1e4cbbc3858ec7e680006e5ca590d89a5e083235988f26a004acf7244389ac01", + "sha256:23344e3c2177baf6975fbfa361ed92eb7d36d08f454636e5054b3faa7c2aff8a", + "sha256:289b31885b4996ce04cadfd5fc03d034dce8e2a8234479f7c9e23b9e245db06b", + "sha256:2c1c570ae7bf1bab304f29427e2475fe1856814312c4a1cf1cd0ee133f07a3c6", + "sha256:2c227c0ed371771ffda256034427320870e8ea2e4fd0c0a618c766e7c49aad73", + "sha256:3bb9b63d319402cead8bbd9dd55dca3b667d2997e9a0d8a1f9b6cc274db4baee", + "sha256:3ef2183de67b59930d2db8b8e8d4d58e00a50fcc5e92f4f678f6eed7a1c72d55", + "sha256:43b8ed3dbfd9171e44c554cb4acf4ee4505caa84c5e341858b50ea27dd2b6e12", + "sha256:47bcf3c5e6c1e87ceb86cdda2ee983fa0fe56a999e6185099b3c93a223f2fa9b", + "sha256:5263db1e2e1e8ae30500cdd75a979ff99dcc184201e6b4b820d0de74834d2323", + "sha256:5b1451727f02e7acbdf6aae4e06d75f66ee82966ff9114550381c3271a90f56c", + "sha256:6996883a8a6ff9117cbb3d6f5b0dcbbae6fb9e31e1a3e4e2f95e0214d9a1c655", + "sha256:6c96f64a54f030366657a54bb90b3093afc9c16c8e0dfa29fc0d6dbe169103a5", + "sha256:7332d5c3e35154cd234fd79573736ddcf7a0ade7a986db35b6196b9171493e75", + "sha256:7885b6f32c4a898e825bb7f56f36a02781ac4a951c63e4169f0afcf9c8c30dfb", + "sha256:7b0f63f10a166583ab744a58baad04e0f52cfea1ac27bfa1b0c21a48d1003c23", + "sha256:819f95d4eba3f9e484dd115ab7ab72845cf766b84286a00d4ecf76d33f1edca1", + "sha256:8968eeaa4d37a38f8ca1f9dbe53526b69628edc9c42229a5b2f56d98bb828c1f", + "sha256:89ebf69cb19a33d625db72d2ac589d26e936b8f7628531269accf4a3196e7872", + "sha256:8daecd778c1da45b8bd54fd41ffcd471a86beed3d8e57a43acf7a8d63bba4058", + "sha256:955ba8ea73cf3ed8bd2f963b4cb9f8f0dcb27becd2f4b3dd536fd24c45533454", + "sha256:964f18a59f5a64c0170f684c417f4fe3e695a536612e13074c4dd5d1c6d7c882", + "sha256:969843fbdfbf56cdb71da6f0bdf50f9985b8b8aeb630102945306cf10a9c6af2", + "sha256:996021ef33e0f50b97ff2d6b5f422a0fe5577de21a8873b58a779a5ddd1c3132", + "sha256:9e9c9078a7ce07e6fce366bd818be89365a35d2e4b163268f0ca9ba7e13bb2f6", + "sha256:a04901757cb0fb0f5602ac11dda48f5510f94372144d06c2563ba56c480b467c", + "sha256:a7bf1492429f18d205f3a818da3ff1f242f60aa59006e53dee00b4ef592a3363", + "sha256:aa0af2deb166a5e26e0d554b824605e660039b161e37ed4f01b8d04beec184f3", + "sha256:abfb15a6a7822f0fae681785cb38860e7a2cb1616a708d53df557b3d76c5bfd4", + "sha256:b253fe4df2afea4dfa6b1fa8c5fef212aff8bcaaeb4207e81eed05cb5e4a7919", + "sha256:b27f082f47d23cffc4cf1388b84fdc45c4ef6015f906cd7e0d988d9e35d36349", + "sha256:b33aea449e7f46738811fbc6f0b3177c6777a572207412bbbf6f525ffed001ae", + "sha256:b44f9421c4505c548435244d74037618f452844c5d3c67719d8a55e2613549da", + "sha256:bcc371151d1512201d0214c36c0c150b1dc64f19c2b1a8c9cb1d7c7c15ebd93f", + "sha256:c2851deeabd96d3f6283e9c6b26e0bfed4de2dc6fb15edf913e78b79fc5909ed", + "sha256:cdfd501c7ac5b198c15df800a3a34c38345f5182e5f80770caf362bccca65628", + "sha256:d2c0caffa47606d6d7c8af94ba42547bd2a441f06c74fd90a1ffe328524a6c64", + "sha256:dcb2db95e629962db5a355047fb8aefb012df6c8ae608930d391619dbd96fd86", + "sha256:e0eeb9c112fec2031927a1745788a181d0eecbacbed941fc5c4f7bc3f7b273bf", + "sha256:e154891263306200260d7f3051982774d7b9ef35af3509d5adbbe539afd2610c", + "sha256:e2e023a42dcbab8ed31f97c2bcdb980b7fbe0ada34037d87ba9d799664b58ded", + "sha256:e64be68255234bb489a574c4f2f8df7029c98c81ec4d160d6cd836e7f0679390", + "sha256:e82d6b930e02e80e5109b678c663a9ed210680ded81c1abaf54635d88d1da298" ], "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", - "version": "==1.0.1" + "version": "==1.1.0" }, "humanfriendly": { "hashes": [ @@ -294,36 +300,40 @@ }, "lxml": { "hashes": [ - "sha256:06748c7192eab0f48e3d35a7adae609a329c6257495d5e53878003660dc0fec6", - "sha256:0790ddca3f825dd914978c94c2545dbea5f56f008b050e835403714babe62a5f", - "sha256:1aa7a6197c1cdd65d974f3e4953764eee3d9c7b67e3966616b41fab7f8f516b7", - "sha256:22c6d34fdb0e65d5f782a4d1a1edb52e0a8365858dafb1c08cb1d16546cf0786", - "sha256:2754d4406438c83144f9ffd3628bbe2dcc6d62b20dbc5c1ec4bc4385e5d44b42", - "sha256:27ee0faf8077c7c1a589573b1450743011117f1aa1a91d5ae776bbc5ca6070f2", - "sha256:2b02c106709466a93ed424454ce4c970791c486d5fcdf52b0d822a7e29789626", - "sha256:2d1ddce96cf15f1254a68dba6935e6e0f1fe39247de631c115e84dd404a6f031", - "sha256:4f282737d187ae723b2633856085c31ae5d4d432968b7f3f478a48a54835f5c4", - "sha256:51bb4edeb36d24ec97eb3e6a6007be128b720114f9a875d6b370317d62ac80b9", - "sha256:7eee37c1b9815e6505847aa5e68f192e8a1b730c5c7ead39ff317fde9ce29448", - "sha256:7fd88cb91a470b383aafad554c3fe1ccf6dfb2456ff0e84b95335d582a799804", - "sha256:9144ce36ca0824b29ebc2e02ca186e54040ebb224292072250467190fb613b96", - "sha256:925baf6ff1ef2c45169f548cc85204433e061360bfa7d01e1be7ae38bef73194", - "sha256:a636346c6c0e1092ffc202d97ec1843a75937d8c98aaf6771348ad6422e44bb0", - "sha256:a87dbee7ad9dce3aaefada2081843caf08a44a8f52e03e0a4cc5819f8398f2f4", - "sha256:a9e3b8011388e7e373565daa5e92f6c9cb844790dc18e43073212bb3e76f7007", - "sha256:afb53edf1046599991fb4a7d03e601ab5f5422a5435c47ee6ba91ec3b61416a6", - "sha256:b26719890c79a1dae7d53acac5f089d66fd8cc68a81f4e4bd355e45470dc25e1", - "sha256:b7462cdab6fffcda853338e1741ce99706cdf880d921b5a769202ea7b94e8528", - "sha256:b77975465234ff49fdad871c08aa747aae06f5e5be62866595057c43f8d2f62c", - "sha256:c47a8a5d00060122ca5908909478abce7bbf62d812e3fc35c6c802df8fb01fe7", - "sha256:c79e5debbe092e3c93ca4aee44c9a7631bdd407b2871cb541b979fd350bbbc29", - "sha256:d8d40e0121ca1606aa9e78c28a3a7d88a05c06b3ca61630242cded87d8ce55fa", - "sha256:ee2be8b8f72a2772e72ab926a3bccebf47bb727bda41ae070dc91d1fb759b726", - "sha256:f95d28193c3863132b1f55c1056036bf580b5a488d908f7d22a04ace8935a3a9", - "sha256:fadd2a63a2bfd7fb604508e553d1cf68eca250b2fbdbd81213b5f6f2fbf23529" - ], - "index": "pypi", - "version": "==4.5.1" + "sha256:05a444b207901a68a6526948c7cc8f9fe6d6f24c70781488e32fd74ff5996e3f", + "sha256:08fc93257dcfe9542c0a6883a25ba4971d78297f63d7a5a26ffa34861ca78730", + "sha256:107781b213cf7201ec3806555657ccda67b1fccc4261fb889ef7fc56976db81f", + "sha256:121b665b04083a1e85ff1f5243d4a93aa1aaba281bc12ea334d5a187278ceaf1", + "sha256:1fa21263c3aba2b76fd7c45713d4428dbcc7644d73dcf0650e9d344e433741b3", + "sha256:2b30aa2bcff8e958cd85d907d5109820b01ac511eae5b460803430a7404e34d7", + "sha256:4b4a111bcf4b9c948e020fd207f915c24a6de3f1adc7682a2d92660eb4e84f1a", + "sha256:5591c4164755778e29e69b86e425880f852464a21c7bb53c7ea453bbe2633bbe", + "sha256:59daa84aef650b11bccd18f99f64bfe44b9f14a08a28259959d33676554065a1", + "sha256:5a9c8d11aa2c8f8b6043d845927a51eb9102eb558e3f936df494e96393f5fd3e", + "sha256:5dd20538a60c4cc9a077d3b715bb42307239fcd25ef1ca7286775f95e9e9a46d", + "sha256:74f48ec98430e06c1fa8949b49ebdd8d27ceb9df8d3d1c92e1fdc2773f003f20", + "sha256:786aad2aa20de3dbff21aab86b2fb6a7be68064cbbc0219bde414d3a30aa47ae", + "sha256:7ad7906e098ccd30d8f7068030a0b16668ab8aa5cda6fcd5146d8d20cbaa71b5", + "sha256:80a38b188d20c0524fe8959c8ce770a8fdf0e617c6912d23fc97c68301bb9aba", + "sha256:8f0ec6b9b3832e0bd1d57af41f9238ea7709bbd7271f639024f2fc9d3bb01293", + "sha256:92282c83547a9add85ad658143c76a64a8d339028926d7dc1998ca029c88ea6a", + "sha256:94150231f1e90c9595ccc80d7d2006c61f90a5995db82bccbca7944fd457f0f6", + "sha256:9dc9006dcc47e00a8a6a029eb035c8f696ad38e40a27d073a003d7d1443f5d88", + "sha256:a76979f728dd845655026ab991df25d26379a1a8fc1e9e68e25c7eda43004bed", + "sha256:aa8eba3db3d8761db161003e2d0586608092e217151d7458206e243be5a43843", + "sha256:bea760a63ce9bba566c23f726d72b3c0250e2fa2569909e2d83cda1534c79443", + "sha256:c3f511a3c58676147c277eff0224c061dd5a6a8e1373572ac817ac6324f1b1e0", + "sha256:c9d317efde4bafbc1561509bfa8a23c5cab66c44d49ab5b63ff690f5159b2304", + "sha256:cc411ad324a4486b142c41d9b2b6a722c534096963688d879ea6fa8a35028258", + "sha256:cdc13a1682b2a6241080745b1953719e7fe0850b40a5c71ca574f090a1391df6", + "sha256:cfd7c5dd3c35c19cec59c63df9571c67c6d6e5c92e0fe63517920e97f61106d1", + "sha256:e1cacf4796b20865789083252186ce9dc6cc59eca0c2e79cca332bdff24ac481", + "sha256:e70d4e467e243455492f5de463b72151cc400710ac03a0678206a5f27e79ddef", + "sha256:ecc930ae559ea8a43377e8b60ca6f8d61ac532fc57efb915d899de4a67928efd", + "sha256:f161af26f596131b63b236372e4ce40f3167c1b5b5d459b29d2514bd8c9dc9ee" + ], + "index": "pypi", + "version": "==4.5.2" }, "markdownify": { "hashes": [ @@ -532,11 +542,11 @@ }, "sentry-sdk": { "hashes": [ - "sha256:da06bc3641e81ec2c942f87a0676cd9180044fa3d1697524a0005345997542e2", - "sha256:e80d61af85d99a1222c1a3e2a24023618374cd50a99673aa7fa3cf920e7d813b" + "sha256:2de15b13836fa3522815a933bd9c887c77f4868071043349f94f1b896c1bcfb8", + "sha256:38bb09d0277117f76507c8728d9a5156f09a47ac5175bb8072513859d19a593b" ], "index": "pypi", - "version": "==0.16.0" + "version": "==0.16.2" }, "six": { "hashes": [ @@ -632,13 +642,21 @@ "index": "pypi", "version": "==3.3.0" }, + "typing-extensions": { + "hashes": [ + "sha256:6e95524d8a547a91e08f404ae485bbb71962de46967e1b71a0cb89af24e761c5", + "sha256:79ee589a3caca649a9bfd2a8de4709837400dfa00b6cc81962a1e6a1815969ae", + "sha256:f8d2bd89d25bc39dabe7d23df520442fa1d8969b82544370e03d88b5a591c392" + ], + "version": "==3.7.4.2" + }, "urllib3": { "hashes": [ - "sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527", - "sha256:88206b0eb87e6d677d424843ac5209e3fb9d0190d0ee169599165ec25e9d9115" + "sha256:91056c15fa70756691db97756772bb1eb9678fa585d9184f24534b100dc60f4a", + "sha256:e7983572181f5e1522d9c98453462384ee92a0be7fac5f1413a1e35c56cc0461" ], "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'", - "version": "==1.25.9" + "version": "==1.25.10" }, "websockets": { "hashes": [ @@ -670,26 +688,26 @@ }, "yarl": { "hashes": [ - "sha256:0c2ab325d33f1b824734b3ef51d4d54a54e0e7a23d13b86974507602334c2cce", - "sha256:0ca2f395591bbd85ddd50a82eb1fde9c1066fafe888c5c7cc1d810cf03fd3cc6", - "sha256:2098a4b4b9d75ee352807a95cdf5f10180db903bc5b7270715c6bbe2551f64ce", - "sha256:25e66e5e2007c7a39541ca13b559cd8ebc2ad8fe00ea94a2aad28a9b1e44e5ae", - "sha256:26d7c90cb04dee1665282a5d1a998defc1a9e012fdca0f33396f81508f49696d", - "sha256:308b98b0c8cd1dfef1a0311dc5e38ae8f9b58349226aa0533f15a16717ad702f", - "sha256:3ce3d4f7c6b69c4e4f0704b32eca8123b9c58ae91af740481aa57d7857b5e41b", - "sha256:58cd9c469eced558cd81aa3f484b2924e8897049e06889e8ff2510435b7ef74b", - "sha256:5b10eb0e7f044cf0b035112446b26a3a2946bca9d7d7edb5e54a2ad2f6652abb", - "sha256:6faa19d3824c21bcbfdfce5171e193c8b4ddafdf0ac3f129ccf0cdfcb083e462", - "sha256:944494be42fa630134bf907714d40207e646fd5a94423c90d5b514f7b0713fea", - "sha256:a161de7e50224e8e3de6e184707476b5a989037dcb24292b391a3d66ff158e70", - "sha256:a4844ebb2be14768f7994f2017f70aca39d658a96c786211be5ddbe1c68794c1", - "sha256:c2b509ac3d4b988ae8769901c66345425e361d518aecbe4acbfc2567e416626a", - "sha256:c9959d49a77b0e07559e579f38b2f3711c2b8716b8410b320bf9713013215a1b", - "sha256:d8cdee92bc930d8b09d8bd2043cedd544d9c8bd7436a77678dd602467a993080", - "sha256:e15199cdb423316e15f108f51249e44eb156ae5dba232cb73be555324a1d49c2" + "sha256:1707230e1ea48ea06a3e20acb4ce05a38d2465bd9566c21f48f6212a88e47536", + "sha256:1f269e8e6676193a94635399a77c9059e1826fb6265c9204c9e5a8ccd36006e1", + "sha256:2657716c1fc998f5f2675c0ee6ce91282e0da0ea9e4a94b584bb1917e11c1559", + "sha256:431faa6858f0ea323714d8b7b4a7da1db2eeb9403607f0eaa3800ab2c5a4b627", + "sha256:5bbcb195da7de57f4508b7508c33f7593e9516e27732d08b9aad8586c7b8c384", + "sha256:5c82f5b1499342339f22c83b97dbe2b8a09e47163fab86cd934a8dd46620e0fb", + "sha256:5d410f69b4f92c5e1e2a8ffb73337cd8a274388c6975091735795588a538e605", + "sha256:66b4f345e9573e004b1af184bc00431145cf5e089a4dcc1351505c1f5750192c", + "sha256:875b2a741ce0208f3b818008a859ab5d0f461e98a32bbdc6af82231a9e761c55", + "sha256:9a3266b047d15e78bba38c8455bf68b391c040231ca5965ef867f7cbbc60bde5", + "sha256:9a592c4aa642249e9bdaf76897d90feeb08118626b363a6be8788a9b300274b5", + "sha256:a1772068401d425e803999dada29a6babf041786e08be5e79ef63c9ecc4c9575", + "sha256:b065a5c3e050395ae563019253cc6c769a50fd82d7fa92d07476273521d56b7c", + "sha256:b325fefd574ebef50e391a1072d1712a60348ca29c183e1d546c9d87fec2cd32", + "sha256:cf5eb664910d759bbae0b76d060d6e21f8af5098242d66c448bbebaf2a7bfa70", + "sha256:f058b6541477022c7b54db37229f87dacf3b565de4f901ff5a0a78556a174fea", + "sha256:f5cfed0766837303f688196aa7002730d62c5cc802d98c6395ea1feb87252727" ], "markers": "python_version >= '3.5'", - "version": "==1.4.2" + "version": "==1.5.0" } }, "develop": { @@ -718,43 +736,43 @@ }, "coverage": { "hashes": [ - "sha256:0fc4e0d91350d6f43ef6a61f64a48e917637e1dcfcba4b4b7d543c628ef82c2d", - "sha256:10f2a618a6e75adf64329f828a6a5b40244c1c50f5ef4ce4109e904e69c71bd2", - "sha256:12eaccd86d9a373aea59869bc9cfa0ab6ba8b1477752110cb4c10d165474f703", - "sha256:1874bdc943654ba46d28f179c1846f5710eda3aeb265ff029e0ac2b52daae404", - "sha256:1dcebae667b73fd4aa69237e6afb39abc2f27520f2358590c1b13dd90e32abe7", - "sha256:1e58fca3d9ec1a423f1b7f2aa34af4f733cbfa9020c8fe39ca451b6071237405", - "sha256:214eb2110217f2636a9329bc766507ab71a3a06a8ea30cdeebb47c24dce5972d", - "sha256:25fe74b5b2f1b4abb11e103bb7984daca8f8292683957d0738cd692f6a7cc64c", - "sha256:32ecee61a43be509b91a526819717d5e5650e009a8d5eda8631a59c721d5f3b6", - "sha256:3740b796015b889e46c260ff18b84683fa2e30f0f75a171fb10d2bf9fb91fc70", - "sha256:3b2c34690f613525672697910894b60d15800ac7e779fbd0fccf532486c1ba40", - "sha256:41d88736c42f4a22c494c32cc48a05828236e37c991bd9760f8923415e3169e4", - "sha256:42fa45a29f1059eda4d3c7b509589cc0343cd6bbf083d6118216830cd1a51613", - "sha256:4bb385a747e6ae8a65290b3df60d6c8a692a5599dc66c9fa3520e667886f2e10", - "sha256:509294f3e76d3f26b35083973fbc952e01e1727656d979b11182f273f08aa80b", - "sha256:5c74c5b6045969b07c9fb36b665c9cac84d6c174a809fc1b21bdc06c7836d9a0", - "sha256:60a3d36297b65c7f78329b80120f72947140f45b5c7a017ea730f9112b40f2ec", - "sha256:6f91b4492c5cde83bfe462f5b2b997cdf96a138f7c58b1140f05de5751623cf1", - "sha256:7403675df5e27745571aba1c957c7da2dacb537c21e14007ec3a417bf31f7f3d", - "sha256:87bdc8135b8ee739840eee19b184804e5d57f518578ffc797f5afa2c3c297913", - "sha256:8a3decd12e7934d0254939e2bf434bf04a5890c5bf91a982685021786a08087e", - "sha256:9702e2cb1c6dec01fb8e1a64c015817c0800a6eca287552c47a5ee0ebddccf62", - "sha256:a4d511012beb967a39580ba7d2549edf1e6865a33e5fe51e4dce550522b3ac0e", - "sha256:bbb387811f7a18bdc61a2ea3d102be0c7e239b0db9c83be7bfa50f095db5b92a", - "sha256:bfcc811883699ed49afc58b1ed9f80428a18eb9166422bce3c31a53dba00fd1d", - "sha256:c32aa13cc3fe86b0f744dfe35a7f879ee33ac0a560684fef0f3e1580352b818f", - "sha256:ca63dae130a2e788f2b249200f01d7fa240f24da0596501d387a50e57aa7075e", - "sha256:d54d7ea74cc00482a2410d63bf10aa34ebe1c49ac50779652106c867f9986d6b", - "sha256:d67599521dff98ec8c34cd9652cbcfe16ed076a2209625fca9dc7419b6370e5c", - "sha256:d82db1b9a92cb5c67661ca6616bdca6ff931deceebb98eecbd328812dab52032", - "sha256:d9ad0a988ae20face62520785ec3595a5e64f35a21762a57d115dae0b8fb894a", - "sha256:ebf2431b2d457ae5217f3a1179533c456f3272ded16f8ed0b32961a6d90e38ee", - "sha256:ed9a21502e9223f563e071759f769c3d6a2e1ba5328c31e86830368e8d78bc9c", - "sha256:f50632ef2d749f541ca8e6c07c9928a37f87505ce3a9f20c8446ad310f1aa87b" - ], - "index": "pypi", - "version": "==5.2" + "sha256:098a703d913be6fbd146a8c50cc76513d726b022d170e5e98dc56d958fd592fb", + "sha256:16042dc7f8e632e0dcd5206a5095ebd18cb1d005f4c89694f7f8aafd96dd43a3", + "sha256:1adb6be0dcef0cf9434619d3b892772fdb48e793300f9d762e480e043bd8e716", + "sha256:27ca5a2bc04d68f0776f2cdcb8bbd508bbe430a7bf9c02315cd05fb1d86d0034", + "sha256:28f42dc5172ebdc32622a2c3f7ead1b836cdbf253569ae5673f499e35db0bac3", + "sha256:2fcc8b58953d74d199a1a4d633df8146f0ac36c4e720b4a1997e9b6327af43a8", + "sha256:304fbe451698373dc6653772c72c5d5e883a4aadaf20343592a7abb2e643dae0", + "sha256:30bc103587e0d3df9e52cd9da1dd915265a22fad0b72afe54daf840c984b564f", + "sha256:40f70f81be4d34f8d491e55936904db5c527b0711b2a46513641a5729783c2e4", + "sha256:4186fc95c9febeab5681bc3248553d5ec8c2999b8424d4fc3a39c9cba5796962", + "sha256:46794c815e56f1431c66d81943fa90721bb858375fb36e5903697d5eef88627d", + "sha256:4869ab1c1ed33953bb2433ce7b894a28d724b7aa76c19b11e2878034a4e4680b", + "sha256:4f6428b55d2916a69f8d6453e48a505c07b2245653b0aa9f0dee38785939f5e4", + "sha256:52f185ffd3291196dc1aae506b42e178a592b0b60a8610b108e6ad892cfc1bb3", + "sha256:538f2fd5eb64366f37c97fdb3077d665fa946d2b6d95447622292f38407f9258", + "sha256:64c4f340338c68c463f1b56e3f2f0423f7b17ba6c3febae80b81f0e093077f59", + "sha256:675192fca634f0df69af3493a48224f211f8db4e84452b08d5fcebb9167adb01", + "sha256:700997b77cfab016533b3e7dbc03b71d33ee4df1d79f2463a318ca0263fc29dd", + "sha256:8505e614c983834239f865da2dd336dcf9d72776b951d5dfa5ac36b987726e1b", + "sha256:962c44070c281d86398aeb8f64e1bf37816a4dfc6f4c0f114756b14fc575621d", + "sha256:9e536783a5acee79a9b308be97d3952b662748c4037b6a24cbb339dc7ed8eb89", + "sha256:9ea749fd447ce7fb1ac71f7616371f04054d969d412d37611716721931e36efd", + "sha256:a34cb28e0747ea15e82d13e14de606747e9e484fb28d63c999483f5d5188e89b", + "sha256:a3ee9c793ffefe2944d3a2bd928a0e436cd0ac2d9e3723152d6fd5398838ce7d", + "sha256:aab75d99f3f2874733946a7648ce87a50019eb90baef931698f96b76b6769a46", + "sha256:b1ed2bdb27b4c9fc87058a1cb751c4df8752002143ed393899edb82b131e0546", + "sha256:b360d8fd88d2bad01cb953d81fd2edd4be539df7bfec41e8753fe9f4456a5082", + "sha256:b8f58c7db64d8f27078cbf2a4391af6aa4e4767cc08b37555c4ae064b8558d9b", + "sha256:c1bbb628ed5192124889b51204de27c575b3ffc05a5a91307e7640eff1d48da4", + "sha256:c2ff24df02a125b7b346c4c9078c8936da06964cc2d276292c357d64378158f8", + "sha256:c890728a93fffd0407d7d37c1e6083ff3f9f211c83b4316fae3778417eab9811", + "sha256:c96472b8ca5dc135fb0aa62f79b033f02aa434fb03a8b190600a5ae4102df1fd", + "sha256:ce7866f29d3025b5b34c2e944e66ebef0d92e4a4f2463f7266daa03a1332a651", + "sha256:e26c993bd4b220429d4ec8c1468eca445a4064a61c74ca08da7429af9bc53bb0" + ], + "index": "pypi", + "version": "==5.2.1" }, "distlib": { "hashes": [ @@ -780,11 +798,11 @@ }, "flake8-annotations": { "hashes": [ - "sha256:babc81a17a5f1a63464195917e20d3e8663fb712b3633d4522dbfc407cff31b3", - "sha256:fcd833b415726a7a374922c95a5c47a7a4d8ea71cb4a586369c665e7476146e1" + "sha256:7816a5d8f65ffdf37b8e21e5b17e0fd1e492aa92638573276de066e889a22b26", + "sha256:8d18db74a750dd97f40b483cc3ef80d07d03f687525bad8fd83365dcd3bfd414" ], "index": "pypi", - "version": "==2.2.0" + "version": "==2.3.0" }, "flake8-bugbear": { "hashes": [ @@ -842,11 +860,11 @@ }, "identify": { "hashes": [ - "sha256:c4d07f2b979e3931894170a9e0d4b8281e6905ea6d018c326f7ffefaf20db680", - "sha256:dac33eff90d57164e289fb20bf4e131baef080947ee9bf45efcd0da8d19064bf" + "sha256:110ed090fec6bce1aabe3c72d9258a9de82207adeaa5a05cd75c635880312f9a", + "sha256:ccd88716b890ecbe10920659450a635d2d25de499b9a638525a48b48261d989b" ], "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", - "version": "==1.4.21" + "version": "==1.4.25" }, "mccabe": { "hashes": [ @@ -950,11 +968,11 @@ }, "virtualenv": { "hashes": [ - "sha256:c11a475400e98450403c0364eb3a2d25d42f71cf1493da64390487b666de4324", - "sha256:e10cc66f40cbda459720dfe1d334c4dc15add0d80f09108224f171006a97a172" + "sha256:688a61d7976d82b92f7906c367e83bb4b3f0af96f8f75bfcd3da95608fe8ac6c", + "sha256:8f582a030156282a9ee9d319984b759a232b07f86048c1d6a9e394afa44e78c8" ], "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", - "version": "==20.0.26" + "version": "==20.0.28" } } } -- cgit v1.2.3 From 71b9ab4e1e9d75e9240bc0c6825f43d978ef922b Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> Date: Mon, 27 Jul 2020 12:18:25 +0200 Subject: Update IDs of Code Jam roles I've updated the IDs of the two Code Jam Roles to the newly create roles we have. --- config-default.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config-default.yml b/config-default.yml index d0262be33..fc093cc32 100644 --- a/config-default.yml +++ b/config-default.yml @@ -236,8 +236,8 @@ guild: owners: &OWNERS_ROLE 267627879762755584 # Code Jam - jammers: 591786436651646989 - team_leaders: 501324292341104650 + jammers: 737249140966162473 + team_leaders: 737250302834638889 moderation_roles: - *OWNERS_ROLE -- cgit v1.2.3 From 1c6e2f23a9b75be5e2a7e410c70fadfbf6c6b090 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Tue, 28 Jul 2020 16:04:10 +0800 Subject: Allow specifying a channel to send !embed embeds --- bot/cogs/bot.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index a79b37d25..79510739c 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -72,10 +72,14 @@ class BotCog(Cog, name="Bot"): @command(name='embed') @with_role(*MODERATION_ROLES) - async def embed_command(self, ctx: Context, *, text: str) -> None: - """Send the input within an embed to the current channel.""" + async def embed_command(self, ctx: Context, channel: Optional[TextChannel], *, text: str) -> None: + """Send the input within an embed to either a specified channel or the current channel.""" embed = Embed(description=text) - await ctx.send(embed=embed) + + if channel is None: + await ctx.send(embed=embed) + else: + await channel.send(embed=embed) def codeblock_stripping(self, msg: str, bad_ticks: bool) -> Optional[Tuple[Tuple[str, ...], str]]: """ -- cgit v1.2.3 From 3fa1a0d03446500246f94df74fc3e3f86afe91ca Mon Sep 17 00:00:00 2001 From: Senjan21 <53477086+Senjan21@users.noreply.github.com> Date: Tue, 28 Jul 2020 10:22:39 +0200 Subject: fix poll command by using clean_content converter --- bot/cogs/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/utils.py b/bot/cogs/utils.py index 017f3419e..11b8e3e5e 100644 --- a/bot/cogs/utils.py +++ b/bot/cogs/utils.py @@ -7,7 +7,7 @@ from io import StringIO from typing import Tuple, Union from discord import Colour, Embed, utils -from discord.ext.commands import BadArgument, Cog, Context, command +from discord.ext.commands import BadArgument, Cog, Context, command, clean_content from bot.bot import Bot from bot.constants import Channels, MODERATION_ROLES, STAFF_ROLES @@ -225,7 +225,7 @@ class Utils(Cog): @command(aliases=("poll",)) @with_role(*MODERATION_ROLES) - async def vote(self, ctx: Context, title: str, *options: str) -> None: + async def vote(self, ctx: Context, title: clean_content(fix_channel_mentions=True), *options: str) -> None: """ Build a quick voting poll with matching reactions with the provided options. -- cgit v1.2.3 From 8be7126e17e1fa9f671ab8acc52cb6d495084bd1 Mon Sep 17 00:00:00 2001 From: Senjan21 <53477086+Senjan21@users.noreply.github.com> Date: Tue, 28 Jul 2020 10:30:00 +0200 Subject: correct import order --- bot/cogs/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/utils.py b/bot/cogs/utils.py index 11b8e3e5e..91c6cb36e 100644 --- a/bot/cogs/utils.py +++ b/bot/cogs/utils.py @@ -7,7 +7,7 @@ from io import StringIO from typing import Tuple, Union from discord import Colour, Embed, utils -from discord.ext.commands import BadArgument, Cog, Context, command, clean_content +from discord.ext.commands import BadArgument, Cog, Context, clean_content, command from bot.bot import Bot from bot.constants import Channels, MODERATION_ROLES, STAFF_ROLES -- cgit v1.2.3 From f3fb8190c7c9541146ed79df2b0ad906fc067414 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 28 Jul 2020 21:24:25 +0300 Subject: Handle message unpinning better --- bot/cogs/help_channels.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index b06934eff..2c53069f0 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -556,8 +556,11 @@ class HelpChannels(Scheduler, commands.Cog): try: await self.bot.http.unpin_message(channel.id, msg_id) - except discord.HTTPException: - log.trace(f"Message {msg_id} don't exist, can't unpin.") + except discord.HTTPException as e: + if e.code == 10008: + log.trace(f"Message {msg_id} don't exist, can't unpin.") + else: + log.warn(f"Got unexpected status {e.code} when unpinning message {msg_id}: {e.text}") else: log.trace(f"Unpinned message {msg_id}.") -- cgit v1.2.3 From a4e5044596492fe56f2c2d36468f126907602b98 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 13:18:06 +0200 Subject: Don't ping everyone when tripping filter in DMs. We don't need a ping in #mod-alerts whenever someone is tripping a filter (like invites or bad language) in a DM to the bot. We can still send an embed, so that we can action it, but there is no urgent need to respond if it's just a direct message to the bot. This is particularly true now that we have #dm-log. --- bot/cogs/filtering.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index bd665f424..29aac812f 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -339,7 +339,7 @@ class Filtering(Cog): text=message, thumbnail=msg.author.avatar_url_as(static_format="png"), channel_id=Channels.mod_alerts, - ping_everyone=Filter.ping_everyone, + ping_everyone=Filter.ping_everyone if not is_private else False, additional_embeds=additional_embeds, additional_embeds_msg=additional_embeds_msg ) -- cgit v1.2.3 From 1b6be865eddeab9199177d74ec07f8cd22051ca4 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 14:17:44 +0200 Subject: Expect status 400 for duplicates. --- bot/cogs/filter_lists.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index d1db9830e..9bd2da330 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -64,10 +64,10 @@ class FilterLists(Cog): json=payload ) except ResponseCodeError as e: - if e.status == 500: + if e.status == 400: await ctx.message.add_reaction("❌") log.debug( - f"{ctx.author} tried to add data to a {allow_type}, but the API returned 500, " + f"{ctx.author} tried to add data to a {allow_type}, but the API returned 400, " "probably because the request violated the UniqueConstraint." ) raise BadArgument( -- cgit v1.2.3 From 222cdce0b9771b0c121da39b5f38363baf8bce09 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 14:25:00 +0200 Subject: Use a defaultdict(list) for filter_list_cache. --- bot/bot.py | 5 +++-- bot/cogs/filter_lists.py | 6 +++--- bot/cogs/filtering.py | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 3dfb4e948..a309e7192 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -2,6 +2,7 @@ import asyncio import logging import socket import warnings +from collections import defaultdict from typing import Optional import aiohttp @@ -34,7 +35,7 @@ class Bot(commands.Bot): self.redis_ready = asyncio.Event() self.redis_closed = False self.api_client = api.APIClient(loop=self.loop) - self.filter_list_cache = {} + self.filter_list_cache = defaultdict(list) self._connector = None self._resolver = None @@ -64,7 +65,7 @@ class Bot(commands.Bot): "created_at": item.get("created_at"), "updated_at": item.get("updated_at"), } - self.filter_list_cache.setdefault(f"{type_}.{allowed}", []).append(metadata) + self.filter_list_cache[f"{type_}.{allowed}"].append(metadata) async def _create_redis_session(self) -> None: """ diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index 9bd2da330..63d74e421 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -88,7 +88,7 @@ class FilterLists(Cog): "created_at": item.get("created_at"), "updated_at": item.get("updated_at"), } - self.bot.filter_list_cache.setdefault(f"{type_}.{allowed}", []).append(metadata) + self.bot.filter_list_cache[f"{type_}.{allowed}"].append(metadata) await ctx.message.add_reaction("✅") async def _delete_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType, content: str) -> None: @@ -110,7 +110,7 @@ class FilterLists(Cog): # Find the content and delete it. log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}") - for allow_list in self.bot.filter_list_cache.get(f"{list_type}.{allowed}", []): + for allow_list in self.bot.filter_list_cache[f"{list_type}.{allowed}"]: if content == allow_list.get("content"): item = allow_list break @@ -125,7 +125,7 @@ class FilterLists(Cog): async def _list_all_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType) -> None: """Paginate and display all items in a filterlist.""" allow_type = "whitelist" if allowed else "blacklist" - result = self.bot.filter_list_cache.get(f"{list_type}.{allowed}", []) + result = self.bot.filter_list_cache[f"{list_type}.{allowed}"] # Build a list of lines we want to show in the paginator lines = [] diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 652af5ff5..9f9bcc464 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -101,7 +101,7 @@ class Filtering(Cog): def _get_filterlist_items(self, list_type: str, *, allowed: bool, compiled: Optional[bool] = False) -> list: """Fetch items from the filter_list_cache.""" - items = self.bot.filter_list_cache.get(f"{list_type.upper()}.{allowed}", []) + items = self.bot.filter_list_cache[f"{list_type.upper()}.{allowed}"] if compiled: return [re.compile(fr'{item["content"]}', flags=re.IGNORECASE) for item in items] -- cgit v1.2.3 From 1a0b2938dab931b6dc482a6b7a4b17549e0cf36f Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 14:30:48 +0200 Subject: Kaizen - group private methods together. --- bot/bot.py | 86 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index a309e7192..3da5c0bb8 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -91,6 +91,49 @@ class Bot(commands.Bot): self.redis_closed = False self.redis_ready.set() + def _recreate(self) -> None: + """Re-create the connector, aiohttp session, the APIClient and the Redis session.""" + # Use asyncio for DNS resolution instead of threads so threads aren't spammed. + # Doesn't seem to have any state with regards to being closed, so no need to worry? + self._resolver = aiohttp.AsyncResolver() + + # Its __del__ does send a warning but it doesn't always show up for some reason. + if self._connector and not self._connector._closed: + log.warning( + "The previous connector was not closed; it will remain open and be overwritten" + ) + + if self.redis_session and not self.redis_session.closed: + log.warning( + "The previous redis pool was not closed; it will remain open and be overwritten" + ) + + # Create the redis session + self.loop.create_task(self._create_redis_session()) + + # Use AF_INET as its socket family to prevent HTTPS related problems both locally + # and in production. + self._connector = aiohttp.TCPConnector( + resolver=self._resolver, + family=socket.AF_INET, + ) + + # Client.login() will call HTTPClient.static_login() which will create a session using + # this connector attribute. + self.http.connector = self._connector + + # Its __del__ does send a warning but it doesn't always show up for some reason. + if self.http_session and not self.http_session.closed: + log.warning( + "The previous session was not closed; it will remain open and be overwritten" + ) + + self.http_session = aiohttp.ClientSession(connector=self._connector) + self.api_client.recreate(force=True, connector=self._connector) + + # Build the FilterList cache + self.loop.create_task(self._cache_filter_list_data()) + def add_cog(self, cog: commands.Cog) -> None: """Adds a "cog" to the bot and logs the operation.""" super().add_cog(cog) @@ -137,49 +180,6 @@ class Bot(commands.Bot): await self.stats.create_socket() await super().login(*args, **kwargs) - def _recreate(self) -> None: - """Re-create the connector, aiohttp session, the APIClient and the Redis session.""" - # Use asyncio for DNS resolution instead of threads so threads aren't spammed. - # Doesn't seem to have any state with regards to being closed, so no need to worry? - self._resolver = aiohttp.AsyncResolver() - - # Its __del__ does send a warning but it doesn't always show up for some reason. - if self._connector and not self._connector._closed: - log.warning( - "The previous connector was not closed; it will remain open and be overwritten" - ) - - if self.redis_session and not self.redis_session.closed: - log.warning( - "The previous redis pool was not closed; it will remain open and be overwritten" - ) - - # Create the redis session - self.loop.create_task(self._create_redis_session()) - - # Use AF_INET as its socket family to prevent HTTPS related problems both locally - # and in production. - self._connector = aiohttp.TCPConnector( - resolver=self._resolver, - family=socket.AF_INET, - ) - - # Client.login() will call HTTPClient.static_login() which will create a session using - # this connector attribute. - self.http.connector = self._connector - - # Its __del__ does send a warning but it doesn't always show up for some reason. - if self.http_session and not self.http_session.closed: - log.warning( - "The previous session was not closed; it will remain open and be overwritten" - ) - - self.http_session = aiohttp.ClientSession(connector=self._connector) - self.api_client.recreate(force=True, connector=self._connector) - - # Build the FilterList cache - self.loop.create_task(self._cache_filter_list_data()) - async def on_guild_available(self, guild: discord.Guild) -> None: """ Set the internal guild available event when constants.Guild.id becomes available. -- cgit v1.2.3 From a23b273734ddee5fd082bda4fa14aebfff1317ca Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 14:39:05 +0200 Subject: Make a helper for inserting filter lists. --- bot/bot.py | 26 +++++++++++++++----------- bot/cogs/filter_lists.py | 11 +---------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 3da5c0bb8..203b35ba0 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -3,7 +3,7 @@ import logging import socket import warnings from collections import defaultdict -from typing import Optional +from typing import Any, Dict, Optional import aiohttp import aioredis @@ -56,16 +56,7 @@ class Bot(commands.Bot): full_cache = await self.api_client.get('bot/filter-lists') for item in full_cache: - type_ = item.get("type") - allowed = item.get("allowed") - metadata = { - "content": item.get("content"), - "comment": item.get("comment"), - "id": item.get("id"), - "created_at": item.get("created_at"), - "updated_at": item.get("updated_at"), - } - self.filter_list_cache[f"{type_}.{allowed}"].append(metadata) + self.insert_item_into_filter_list_cache(item) async def _create_redis_session(self) -> None: """ @@ -174,6 +165,19 @@ class Bot(commands.Bot): self.redis_ready.clear() await self.redis_session.wait_closed() + def insert_item_into_filter_list_cache(self, item: Dict[Any]) -> None: + """Add an item to the bots filter_list_cache.""" + type_ = item["type"] + allowed = item["allowed"] + metadata = { + "id": item["id"], + "content": item["content"], + "comment": item["comment"], + "created_at": item["created_at"], + "updated_at": item["updated_at"], + } + self.filter_list_cache[f"{type_}.{allowed}"].append(metadata) + async def login(self, *args, **kwargs) -> None: """Re-create the connector and set up sessions before logging into Discord.""" self._recreate() diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index 63d74e421..e0d057595 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -79,16 +79,7 @@ class FilterLists(Cog): raise # Insert the item into the cache - type_ = item.get("type") - allowed = item.get("allowed") - metadata = { - "content": item.get("content"), - "comment": item.get("comment"), - "id": item.get("id"), - "created_at": item.get("created_at"), - "updated_at": item.get("updated_at"), - } - self.bot.filter_list_cache[f"{type_}.{allowed}"].append(metadata) + self.bot.insert_item_into_filter_list_cache(item) await ctx.message.add_reaction("✅") async def _delete_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType, content: str) -> None: -- cgit v1.2.3 From a7a3e29ca901b84570e5a1ff1e4c2bcf22b86552 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 14:55:59 +0200 Subject: Make a helper for validating guild invites. --- bot/cogs/filter_lists.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index e0d057595..a93de2de9 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -33,13 +33,7 @@ class FilterLists(Cog): # If this is a server invite, we gotta validate it. if list_type == "GUILD_INVITE": - log.trace(f"{content} is a guild invite, attempting to validate.") - validator = ValidDiscordServerInvite() - guild_data = await validator.convert(ctx, content) - - # If we make it this far without raising a BadArgument, the invite is - # valid. Let's convert the content to an ID. - log.trace(f"{content} validated as server invite. Converting to ID.") + guild_data = await self._validate_guild_invite(ctx, content) content = guild_data.get("id") # Unless the user has specified another comment, let's @@ -86,17 +80,10 @@ class FilterLists(Cog): """Remove an item from a filterlist.""" item = None allow_type = "whitelist" if allowed else "blacklist" - id_converter = IDConverter() # If this is a server invite, we need to convert it. - if list_type == "GUILD_INVITE" and not id_converter._get_id_match(content): - log.trace(f"{content} is a guild invite, attempting to validate.") - validator = ValidDiscordServerInvite() - guild_data = await validator.convert(ctx, content) - - # If we make it this far without raising a BadArgument, the invite is - # valid. Let's convert the content to an ID. - log.trace(f"{content} validated as server invite. Converting to ID.") + if list_type == "GUILD_INVITE" and not IDConverter()._get_id_match(content): + guild_data = await self._validate_guild_invite(ctx, content) content = guild_data.get("id") # Find the content and delete it. @@ -143,6 +130,21 @@ class FilterLists(Cog): embed.description = "Hmmm, seems like there's nothing here yet." await ctx.send(embed=embed) + async def _validate_guild_invite(self, ctx: Context, invite: str) -> dict: + """ + Validates a guild invite, and returns the guild info as a dict. + + Will raise a BadArgument if the guild invite is invalid. + """ + log.trace(f"Attempting to validate whether or not {invite} is a guild invite.") + validator = ValidDiscordServerInvite() + guild_data = await validator.convert(ctx, invite) + + # If we make it this far without raising a BadArgument, the invite is + # valid. Let's return a dict of guild information. + log.trace(f"{invite} validated as server invite. Converting to ID.") + return guild_data + @group(aliases=("allowlist", "allow", "al", "wl")) async def whitelist(self, ctx: Context) -> None: """Group for whitelisting commands.""" -- cgit v1.2.3 From 4e1609695762524bc707b6a8d39e88c2710cff6b Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 15:03:15 +0200 Subject: Refactor filtering: use non-compiled expressions. --- bot/cogs/filtering.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 9f9bcc464..7787d396d 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -99,14 +99,10 @@ class Filtering(Cog): self.bot.loop.create_task(self.reschedule_offensive_msg_deletion()) - def _get_filterlist_items(self, list_type: str, *, allowed: bool, compiled: Optional[bool] = False) -> list: + def _get_filterlist_items(self, list_type: str, *, allowed: bool) -> list: """Fetch items from the filter_list_cache.""" items = self.bot.filter_list_cache[f"{list_type.upper()}.{allowed}"] - - if compiled: - return [re.compile(fr'{item["content"]}', flags=re.IGNORECASE) for item in items] - else: - return [item["content"] for item in items] + return [item["content"] for item in items] @staticmethod def _expand_spoilers(text: str) -> str: @@ -143,9 +139,9 @@ class Filtering(Cog): def get_name_matches(self, name: str) -> List[re.Match]: """Check bad words from passed string (name). Return list of matches.""" matches = [] - watchlist_patterns = self._get_filterlist_items('word_watchlist', allowed=False, compiled=True) + watchlist_patterns = self._get_filterlist_items('word_watchlist', allowed=False) for pattern in watchlist_patterns: - if match := pattern.search(name): + if match := re.search(pattern, name, flags=re.IGNORECASE): matches.append(match) return matches @@ -408,9 +404,9 @@ class Filtering(Cog): if URL_RE.search(text): return False - watchlist_patterns = self._get_filterlist_items('word_watchlist', allowed=False, compiled=True) + watchlist_patterns = self._get_filterlist_items('word_watchlist', allowed=False) for pattern in watchlist_patterns: - match = pattern.search(text) + match = re.search(pattern, text, flags=re.IGNORECASE) if match: return match -- cgit v1.2.3 From e73589a0cc490187cb7aa3039628a29e1c1650c9 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 15:19:04 +0200 Subject: Fix imports in converters.py --- bot/converters.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bot/converters.py b/bot/converters.py index 158bf1a16..77d0bead7 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -1,14 +1,15 @@ -import dateutil.parser -import dateutil.tz -import discord import logging import re import typing as t -from aiohttp import ClientConnectorError from datetime import datetime +from ssl import CertificateError + +import dateutil.parser +import dateutil.tz +import discord +from aiohttp import ClientConnectorError from dateutil.relativedelta import relativedelta from discord.ext.commands import BadArgument, Context, Converter, IDConverter, UserConverter -from ssl import CertificateError from bot.api import ResponseCodeError from bot.constants import URLs -- cgit v1.2.3 From e93cdca80026704d540c87e36a56ce059e8d5499 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 15:38:20 +0200 Subject: Fix a bad type annotation. --- bot/bot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 203b35ba0..5deb986ec 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -3,7 +3,7 @@ import logging import socket import warnings from collections import defaultdict -from typing import Any, Dict, Optional +from typing import Dict, Optional import aiohttp import aioredis @@ -165,7 +165,7 @@ class Bot(commands.Bot): self.redis_ready.clear() await self.redis_session.wait_closed() - def insert_item_into_filter_list_cache(self, item: Dict[Any]) -> None: + def insert_item_into_filter_list_cache(self, item: Dict[str, str]) -> None: """Add an item to the bots filter_list_cache.""" type_ = item["type"] allowed = item["allowed"] -- cgit v1.2.3 From e0837f4f6dd7c5c2d6fc0811dccfaf1ecae768ba Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 20:14:52 +0200 Subject: Restructure bot.filter_list_cache. This is an optimization designed to eliminate all the list comprehensions we were doing inside antimalware and filtering. The cache is now structured so that the content is the key and the metadata is the value. --- bot/bot.py | 8 ++++---- bot/cogs/antimalware.py | 2 +- bot/cogs/filter_lists.py | 18 +++++++++--------- bot/cogs/filtering.py | 3 +-- tests/bot/cogs/test_antimalware.py | 10 +++++----- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 5deb986ec..4492feaa9 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -35,7 +35,7 @@ class Bot(commands.Bot): self.redis_ready = asyncio.Event() self.redis_closed = False self.api_client = api.APIClient(loop=self.loop) - self.filter_list_cache = defaultdict(list) + self.filter_list_cache = defaultdict(dict) self._connector = None self._resolver = None @@ -169,14 +169,14 @@ class Bot(commands.Bot): """Add an item to the bots filter_list_cache.""" type_ = item["type"] allowed = item["allowed"] - metadata = { + content = item["content"] + + self.filter_list_cache[f"{type_}.{allowed}"][content] = { "id": item["id"], - "content": item["content"], "comment": item["comment"], "created_at": item["created_at"], "updated_at": item["updated_at"], } - self.filter_list_cache[f"{type_}.{allowed}"].append(metadata) async def login(self, *args, **kwargs) -> None: """Re-create the connector and set up sessions before logging into Discord.""" diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index 9a100b3fc..c76bd2c60 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -40,7 +40,7 @@ class AntiMalware(Cog): def _get_whitelisted_file_formats(self) -> list: """Get the file formats currently on the whitelist.""" - return [item['content'] for item in self.bot.filter_list_cache['file_format.True']] + return self.bot.filter_list_cache['FILE_FORMAT.True'].keys() def _get_disallowed_extensions(self, message: Message) -> t.Iterable[str]: """Get an iterable containing all the disallowed extensions of attachments.""" diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index a93de2de9..3331be014 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -88,16 +88,16 @@ class FilterLists(Cog): # Find the content and delete it. log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}") - for allow_list in self.bot.filter_list_cache[f"{list_type}.{allowed}"]: - if content == allow_list.get("content"): - item = allow_list + for allow_list, metadata in self.bot.filter_list_cache[f"{list_type}.{allowed}"].items(): + if content == allow_list: + item = metadata break if item is not None: await self.bot.api_client.delete( - f"bot/filter-lists/{item.get('id')}" + f"bot/filter-lists/{item['id']}" ) - self.bot.filter_list_cache[f"{list_type}.{allowed}"].remove(item) + del self.bot.filter_list_cache[f"{list_type}.{allowed}"][content] await ctx.message.add_reaction("✅") async def _list_all_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType) -> None: @@ -107,11 +107,11 @@ class FilterLists(Cog): # Build a list of lines we want to show in the paginator lines = [] - for item in result: - line = f"• `{item.get('content')}`" + for content, metadata in result.items(): + line = f"• `{content}`" - if item.get("comment"): - line += f" - {item.get('comment')}" + if metadata.get("comment"): + line += f" - {metadata.get('comment')}" lines.append(line) lines = sorted(lines) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 7787d396d..0951cb740 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -101,8 +101,7 @@ class Filtering(Cog): def _get_filterlist_items(self, list_type: str, *, allowed: bool) -> list: """Fetch items from the filter_list_cache.""" - items = self.bot.filter_list_cache[f"{list_type.upper()}.{allowed}"] - return [item["content"] for item in items] + return self.bot.filter_list_cache[f"{list_type.upper()}.{allowed}"].keys() @staticmethod def _expand_spoilers(text: str) -> str: diff --git a/tests/bot/cogs/test_antimalware.py b/tests/bot/cogs/test_antimalware.py index 664fa8f19..82eadf226 100644 --- a/tests/bot/cogs/test_antimalware.py +++ b/tests/bot/cogs/test_antimalware.py @@ -15,11 +15,11 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase): """Sets up fresh objects for each test.""" self.bot = MockBot() self.bot.filter_list_cache = { - "file_format.True": [ - {"content": ".first"}, - {"content": ".second"}, - {"content": ".third"} - ] + "file_format.True": { + ".first": {}, + ".second": {}, + ".third": {}, + } } self.cog = antimalware.AntiMalware(self.bot) self.message = MockMessage() -- cgit v1.2.3 From 48bc968d3c03032beed8ac110b76dc468262a4d3 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 20:15:17 +0200 Subject: word_watchlist -> filter_token in filtering.py. --- bot/cogs/filtering.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 0951cb740..8670e1c8c 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -138,7 +138,7 @@ class Filtering(Cog): def get_name_matches(self, name: str) -> List[re.Match]: """Check bad words from passed string (name). Return list of matches.""" matches = [] - watchlist_patterns = self._get_filterlist_items('word_watchlist', allowed=False) + watchlist_patterns = self._get_filterlist_items('filter_token', allowed=False) for pattern in watchlist_patterns: if match := re.search(pattern, name, flags=re.IGNORECASE): matches.append(match) @@ -403,7 +403,7 @@ class Filtering(Cog): if URL_RE.search(text): return False - watchlist_patterns = self._get_filterlist_items('word_watchlist', allowed=False) + watchlist_patterns = self._get_filterlist_items('filter_token', allowed=False) for pattern in watchlist_patterns: match = re.search(pattern, text, flags=re.IGNORECASE) if match: -- cgit v1.2.3 From 13a5f35273da39aafdcda7b257364a7756b028ff Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 20:15:48 +0200 Subject: We search for an invite instead of matching one. This means we can validate invites that start with https://, whereas before we could not. --- bot/converters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/converters.py b/bot/converters.py index 77d0bead7..5912e3e61 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -57,7 +57,7 @@ class ValidDiscordServerInvite(Converter): async def convert(self, ctx: Context, server_invite: str) -> dict: """Check whether the string is a valid Discord server invite.""" - invite_code = INVITE_RE.match(server_invite) + invite_code = INVITE_RE.search(server_invite) if invite_code: response = await ctx.bot.http_session.get( f"{URLs.discord_invite_api}/{invite_code[1]}" -- cgit v1.2.3 From 0cfc918c6d68764c380f1188f3bc5508e6b27030 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 20:24:06 +0200 Subject: Fix broken antimalware tests. --- tests/bot/cogs/test_antimalware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bot/cogs/test_antimalware.py b/tests/bot/cogs/test_antimalware.py index 82eadf226..ecb7abf00 100644 --- a/tests/bot/cogs/test_antimalware.py +++ b/tests/bot/cogs/test_antimalware.py @@ -15,7 +15,7 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase): """Sets up fresh objects for each test.""" self.bot = MockBot() self.bot.filter_list_cache = { - "file_format.True": { + "FILE_FORMAT.True": { ".first": {}, ".second": {}, ".third": {}, -- cgit v1.2.3 From dd3275e8a8552f9d7580f9e2a070e8fae1d41b5d Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 21:49:33 +0200 Subject: Apply suggested change from @MarkKoz. --- bot/cogs/filter_lists.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index 3331be014..f133d53d9 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -88,10 +88,7 @@ class FilterLists(Cog): # Find the content and delete it. log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}") - for allow_list, metadata in self.bot.filter_list_cache[f"{list_type}.{allowed}"].items(): - if content == allow_list: - item = metadata - break + item = self.bot.filter_list_cache[f"{list_type}.{allowed}"].get(content) if item is not None: await self.bot.api_client.delete( -- cgit v1.2.3 From 4d1099938f4582330ce6c732dac4862df6ec68e4 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 21:54:27 +0200 Subject: Make sure file formats have leading dots. --- bot/cogs/filter_lists.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index f133d53d9..8831a2143 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -43,6 +43,10 @@ class FilterLists(Cog): if not comment: comment = guild_data.get("name") + # If it's a file format, let's make sure it has a leading dot. + elif list_type == "FILE_FORMAT" and not content.startswith("."): + content = f".{content}" + # Try to add the item to the database log.trace(f"Trying to add the {content} item to the {list_type} {allow_type}") payload = { @@ -86,6 +90,10 @@ class FilterLists(Cog): guild_data = await self._validate_guild_invite(ctx, content) content = guild_data.get("id") + # If it's a file format, let's make sure it has a leading dot. + elif list_type == "FILE_FORMAT" and not content.startswith("."): + content = f".{content}" + # Find the content and delete it. log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}") item = self.bot.filter_list_cache[f"{list_type}.{allowed}"].get(content) -- cgit v1.2.3 From 0f8a89bd8be9b5bd6fbad989ad3aa57103a1f9da Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 29 Jul 2020 23:46:23 +0200 Subject: Dynamically amend types to filterlist docstrings. We want the !help invocations to give you all the information you need in order to use the command. That also means we need to provide the valid filterlist types, which are subject to change. So, we fetch the valid ones from the API and then dynamically insert them into the docstrings. --- bot/cogs/filter_lists.py | 24 ++++++++++++++++++++++++ bot/converters.py | 19 ++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index 8831a2143..fbd070bb9 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -17,8 +17,32 @@ log = logging.getLogger(__name__) class FilterLists(Cog): """Commands for blacklisting and whitelisting things.""" + methods_with_filterlist_types = [ + "allow_add", + "allow_delete", + "allow_get", + "deny_add", + "deny_delete", + "deny_get", + ] + def __init__(self, bot: Bot) -> None: self.bot = bot + self.bot.loop.create_task(self._amend_docstrings()) + + async def _amend_docstrings(self) -> None: + """Add the valid FilterList types to the docstrings, so they'll appear in !help invocations.""" + await self.bot.wait_until_guild_available() + + # Add valid filterlist types to the docstrings + valid_types = await ValidFilterListType.get_valid_types(self.bot) + valid_types = [f"`{type_.lower()}`" for type_ in valid_types] + + for method_name in self.methods_with_filterlist_types: + command = getattr(self, method_name) + command.help = ( + f"{command.help}\n\nValid **list_type** values are {', '.join(valid_types)}." + ) async def _add_data( self, diff --git a/bot/converters.py b/bot/converters.py index 5912e3e61..c9f525dd1 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -9,7 +9,7 @@ import dateutil.tz import discord from aiohttp import ClientConnectorError from dateutil.relativedelta import relativedelta -from discord.ext.commands import BadArgument, Context, Converter, IDConverter, UserConverter +from discord.ext.commands import BadArgument, Bot, Context, Converter, IDConverter, UserConverter from bot.api import ResponseCodeError from bot.constants import URLs @@ -81,14 +81,23 @@ class ValidFilterListType(Converter): passes through the given argument otherwise. """ - async def convert(self, ctx: Context, list_type: str) -> str: - """Checks whether the given string is a valid FilterList type.""" + @staticmethod + async def get_valid_types(bot: Bot) -> list: + """ + Try to get a list of valid filter list types. + + Raise a BadArgument if the API can't respond. + """ try: - valid_types = await ctx.bot.api_client.get('bot/filter-lists/get-types') + valid_types = await bot.api_client.get('bot/filter-lists/get-types') except ResponseCodeError: raise BadArgument("Cannot validate list_type: Unable to fetch valid types from API.") - valid_types = [enum for enum, classname in valid_types] + return [enum for enum, classname in valid_types] + + async def convert(self, ctx: Context, list_type: str) -> str: + """Checks whether the given string is a valid FilterList type.""" + valid_types = await self.get_valid_types(ctx.bot) list_type = list_type.upper() if list_type not in valid_types: -- cgit v1.2.3 From 9795d680b50a704424959d581d1f137b28f4e859 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Thu, 30 Jul 2020 00:10:33 +0200 Subject: Add more explicit feedback to failures. For deleting and listing data, we now get some more feedback when things fail. --- bot/cogs/filter_lists.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index fbd070bb9..52db1fcb5 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -106,7 +106,6 @@ class FilterLists(Cog): async def _delete_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType, content: str) -> None: """Remove an item from a filterlist.""" - item = None allow_type = "whitelist" if allowed else "blacklist" # If this is a server invite, we need to convert it. @@ -123,11 +122,20 @@ class FilterLists(Cog): item = self.bot.filter_list_cache[f"{list_type}.{allowed}"].get(content) if item is not None: - await self.bot.api_client.delete( - f"bot/filter-lists/{item['id']}" - ) - del self.bot.filter_list_cache[f"{list_type}.{allowed}"][content] - await ctx.message.add_reaction("✅") + try: + await self.bot.api_client.delete( + f"bot/filter-lists/{item['id']}" + ) + del self.bot.filter_list_cache[f"{list_type}.{allowed}"][content] + await ctx.message.add_reaction("✅") + except ResponseCodeError as e: + log.debug( + f"{ctx.author} tried to delete an item with the id {item['id']}, but " + f"the API raised an unexpected error: {e}" + ) + await ctx.message.add_reaction("❌") + else: + await ctx.message.add_reaction("❌") async def _list_all_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType) -> None: """Paginate and display all items in a filterlist.""" @@ -158,8 +166,10 @@ class FilterLists(Cog): else: embed.description = "Hmmm, seems like there's nothing here yet." await ctx.send(embed=embed) + await ctx.message.add_reaction("❌") - async def _validate_guild_invite(self, ctx: Context, invite: str) -> dict: + @staticmethod + async def _validate_guild_invite(ctx: Context, invite: str) -> dict: """ Validates a guild invite, and returns the guild info as a dict. -- cgit v1.2.3 From f97defcab304e6f2e3175f10e9888db30a0be0c8 Mon Sep 17 00:00:00 2001 From: Senjan21 <53477086+Senjan21@users.noreply.github.com> Date: Thu, 30 Jul 2020 13:54:55 +0200 Subject: Fix channel moving incase `message.pin` fails --- bot/cogs/help_channels.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 1f87c3e39..5d4346000 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -701,6 +701,8 @@ class HelpChannels(commands.Cog): await message.pin() except discord.NotFound: log.info(f"Pinning message {message.id} ({channel}) failed because message got deleted.") + except discord.HTTPException as e: + log.info(f"Pinning message {message.id} ({channel.id}) failed with code {e.code}", exc_info=e) else: await self.question_messages.set(channel.id, message.id) -- cgit v1.2.3 From 873ecb9e11e72b1d62aa288660e0e582038d365e Mon Sep 17 00:00:00 2001 From: Senjan21 <53477086+Senjan21@users.noreply.github.com> Date: Thu, 30 Jul 2020 18:49:26 +0200 Subject: Change regex so it catches new discord URL --- bot/cogs/webhook_remover.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/webhook_remover.py b/bot/cogs/webhook_remover.py index 543869215..91bcaa1e9 100644 --- a/bot/cogs/webhook_remover.py +++ b/bot/cogs/webhook_remover.py @@ -8,7 +8,7 @@ from bot.bot import Bot from bot.cogs.moderation.modlog import ModLog from bot.constants import Channels, Colours, Event, Icons -WEBHOOK_URL_RE = re.compile(r"((?:https?://)?discordapp\.com/api/webhooks/\d+/)\S+/?", re.I) +WEBHOOK_URL_RE = re.compile(r"((?:https?://)?discord(app)?\.com/api/webhooks/\d+/)\S+/?", re.I) ALERT_MESSAGE_TEMPLATE = ( "{user}, looks like you posted a Discord webhook URL. Therefore, your " -- cgit v1.2.3 From 663eac8f9f30812f6ee6c95b134e5e62fa7273d6 Mon Sep 17 00:00:00 2001 From: Senjan21 <53477086+Senjan21@users.noreply.github.com> Date: Thu, 30 Jul 2020 19:03:33 +0200 Subject: Use non-capturing group instead. --- bot/cogs/webhook_remover.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/webhook_remover.py b/bot/cogs/webhook_remover.py index 91bcaa1e9..1ed8072f2 100644 --- a/bot/cogs/webhook_remover.py +++ b/bot/cogs/webhook_remover.py @@ -8,7 +8,7 @@ from bot.bot import Bot from bot.cogs.moderation.modlog import ModLog from bot.constants import Channels, Colours, Event, Icons -WEBHOOK_URL_RE = re.compile(r"((?:https?://)?discord(app)?\.com/api/webhooks/\d+/)\S+/?", re.I) +WEBHOOK_URL_RE = re.compile(r"((?:https?://)?discord(?:app)\.com/api/webhooks/\d+/)\S+/?", re.I) ALERT_MESSAGE_TEMPLATE = ( "{user}, looks like you posted a Discord webhook URL. Therefore, your " -- cgit v1.2.3 From 3586c1e187ecbee957084b2e2e6fbcf9bc2e2859 Mon Sep 17 00:00:00 2001 From: Senjan21 <53477086+Senjan21@users.noreply.github.com> Date: Thu, 30 Jul 2020 19:05:28 +0200 Subject: Missed `?` in regex. --- bot/cogs/webhook_remover.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/webhook_remover.py b/bot/cogs/webhook_remover.py index 1ed8072f2..ac9f7c20f 100644 --- a/bot/cogs/webhook_remover.py +++ b/bot/cogs/webhook_remover.py @@ -8,7 +8,7 @@ from bot.bot import Bot from bot.cogs.moderation.modlog import ModLog from bot.constants import Channels, Colours, Event, Icons -WEBHOOK_URL_RE = re.compile(r"((?:https?://)?discord(?:app)\.com/api/webhooks/\d+/)\S+/?", re.I) +WEBHOOK_URL_RE = re.compile(r"((?:https?://)?discord(?:app)?\.com/api/webhooks/\d+/)\S+/?", re.I) ALERT_MESSAGE_TEMPLATE = ( "{user}, looks like you posted a Discord webhook URL. Therefore, your " -- cgit v1.2.3 From 6ee96040febd8ba1f0b1f781ca996c1097f4b87c Mon Sep 17 00:00:00 2001 From: Senjan21 <53477086+Senjan21@users.noreply.github.com> Date: Thu, 30 Jul 2020 19:10:18 +0200 Subject: Use full flag name for case-insensitivity requested by lemon --- bot/cogs/webhook_remover.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/webhook_remover.py b/bot/cogs/webhook_remover.py index ac9f7c20f..5812da87c 100644 --- a/bot/cogs/webhook_remover.py +++ b/bot/cogs/webhook_remover.py @@ -8,7 +8,7 @@ from bot.bot import Bot from bot.cogs.moderation.modlog import ModLog from bot.constants import Channels, Colours, Event, Icons -WEBHOOK_URL_RE = re.compile(r"((?:https?://)?discord(?:app)?\.com/api/webhooks/\d+/)\S+/?", re.I) +WEBHOOK_URL_RE = re.compile(r"((?:https?://)?discord(?:app)?\.com/api/webhooks/\d+/)\S+/?", re.IGNORECASE) ALERT_MESSAGE_TEMPLATE = ( "{user}, looks like you posted a Discord webhook URL. Therefore, your " -- cgit v1.2.3 From da484b1b22a0d346a1dbc1abf2ffe1027a7e5031 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 31 Jul 2020 19:52:01 +0200 Subject: Remove superfluous Available help channels. This adds a little bit of logic to the Help Channel `init_available` coroutine, which runs when the cog loads. This ensures that if there are more help channels in available than there should be, we remove the superfluos ones. Previously, if the bot started with too many channels, it would maintain and defend that excessive amount. This is because we never actually count the number of channels before adding in new available channels whenever one disappears. If we ever get too many available channels in the future, this can be solved by simply reloading this cog. --- bot/cogs/help_channels.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 5d4346000..1be980472 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -364,10 +364,18 @@ class HelpChannels(commands.Cog): channels = list(self.get_category_channels(self.available_category)) missing = constants.HelpChannels.max_available - len(channels) - log.trace(f"Moving {missing} missing channels to the Available category.") - - for _ in range(missing): - await self.move_to_available() + # If we've got less than `max_available` channel available, we should add some. + if missing > 0: + log.trace(f"Moving {missing} missing channels to the Available category.") + for _ in range(missing): + await self.move_to_available() + + # If for some reason we have more than `max_available` channels available, + # we should move the superfluous ones over to dormant. + elif missing < 0: + log.trace(f"Moving {abs(missing)} superfluous available channels over to the Dormant category.") + for channel in channels[:abs(missing)]: + await self.move_to_dormant(channel, "auto") async def init_categories(self) -> None: """Get the help category objects. Remove the cog if retrieval fails.""" -- cgit v1.2.3 From 6c934ebdf5dfa3025347a1b345b41f0f62ec76cb Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 3 Aug 2020 10:04:58 +0200 Subject: Sort all load_extension groups alphabetically. --- bot/__main__.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bot/__main__.py b/bot/__main__.py index c2271cd16..fcef2239e 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -34,35 +34,34 @@ bot = Bot( ) # Internal/debug +bot.load_extension("bot.cogs.config_verifier") bot.load_extension("bot.cogs.error_handler") bot.load_extension("bot.cogs.filtering") bot.load_extension("bot.cogs.logging") bot.load_extension("bot.cogs.security") -bot.load_extension("bot.cogs.config_verifier") # Commands, etc bot.load_extension("bot.cogs.antimalware") bot.load_extension("bot.cogs.antispam") bot.load_extension("bot.cogs.bot") bot.load_extension("bot.cogs.clean") +bot.load_extension("bot.cogs.doc") bot.load_extension("bot.cogs.extensions") bot.load_extension("bot.cogs.help") - -bot.load_extension("bot.cogs.doc") bot.load_extension("bot.cogs.verification") # Feature cogs bot.load_extension("bot.cogs.alias") -bot.load_extension("bot.cogs.filter_lists") bot.load_extension("bot.cogs.defcon") bot.load_extension("bot.cogs.dm_relay") bot.load_extension("bot.cogs.duck_pond") bot.load_extension("bot.cogs.eval") +bot.load_extension("bot.cogs.filter_lists") bot.load_extension("bot.cogs.information") bot.load_extension("bot.cogs.jams") bot.load_extension("bot.cogs.moderation") -bot.load_extension("bot.cogs.python_news") bot.load_extension("bot.cogs.off_topic_names") +bot.load_extension("bot.cogs.python_news") bot.load_extension("bot.cogs.reddit") bot.load_extension("bot.cogs.reminders") bot.load_extension("bot.cogs.site") -- cgit v1.2.3 From fff5493b9cec4ed920acee82698c34eef76206a4 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 3 Aug 2020 10:07:06 +0200 Subject: Adding a beautiful walrus to filter_lists.py. Thanks @Den4200! --- bot/cogs/filter_lists.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index 52db1fcb5..496d45322 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -147,8 +147,8 @@ class FilterLists(Cog): for content, metadata in result.items(): line = f"• `{content}`" - if metadata.get("comment"): - line += f" - {metadata.get('comment')}" + if comment := metadata.get("comment"): + line += f" - {comment}" lines.append(line) lines = sorted(lines) -- cgit v1.2.3 From 5a339639e598f2e84ec9367dd2ea519befd4f011 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 3 Aug 2020 10:10:17 +0200 Subject: Change some errant single quotes to doubles. --- bot/cogs/filter_lists.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index 496d45322..e50411d51 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -74,10 +74,10 @@ class FilterLists(Cog): # Try to add the item to the database log.trace(f"Trying to add the {content} item to the {list_type} {allow_type}") payload = { - 'allowed': allowed, - 'type': list_type, - 'content': content, - 'comment': comment, + "allowed": allowed, + "type": list_type, + "content": content, + "comment": comment, } try: -- cgit v1.2.3 From 7a84ed8dbec5c1497a08865fa3144eb867dc1636 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 3 Aug 2020 10:15:09 +0200 Subject: Move function params to 4-space indentation. --- bot/cogs/filter_lists.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index e50411d51..8aa5a0a08 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -45,12 +45,12 @@ class FilterLists(Cog): ) async def _add_data( - self, - ctx: Context, - allowed: bool, - list_type: ValidFilterListType, - content: str, - comment: Optional[str] = None, + self, + ctx: Context, + allowed: bool, + list_type: ValidFilterListType, + content: str, + comment: Optional[str] = None, ) -> None: """Add an item to a filterlist.""" allow_type = "whitelist" if allowed else "blacklist" @@ -198,24 +198,24 @@ class FilterLists(Cog): @whitelist.command(name="add", aliases=("a", "set")) async def allow_add( - self, - ctx: Context, - list_type: ValidFilterListType, - content: str, - *, - comment: Optional[str] = None, + self, + ctx: Context, + list_type: ValidFilterListType, + content: str, + *, + comment: Optional[str] = None, ) -> None: """Add an item to the specified allowlist.""" await self._add_data(ctx, True, list_type, content, comment) @blacklist.command(name="add", aliases=("a", "set")) async def deny_add( - self, - ctx: Context, - list_type: ValidFilterListType, - content: str, - *, - comment: Optional[str] = None, + self, + ctx: Context, + list_type: ValidFilterListType, + content: str, + *, + comment: Optional[str] = None, ) -> None: """Add an item to the specified denylist.""" await self._add_data(ctx, False, list_type, content, comment) -- cgit v1.2.3 From 134ea0e449005a771c8184189a8d319e5d4b26a0 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 3 Aug 2020 13:33:17 +0200 Subject: Move function params to 4-space indentation. --- bot/bot.py | 4 ++-- bot/cogs/filter_lists.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 4492feaa9..756449293 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -51,7 +51,7 @@ class Bot(commands.Bot): self.stats = AsyncStatsClient(self.loop, statsd_url, 8125, prefix="bot") - async def _cache_filter_list_data(self) -> None: + async def cache_filter_list_data(self) -> None: """Cache all the data in the FilterList on the site.""" full_cache = await self.api_client.get('bot/filter-lists') @@ -123,7 +123,7 @@ class Bot(commands.Bot): self.api_client.recreate(force=True, connector=self._connector) # Build the FilterList cache - self.loop.create_task(self._cache_filter_list_data()) + self.loop.create_task(self.cache_filter_list_data()) def add_cog(self, cog: commands.Cog) -> None: """Adds a "cog" to the bot and logs the operation.""" diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index 8aa5a0a08..6249774bb 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -168,6 +168,11 @@ class FilterLists(Cog): await ctx.send(embed=embed) await ctx.message.add_reaction("❌") + async def _sync_data(self) -> None: + """Syncs the filterlists with the API.""" + log.trace("Synchronizing FilterList cache with data from the API.") + await self.bot.cache_filter_list_data() + @staticmethod async def _validate_guild_invite(ctx: Context, invite: str) -> dict: """ @@ -240,6 +245,16 @@ class FilterLists(Cog): """Get the contents of a specified denylist.""" await self._list_all_data(ctx, False, list_type) + @whitelist.command(name="sync", aliases=("s",)) + async def allow_sync(self, _: Context) -> None: + """Syncs both allowlists and denylists with the API.""" + await self._sync_data() + + @blacklist.command(name="sync", aliases=("s",)) + async def deny_sync(self, _: Context) -> None: + """Syncs both allowlists and denylists with the API.""" + await self._sync_data() + def cog_check(self, ctx: Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" return with_role_check(ctx, *constants.MODERATION_ROLES) -- cgit v1.2.3 From 312d31d408e2580a08b5b36a6f885f6d1a5955b9 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 3 Aug 2020 14:08:08 +0200 Subject: Add some feedback to the _sync_data helper. Previously, this would not provide any feedback at all, which is really terrible UX. Sorry about that. This also adds error handling in case the API call fails. --- bot/cogs/filter_lists.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index 6249774bb..c15adc461 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -168,10 +168,18 @@ class FilterLists(Cog): await ctx.send(embed=embed) await ctx.message.add_reaction("❌") - async def _sync_data(self) -> None: + async def _sync_data(self, ctx: Context) -> None: """Syncs the filterlists with the API.""" - log.trace("Synchronizing FilterList cache with data from the API.") - await self.bot.cache_filter_list_data() + try: + log.trace("Attempting to sync FilterList cache with data from the API.") + await self.bot.cache_filter_list_data() + await ctx.message.add_reaction("✅") + except ResponseCodeError as e: + log.debug( + f"{ctx.author} tried to sync FilterList cache data but " + f"the API raised an unexpected error: {e}" + ) + await ctx.message.add_reaction("❌") @staticmethod async def _validate_guild_invite(ctx: Context, invite: str) -> dict: @@ -246,14 +254,14 @@ class FilterLists(Cog): await self._list_all_data(ctx, False, list_type) @whitelist.command(name="sync", aliases=("s",)) - async def allow_sync(self, _: Context) -> None: + async def allow_sync(self, ctx: Context) -> None: """Syncs both allowlists and denylists with the API.""" - await self._sync_data() + await self._sync_data(ctx) @blacklist.command(name="sync", aliases=("s",)) - async def deny_sync(self, _: Context) -> None: + async def deny_sync(self, ctx: Context) -> None: """Syncs both allowlists and denylists with the API.""" - await self._sync_data() + await self._sync_data(ctx) def cog_check(self, ctx: Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" -- cgit v1.2.3 From e73f77a34c3b2f0ec226acbdfc490f93896784d0 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 3 Aug 2020 17:40:09 +0200 Subject: Add support for plural FilterList types. This will allow mods to use '!whitelist get guild_invites' in addition to '!whitelist get guild_invite' This is just a naive implementation which works if the plural form is a simple s at the end of the word. It's implemented into the converter. --- bot/converters.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/bot/converters.py b/bot/converters.py index c9f525dd1..1358cbf1e 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -101,11 +101,23 @@ class ValidFilterListType(Converter): list_type = list_type.upper() if list_type not in valid_types: - valid_types_list = '\n'.join([f"• {type_.lower()}" for type_ in valid_types]) - raise BadArgument( - f"You have provided an invalid list type!\n\n" - f"Please provide one of the following: \n{valid_types_list}" - ) + + # Maybe the user is using the plural form of this type, + # e.g. "guild_invites" instead of "guild_invite". + # + # This code will support the simple plural form (a single 's' at the end), + # which works for all current list types, but if a list type is added in the future + # which has an irregular plural form (like 'ies'), this code will need to be + # refactored to support this. + if list_type.endswith("S") and list_type[:-1] in valid_types: + list_type = list_type[:-1] + + else: + valid_types_list = '\n'.join([f"• {type_.lower()}" for type_ in valid_types]) + raise BadArgument( + f"You have provided an invalid list type!\n\n" + f"Please provide one of the following: \n{valid_types_list}" + ) return list_type -- cgit v1.2.3 From 239fa5f43ab79435151657671ebcf21eac706fc6 Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Tue, 4 Aug 2020 15:30:34 +0100 Subject: Revert "Disabled burst_shared filter temporarily" This reverts commit be14db91b1c70993773e67cfa663fef0cfa85666. --- config-default.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config-default.yml b/config-default.yml index 14a073611..aacbe170f 100644 --- a/config-default.yml +++ b/config-default.yml @@ -358,6 +358,10 @@ anti_spam: interval: 10 max: 7 + burst_shared: + interval: 10 + max: 20 + chars: interval: 5 max: 3_000 -- cgit v1.2.3 From f553785858e70ff78c6fef5a5a4fa3e75c09a55e Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 12:32:44 -0700 Subject: HelpChannels: move unpinning to separate function --- bot/cogs/help_channels.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 1be980472..61e8d4384 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -551,18 +551,6 @@ class HelpChannels(commands.Cog): A caller argument is provided for metrics. """ - msg_id = await self.question_messages.pop(channel.id) - - try: - await self.bot.http.unpin_message(channel.id, msg_id) - except discord.HTTPException as e: - if e.code == 10008: - log.trace(f"Message {msg_id} don't exist, can't unpin.") - else: - log.warn(f"Got unexpected status {e.code} when unpinning message {msg_id}: {e.text}") - else: - log.trace(f"Unpinned message {msg_id}.") - log.info(f"Moving #{channel} ({channel.id}) to the Dormant category.") await self.move_to_bottom_position( @@ -587,6 +575,8 @@ class HelpChannels(commands.Cog): embed = discord.Embed(description=DORMANT_MSG) await channel.send(embed=embed) + await self.unpin(channel) + log.trace(f"Pushing #{channel} ({channel.id}) into the channel queue.") self.channel_queue.put_nowait(channel) self.report_stats() @@ -863,6 +853,20 @@ class HelpChannels(commands.Cog): log.trace(f"Channel #{channel} ({channel_id}) retrieved.") return channel + async def unpin(self, channel: discord.TextChannel) -> None: + """Unpin the initial question message sent in `channel`.""" + msg_id = await self.question_messages.pop(channel.id) + + try: + await self.bot.http.unpin_message(channel.id, msg_id) + except discord.HTTPException as e: + if e.code == 10008: + log.trace(f"Message {msg_id} don't exist, can't unpin.") + else: + log.warn(f"Got unexpected status {e.code} when unpinning message {msg_id}: {e.text}") + else: + log.trace(f"Unpinned message {msg_id}.") + async def wait_for_dormant_channel(self) -> discord.TextChannel: """Wait for a dormant channel to become available in the queue and return it.""" log.trace("Waiting for a dormant channel.") -- cgit v1.2.3 From 9b9a4390111c1a87e0fff87eae134a0745c26345 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 12:36:14 -0700 Subject: HelpChannels: add more detail to unpin log messages --- bot/cogs/help_channels.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 61e8d4384..e281615c2 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -855,17 +855,21 @@ class HelpChannels(commands.Cog): async def unpin(self, channel: discord.TextChannel) -> None: """Unpin the initial question message sent in `channel`.""" + channel_str = f"#{channel} ({channel.id})" + msg_id = await self.question_messages.pop(channel.id) try: await self.bot.http.unpin_message(channel.id, msg_id) except discord.HTTPException as e: if e.code == 10008: - log.trace(f"Message {msg_id} don't exist, can't unpin.") + log.debug(f"Message {msg_id} in {channel_str} doesn't exist; can't unpin.") else: - log.warn(f"Got unexpected status {e.code} when unpinning message {msg_id}: {e.text}") + log.exception( + f"Error unpinning message {msg_id} in {channel_str}: {e.status} ({e.code})" + ) else: - log.trace(f"Unpinned message {msg_id}.") + log.trace(f"Unpinned message {msg_id} in {channel_str}.") async def wait_for_dormant_channel(self) -> discord.TextChannel: """Wait for a dormant channel to become available in the queue and return it.""" -- cgit v1.2.3 From 6f31a1141b513dd6031949467e5409df0d6a3181 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 12:44:18 -0700 Subject: HelpChannels: don't unpin message if ID is None Fixes #1082 Fixes BOT-7G --- bot/cogs/help_channels.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index e281615c2..5e09e0a88 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -858,6 +858,9 @@ class HelpChannels(commands.Cog): channel_str = f"#{channel} ({channel.id})" msg_id = await self.question_messages.pop(channel.id) + if msg_id is None: + log.debug(f"{channel_str} doesn't have a message pinned.") + return try: await self.bot.http.unpin_message(channel.id, msg_id) -- cgit v1.2.3 From a58b4e121eabeb85aeba5d778064f772f049e21b Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 13:04:31 -0700 Subject: HelpChannels: create a generic function to handle pin errors This can be used for both pinning and unpinning messages. The error handling code was largely similar between them. --- bot/cogs/help_channels.py | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 5e09e0a88..b452cc574 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -853,26 +853,41 @@ class HelpChannels(commands.Cog): log.trace(f"Channel #{channel} ({channel_id}) retrieved.") return channel - async def unpin(self, channel: discord.TextChannel) -> None: - """Unpin the initial question message sent in `channel`.""" - channel_str = f"#{channel} ({channel.id})" + async def pin_wrapper(self, msg_id: int, channel: discord.TextChannel, *, pin: bool) -> bool: + """ + Pin message `msg_id` in `channel` if `pin` is True or unpin if it's False. - msg_id = await self.question_messages.pop(channel.id) - if msg_id is None: - log.debug(f"{channel_str} doesn't have a message pinned.") - return + Return True if successful and False otherwise. + """ + channel_str = f"#{channel} ({channel.id})" + if pin: + func = self.bot.http.pin_message + verb = "pin" + else: + func = self.bot.http.unpin_message + verb = "unpin" try: - await self.bot.http.unpin_message(channel.id, msg_id) + await func(channel.id, msg_id) except discord.HTTPException as e: if e.code == 10008: - log.debug(f"Message {msg_id} in {channel_str} doesn't exist; can't unpin.") + log.debug(f"Message {msg_id} in {channel_str} doesn't exist; can't {verb}.") else: log.exception( - f"Error unpinning message {msg_id} in {channel_str}: {e.status} ({e.code})" + f"Error {verb}ning message {msg_id} in {channel_str}: {e.status} ({e.code})" ) + return False + else: + log.trace(f"{verb.capitalize()}ned message {msg_id} in {channel_str}.") + return True + + async def unpin(self, channel: discord.TextChannel) -> None: + """Unpin the initial question message sent in `channel`.""" + msg_id = await self.question_messages.pop(channel.id) + if msg_id is None: + log.debug(f"#{channel} ({channel.id}) doesn't have a message pinned.") else: - log.trace(f"Unpinned message {msg_id} in {channel_str}.") + await self.pin_wrapper(msg_id, channel, pin=False) async def wait_for_dormant_channel(self) -> discord.TextChannel: """Wait for a dormant channel to become available in the queue and return it.""" -- cgit v1.2.3 From 4b7f19287c3d212a55276b0862f6a629269eaf92 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 13:09:20 -0700 Subject: HelpChannels: create separate function to pin a message --- bot/cogs/help_channels.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index b452cc574..d826463af 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -694,15 +694,8 @@ class HelpChannels(commands.Cog): log.info(f"Channel #{channel} was claimed by `{message.author.id}`.") await self.move_to_in_use(channel) await self.revoke_send_permissions(message.author) - # Pin message for better access and store this to cache - try: - await message.pin() - except discord.NotFound: - log.info(f"Pinning message {message.id} ({channel}) failed because message got deleted.") - except discord.HTTPException as e: - log.info(f"Pinning message {message.id} ({channel.id}) failed with code {e.code}", exc_info=e) - else: - await self.question_messages.set(channel.id, message.id) + + await self.pin(message) # Add user with channel for dormant check. await self.help_channel_claimants.set(channel.id, message.author.id) @@ -881,6 +874,11 @@ class HelpChannels(commands.Cog): log.trace(f"{verb.capitalize()}ned message {msg_id} in {channel_str}.") return True + async def pin(self, message: discord.Message) -> None: + """Pin an initial question `message` and store it in a cache.""" + if await self.pin_wrapper(message.id, message.channel, pin=True): + await self.question_messages.set(message.channel.id, message.id) + async def unpin(self, channel: discord.TextChannel) -> None: """Unpin the initial question message sent in `channel`.""" msg_id = await self.question_messages.pop(channel.id) -- cgit v1.2.3 From 2e46838aa561d93f70351d08ea275fd0c8b95de2 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 13:50:19 -0700 Subject: HelpChannels: more accurate empty check The bot's pin message was being picked up as the last message, so the system was not considering the channel empty. --- bot/cogs/help_channels.py | 18 +++++++++++++++--- config-default.yml | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index d826463af..5ecf40e54 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -737,9 +737,21 @@ class HelpChannels(commands.Cog): self.scheduler.schedule_later(delay, msg.channel.id, self.move_idle_channel(msg.channel)) async def is_empty(self, channel: discord.TextChannel) -> bool: - """Return True if the most recent message in `channel` is the bot's `AVAILABLE_MSG`.""" - msg = await self.get_last_message(channel) - return self.match_bot_embed(msg, AVAILABLE_MSG) + """Return True if there's an AVAILABLE_MSG and the messages leading up are bot messages.""" + found = False + + # A limit of 100 results in a single API call. + # If AVAILABLE_MSG isn't found within 100 messages, then assume the channel is not empty. + # Not gonna do an extensive search for it cause it's too expensive. + async for msg in channel.history(limit=100): + if not msg.author.bot: + return False + + if self.match_bot_embed(msg, AVAILABLE_MSG): + found = True + break + + return found async def check_cooldowns(self) -> None: """Remove expired cooldowns and re-schedule active ones.""" diff --git a/config-default.yml b/config-default.yml index aacbe170f..4bd90511c 100644 --- a/config-default.yml +++ b/config-default.yml @@ -432,8 +432,8 @@ help_channels: # Allowed duration of inactivity before making a channel dormant idle_minutes: 30 - # Allowed duration of inactivity when question message deleted - # and no one other sent before message making channel dormant. + # Allowed duration of inactivity when channel is empty (due to deleted messages) + # before message making a channel dormant deleted_idle_minutes: 5 # Maximum number of channels to put in the available category -- cgit v1.2.3 From f2779147f1e3c99436c1437c9b405479e498c17f Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 13:57:14 -0700 Subject: HelpChannels: add logging to is_empty --- bot/cogs/help_channels.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 5ecf40e54..a13207d20 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -738,6 +738,7 @@ class HelpChannels(commands.Cog): async def is_empty(self, channel: discord.TextChannel) -> bool: """Return True if there's an AVAILABLE_MSG and the messages leading up are bot messages.""" + log.trace(f"Checking if #{channel} ({channel.id}) is empty.") found = False # A limit of 100 results in a single API call. @@ -745,9 +746,11 @@ class HelpChannels(commands.Cog): # Not gonna do an extensive search for it cause it's too expensive. async for msg in channel.history(limit=100): if not msg.author.bot: + log.trace(f"#{channel} ({channel.id}) has a non-bot message.") return False if self.match_bot_embed(msg, AVAILABLE_MSG): + log.trace(f"#{channel} ({channel.id}) has the available message embed.") found = True break -- cgit v1.2.3 From 3e5558a8ccf79dfeb3efbb63d48d807ba67c8377 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 17:31:50 -0700 Subject: Cancel scheduled tasks when cogs unload When cogs reload, they used new Scheduler instances, which aren't aware of previously scheduled tasks. This led to duplicate scheduled tasks when cogs re-scheduled tasks upon initialisation. Fixes #1080 Fixes BOT-7H --- bot/cogs/filtering.py | 4 ++++ bot/cogs/moderation/scheduler.py | 4 ++++ bot/cogs/moderation/silence.py | 3 ++- bot/cogs/reminders.py | 4 ++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 64afd184d..4ec95ad73 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -99,6 +99,10 @@ class Filtering(Cog): self.bot.loop.create_task(self.reschedule_offensive_msg_deletion()) + def cog_unload(self) -> None: + """Cancel scheduled tasks.""" + self.scheduler.cancel_all() + def _get_filterlist_items(self, list_type: str, *, allowed: bool) -> list: """Fetch items from the filter_list_cache.""" return self.bot.filter_list_cache[f"{list_type.upper()}.{allowed}"].keys() diff --git a/bot/cogs/moderation/scheduler.py b/bot/cogs/moderation/scheduler.py index 601e238c9..75028d851 100644 --- a/bot/cogs/moderation/scheduler.py +++ b/bot/cogs/moderation/scheduler.py @@ -31,6 +31,10 @@ class InfractionScheduler: self.bot.loop.create_task(self.reschedule_infractions(supported_infractions)) + def cog_unload(self) -> None: + """Cancel scheduled tasks.""" + self.scheduler.cancel_all() + @property def mod_log(self) -> ModLog: """Get the currently loaded ModLog cog instance.""" diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index ae4fb7b64..f8a6592bc 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -152,7 +152,8 @@ class Silence(commands.Cog): return False def cog_unload(self) -> None: - """Send alert with silenced channels on unload.""" + """Send alert with silenced channels and cancel scheduled tasks on unload.""" + self.scheduler.cancel_all() if self.muted_channels: channels_string = ''.join(channel.mention for channel in self.muted_channels) message = f"<@&{Roles.moderators}> channels left silenced on cog unload: {channels_string}" diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index b5998cc0e..670493bcf 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -37,6 +37,10 @@ class Reminders(Cog): self.bot.loop.create_task(self.reschedule_reminders()) + def cog_unload(self) -> None: + """Cancel scheduled tasks.""" + self.scheduler.cancel_all() + async def reschedule_reminders(self) -> None: """Get all current reminders from the API and reschedule them.""" await self.bot.wait_until_guild_available() -- cgit v1.2.3 From 61400aabf6d6d30d09f16e91eb43894fa2b56ff7 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 17:48:11 -0700 Subject: Source: raise BadArgument for dynamically-created objects Commands, cogs, etc. created via internal eval won't have a source file associated with them, making source retrieval impossible. Fixes #1083 Fixes BOT-7K --- bot/cogs/source.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index f1db745cd..89548613d 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -60,7 +60,11 @@ class BotSource(commands.Cog): await ctx.send(embed=embed) def get_source_link(self, source_item: SourceType) -> Tuple[str, str, Optional[int]]: - """Build GitHub link of source item, return this link, file location and first line number.""" + """ + Build GitHub link of source item, return this link, file location and first line number. + + Raise BadArgument if `source_item` is a dynamically-created object (e.g. via internal eval). + """ if isinstance(source_item, commands.HelpCommand): src = type(source_item) filename = inspect.getsourcefile(src) @@ -78,10 +82,17 @@ class BotSource(commands.Cog): filename = tags_cog._cache[source_item]["location"] else: src = type(source_item) - filename = inspect.getsourcefile(src) + try: + filename = inspect.getsourcefile(src) + except TypeError: + raise commands.BadArgument("Cannot get source for a dynamically-created object.") if not isinstance(source_item, str): - lines, first_line_no = inspect.getsourcelines(src) + try: + lines, first_line_no = inspect.getsourcelines(src) + except OSError: + raise commands.BadArgument("Cannot get source for a dynamically-created object.") + lines_extension = f"#L{first_line_no}-L{first_line_no+len(lines)-1}" else: first_line_no = None -- cgit v1.2.3 From bcb8f27cba8d1413d302d11e38d122f915f96e14 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 17:52:16 -0700 Subject: Source: remove redundant check for help commands The code is identical to the else block and there's no reason for help commands to have an explicit check. --- bot/cogs/source.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 89548613d..205e0ba81 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -65,10 +65,7 @@ class BotSource(commands.Cog): Raise BadArgument if `source_item` is a dynamically-created object (e.g. via internal eval). """ - if isinstance(source_item, commands.HelpCommand): - src = type(source_item) - filename = inspect.getsourcefile(src) - elif isinstance(source_item, commands.Command): + if isinstance(source_item, commands.Command): if source_item.cog_name == "Alias": cmd_name = source_item.callback.__name__.replace("_alias", "") cmd = self.bot.get_command(cmd_name.replace("_", " ")) -- cgit v1.2.3 From 59c62162e0e0abad53dfbaad0e197a0fbab2f22f Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 18:09:13 -0700 Subject: HelpChannels: use more reliable check for claimed channel Using the channel's category isn't reliable since it may take Discord a while to actually move the channel once it's received a request from the bot. I suppose using redis technically has the same problem, but it should be much faster and less susceptible to lag than Discord. Fixes #1074 --- bot/cogs/help_channels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 1be980472..975043df9 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -694,7 +694,7 @@ class HelpChannels(commands.Cog): async with self.on_message_lock: log.trace(f"on_message lock acquired for {message.id}.") - if not self.is_in_category(channel, constants.Categories.help_available): + if await self.help_channel_claimants.contains(channel.id): log.debug( f"Message {message.id} will not make #{channel} ({channel.id}) in-use " f"because another message in the channel already triggered that." -- cgit v1.2.3 From bcd2ef98ab91a48ba7b8769f626ff7beb14db663 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Wed, 5 Aug 2020 15:26:54 +0200 Subject: Redis: remove erroneous `_redis` alias If a RedisCache instance was being accessed before bot has created the `redis_cache` instance, the `_redis` alias was being set to None, causing AttributeErrors in lookups. See: #1090 --- bot/utils/redis_cache.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/bot/utils/redis_cache.py b/bot/utils/redis_cache.py index 58cfe1df5..52b689b49 100644 --- a/bot/utils/redis_cache.py +++ b/bot/utils/redis_cache.py @@ -226,7 +226,6 @@ class RedisCache: for attribute in vars(instance).values(): if isinstance(attribute, Bot): self.bot = attribute - self._redis = self.bot.redis_session return self else: error_message = ( @@ -251,7 +250,7 @@ class RedisCache: value = self._value_to_typestring(value) log.trace(f"Setting {key} to {value}.") - await self._redis.hset(self._namespace, key, value) + await self.bot.redis_session.hset(self._namespace, key, value) async def get(self, key: RedisKeyType, default: Optional[RedisValueType] = None) -> Optional[RedisValueType]: """Get an item from the Redis cache.""" @@ -259,7 +258,7 @@ class RedisCache: key = self._key_to_typestring(key) log.trace(f"Attempting to retrieve {key}.") - value = await self._redis.hget(self._namespace, key) + value = await self.bot.redis_session.hget(self._namespace, key) if value is None: log.trace(f"Value not found, returning default value {default}") @@ -281,7 +280,7 @@ class RedisCache: key = self._key_to_typestring(key) log.trace(f"Attempting to delete {key}.") - return await self._redis.hdel(self._namespace, key) + return await self.bot.redis_session.hdel(self._namespace, key) async def contains(self, key: RedisKeyType) -> bool: """ @@ -291,7 +290,7 @@ class RedisCache: """ await self._validate_cache() key = self._key_to_typestring(key) - exists = await self._redis.hexists(self._namespace, key) + exists = await self.bot.redis_session.hexists(self._namespace, key) log.trace(f"Testing if {key} exists in the RedisCache - Result is {exists}") return exists @@ -314,7 +313,7 @@ class RedisCache: """ await self._validate_cache() items = self._dict_from_typestring( - await self._redis.hgetall(self._namespace) + await self.bot.redis_session.hgetall(self._namespace) ).items() log.trace(f"Retrieving all key/value pairs from cache, total of {len(items)} items.") @@ -323,7 +322,7 @@ class RedisCache: async def length(self) -> int: """Return the number of items in the Redis cache.""" await self._validate_cache() - number_of_items = await self._redis.hlen(self._namespace) + number_of_items = await self.bot.redis_session.hlen(self._namespace) log.trace(f"Returning length. Result is {number_of_items}.") return number_of_items @@ -335,7 +334,7 @@ class RedisCache: """Deletes the entire hash from the Redis cache.""" await self._validate_cache() log.trace("Clearing the cache of all key/value pairs.") - await self._redis.delete(self._namespace) + await self.bot.redis_session.delete(self._namespace) async def pop(self, key: RedisKeyType, default: Optional[RedisValueType] = None) -> RedisValueType: """Get the item, remove it from the cache, and provide a default if not found.""" @@ -364,7 +363,7 @@ class RedisCache: """ await self._validate_cache() log.trace(f"Updating the cache with the following items:\n{items}") - await self._redis.hmset_dict(self._namespace, self._dict_to_typestring(items)) + await self.bot.redis_session.hmset_dict(self._namespace, self._dict_to_typestring(items)) async def increment(self, key: RedisKeyType, amount: Optional[int, float] = 1) -> None: """ -- cgit v1.2.3 From 5a7ca92cf5d5ae7c7d4aa7ba086237586832af1a Mon Sep 17 00:00:00 2001 From: kwzrd Date: Wed, 5 Aug 2020 17:27:08 +0200 Subject: Revert "HelpChannels: use more reliable check for claimed channel" This reverts commit 59c62162 --- bot/cogs/help_channels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 975043df9..1be980472 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -694,7 +694,7 @@ class HelpChannels(commands.Cog): async with self.on_message_lock: log.trace(f"on_message lock acquired for {message.id}.") - if await self.help_channel_claimants.contains(channel.id): + if not self.is_in_category(channel, constants.Categories.help_available): log.debug( f"Message {message.id} will not make #{channel} ({channel.id}) in-use " f"because another message in the channel already triggered that." -- cgit v1.2.3 From 9c76e33fbce15b4c42ca2e3966676bec27cfc2c0 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 5 Aug 2020 15:34:58 -0700 Subject: HelpChannels: clear claimant cache when channel goes dormant The claimed channel check in `on_message` relies on the cache being cleared when a channel goes dormant. If it's not cleared, it will think the channel is still in use. --- bot/cogs/help_channels.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 975043df9..5f7bb748c 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -215,9 +215,6 @@ class HelpChannels(commands.Cog): log.trace("close command invoked; checking if the channel is in-use.") if ctx.channel.category == self.in_use_category: if await self.dormant_check(ctx): - - # Remove the claimant and the cooldown role - await self.help_channel_claimants.delete(ctx.channel.id) await self.remove_cooldown_role(ctx.author) # Ignore missing task when cooldown has passed but the channel still isn't dormant. @@ -551,6 +548,7 @@ class HelpChannels(commands.Cog): A caller argument is provided for metrics. """ + await self.help_channel_claimants.delete(channel.id) msg_id = await self.question_messages.pop(channel.id) try: -- cgit v1.2.3 From 3bfb3f09bae0f218a06db5f518496be397ed4b66 Mon Sep 17 00:00:00 2001 From: Dennis Pham Date: Wed, 5 Aug 2020 22:00:59 -0400 Subject: Guild invite regex: Add support for dashes in the invite code --- bot/utils/regex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/utils/regex.py b/bot/utils/regex.py index d194f93cb..0d2068f90 100644 --- a/bot/utils/regex.py +++ b/bot/utils/regex.py @@ -7,6 +7,6 @@ INVITE_RE = re.compile( r"discord(?:[\.,]|dot)me|" # or discord.me r"discord(?:[\.,]|dot)io" # or discord.io. r")(?:[\/]|slash)" # / or 'slash' - r"([a-zA-Z0-9]+)", # the invite code itself + r"([a-zA-Z0-9\-]+)", # the invite code itself flags=re.IGNORECASE ) -- cgit v1.2.3 From 673daebe463995de9f53361b3294ad5e496be476 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Thu, 6 Aug 2020 11:29:08 -0700 Subject: Deps: update discord.py to 1.4.0 It was released on PyPI. No longer need to clone via git. --- Dockerfile | 5 --- Pipfile | 2 +- Pipfile.lock | 123 ++++++++++++++++++++++++----------------------------------- 3 files changed, 51 insertions(+), 79 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0b1674e7a..06a538b2a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,11 +6,6 @@ ENV PIP_NO_CACHE_DIR=false \ PIPENV_IGNORE_VIRTUALENVS=1 \ PIPENV_NOSPIN=1 -RUN apt-get -y update \ - && apt-get install -y \ - git \ - && rm -rf /var/lib/apt/lists/* - # Install pipenv RUN pip install -U pipenv diff --git a/Pipfile b/Pipfile index 4db8a238b..6fff2223e 100644 --- a/Pipfile +++ b/Pipfile @@ -12,7 +12,7 @@ beautifulsoup4 = "~=4.9" colorama = {version = "~=0.4.3",sys_platform = "== 'win32'"} coloredlogs = "~=14.0" deepdiff = "~=4.0" -discord-py = {git = "https://github.com/Rapptz/discord.py.git",ref = "0bc15fa130b8f01fe2d67446a2184d474b0d0ba7"} +discord.py = "~=1.4.0" fakeredis = "~=1.4" feedparser = "~=5.2" fuzzywuzzy = "~=0.17" diff --git a/Pipfile.lock b/Pipfile.lock index c8cd96d3d..50ddd478c 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "eab4852974d26bd2c10362540c3e01d34af62446cb4e1915ec9a0bf2bddf4d94" + "sha256": "1905fd7eb15074ddbf04f2177b6cdd65edc4c74cb5fcbf4e6ca08ef649ba8a3c" }, "pipfile-spec": 6, "requires": { @@ -60,11 +60,11 @@ }, "aiormq": { "hashes": [ - "sha256:41a9d4eb17db805f30ed172f3f609fe0c2b16657fb15b1b67df19d251dd93c0d", - "sha256:7c19477a9450824cb79f9949fd238f4148e2c0dca67756a2868863c387209f04" + "sha256:106695a836f19c1af6c46b58e8aac80e00f86c5b3287a3c6483a1ee369cc95c9", + "sha256:9f6dbf6155fe2b7a3d24bf68de97fb812db0fac0a54e96bc1af14ea95078ba7f" ], "markers": "python_version >= '3.6'", - "version": "==3.2.2" + "version": "==3.2.3" }, "alabaster": { "hashes": [ @@ -177,9 +177,22 @@ "index": "pypi", "version": "==4.3.2" }, - "discord-py": { - "git": "https://github.com/Rapptz/discord.py.git", - "ref": "0bc15fa130b8f01fe2d67446a2184d474b0d0ba7" + "discord": { + "hashes": [ + "sha256:9d4debb4a37845543bd4b92cb195bc53a302797333e768e70344222857ff1559", + "sha256:ff6653655e342e7721dfb3f10421345fd852c2a33f2cca912b1c39b3778a9429" + ], + "index": "pypi", + "py": "~=1.4.0", + "version": "==1.0.1" + }, + "discord.py": { + "hashes": [ + "sha256:2b1846bfa382b54f4eace8e437a9f59f185388c5b08749ac0e1bbd98e05bfde5", + "sha256:f3db9531fccc391f51de65cfa46133106a9ba12ff2927aca6c14bffd3b7f17b5" + ], + "markers": "python_full_version >= '3.5.3'", + "version": "==1.4.0" }, "docutils": { "hashes": [ @@ -191,11 +204,11 @@ }, "fakeredis": { "hashes": [ - "sha256:4d170886865a91dbc8b7f8cbd4e5d488f4c5f2f25dfae127f001617bbe9e8f97", - "sha256:647b2593d349d9d4e566c8dadb2e4c71ba35be5bdc4f1f7ac2d565a12a965053" + "sha256:790c85ad0f3b2967aba1f51767021bc59760fcb612159584be018ea7384f7fd2", + "sha256:fdfe06f277092d022c271fcaefdc1f0c8d9bfa8cb15374cae41d66a20bd96d2b" ], "index": "pypi", - "version": "==1.4.1" + "version": "==1.4.2" }, "feedparser": { "hashes": [ @@ -542,11 +555,11 @@ }, "sentry-sdk": { "hashes": [ - "sha256:2de15b13836fa3522815a933bd9c887c77f4868071043349f94f1b896c1bcfb8", - "sha256:38bb09d0277117f76507c8728d9a5156f09a47ac5175bb8072513859d19a593b" + "sha256:21b17d6aa064c0fb703a7c00f77cf6c9c497cf2f83345c28892980a5e742d116", + "sha256:4fc97114c77d005467b9b1a29f042e2bc01923cb683b0ef0bbda46e79fa12532" ], "index": "pypi", - "version": "==0.16.2" + "version": "==0.16.3" }, "six": { "hashes": [ @@ -642,14 +655,6 @@ "index": "pypi", "version": "==3.3.0" }, - "typing-extensions": { - "hashes": [ - "sha256:6e95524d8a547a91e08f404ae485bbb71962de46967e1b71a0cb89af24e761c5", - "sha256:79ee589a3caca649a9bfd2a8de4709837400dfa00b6cc81962a1e6a1815969ae", - "sha256:f8d2bd89d25bc39dabe7d23df520442fa1d8969b82544370e03d88b5a591c392" - ], - "version": "==3.7.4.2" - }, "urllib3": { "hashes": [ "sha256:91056c15fa70756691db97756772bb1eb9678fa585d9184f24534b100dc60f4a", @@ -658,56 +663,28 @@ "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'", "version": "==1.25.10" }, - "websockets": { - "hashes": [ - "sha256:0e4fb4de42701340bd2353bb2eee45314651caa6ccee80dbd5f5d5978888fed5", - "sha256:1d3f1bf059d04a4e0eb4985a887d49195e15ebabc42364f4eb564b1d065793f5", - "sha256:20891f0dddade307ffddf593c733a3fdb6b83e6f9eef85908113e628fa5a8308", - "sha256:295359a2cc78736737dd88c343cd0747546b2174b5e1adc223824bcaf3e164cb", - "sha256:2db62a9142e88535038a6bcfea70ef9447696ea77891aebb730a333a51ed559a", - "sha256:3762791ab8b38948f0c4d281c8b2ddfa99b7e510e46bd8dfa942a5fff621068c", - "sha256:3db87421956f1b0779a7564915875ba774295cc86e81bc671631379371af1170", - "sha256:3ef56fcc7b1ff90de46ccd5a687bbd13a3180132268c4254fc0fa44ecf4fc422", - "sha256:4f9f7d28ce1d8f1295717c2c25b732c2bc0645db3215cf757551c392177d7cb8", - "sha256:5c01fd846263a75bc8a2b9542606927cfad57e7282965d96b93c387622487485", - "sha256:5c65d2da8c6bce0fca2528f69f44b2f977e06954c8512a952222cea50dad430f", - "sha256:751a556205d8245ff94aeef23546a1113b1dd4f6e4d102ded66c39b99c2ce6c8", - "sha256:7ff46d441db78241f4c6c27b3868c9ae71473fe03341340d2dfdbe8d79310acc", - "sha256:965889d9f0e2a75edd81a07592d0ced54daa5b0785f57dc429c378edbcffe779", - "sha256:9b248ba3dd8a03b1a10b19efe7d4f7fa41d158fdaa95e2cf65af5a7b95a4f989", - "sha256:9bef37ee224e104a413f0780e29adb3e514a5b698aabe0d969a6ba426b8435d1", - "sha256:c1ec8db4fac31850286b7cd3b9c0e1b944204668b8eb721674916d4e28744092", - "sha256:c8a116feafdb1f84607cb3b14aa1418424ae71fee131642fc568d21423b51824", - "sha256:ce85b06a10fc65e6143518b96d3dca27b081a740bae261c2fb20375801a9d56d", - "sha256:d705f8aeecdf3262379644e4b55107a3b55860eb812b673b28d0fbc347a60c55", - "sha256:e898a0863421650f0bebac8ba40840fc02258ef4714cb7e1fd76b6a6354bda36", - "sha256:f8a7bff6e8664afc4e6c28b983845c5bc14965030e3fb98789734d416af77c4b" - ], - "markers": "python_full_version >= '3.6.1'", - "version": "==8.1" - }, "yarl": { "hashes": [ - "sha256:1707230e1ea48ea06a3e20acb4ce05a38d2465bd9566c21f48f6212a88e47536", - "sha256:1f269e8e6676193a94635399a77c9059e1826fb6265c9204c9e5a8ccd36006e1", - "sha256:2657716c1fc998f5f2675c0ee6ce91282e0da0ea9e4a94b584bb1917e11c1559", - "sha256:431faa6858f0ea323714d8b7b4a7da1db2eeb9403607f0eaa3800ab2c5a4b627", - "sha256:5bbcb195da7de57f4508b7508c33f7593e9516e27732d08b9aad8586c7b8c384", - "sha256:5c82f5b1499342339f22c83b97dbe2b8a09e47163fab86cd934a8dd46620e0fb", - "sha256:5d410f69b4f92c5e1e2a8ffb73337cd8a274388c6975091735795588a538e605", - "sha256:66b4f345e9573e004b1af184bc00431145cf5e089a4dcc1351505c1f5750192c", - "sha256:875b2a741ce0208f3b818008a859ab5d0f461e98a32bbdc6af82231a9e761c55", - "sha256:9a3266b047d15e78bba38c8455bf68b391c040231ca5965ef867f7cbbc60bde5", - "sha256:9a592c4aa642249e9bdaf76897d90feeb08118626b363a6be8788a9b300274b5", - "sha256:a1772068401d425e803999dada29a6babf041786e08be5e79ef63c9ecc4c9575", - "sha256:b065a5c3e050395ae563019253cc6c769a50fd82d7fa92d07476273521d56b7c", - "sha256:b325fefd574ebef50e391a1072d1712a60348ca29c183e1d546c9d87fec2cd32", - "sha256:cf5eb664910d759bbae0b76d060d6e21f8af5098242d66c448bbebaf2a7bfa70", - "sha256:f058b6541477022c7b54db37229f87dacf3b565de4f901ff5a0a78556a174fea", - "sha256:f5cfed0766837303f688196aa7002730d62c5cc802d98c6395ea1feb87252727" + "sha256:040b237f58ff7d800e6e0fd89c8439b841f777dd99b4a9cca04d6935564b9409", + "sha256:17668ec6722b1b7a3a05cc0167659f6c95b436d25a36c2d52db0eca7d3f72593", + "sha256:3a584b28086bc93c888a6c2aa5c92ed1ae20932f078c46509a66dce9ea5533f2", + "sha256:4439be27e4eee76c7632c2427ca5e73703151b22cae23e64adb243a9c2f565d8", + "sha256:48e918b05850fffb070a496d2b5f97fc31d15d94ca33d3d08a4f86e26d4e7c5d", + "sha256:9102b59e8337f9874638fcfc9ac3734a0cfadb100e47d55c20d0dc6087fb4692", + "sha256:9b930776c0ae0c691776f4d2891ebc5362af86f152dd0da463a6614074cb1b02", + "sha256:b3b9ad80f8b68519cc3372a6ca85ae02cc5a8807723ac366b53c0f089db19e4a", + "sha256:bc2f976c0e918659f723401c4f834deb8a8e7798a71be4382e024bcc3f7e23a8", + "sha256:c22c75b5f394f3d47105045ea551e08a3e804dc7e01b37800ca35b58f856c3d6", + "sha256:c52ce2883dc193824989a9b97a76ca86ecd1fa7955b14f87bf367a61b6232511", + "sha256:ce584af5de8830d8701b8979b18fcf450cef9a382b1a3c8ef189bedc408faf1e", + "sha256:da456eeec17fa8aa4594d9a9f27c0b1060b6a75f2419fe0c00609587b2695f4a", + "sha256:db6db0f45d2c63ddb1a9d18d1b9b22f308e52c83638c26b422d520a815c4b3fb", + "sha256:df89642981b94e7db5596818499c4b2219028f2a528c9c37cc1de45bf2fd3a3f", + "sha256:f18d68f2be6bf0e89f1521af2b1bb46e66ab0018faafa81d70f358153170a317", + "sha256:f379b7f83f23fe12823085cd6b906edc49df969eb99757f58ff382349a3303c6" ], "markers": "python_version >= '3.5'", - "version": "==1.5.0" + "version": "==1.5.1" } }, "develop": { @@ -728,11 +705,11 @@ }, "cfgv": { "hashes": [ - "sha256:1ccf53320421aeeb915275a196e23b3b8ae87dea8ac6698b1638001d4a486d53", - "sha256:c8e8f552ffcc6194f4e18dd4f68d9aef0c0d58ae7e7be8c82bee3c5e9edfa513" + "sha256:32e43d604bbe7896fe7c248a9c2276447dbef840feb28fe20494f62af110211d", + "sha256:cf22deb93d4bcf92f345a5c3cd39d3d41d6340adc60c78bbbd6588c384fda6a1" ], "markers": "python_full_version >= '3.6.1'", - "version": "==3.1.0" + "version": "==3.2.0" }, "coverage": { "hashes": [ @@ -968,11 +945,11 @@ }, "virtualenv": { "hashes": [ - "sha256:688a61d7976d82b92f7906c367e83bb4b3f0af96f8f75bfcd3da95608fe8ac6c", - "sha256:8f582a030156282a9ee9d319984b759a232b07f86048c1d6a9e394afa44e78c8" + "sha256:7b54fd606a1b85f83de49ad8d80dbec08e983a2d2f96685045b262ebc7481ee5", + "sha256:8cd7b2a4850b003a11be2fc213e206419efab41115cc14bca20e69654f2ac08e" ], "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", - "version": "==20.0.28" + "version": "==20.0.30" } } } -- cgit v1.2.3 From 806825ec56e13391fecd45ba0e0da6ab365e11ec Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 7 Aug 2020 11:08:12 -0700 Subject: HelpChannels: simplify control flow in is_empty --- bot/cogs/help_channels.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index a13207d20..bdfbf3392 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -739,7 +739,6 @@ class HelpChannels(commands.Cog): async def is_empty(self, channel: discord.TextChannel) -> bool: """Return True if there's an AVAILABLE_MSG and the messages leading up are bot messages.""" log.trace(f"Checking if #{channel} ({channel.id}) is empty.") - found = False # A limit of 100 results in a single API call. # If AVAILABLE_MSG isn't found within 100 messages, then assume the channel is not empty. @@ -751,10 +750,9 @@ class HelpChannels(commands.Cog): if self.match_bot_embed(msg, AVAILABLE_MSG): log.trace(f"#{channel} ({channel.id}) has the available message embed.") - found = True - break + return True - return found + return False async def check_cooldowns(self) -> None: """Remove expired cooldowns and re-schedule active ones.""" -- cgit v1.2.3 From 3cd4c92b1e24c8cfdae8c5c68c19607c62cc01ed Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Sat, 8 Aug 2020 18:32:47 +0100 Subject: Remove unnecessary edits during pagination --- bot/pagination.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/bot/pagination.py b/bot/pagination.py index 94c2d7c0c..bab98cacf 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -313,8 +313,6 @@ class LinePaginator(Paginator): log.debug(f"Got first page reaction - changing to page 1/{len(paginator.pages)}") - embed.description = "" - await message.edit(embed=embed) embed.description = paginator.pages[current_page] if footer_text: embed.set_footer(text=f"{footer_text} (Page {current_page + 1}/{len(paginator.pages)})") @@ -328,8 +326,6 @@ class LinePaginator(Paginator): log.debug(f"Got last page reaction - changing to page {current_page + 1}/{len(paginator.pages)}") - embed.description = "" - await message.edit(embed=embed) embed.description = paginator.pages[current_page] if footer_text: embed.set_footer(text=f"{footer_text} (Page {current_page + 1}/{len(paginator.pages)})") @@ -347,8 +343,6 @@ class LinePaginator(Paginator): current_page -= 1 log.debug(f"Got previous page reaction - changing to page {current_page + 1}/{len(paginator.pages)}") - embed.description = "" - await message.edit(embed=embed) embed.description = paginator.pages[current_page] if footer_text: @@ -368,8 +362,6 @@ class LinePaginator(Paginator): current_page += 1 log.debug(f"Got next page reaction - changing to page {current_page + 1}/{len(paginator.pages)}") - embed.description = "" - await message.edit(embed=embed) embed.description = paginator.pages[current_page] if footer_text: @@ -532,8 +524,6 @@ class ImagePaginator(Paginator): reaction_type = "next" # Magic happens here, after page and reaction_type is set - embed.description = "" - await message.edit(embed=embed) embed.description = paginator.pages[current_page] image = paginator.images[current_page] -- cgit v1.2.3