Skip to content

Commit

Permalink
notify mods about timeout cap upon edit
Browse files Browse the repository at this point in the history
  • Loading branch information
shtlrs committed Mar 18, 2024
1 parent 2e99fcd commit 36ee066
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
21 changes: 18 additions & 3 deletions bot/exts/moderation/infraction/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import arrow
import discord
from dateutil.relativedelta import relativedelta
from discord.ext.commands import Context
from discord.ext.commands import Bot, Context
from pydis_core.site_api import ResponseCodeError

import bot
from bot.constants import Categories, Colours, Icons
from bot.constants import Categories, Channels, Colours, Icons
from bot.converters import DurationOrExpiry, MemberOrUser
from bot.errors import InvalidInfractedUserError
from bot.log import get_logger
from bot.utils import time
from bot.utils.channel import is_in_category
from bot.utils.channel import is_in_category, is_mod_channel
from bot.utils.time import unpack_duration

log = get_logger(__name__)
Expand Down Expand Up @@ -64,6 +64,11 @@


MAXIMUM_TIMEOUT_DAYS = datetime.timedelta(days=28)
TIMEOUT_CAP_MESSAGE = (
f"The timeout for {{0}} can't be longer than {MAXIMUM_TIMEOUT_DAYS.days} days."
" I'll pretend that's what you meant."
)


async def post_user(ctx: Context, user: MemberOrUser) -> dict | None:
"""
Expand Down Expand Up @@ -319,3 +324,13 @@ def cap_timeout_duration(duration: datetime.datetime | relativedelta) -> tuple[b
# Duration cap is exclusive. This is to still allow specifying "28d".
duration -= datetime.timedelta(minutes=1)
return capped, duration


async def notify_timeout_cap(bot: Bot, ctx: Context, user: discord.Member) -> None:
"""Notifies moderators about a timeout duration being capped."""
cap_message_for_user = TIMEOUT_CAP_MESSAGE.format(user.mention)
if is_mod_channel(ctx.channel):
await ctx.reply(f":warning: {cap_message_for_user}")
else:
await bot.get_channel(Channels.mods).send(
f":warning: {ctx.author.mention} {cap_message_for_user}")
17 changes: 2 additions & 15 deletions bot/exts/moderation/infraction/infractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@

from bot import constants
from bot.bot import Bot
from bot.constants import Channels, Event
from bot.constants import Event
from bot.converters import Age, Duration, DurationOrExpiry, MemberOrUser, UnambiguousMemberOrUser
from bot.decorators import ensure_future_timestamp, respect_role_hierarchy
from bot.exts.moderation.infraction import _utils
from bot.exts.moderation.infraction._scheduler import InfractionScheduler
from bot.log import get_logger
from bot.utils.channel import is_mod_channel
from bot.utils.messages import format_user

log = get_logger(__name__)
Expand Down Expand Up @@ -46,12 +45,6 @@
"this message to appeal your ban."
)
COMP_BAN_DURATION = timedelta(days=4)
# Timeout
MAXIMUM_TIMEOUT_DAYS = timedelta(days=28)
TIMEOUT_CAP_MESSAGE = (
f"The timeout for {{0}} can't be longer than {MAXIMUM_TIMEOUT_DAYS.days} days."
" I'll pretend that's what you meant."
)


class Infractions(InfractionScheduler, commands.Cog):
Expand Down Expand Up @@ -232,13 +225,7 @@ async def timeout(
else:
capped, duration = _utils.cap_timeout_duration(duration)
if capped:
cap_message_for_user = TIMEOUT_CAP_MESSAGE.format(user.mention)
if is_mod_channel(ctx.channel):
await ctx.reply(f":warning: {cap_message_for_user}")
else:
await self.bot.get_channel(Channels.mods).send(
f":warning: {ctx.author.mention} {cap_message_for_user}"
)
await _utils.notify_timeout_cap(self.bot, ctx, user)

await self.apply_timeout(ctx, user, reason, duration_or_expiry=duration)

Expand Down
4 changes: 3 additions & 1 deletion bot/exts/moderation/infraction/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ async def infraction_edit(
self.infractions_cog.schedule_expiration(new_infraction)
# Timeouts are handled by Discord itself, so we need to edit the expiry in Discord as well
if user and infraction["type"] == "timeout":
_, duration = _utils.cap_timeout_duration(expiry)
capped, duration = _utils.cap_timeout_duration(expiry)
if capped:
await _utils.notify_timeout_cap(self.bot, ctx, user)
await user.edit(reason=reason, timed_out_until=expiry)

log_text += f"""
Expand Down

0 comments on commit 36ee066

Please sign in to comment.