aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2020-11-24 13:19:51 +0300
committerGravatar Hassan Abouelela <[email protected]>2020-11-24 13:21:21 +0300
commit7cb3024e71cd81e9ef29f5f10cb2bc5fe62ad846 (patch)
tree49e43a52e736ce5ce195de68e33d32da8b140655
parentFixes Typo in Doc (diff)
Refractors SendMessage Function
Refractors the send message function in silence to make it more understandable and flexible. Signed-off-by: Hassan Abouelela <[email protected]>
-rw-r--r--bot/exts/moderation/silence.py31
-rw-r--r--tests/bot/exts/moderation/test_silence.py54
2 files changed, 46 insertions, 39 deletions
diff --git a/bot/exts/moderation/silence.py b/bot/exts/moderation/silence.py
index 8ad30f0d9..7bc51ee93 100644
--- a/bot/exts/moderation/silence.py
+++ b/bot/exts/moderation/silence.py
@@ -26,7 +26,7 @@ MSG_SILENCE_SUCCESS = f"{Emojis.check_mark} silenced current channel for {{durat
MSG_UNSILENCE_FAIL = f"{Emojis.cross_mark} current channel was not silenced."
MSG_UNSILENCE_MANUAL = (
- f"{Emojis.cross_mark} current channel was not unsilenced because the channel overwrites were "
+ f"{Emojis.cross_mark} current channel was not unsilenced because the current overwrites were "
f"set manually or the cache was prematurely cleared. "
f"Please edit the overwrites manually to unsilence."
)
@@ -119,30 +119,27 @@ class Silence(commands.Cog):
async def send_message(
self, message: str, source_channel: TextChannel, target_channel: Union[TextChannel, VoiceChannel],
- alert_target: bool = False, duration: int = 0
+ alert_target: bool = False
) -> None:
"""Helper function to send message confirmation to `source_channel`, and notification to `target_channel`."""
- await source_channel.send(
- message.replace("current", target_channel.mention if source_channel != target_channel else "current")
- .replace("{duration}", str(duration))
- )
-
+ # Get TextChannel connected to VoiceChannel if channel is of type voice
voice_chat = None
if isinstance(target_channel, VoiceChannel):
- # Send to relevant channel
voice_chat = await self._get_related_text_channel(target_channel)
- if alert_target and source_channel != target_channel:
- if isinstance(target_channel, VoiceChannel):
- if voice_chat is None or voice_chat == source_channel:
- return
+ # Reply to invocation channel
+ source_reply = message
+ if source_channel != target_channel:
+ source_reply = source_reply.replace("current channel", target_channel.mention)
+ await source_channel.send(source_reply)
- await voice_chat.send(
- message.replace("{duration}", str(duration)).replace("current", target_channel.mention)
- )
+ # Reply to target channel
+ if alert_target and source_channel != target_channel and source_channel != voice_chat:
+ if isinstance(target_channel, VoiceChannel) and (voice_chat is not None or voice_chat != source_channel):
+ await voice_chat.send(message.replace("current channel", target_channel.mention))
else:
- await target_channel.send(message.replace("{duration}", str(duration)))
+ await target_channel.send(message)
@commands.command(aliases=("hush",))
@lock_arg(LOCK_NAMESPACE, "ctx", attrgetter("channel"), raise_error=True)
@@ -179,7 +176,7 @@ class Silence(commands.Cog):
else:
log.info(f"Silenced {channel_info} for {duration} minute(s).")
- await self.send_message(MSG_SILENCE_SUCCESS, ctx.channel, channel, True, duration)
+ await self.send_message(MSG_SILENCE_SUCCESS.format(duration=duration), ctx.channel, channel, True)
@commands.command(aliases=("unhush",))
async def unsilence(self, ctx: Context, *, channel: Union[TextChannel, VoiceChannel] = None) -> None:
diff --git a/tests/bot/exts/moderation/test_silence.py b/tests/bot/exts/moderation/test_silence.py
index 5c6e2d0f1..70678d207 100644
--- a/tests/bot/exts/moderation/test_silence.py
+++ b/tests/bot/exts/moderation/test_silence.py
@@ -7,7 +7,7 @@ from unittest.mock import Mock
from async_rediscache import RedisSession
from discord import PermissionOverwrite
-from bot.constants import Channels, Emojis, Guild, Roles
+from bot.constants import Channels, Guild, Roles
from bot.exts.moderation import silence
from tests.helpers import MockBot, MockContext, MockGuild, MockMember, MockTextChannel, MockVoiceChannel, autospec
@@ -198,42 +198,48 @@ class SilenceCogTests(unittest.IsolatedAsyncioTestCase):
reset()
with self.subTest("Replacement One Channel Test"):
- await self.cog.send_message("The following should be replaced: current",
- text_channel_1, text_channel_2, False)
- text_channel_1.send.assert_called_once_with(f"The following should be replaced: {text_channel_1.mention}")
+ await self.cog.send_message(
+ "Current. The following should be replaced: current channel.", text_channel_1, text_channel_2, False
+ )
+
+ text_channel_1.send.assert_called_once_with(
+ f"Current. The following should be replaced: {text_channel_1.mention}."
+ )
+
text_channel_2.send.assert_not_called()
reset()
with self.subTest("Replacement Two Channel Test"):
- await self.cog.send_message("The following should be replaced: current",
- text_channel_1, text_channel_2, True)
- text_channel_1.send.assert_called_once_with(f"The following should be replaced: {text_channel_1.mention}")
- text_channel_2.send.assert_called_once_with("The following should be replaced: current")
+ await self.cog.send_message(
+ "Current. The following should be replaced: current channel.", text_channel_1, text_channel_2, True
+ )
- reset()
- with self.subTest("Replace Duration"):
- await self.cog.send_message(f"{Emojis.check_mark} The following should be replaced: {{duration}}",
- text_channel_1, text_channel_2, False)
- text_channel_1.send.assert_called_once_with(f"{Emojis.check_mark} The following should be replaced: 0")
- text_channel_2.send.assert_not_called()
+ text_channel_1.send.assert_called_once_with(
+ f"Current. The following should be replaced: {text_channel_1.mention}."
+ )
+
+ text_channel_2.send.assert_called_once_with("Current. The following should be replaced: current channel.")
reset()
with self.subTest("Text and Voice"):
- await self.cog.send_message("This should show up just here",
- text_channel_1, voice_channel, False)
+ 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")
reset()
with self.subTest("Text and Voice"):
- await self.cog.send_message("This should show up as current",
- text_channel_1, voice_channel, True)
+ await self.cog.send_message(
+ "This should show up as current channel", text_channel_1, voice_channel, True
+ )
text_channel_1.send.assert_called_once_with(f"This should show up as {voice_channel.mention}")
text_channel_2.send.assert_called_once_with(f"This should show up as {voice_channel.mention}")
reset()
with self.subTest("Text and Voice Same Invocation"):
- await self.cog.send_message("This should show up as current",
- text_channel_2, voice_channel, True)
+ await self.cog.send_message(
+ "This should show up as current channel", text_channel_2, voice_channel, True
+ )
text_channel_2.send.assert_called_once_with(f"This should show up as {voice_channel.mention}")
async def test_get_related_text_channel(self):
@@ -455,7 +461,9 @@ class SilenceTests(unittest.IsolatedAsyncioTestCase):
if ctx.channel == target or target is None:
ctx.channel.send.assert_called_once_with(message)
else:
- ctx.channel.send.assert_called_once_with(message.replace("current", text_channel.mention))
+ ctx.channel.send.assert_called_once_with(
+ message.replace("current channel", text_channel.mention)
+ )
target.send.assert_called_once_with(message)
ctx.channel.send.reset_mock()
@@ -643,7 +651,9 @@ class UnsilenceTests(unittest.IsolatedAsyncioTestCase):
if ctx.channel == target or target is None:
ctx.channel.send.assert_called_once_with(message)
else:
- ctx.channel.send.assert_called_once_with(message.replace("current", text_channel.mention))
+ ctx.channel.send.assert_called_once_with(
+ message.replace("current channel", text_channel.mention)
+ )
target.send.assert_called_once_with(message)
ctx.channel.send.reset_mock()