aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/cogs/moderation/silence.py24
-rw-r--r--tests/bot/cogs/moderation/test_silence.py28
2 files changed, 8 insertions, 44 deletions
diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py
index 8ed1cb28b..5df1fbbc0 100644
--- a/bot/cogs/moderation/silence.py
+++ b/bot/cogs/moderation/silence.py
@@ -15,26 +15,12 @@ from bot.utils.checks import with_role_check
log = logging.getLogger(__name__)
-class FirstHash(tuple):
- """Tuple with only first item used for hash and eq."""
-
- def __new__(cls, *args):
- """Construct tuple from `args`."""
- return super().__new__(cls, args)
-
- def __hash__(self):
- return hash((self[0],))
-
- def __eq__(self, other: "FirstHash"):
- return self[0] == other[0]
-
-
class SilenceNotifier(tasks.Loop):
"""Loop notifier for posting notices to `alert_channel` containing added channels."""
def __init__(self, alert_channel: TextChannel):
super().__init__(self._notifier, seconds=1, minutes=0, hours=0, count=None, reconnect=True, loop=None)
- self._silenced_channels = set()
+ self._silenced_channels = {}
self._alert_channel = alert_channel
def add_channel(self, channel: TextChannel) -> None:
@@ -42,12 +28,12 @@ class SilenceNotifier(tasks.Loop):
if not self._silenced_channels:
self.start()
log.info("Starting notifier loop.")
- self._silenced_channels.add(FirstHash(channel, self._current_loop))
+ self._silenced_channels[channel] = self._current_loop
def remove_channel(self, channel: TextChannel) -> None:
"""Remove channel from `_silenced_channels` and stop loop if no channels remain."""
with suppress(KeyError):
- self._silenced_channels.remove(FirstHash(channel))
+ del self._silenced_channels[channel]
if not self._silenced_channels:
self.stop()
log.info("Stopping notifier loop.")
@@ -58,11 +44,11 @@ class SilenceNotifier(tasks.Loop):
if self._current_loop and not self._current_loop/60 % 15:
log.debug(
f"Sending notice with channels: "
- f"{', '.join(f'#{channel} ({channel.id})' for channel, _ in self._silenced_channels)}."
+ f"{', '.join(f'#{channel} ({channel.id})' for channel in self._silenced_channels)}."
)
channels_text = ', '.join(
f"{channel.mention} for {(self._current_loop-start)//60} min"
- for channel, start in self._silenced_channels
+ for channel, start in self._silenced_channels.items()
)
await self._alert_channel.send(f"<@&{Roles.moderators}> currently silenced channels: {channels_text}")
diff --git a/tests/bot/cogs/moderation/test_silence.py b/tests/bot/cogs/moderation/test_silence.py
index d4719159e..6114fee21 100644
--- a/tests/bot/cogs/moderation/test_silence.py
+++ b/tests/bot/cogs/moderation/test_silence.py
@@ -2,33 +2,11 @@ import unittest
from unittest import mock
from unittest.mock import MagicMock, Mock
-from bot.cogs.moderation.silence import FirstHash, Silence, SilenceNotifier
+from bot.cogs.moderation.silence import Silence, SilenceNotifier
from bot.constants import Channels, Emojis, Guild, Roles
from tests.helpers import MockBot, MockContext, MockTextChannel
-class FirstHashTests(unittest.TestCase):
- def setUp(self) -> None:
- self.test_cases = (
- (FirstHash(0, 4), FirstHash(0, 5)),
- (FirstHash("string", None), FirstHash("string", True))
- )
-
- def test_hashes_equal(self):
- """Check hashes equal with same first item."""
-
- for tuple1, tuple2 in self.test_cases:
- with self.subTest(tuple1=tuple1, tuple2=tuple2):
- self.assertEqual(hash(tuple1), hash(tuple2))
-
- def test_eq(self):
- """Check objects are equal with same first item."""
-
- for tuple1, tuple2 in self.test_cases:
- with self.subTest(tuple1=tuple1, tuple2=tuple2):
- self.assertTrue(tuple1 == tuple2)
-
-
class SilenceNotifierTests(unittest.IsolatedAsyncioTestCase):
def setUp(self) -> None:
self.alert_channel = MockTextChannel()
@@ -41,7 +19,7 @@ class SilenceNotifierTests(unittest.IsolatedAsyncioTestCase):
channel = Mock()
with mock.patch.object(self.notifier, "_silenced_channels") as silenced_channels:
self.notifier.add_channel(channel)
- silenced_channels.add.assert_called_with(FirstHash(channel, self.notifier._current_loop))
+ silenced_channels.__setitem__.assert_called_with(channel, self.notifier._current_loop)
def test_add_channel_starts_loop(self):
"""Loop is started if `_silenced_channels` was empty."""
@@ -59,7 +37,7 @@ class SilenceNotifierTests(unittest.IsolatedAsyncioTestCase):
channel = Mock()
with mock.patch.object(self.notifier, "_silenced_channels") as silenced_channels:
self.notifier.remove_channel(channel)
- silenced_channels.remove.assert_called_with(FirstHash(channel))
+ silenced_channels.__delitem__.assert_called_with(channel)
def test_remove_channel_stops_loop(self):
"""Notifier loop is stopped if `_silenced_channels` is empty after remove."""