From eac3be892a31a508f966fd73f1802086d83ed954 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Mon, 1 Jun 2020 19:09:22 +0200 Subject: Use Scheduler instead of asyncio.sleep on silence cog `asyncio.sleep` doesn't provide us with the ability to stop that timer, while in most of the cases, this is fine, there is a possibility that channel will be unsilenced manually and silenced again, but this sleep from the first silence will cancel the second (new) silence. This will replace this `asyncio.sleep` with Scheduler which provides the ability to cancel the unsilencing task when aborted manually. That means we also have the ability to send a response if the channel is not silenced and someone tries to unsilence it. --- bot/cogs/moderation/silence.py | 66 +++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index 25febfa51..a2fd39906 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -1,19 +1,54 @@ import asyncio +import datetime import logging +from collections import namedtuple from contextlib import suppress from typing import Optional -from discord import TextChannel -from discord.ext import commands, tasks -from discord.ext.commands import Context - from bot.bot import Bot -from bot.constants import Channels, Emojis, Guild, MODERATION_ROLES, Roles +from bot.constants import MODERATION_ROLES, Channels, Emojis, Guild, Roles from bot.converters import HushDurationConverter +from bot.utils import time from bot.utils.checks import with_role_check +from bot.utils.scheduling import Scheduler +from discord import TextChannel +from discord.ext import commands, tasks +from discord.ext.commands import Context log = logging.getLogger(__name__) +SilencedChannel = namedtuple( + "SilencedChannel", ("id", "ctx", "silence", "stop") +) + + +class UnsilenceScheduler(Scheduler): + """Scheduler for unsilencing channels""" + + def __init__(self, bot: Bot): + super().__init__() + + self.bot = bot + + async def schedule_unsilence(self, channel: SilencedChannel) -> None: + """Schedule expiration for silenced channels""" + await self.bot.wait_until_guild_available() + log.debug("Scheduling unsilencer") + self.schedule_task(channel.id, channel) + + async def _scheduled_task(self, channel: SilencedChannel) -> None: + """ + Removes expired silenced channel from `silence.muted_channels` + and calls `silence.unsilence` to unsilence the channel + after the silence expires + """ + await time.wait_until(channel.stop) + log.info("Unsilencing channel after set delay.") + + # Because `silence.unsilence` explicitly cancels this scheduled task, it is shielded + # to avoid prematurely cancelling itself. + await asyncio.shield(channel.ctx.invoke(channel.silence.unsilence)) + class SilenceNotifier(tasks.Loop): """Loop notifier for posting notices to `alert_channel` containing added channels.""" @@ -61,6 +96,7 @@ class Silence(commands.Cog): self.muted_channels = set() self._get_instance_vars_task = self.bot.loop.create_task(self._get_instance_vars()) self._get_instance_vars_event = asyncio.Event() + self.scheduler = UnsilenceScheduler(bot) async def _get_instance_vars(self) -> None: """Get instance variables after they're available to get from the guild.""" @@ -90,9 +126,15 @@ class Silence(commands.Cog): return await ctx.send(f"{Emojis.check_mark} silenced current channel for {duration} minute(s).") - await asyncio.sleep(duration*60) - log.info("Unsilencing channel after set delay.") - await ctx.invoke(self.unsilence) + + channel = SilencedChannel( + id=ctx.channel.id, + ctx=ctx, + silence=self, + stop=datetime.datetime.now() + datetime.timedelta(minutes=duration), + ) + + await self.scheduler.schedule_unsilence(channel) @commands.command(aliases=("unhush",)) async def unsilence(self, ctx: Context) -> None: @@ -103,8 +145,11 @@ class Silence(commands.Cog): """ await self._get_instance_vars_event.wait() log.debug(f"Unsilencing channel #{ctx.channel} from {ctx.author}'s command.") - if await self._unsilence(ctx.channel): - await ctx.send(f"{Emojis.check_mark} unsilenced current channel.") + if not await self._unsilence(ctx.channel): + await ctx.send(f"{Emojis.cross_mark} current channel is not silenced.") + return + + await ctx.send(f"{Emojis.check_mark} unsilenced current channel.") async def _silence(self, channel: TextChannel, persistent: bool, duration: Optional[int]) -> bool: """ @@ -141,6 +186,7 @@ class Silence(commands.Cog): await channel.set_permissions(self._verified_role, **dict(current_overwrite, send_messages=None)) log.info(f"Unsilenced channel #{channel} ({channel.id}).") self.notifier.remove_channel(channel) + self.scheduler.cancel_task(channel.id) self.muted_channels.discard(channel) return True log.info(f"Tried to unsilence channel #{channel} ({channel.id}) but the channel was not silenced.") -- cgit v1.2.3 From 9b0df33c90326f62d699afc70c13d3d375affeab Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Mon, 1 Jun 2020 23:08:32 +0200 Subject: Fix Formatting/Styling --- bot/cogs/moderation/silence.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index a2fd39906..448f17966 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -6,11 +6,12 @@ from contextlib import suppress from typing import Optional from bot.bot import Bot -from bot.constants import MODERATION_ROLES, Channels, Emojis, Guild, Roles +from bot.constants import Channels, Emojis, Guild, MODERATION_ROLES, Roles from bot.converters import HushDurationConverter from bot.utils import time from bot.utils.checks import with_role_check from bot.utils.scheduling import Scheduler + from discord import TextChannel from discord.ext import commands, tasks from discord.ext.commands import Context @@ -23,7 +24,7 @@ SilencedChannel = namedtuple( class UnsilenceScheduler(Scheduler): - """Scheduler for unsilencing channels""" + """Scheduler for unsilencing channels.""" def __init__(self, bot: Bot): super().__init__() @@ -31,17 +32,13 @@ class UnsilenceScheduler(Scheduler): self.bot = bot async def schedule_unsilence(self, channel: SilencedChannel) -> None: - """Schedule expiration for silenced channels""" + """Schedule expiration for silenced channels.""" await self.bot.wait_until_guild_available() log.debug("Scheduling unsilencer") self.schedule_task(channel.id, channel) async def _scheduled_task(self, channel: SilencedChannel) -> None: - """ - Removes expired silenced channel from `silence.muted_channels` - and calls `silence.unsilence` to unsilence the channel - after the silence expires - """ + """Calls `silence.unsilence` on expired silenced channel to unsilence it.""" await time.wait_until(channel.stop) log.info("Unsilencing channel after set delay.") -- cgit v1.2.3 From 0a7b64df552d394e9d1f38fb167ec93334b9bead Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Mon, 1 Jun 2020 23:24:07 +0200 Subject: Optimize Imports --- bot/cogs/moderation/silence.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index 448f17966..5dfa9cc8a 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -5,6 +5,10 @@ from collections import namedtuple from contextlib import suppress from typing import Optional +from discord import TextChannel +from discord.ext import commands, tasks +from discord.ext.commands import Context + from bot.bot import Bot from bot.constants import Channels, Emojis, Guild, MODERATION_ROLES, Roles from bot.converters import HushDurationConverter @@ -12,10 +16,6 @@ from bot.utils import time from bot.utils.checks import with_role_check from bot.utils.scheduling import Scheduler -from discord import TextChannel -from discord.ext import commands, tasks -from discord.ext.commands import Context - log = logging.getLogger(__name__) SilencedChannel = namedtuple( -- cgit v1.2.3 From 469692d53a4ed74500a5806273e9c778a97afae8 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Thu, 4 Jun 2020 22:50:36 +0200 Subject: Use Scheduler inside the cog - There shouldn't be another class only for Scheduler instead, we can implement it directly into Silence class --- bot/cogs/moderation/silence.py | 50 +++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index 5dfa9cc8a..398a70e51 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -19,34 +19,10 @@ from bot.utils.scheduling import Scheduler log = logging.getLogger(__name__) SilencedChannel = namedtuple( - "SilencedChannel", ("id", "ctx", "silence", "stop") + "SilencedChannel", ("id", "ctx", "stop") ) -class UnsilenceScheduler(Scheduler): - """Scheduler for unsilencing channels.""" - - def __init__(self, bot: Bot): - super().__init__() - - self.bot = bot - - async def schedule_unsilence(self, channel: SilencedChannel) -> None: - """Schedule expiration for silenced channels.""" - await self.bot.wait_until_guild_available() - log.debug("Scheduling unsilencer") - self.schedule_task(channel.id, channel) - - async def _scheduled_task(self, channel: SilencedChannel) -> None: - """Calls `silence.unsilence` on expired silenced channel to unsilence it.""" - await time.wait_until(channel.stop) - log.info("Unsilencing channel after set delay.") - - # Because `silence.unsilence` explicitly cancels this scheduled task, it is shielded - # to avoid prematurely cancelling itself. - await asyncio.shield(channel.ctx.invoke(channel.silence.unsilence)) - - class SilenceNotifier(tasks.Loop): """Loop notifier for posting notices to `alert_channel` containing added channels.""" @@ -85,7 +61,7 @@ class SilenceNotifier(tasks.Loop): await self._alert_channel.send(f"<@&{Roles.moderators}> currently silenced channels: {channels_text}") -class Silence(commands.Cog): +class Silence(Scheduler, commands.Cog): """Commands for stopping channel messages for `verified` role in a channel.""" def __init__(self, bot: Bot): @@ -93,7 +69,22 @@ class Silence(commands.Cog): self.muted_channels = set() self._get_instance_vars_task = self.bot.loop.create_task(self._get_instance_vars()) self._get_instance_vars_event = asyncio.Event() - self.scheduler = UnsilenceScheduler(bot) + super().__init__() + + async def schedule_unsilence(self, channel: SilencedChannel) -> None: + """Schedule expiration for silenced channels.""" + await self.bot.wait_until_guild_available() + log.debug("Scheduling unsilencer") + self.schedule_task(channel.id, channel) + + async def _scheduled_task(self, channel: SilencedChannel) -> None: + """Calls `self.unsilence` on expired silenced channel to unsilence it.""" + await time.wait_until(channel.stop) + log.info("Unsilencing channel after set delay.") + + # Because `self.unsilence` explicitly cancels this scheduled tas, it is shielded + # to avoid prematurely cancelling itself + await asyncio.shield(channel.ctx.invoke(self.unsilence)) async def _get_instance_vars(self) -> None: """Get instance variables after they're available to get from the guild.""" @@ -127,11 +118,10 @@ class Silence(commands.Cog): channel = SilencedChannel( id=ctx.channel.id, ctx=ctx, - silence=self, stop=datetime.datetime.now() + datetime.timedelta(minutes=duration), ) - await self.scheduler.schedule_unsilence(channel) + await self.schedule_unsilence(channel) @commands.command(aliases=("unhush",)) async def unsilence(self, ctx: Context) -> None: @@ -183,7 +173,7 @@ class Silence(commands.Cog): await channel.set_permissions(self._verified_role, **dict(current_overwrite, send_messages=None)) log.info(f"Unsilenced channel #{channel} ({channel.id}).") self.notifier.remove_channel(channel) - self.scheduler.cancel_task(channel.id) + self.cancel_task(channel.id) self.muted_channels.discard(channel) return True log.info(f"Tried to unsilence channel #{channel} ({channel.id}) but the channel was not silenced.") -- cgit v1.2.3 From 2d7c8e8238179ece54b388deeeb0734ce330b707 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 6 Jun 2020 12:17:24 +0200 Subject: Apply suggestions from review --- bot/cogs/moderation/silence.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index 398a70e51..94c560c8e 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -1,9 +1,7 @@ import asyncio -import datetime import logging -from collections import namedtuple from contextlib import suppress -from typing import Optional +from typing import Optional, NamedTuple from discord import TextChannel from discord.ext import commands, tasks @@ -12,14 +10,13 @@ from discord.ext.commands import Context from bot.bot import Bot from bot.constants import Channels, Emojis, Guild, MODERATION_ROLES, Roles from bot.converters import HushDurationConverter -from bot.utils import time from bot.utils.checks import with_role_check from bot.utils.scheduling import Scheduler log = logging.getLogger(__name__) -SilencedChannel = namedtuple( - "SilencedChannel", ("id", "ctx", "stop") +SilencedChannel = NamedTuple( + "SilencedChannel", [("ctx", Context), ("delay", int)] ) @@ -65,11 +62,11 @@ class Silence(Scheduler, commands.Cog): """Commands for stopping channel messages for `verified` role in a channel.""" def __init__(self, bot: Bot): + super().__init__() self.bot = bot self.muted_channels = set() self._get_instance_vars_task = self.bot.loop.create_task(self._get_instance_vars()) self._get_instance_vars_event = asyncio.Event() - super().__init__() async def schedule_unsilence(self, channel: SilencedChannel) -> None: """Schedule expiration for silenced channels.""" @@ -79,10 +76,10 @@ class Silence(Scheduler, commands.Cog): async def _scheduled_task(self, channel: SilencedChannel) -> None: """Calls `self.unsilence` on expired silenced channel to unsilence it.""" - await time.wait_until(channel.stop) + await asyncio.sleep(channel.delay) log.info("Unsilencing channel after set delay.") - # Because `self.unsilence` explicitly cancels this scheduled tas, it is shielded + # Because `self.unsilence` explicitly cancels this scheduled task, it is shielded # to avoid prematurely cancelling itself await asyncio.shield(channel.ctx.invoke(self.unsilence)) @@ -116,12 +113,11 @@ class Silence(Scheduler, commands.Cog): await ctx.send(f"{Emojis.check_mark} silenced current channel for {duration} minute(s).") channel = SilencedChannel( - id=ctx.channel.id, ctx=ctx, - stop=datetime.datetime.now() + datetime.timedelta(minutes=duration), + stop=duration*60, ) - await self.schedule_unsilence(channel) + await self.schedule_task(ctx.channel.id, channel) @commands.command(aliases=("unhush",)) async def unsilence(self, ctx: Context) -> None: @@ -134,9 +130,8 @@ class Silence(Scheduler, commands.Cog): log.debug(f"Unsilencing channel #{ctx.channel} from {ctx.author}'s command.") if not await self._unsilence(ctx.channel): await ctx.send(f"{Emojis.cross_mark} current channel is not silenced.") - return - - await ctx.send(f"{Emojis.check_mark} unsilenced current channel.") + else: + await ctx.send(f"{Emojis.check_mark} unsilenced current channel.") async def _silence(self, channel: TextChannel, persistent: bool, duration: Optional[int]) -> bool: """ -- cgit v1.2.3 From 1859ae7a43bc794b735c3445b1873420da5a7001 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 6 Jun 2020 12:20:26 +0200 Subject: Fix import order --- bot/cogs/moderation/silence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index 94c560c8e..c451cea0b 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -1,7 +1,7 @@ import asyncio import logging from contextlib import suppress -from typing import Optional, NamedTuple +from typing import NamedTuple, Optional from discord import TextChannel from discord.ext import commands, tasks -- cgit v1.2.3 From 3c3c8c210507170a2502a4906265af6a2b2525ac Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 6 Jun 2020 12:26:14 +0200 Subject: Remove unnecessary schedule_unsilence - As suggested, this function is not necessary - Also fixed no longer valid`stop`in SilencedChannel NamedTuple --- bot/cogs/moderation/silence.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index c451cea0b..8223df491 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -68,12 +68,6 @@ class Silence(Scheduler, commands.Cog): self._get_instance_vars_task = self.bot.loop.create_task(self._get_instance_vars()) self._get_instance_vars_event = asyncio.Event() - async def schedule_unsilence(self, channel: SilencedChannel) -> None: - """Schedule expiration for silenced channels.""" - await self.bot.wait_until_guild_available() - log.debug("Scheduling unsilencer") - self.schedule_task(channel.id, channel) - async def _scheduled_task(self, channel: SilencedChannel) -> None: """Calls `self.unsilence` on expired silenced channel to unsilence it.""" await asyncio.sleep(channel.delay) @@ -114,7 +108,7 @@ class Silence(Scheduler, commands.Cog): channel = SilencedChannel( ctx=ctx, - stop=duration*60, + delay=duration*60, ) await self.schedule_task(ctx.channel.id, channel) -- cgit v1.2.3 From 858e2e301234862e66ce03ccd71f46518dcc953f Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 6 Jun 2020 12:31:00 +0200 Subject: Do not await self.schedule_task - self.schedule_task shouldn't be awaited as it isn't a coroutine --- bot/cogs/moderation/silence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index 8223df491..13f84009f 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -111,7 +111,7 @@ class Silence(Scheduler, commands.Cog): delay=duration*60, ) - await self.schedule_task(ctx.channel.id, channel) + self.schedule_task(ctx.channel.id, channel) @commands.command(aliases=("unhush",)) async def unsilence(self, ctx: Context) -> None: -- cgit v1.2.3 From 6e34de6397e6ab7d23c6e6abb74a2156375b2c2d Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 6 Jun 2020 21:29:33 +0200 Subject: Move cancel_task before notifier.remove_channel - as sugested notifier.remove_channel and muted_channels.discard should be together --- bot/cogs/moderation/silence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index 13f84009f..d5b9621d2 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -161,8 +161,8 @@ class Silence(Scheduler, commands.Cog): if current_overwrite.send_messages is False: await channel.set_permissions(self._verified_role, **dict(current_overwrite, send_messages=None)) log.info(f"Unsilenced channel #{channel} ({channel.id}).") - self.notifier.remove_channel(channel) self.cancel_task(channel.id) + self.notifier.remove_channel(channel) self.muted_channels.discard(channel) return True log.info(f"Tried to unsilence channel #{channel} ({channel.id}) but the channel was not silenced.") -- cgit v1.2.3 From 27e269b874fcd038388031959186ffe682c777c0 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 6 Jun 2020 21:36:02 +0200 Subject: Change `is` to `was` for unsilenced channel message - As suggested, `was` is more fitting in the message than `is` --- bot/cogs/moderation/silence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index d5b9621d2..08f3973b0 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -123,7 +123,7 @@ class Silence(Scheduler, commands.Cog): await self._get_instance_vars_event.wait() log.debug(f"Unsilencing channel #{ctx.channel} from {ctx.author}'s command.") if not await self._unsilence(ctx.channel): - await ctx.send(f"{Emojis.cross_mark} current channel is not silenced.") + await ctx.send(f"{Emojis.cross_mark} current channel was not silenced.") else: await ctx.send(f"{Emojis.check_mark} unsilenced current channel.") -- cgit v1.2.3 From be4902cbd66c2f7223608ddbfee4aa4f0e1a011a Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 6 Jun 2020 22:11:53 +0200 Subject: Test for channel not silenced message --- tests/bot/cogs/moderation/test_silence.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/bot/cogs/moderation/test_silence.py b/tests/bot/cogs/moderation/test_silence.py index 3fd149f04..ab3d0742a 100644 --- a/tests/bot/cogs/moderation/test_silence.py +++ b/tests/bot/cogs/moderation/test_silence.py @@ -127,10 +127,20 @@ class SilenceTests(unittest.IsolatedAsyncioTestCase): self.ctx.reset_mock() async def test_unsilence_sent_correct_discord_message(self): - """Proper reply after a successful unsilence.""" - with mock.patch.object(self.cog, "_unsilence", return_value=True): - await self.cog.unsilence.callback(self.cog, self.ctx) - self.ctx.send.assert_called_once_with(f"{Emojis.check_mark} unsilenced current channel.") + """Check if proper message was sent when unsilencing channel.""" + test_cases = ( + (True, f"{Emojis.check_mark} unsilenced current channel."), + (False, f"{Emojis.cross_mark} current channel was not silenced.") + ) + for _unsilence_patch_return, result_message in test_cases: + with self.subTest( + starting_silenced_state=_unsilence_patch_return, + result_message=result_message + ): + with mock.patch.object(self.cog, "_unsilence", return_value=_unsilence_patch_return): + await self.cog.unsilence.callback(self.cog, self.ctx) + self.ctx.send.assert_called_once_with(result_message) + self.ctx.reset_mock() async def test_silence_private_for_false(self): """Permissions are not set and `False` is returned in an already silenced channel.""" -- cgit v1.2.3 From 73f258488d32be6c00aaea2cfa20ff2b24d48b30 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 6 Jun 2020 23:41:59 +0200 Subject: Use class instead of NamedTuple - Using a class is more readable than using a NamedTuple --- bot/cogs/moderation/silence.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index 08f3973b0..c8ab6443b 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -15,9 +15,12 @@ from bot.utils.scheduling import Scheduler log = logging.getLogger(__name__) -SilencedChannel = NamedTuple( - "SilencedChannel", [("ctx", Context), ("delay", int)] -) + +class TaskData(NamedTuple): + """Data for a scheduled task.""" + + delay: int + ctx: Context class SilenceNotifier(tasks.Loop): @@ -68,14 +71,14 @@ class Silence(Scheduler, commands.Cog): self._get_instance_vars_task = self.bot.loop.create_task(self._get_instance_vars()) self._get_instance_vars_event = asyncio.Event() - async def _scheduled_task(self, channel: SilencedChannel) -> None: + async def _scheduled_task(self, task: TaskData) -> None: """Calls `self.unsilence` on expired silenced channel to unsilence it.""" - await asyncio.sleep(channel.delay) + await asyncio.sleep(task.delay) log.info("Unsilencing channel after set delay.") # Because `self.unsilence` explicitly cancels this scheduled task, it is shielded # to avoid prematurely cancelling itself - await asyncio.shield(channel.ctx.invoke(self.unsilence)) + await asyncio.shield(task.ctx.invoke(self.unsilence)) async def _get_instance_vars(self) -> None: """Get instance variables after they're available to get from the guild.""" @@ -106,12 +109,12 @@ class Silence(Scheduler, commands.Cog): await ctx.send(f"{Emojis.check_mark} silenced current channel for {duration} minute(s).") - channel = SilencedChannel( - ctx=ctx, + task_data = TaskData( delay=duration*60, + ctx=ctx ) - self.schedule_task(ctx.channel.id, channel) + self.schedule_task(ctx.channel.id, task_data) @commands.command(aliases=("unhush",)) async def unsilence(self, ctx: Context) -> None: -- cgit v1.2.3