diff options
| -rw-r--r-- | bot/exts/moderation/silence.py | 34 | ||||
| -rw-r--r-- | tests/bot/exts/moderation/test_silence.py | 23 | 
2 files changed, 30 insertions, 27 deletions
diff --git a/bot/exts/moderation/silence.py b/bot/exts/moderation/silence.py index befcb2cc4..31103bc3e 100644 --- a/bot/exts/moderation/silence.py +++ b/bot/exts/moderation/silence.py @@ -2,7 +2,7 @@ import json  import logging  from contextlib import suppress  from datetime import datetime, timedelta, timezone -from typing import Optional, Union +from typing import Optional, OrderedDict, Union  from async_rediscache import RedisCache  from discord import Guild, PermissionOverwrite, TextChannel, VoiceChannel @@ -81,6 +81,16 @@ class SilenceNotifier(tasks.Loop):              ) +async def _select_lock_channel(args: OrderedDict[str, any]) -> TextOrVoiceChannel: +    """Passes the channel to be silenced to the resource lock.""" +    channel = args["channel"] +    if channel is not None: +        return channel + +    else: +        return args["ctx"].channel + +  class Silence(commands.Cog):      """Commands for stopping channel messages for `verified` role in a channel.""" @@ -118,7 +128,7 @@ class Silence(commands.Cog):          message: str,          source_channel: TextChannel,          target_channel: TextOrVoiceChannel, -        alert_target: bool = False +        *, alert_target: bool = False      ) -> None:          """Helper function to send message confirmation to `source_channel`, and notification to `target_channel`."""          # Reply to invocation channel @@ -137,23 +147,14 @@ class Silence(commands.Cog):              elif source_channel != target_channel:                  await target_channel.send(message) -    async def _select_lock_channel(*args) -> TextOrVoiceChannel: -        """Passes the channel to be silenced to the resource lock.""" -        channel = args[0].get("channel") -        if channel is not None: -            return channel - -        else: -            return args[0].get("ctx").channel -      @commands.command(aliases=("hush",))      @lock(LOCK_NAMESPACE, _select_lock_channel, raise_error=True)      async def silence(          self,          ctx: Context,          duration: HushDurationConverter = 10, -        kick: bool = False, -        *, channel: TextOrVoiceChannel = None +        channel: TextOrVoiceChannel = None, +        *, kick: bool = False      ) -> None:          """          Silence the current channel for `duration` minutes or `forever`. @@ -186,11 +187,12 @@ class Silence(commands.Cog):          if duration is None:              self.notifier.add_channel(channel)              log.info(f"Silenced {channel_info} indefinitely.") -            await self.send_message(MSG_SILENCE_PERMANENT, ctx.channel, channel, True) +            await self.send_message(MSG_SILENCE_PERMANENT, ctx.channel, channel, alert_target=True)          else:              log.info(f"Silenced {channel_info} for {duration} minute(s).") -            await self.send_message(MSG_SILENCE_SUCCESS.format(duration=duration), ctx.channel, channel, True) +            formatted_message = MSG_SILENCE_SUCCESS.format(duration=duration) +            await self.send_message(formatted_message, ctx.channel, channel, alert_target=True)      @commands.command(aliases=("unhush",))      async def unsilence(self, ctx: Context, *, channel: TextOrVoiceChannel = None) -> None: @@ -227,7 +229,7 @@ class Silence(commands.Cog):                  await self.send_message(MSG_UNSILENCE_FAIL, msg_channel, channel)          else: -            await self.send_message(MSG_UNSILENCE_SUCCESS, msg_channel, channel, True) +            await self.send_message(MSG_UNSILENCE_SUCCESS, msg_channel, channel, alert_target=True)      async def _set_silence_overwrites(self, channel: TextOrVoiceChannel, kick: bool = False) -> bool:          """Set silence permission overwrites for `channel` and return True if successful.""" diff --git a/tests/bot/exts/moderation/test_silence.py b/tests/bot/exts/moderation/test_silence.py index 31894761c..038e0a1a4 100644 --- a/tests/bot/exts/moderation/test_silence.py +++ b/tests/bot/exts/moderation/test_silence.py @@ -178,20 +178,20 @@ class SilenceCogTests(unittest.IsolatedAsyncioTestCase):              mock_voice_channels.reset_mock()          with self.subTest("Basic One Channel Test"): -            await self.cog.send_message("Text basic message.", text_channel_1, text_channel_2, False) +            await self.cog.send_message("Text basic message.", text_channel_1, text_channel_2, alert_target=False)              text_channel_1.send.assert_called_once_with("Text basic message.")              text_channel_2.send.assert_not_called()          reset()          with self.subTest("Basic Two Channel Test"): -            await self.cog.send_message("Text basic message.", text_channel_1, text_channel_2, True) +            await self.cog.send_message("Text basic message.", text_channel_1, text_channel_2, alert_target=True)              text_channel_1.send.assert_called_once_with("Text basic message.")              text_channel_2.send.assert_called_once_with("Text basic message.")          reset()          with self.subTest("Replacement One Channel Test"):              message = "Current. The following should be replaced: current channel." -            await self.cog.send_message(message, text_channel_1, text_channel_2, False) +            await self.cog.send_message(message, text_channel_1, text_channel_2, alert_target=False)              text_channel_1.send.assert_called_once_with(message.replace("current channel", text_channel_1.mention))              text_channel_2.send.assert_not_called() @@ -199,15 +199,16 @@ class SilenceCogTests(unittest.IsolatedAsyncioTestCase):          reset()          with self.subTest("Replacement Two Channel Test"):              message = "Current. The following should be replaced: current channel." -            await self.cog.send_message(message, text_channel_1, text_channel_2, True) +            await self.cog.send_message(message, text_channel_1, text_channel_2, alert_target=True)              text_channel_1.send.assert_called_once_with(message.replace("current channel", text_channel_1.mention))              text_channel_2.send.assert_called_once_with(message)          reset()          with self.subTest("Text and Voice"): -            await self.cog.send_message("This should show up just here", text_channel_1, voice_channel, False) -            text_channel_1.send.assert_called_once_with("This should show up just here") +            message = "This should show up just here" +            await self.cog.send_message(message, text_channel_1, voice_channel, alert_target=False) +            text_channel_1.send.assert_called_once_with(message)          with mock.patch.object(self.cog, "bot") as bot_mock:              bot_mock.get_channel.return_value = text_channel_2 @@ -215,7 +216,7 @@ class SilenceCogTests(unittest.IsolatedAsyncioTestCase):              reset()              with self.subTest("Text and Voice"):                  message = "This should show up as current channel" -                await self.cog.send_message(message, text_channel_1, voice_channel, True) +                await self.cog.send_message(message, text_channel_1, voice_channel, alert_target=True)                  text_channel_1.send.assert_called_once_with(message.replace("current channel", voice_channel.mention))                  text_channel_2.send.assert_called_once_with(message.replace("current channel", voice_channel.mention)) @@ -226,7 +227,7 @@ class SilenceCogTests(unittest.IsolatedAsyncioTestCase):              reset()              with self.subTest("Text and Voice Same Invocation"):                  message = "This should show up as current channel" -                await self.cog.send_message(message, text_channel_2, voice_channel, True) +                await self.cog.send_message(message, text_channel_2, voice_channel, alert_target=True)                  text_channel_2.send.assert_called_once_with(message.replace("current channel", voice_channel.mention))                  mock_voice_channels.get.assert_called_once_with(voice_channel.id) @@ -436,7 +437,7 @@ class SilenceTests(unittest.IsolatedAsyncioTestCase):              with self.subTest(target_channel=target, message=message):                  with mock.patch.object(self.cog, "bot") as bot_mock:                      bot_mock.get_channel.return_value = AsyncMock() -                    await self.cog.silence.callback(self.cog, ctx, 10, False, channel=target) +                    await self.cog.silence.callback(self.cog, ctx, 10, target, kick=False)                  if ctx.channel == target or target is None:                      ctx.channel.send.assert_called_once_with(message) @@ -822,9 +823,9 @@ class UnsilenceTests(unittest.IsolatedAsyncioTestCase):                  await self.cog._unsilence_wrapper(channel, context)                  if context is None: -                    send_message.assert_called_once_with(message, channel, channel, True) +                    send_message.assert_called_once_with(message, channel, channel, alert_target=True)                  else: -                    send_message.assert_called_once_with(message, context.channel, channel, True) +                    send_message.assert_called_once_with(message, context.channel, channel, alert_target=True)                  channel.set_permissions.assert_called_once_with(role, overwrite=overwrites)                  if channel != ctx.channel:  |