Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(silence): allow to silence threads #3122

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
41 changes: 34 additions & 7 deletions bot/exts/moderation/silence.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,6 @@ async def silence(
channel_info = f"#{channel} ({channel.id})"
log.debug(f"{ctx.author} is silencing channel {channel_info}.")

# Since threads don't have specific overrides, we cannot silence them individually.
# The parent channel has to be muted or the thread should be archived.
if isinstance(channel, Thread):
await ctx.send(":x: Threads cannot be silenced.")
return

if not await self._set_silence_overwrites(channel, kick=kick):
log.info(f"Tried to silence channel {channel_info} but the channel was already silenced.")
await self.send_message(MSG_SILENCE_FAIL, ctx.channel, channel, alert_target=False)
Expand Down Expand Up @@ -242,6 +236,12 @@ async def _set_silence_overwrites(self, channel: TextOrVoiceChannel, *, kick: bo
send_messages_in_threads=overwrite.send_messages_in_threads
)

elif isinstance(channel, Thread):
if channel.id in self.scheduler:
return False
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
await channel.edit(locked=True)
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the thread is already locked, this will just schedule an unlock in the set amount of time. Failing is probably better to keep consistent with how it behaves with text channels.

return True

else:
role = self._verified_voice_role
overwrite = channel.overwrites_for(role)
Expand Down Expand Up @@ -297,6 +297,11 @@ async def _unsilence_wrapper(self, channel: TextOrVoiceChannel, ctx: Context | N
if isinstance(channel, VoiceChannel):
overwrite = channel.overwrites_for(self._verified_voice_role)
has_channel_overwrites = overwrite.speak is False

elif isinstance(channel, Thread):
await self.send_message(MSG_UNSILENCE_FAIL, msg_channel, channel, alert_target=False)
return
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved

else:
overwrite = channel.overwrites_for(self._everyone_role)
has_channel_overwrites = overwrite.send_messages is False or overwrite.add_reactions is False
Expand All @@ -318,8 +323,11 @@ async def _unsilence(self, channel: TextOrVoiceChannel) -> bool:
it, cancel the task, and remove it from the notifier. Notify admins if it has a task but
not cached overwrites.

Return `True` if channel permissions were changed, `False` otherwise.
Return `True` if channel was unsilenced, `False` otherwise.
"""
if isinstance(channel, Thread):
return await self._unsilence_thread(channel)

Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
# Get stored overwrites, and return if channel is unsilenced
prev_overwrites = await self.previous_overwrites.get(channel.id)
if channel.id not in self.scheduler and prev_overwrites is None:
Expand Down Expand Up @@ -373,6 +381,25 @@ async def _unsilence(self, channel: TextOrVoiceChannel) -> bool:

return True

async def _unsilence_thread(self, channel: Thread) -> bool:
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
"""
Unsilence a thread.

This function works the same as `_unsilence`, the only different behaviour regards unsilencing the channel
which doesn't require an edit of the overwrites, instead we unlock the thread.

Return `True` if the thread was unlocked, `False` otherwise.
"""
if channel.id not in self.scheduler:
log.info(f"Tried to unsilence channel #{channel} ({channel.id}) but the channel was not silenced.")
return False

await channel.edit(locked=False)
log.info(f"Unsilenced channel #{channel} ({channel.id}).")
self.scheduler.cancel(channel.id)
self.notifier.remove_channel(channel)
return True

@staticmethod
async def _get_afk_channel(guild: Guild) -> VoiceChannel:
"""Get a guild's AFK channel, or create one if it does not exist."""
Expand Down