aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2021-06-05 14:04:06 +0300
committerGravatar Hassan Abouelela <[email protected]>2021-06-05 14:04:06 +0300
commitf92338ef18d1bc5d11405a5d9e6ede4f9e080110 (patch)
tree122a1d6868c94cd6091c75237aa34d349370cdb9
parentMerge branch 'main' into voicechannel-mute (diff)
Properly Handles Indefinite Silences
Fixes a bug that stopped the duration `forever` from getting used as a valid duration for silence. Signed-off-by: Hassan Abouelela <[email protected]>
-rw-r--r--bot/converters.py6
-rw-r--r--bot/exts/moderation/silence.py7
-rw-r--r--tests/bot/exts/moderation/test_silence.py7
-rw-r--r--tests/bot/test_converters.py2
4 files changed, 16 insertions, 6 deletions
diff --git a/bot/converters.py b/bot/converters.py
index 2a3943831..595809517 100644
--- a/bot/converters.py
+++ b/bot/converters.py
@@ -416,11 +416,11 @@ class HushDurationConverter(Converter):
MINUTES_RE = re.compile(r"(\d+)(?:M|m|$)")
- async def convert(self, ctx: Context, argument: str) -> t.Optional[int]:
+ async def convert(self, ctx: Context, argument: str) -> int:
"""
Convert `argument` to a duration that's max 15 minutes or None.
- If `"forever"` is passed, None is returned; otherwise an int of the extracted time.
+ If `"forever"` is passed, -1 is returned; otherwise an int of the extracted time.
Accepted formats are:
* <duration>,
* <duration>m,
@@ -428,7 +428,7 @@ class HushDurationConverter(Converter):
* forever.
"""
if argument == "forever":
- return None
+ return -1
match = self.MINUTES_RE.match(argument)
if not match:
raise BadArgument(f"{argument} is not a valid minutes duration.")
diff --git a/bot/exts/moderation/silence.py b/bot/exts/moderation/silence.py
index 8e4ce7ae2..8025f3df6 100644
--- a/bot/exts/moderation/silence.py
+++ b/bot/exts/moderation/silence.py
@@ -200,9 +200,9 @@ class Silence(commands.Cog):
ctx: Context,
duration_or_channel: typing.Union[TextOrVoiceChannel, int],
duration: HushDurationConverter
- ) -> typing.Tuple[TextOrVoiceChannel, int]:
+ ) -> typing.Tuple[TextOrVoiceChannel, Optional[int]]:
"""Helper method to parse the arguments of the silence command."""
- duration: int
+ duration: Optional[int]
if duration_or_channel:
if isinstance(duration_or_channel, (TextChannel, VoiceChannel)):
@@ -213,6 +213,9 @@ class Silence(commands.Cog):
else:
channel = ctx.channel
+ if duration == -1:
+ duration = None
+
return channel, duration
async def _set_silence_overwrites(self, channel: TextOrVoiceChannel, *, kick: bool = False) -> bool:
diff --git a/tests/bot/exts/moderation/test_silence.py b/tests/bot/exts/moderation/test_silence.py
index a7ea733c5..59a5893ef 100644
--- a/tests/bot/exts/moderation/test_silence.py
+++ b/tests/bot/exts/moderation/test_silence.py
@@ -641,6 +641,13 @@ class SilenceTests(unittest.IsolatedAsyncioTestCase):
await self.cog.silence.callback(self.cog, ctx, None, None)
self.cog.scheduler.schedule_later.assert_not_called()
+ async def test_indefinite_silence(self):
+ """Test silencing a channel forever."""
+ with mock.patch.object(self.cog, "_schedule_unsilence") as unsilence:
+ ctx = MockContext(channel=self.text_channel)
+ await self.cog.silence.callback(self.cog, ctx, -1)
+ unsilence.assert_awaited_once_with(ctx, ctx.channel, None)
+
@autospec(silence.Silence, "unsilence_timestamps", pass_mocks=False)
class UnsilenceTests(unittest.IsolatedAsyncioTestCase):
diff --git a/tests/bot/test_converters.py b/tests/bot/test_converters.py
index 4af84dde5..2a1c4e543 100644
--- a/tests/bot/test_converters.py
+++ b/tests/bot/test_converters.py
@@ -291,7 +291,7 @@ class ConverterTests(unittest.IsolatedAsyncioTestCase):
("10", 10),
("5m", 5),
("5M", 5),
- ("forever", None),
+ ("forever", -1),
)
converter = HushDurationConverter()
for minutes_string, expected_minutes in test_values: