Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions bot/exts/moderation/silence.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
)
MSG_UNSILENCE_SUCCESS = f"{constants.Emojis.check_mark} unsilenced {{channel}}."

TextOrVoiceChannel = TextChannel | VoiceChannel
TextOrVoiceChannel = TextChannel | VoiceChannel | Thread

VOICE_CHANNELS = {
constants.Channels.code_help_voice_0: constants.Channels.code_help_chat_0,
Expand Down Expand Up @@ -181,10 +181,12 @@ async def silence(
# 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):
if not channel.locked:
await channel.edit(locked=True)
else:
log.info("Thread already silenced!")
await self.send_message(MSG_SILENCE_FAIL, ctx.channel, channel, alert_target=False)
elif not await self._set_silence_overwrites(channel, kick=kick):
log.info("Tried to silence channel %s but the channel was already silenced.", channel_info)
await self.send_message(MSG_SILENCE_FAIL, ctx.channel, channel, alert_target=False)
return
Expand Down Expand Up @@ -215,7 +217,7 @@ def parse_silence_args(
) -> tuple[TextOrVoiceChannel, int | None]:
"""Helper method to parse the arguments of the silence command."""
if duration_or_channel:
if isinstance(duration_or_channel, TextChannel | VoiceChannel):
if isinstance(duration_or_channel, TextChannel | VoiceChannel | Thread):
channel = duration_or_channel
else:
channel = ctx.channel
Expand Down Expand Up @@ -322,21 +324,31 @@ async def _unsilence(self, channel: TextOrVoiceChannel) -> bool:
"""
# 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:
log.info("Tried to unsilence channel #%s (%s) but the channel was not silenced.", channel, channel.id)
return False
if isinstance(channel, Thread):
if not channel.locked and channel.id not in self.scheduler:
log.info("You are trying to unsilence a thread but it's not silenced")
return False
else:
if channel.id not in self.scheduler and prev_overwrites is None:
log.info("Tried to unsilence channel #%s (%s) but the channel was not silenced.", channel, channel.id)
return False

# Select the role based on channel type, and get current overwrites
if isinstance(channel, TextChannel):
if isinstance(channel, Thread):
await channel.edit(locked=False)
elif isinstance(channel, TextChannel):
role = self._everyone_role
overwrite = channel.overwrites_for(role)
permissions = "`Send Messages` and `Add Reactions`"
else:
role = self._verified_voice_role
overwrite = channel.overwrites_for(role)
permissions = "`Speak` and `Connect`"
Comment thread
eldiablotyPY marked this conversation as resolved.




# Check if old overwrites were not stored

if prev_overwrites is None:
log.info("Missing previous overwrites for #%s (%s); defaulting to None.", channel, channel.id)
overwrite.update(
Expand All @@ -350,26 +362,27 @@ async def _unsilence(self, channel: TextOrVoiceChannel) -> bool:
)
else:
overwrite.update(**json.loads(prev_overwrites))


# Update Permissions
# Update Permissions
await channel.set_permissions(role, overwrite=overwrite)
if isinstance(channel, VoiceChannel):
await self._force_voice_sync(channel)

log.info("Unsilenced channel #%s (%s).", channel, channel.id)
log.info("Unsilenced channel #%s (%s).", channel, channel.id)

self.scheduler.cancel(channel.id)
self.notifier.remove_channel(channel)
await self.previous_overwrites.delete(channel.id)
if not isinstance(channel, Thread):
await self.previous_overwrites.delete(channel.id)
await self.unsilence_timestamps.delete(channel.id)

# Alert Admin team if old overwrites were not available
if prev_overwrites is None:
if not isinstance(channel, Thread) and prev_overwrites is None:
await self._mod_alerts_channel.send(
f"<@&{constants.Roles.admins}> Restored overwrites with default values after unsilencing "
f"{channel.mention}. Please check that the {permissions} "
f"overwrites for {role.mention} are at their desired values."
)
)

return True

Expand Down