aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2021-05-12 00:03:26 +0300
committerGravatar Hassan Abouelela <[email protected]>2021-05-12 00:12:00 +0300
commit2f772082f288671354b6bbd71d9a9caad6fe87af (patch)
treea113286f7bf361f4284de38d1ca5956af5784d69
parentRename `Manual` Variable To Clarify Intentions (diff)
Updates Silence To Use `.format`
Uses `.format` to create silence and unsilence messages instead of `.replace`. Signed-off-by: Hassan Abouelela <[email protected]>
-rw-r--r--bot/exts/moderation/silence.py20
-rw-r--r--tests/bot/exts/moderation/test_silence.py19
2 files changed, 20 insertions, 19 deletions
diff --git a/bot/exts/moderation/silence.py b/bot/exts/moderation/silence.py
index 2012d75d9..e5c96e76f 100644
--- a/bot/exts/moderation/silence.py
+++ b/bot/exts/moderation/silence.py
@@ -19,17 +19,17 @@ log = logging.getLogger(__name__)
LOCK_NAMESPACE = "silence"
-MSG_SILENCE_FAIL = f"{constants.Emojis.cross_mark} current channel is already silenced."
-MSG_SILENCE_PERMANENT = f"{constants.Emojis.check_mark} silenced current channel indefinitely."
-MSG_SILENCE_SUCCESS = f"{constants.Emojis.check_mark} silenced current channel for {{duration}} minute(s)."
+MSG_SILENCE_FAIL = f"{constants.Emojis.cross_mark} {{channel}} is already silenced."
+MSG_SILENCE_PERMANENT = f"{constants.Emojis.check_mark} silenced {{channel}} indefinitely."
+MSG_SILENCE_SUCCESS = f"{constants.Emojis.check_mark} silenced {{{{channel}}}} for {{duration}} minute(s)."
-MSG_UNSILENCE_FAIL = f"{constants.Emojis.cross_mark} current channel was not silenced."
+MSG_UNSILENCE_FAIL = f"{constants.Emojis.cross_mark} {{channel}} was not silenced."
MSG_UNSILENCE_MANUAL = (
- f"{constants.Emojis.cross_mark} current channel was not unsilenced because the current overwrites were "
+ f"{constants.Emojis.cross_mark} {{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."
)
-MSG_UNSILENCE_SUCCESS = f"{constants.Emojis.check_mark} unsilenced current channel."
+MSG_UNSILENCE_SUCCESS = f"{constants.Emojis.check_mark} unsilenced {{channel}}."
TextOrVoiceChannel = Union[TextChannel, VoiceChannel]
@@ -135,7 +135,9 @@ class Silence(commands.Cog):
# Reply to invocation channel
source_reply = message
if source_channel != target_channel:
- source_reply = source_reply.replace("current channel", target_channel.mention)
+ source_reply = source_reply.format(channel=target_channel.mention)
+ else:
+ source_reply = source_reply.format(channel="current channel")
await source_channel.send(source_reply)
# Reply to target channel
@@ -143,10 +145,10 @@ class Silence(commands.Cog):
if isinstance(target_channel, VoiceChannel):
voice_chat = self.bot.get_channel(VOICE_CHANNELS.get(target_channel.id))
if voice_chat and source_channel != voice_chat:
- await voice_chat.send(message.replace("current channel", target_channel.mention))
+ await voice_chat.send(message.format(channel=target_channel.mention))
elif source_channel != target_channel:
- await target_channel.send(message)
+ await target_channel.send(message.format(channel="current channel"))
@commands.command(aliases=("hush",))
@lock(LOCK_NAMESPACE, _select_lock_channel, raise_error=True)
diff --git a/tests/bot/exts/moderation/test_silence.py b/tests/bot/exts/moderation/test_silence.py
index de7230ae5..af6dd5a37 100644
--- a/tests/bot/exts/moderation/test_silence.py
+++ b/tests/bot/exts/moderation/test_silence.py
@@ -792,21 +792,20 @@ class SendMessageTests(unittest.IsolatedAsyncioTestCase):
async def test_duration_replacement(self):
"""Tests that the channel name was set correctly for one target channel."""
- message = "Current. The following should be replaced: current channel."
+ message = "Current. The following should be replaced: {channel}."
await self.cog.send_message(message, *self.text_channels, alert_target=False)
- updated_message = message.replace("current channel", self.text_channels[0].mention)
+ updated_message = message.format(channel=self.text_channels[0].mention)
self.text_channels[0].send.assert_awaited_once_with(updated_message)
self.text_channels[1].send.assert_not_called()
async def test_name_replacement_multiple_channels(self):
"""Tests that the channel name was set correctly for two channels."""
- message = "Current. The following should be replaced: current channel."
+ message = "Current. The following should be replaced: {channel}."
await self.cog.send_message(message, *self.text_channels, alert_target=True)
- updated_message = message.replace("current channel", self.text_channels[0].mention)
- self.text_channels[0].send.assert_awaited_once_with(updated_message)
- self.text_channels[1].send.assert_awaited_once_with(message)
+ self.text_channels[0].send.assert_awaited_once_with(message.format(channel=self.text_channels[0].mention))
+ self.text_channels[1].send.assert_awaited_once_with(message.format(channel="current channel"))
async def test_silence_voice(self):
"""Tests that the correct message was sent when a voice channel is muted without alerting."""
@@ -820,10 +819,10 @@ class SendMessageTests(unittest.IsolatedAsyncioTestCase):
with unittest.mock.patch.object(silence, "VOICE_CHANNELS") as mock_voice_channels:
mock_voice_channels.get.return_value = self.text_channels[1].id
- message = "This should show up as current channel."
+ message = "This should show up as {channel}."
await self.cog.send_message(message, self.text_channels[0], self.voice_channel, alert_target=True)
- updated_message = message.replace("current channel", self.voice_channel.mention)
+ updated_message = message.format(channel=self.voice_channel.mention)
self.text_channels[0].send.assert_awaited_once_with(updated_message)
self.text_channels[1].send.assert_awaited_once_with(updated_message)
@@ -834,10 +833,10 @@ class SendMessageTests(unittest.IsolatedAsyncioTestCase):
with unittest.mock.patch.object(silence, "VOICE_CHANNELS") as mock_voice_channels:
mock_voice_channels.get.return_value = self.text_channels[1].id
- message = "This should show up as current channel."
+ message = "This should show up as {channel}."
await self.cog.send_message(message, self.text_channels[1], self.voice_channel, alert_target=True)
- updated_message = message.replace("current channel", self.voice_channel.mention)
+ updated_message = message.format(channel=self.voice_channel.mention)
self.text_channels[1].send.assert_awaited_once_with(updated_message)
mock_voice_channels.get.assert_called_once_with(self.voice_channel.id)