diff options
Diffstat (limited to '')
| -rw-r--r-- | bot/cogs/antimalware.py | 4 | ||||
| -rw-r--r-- | bot/cogs/defcon.py | 12 | ||||
| -rw-r--r-- | bot/cogs/help.py | 34 | ||||
| -rw-r--r-- | tests/bot/cogs/test_antimalware.py | 22 | 
4 files changed, 38 insertions, 34 deletions
diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index c76bd2c60..7894ec48f 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -55,6 +55,10 @@ class AntiMalware(Cog):          if not message.attachments or not message.guild:              return +        # Ignore webhook and bot messages +        if message.webhook_id or message.author.bot: +            return +          # Check if user is staff, if is, return          # Since we only care that roles exist to iterate over, check for the attr rather than a User/Member instance          if hasattr(message.author, "roles") and any(role.id in STAFF_ROLES for role in message.author.roles): diff --git a/bot/cogs/defcon.py b/bot/cogs/defcon.py index de0f4545e..9087ac454 100644 --- a/bot/cogs/defcon.py +++ b/bot/cogs/defcon.py @@ -10,7 +10,7 @@ from discord.ext.commands import Cog, Context, group  from bot.bot import Bot  from bot.cogs.moderation import ModLog -from bot.constants import Channels, Colours, Emojis, Event, Icons, Roles +from bot.constants import Channels, Colours, Emojis, Event, Icons, MODERATION_ROLES, Roles  from bot.decorators import with_role  log = logging.getLogger(__name__) @@ -119,7 +119,7 @@ class Defcon(Cog):                  )      @group(name='defcon', aliases=('dc',), invoke_without_command=True) -    @with_role(Roles.admins, Roles.owners) +    @with_role(*MODERATION_ROLES)      async def defcon_group(self, ctx: Context) -> None:          """Check the DEFCON status or run a subcommand."""          await ctx.send_help(ctx.command) @@ -163,7 +163,7 @@ class Defcon(Cog):              self.bot.stats.gauge("defcon.threshold", days)      @defcon_group.command(name='enable', aliases=('on', 'e'), root_aliases=("defon",)) -    @with_role(Roles.admins, Roles.owners) +    @with_role(*MODERATION_ROLES)      async def enable_command(self, ctx: Context) -> None:          """          Enable DEFCON mode. Useful in a pinch, but be sure you know what you're doing! @@ -176,7 +176,7 @@ class Defcon(Cog):          await self.update_channel_topic()      @defcon_group.command(name='disable', aliases=('off', 'd'), root_aliases=("defoff",)) -    @with_role(Roles.admins, Roles.owners) +    @with_role(*MODERATION_ROLES)      async def disable_command(self, ctx: Context) -> None:          """Disable DEFCON mode. Useful in a pinch, but be sure you know what you're doing!"""          self.enabled = False @@ -184,7 +184,7 @@ class Defcon(Cog):          await self.update_channel_topic()      @defcon_group.command(name='status', aliases=('s',)) -    @with_role(Roles.admins, Roles.owners) +    @with_role(*MODERATION_ROLES)      async def status_command(self, ctx: Context) -> None:          """Check the current status of DEFCON mode."""          embed = Embed( @@ -196,7 +196,7 @@ class Defcon(Cog):          await ctx.send(embed=embed)      @defcon_group.command(name='days') -    @with_role(Roles.admins, Roles.owners) +    @with_role(*MODERATION_ROLES)      async def days_command(self, ctx: Context, days: int) -> None:          """Set how old an account must be to join the server, in days, with DEFCON mode enabled."""          self.days = timedelta(days=days) diff --git a/bot/cogs/help.py b/bot/cogs/help.py index 25ce4ae0f..99d503f5c 100644 --- a/bot/cogs/help.py +++ b/bot/cogs/help.py @@ -1,50 +1,28 @@  import itertools  import logging -from asyncio import TimeoutError  from collections import namedtuple  from contextlib import suppress  from typing import List, Union -from discord import Colour, Embed, Member, Message, NotFound, Reaction, User +from discord import Colour, Embed  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 +from bot.constants import Channels, STAFF_ROLES  from bot.decorators import redirect_output  from bot.pagination import LinePaginator +from bot.utils.messages import wait_for_deletion  log = logging.getLogger(__name__)  COMMANDS_PER_PAGE = 8 -DELETE_EMOJI = Emojis.trashcan  PREFIX = constants.Bot.prefix  Category = namedtuple("Category", ["name", "description", "cogs"]) -async def help_cleanup(bot: Bot, author: Member, message: Message) -> None: -    """ -    Runs the cleanup for the help command. - -    Adds the :trashcan: reaction that, when clicked, will delete the help message. -    After a 300 second timeout, the reaction will be removed. -    """ -    def check(reaction: Reaction, user: User) -> bool: -        """Checks the reaction is :trashcan:, the author is original author and messages are the same.""" -        return str(reaction) == DELETE_EMOJI and user.id == author.id and reaction.message.id == message.id - -    await message.add_reaction(DELETE_EMOJI) - -    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):      """      Raised when a HelpSession Query doesn't match a command or cog. @@ -208,7 +186,7 @@ class CustomHelpCommand(HelpCommand):          """Send help for a single command."""          embed = await self.command_formatting(command)          message = await self.context.send(embed=embed) -        await help_cleanup(self.context.bot, self.context.author, message) +        await wait_for_deletion(message, (self.context.author.id,), self.context.bot)      @staticmethod      def get_commands_brief_details(commands_: List[Command], return_as_list: bool = False) -> Union[List[str], str]: @@ -247,7 +225,7 @@ class CustomHelpCommand(HelpCommand):              embed.description += f"\n**Subcommands:**\n{command_details}"          message = await self.context.send(embed=embed) -        await help_cleanup(self.context.bot, self.context.author, message) +        await wait_for_deletion(message, (self.context.author.id,), self.context.bot)      async def send_cog_help(self, cog: Cog) -> None:          """Send help for a cog.""" @@ -263,7 +241,7 @@ class CustomHelpCommand(HelpCommand):              embed.description += f"\n\n**Commands:**\n{command_details}"          message = await self.context.send(embed=embed) -        await help_cleanup(self.context.bot, self.context.author, message) +        await wait_for_deletion(message, (self.context.author.id,), self.context.bot)      @staticmethod      def _category_key(command: Command) -> str: diff --git a/tests/bot/cogs/test_antimalware.py b/tests/bot/cogs/test_antimalware.py index ecb7abf00..f50c0492d 100644 --- a/tests/bot/cogs/test_antimalware.py +++ b/tests/bot/cogs/test_antimalware.py @@ -23,6 +23,8 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase):          }          self.cog = antimalware.AntiMalware(self.bot)          self.message = MockMessage() +        self.message.webhook_id = None +        self.message.author.bot = None          self.whitelist = [".first", ".second", ".third"]      async def test_message_with_allowed_attachment(self): @@ -48,6 +50,26 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase):          self.message.delete.assert_not_called() +    async def test_webhook_message_with_illegal_extension(self): +        """A webhook message containing an illegal extension should be ignored.""" +        attachment = MockAttachment(filename="python.disallowed") +        self.message.webhook_id = 697140105563078727 +        self.message.attachments = [attachment] + +        await self.cog.on_message(self.message) + +        self.message.delete.assert_not_called() + +    async def test_bot_message_with_illegal_extension(self): +        """A bot message containing an illegal extension should be ignored.""" +        attachment = MockAttachment(filename="python.disallowed") +        self.message.author.bot = 409107086526644234 +        self.message.attachments = [attachment] + +        await self.cog.on_message(self.message) + +        self.message.delete.assert_not_called() +      async def test_message_with_illegal_extension_gets_deleted(self):          """A message containing an illegal extension should send an embed."""          attachment = MockAttachment(filename="python.disallowed")  |