Skip to content

Commit

Permalink
Prevent OverflowError from very large timedeltas in Mutes (Cog-Creato…
Browse files Browse the repository at this point in the history
  • Loading branch information
TrustyJAID authored Apr 21, 2024
1 parent 47d4675 commit 0c9c210
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 36 deletions.
5 changes: 4 additions & 1 deletion redbot/cogs/mutes/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import re
from typing import Optional, TypedDict
from datetime import timedelta
from datetime import timedelta, datetime, timezone
from typing_extensions import Annotated

from discord.ext.commands.converter import Converter
Expand All @@ -29,6 +29,7 @@ def _edgematch(pattern: re.Pattern[str], argument: str) -> Optional[re.Match[str
class _MuteTime(TypedDict, total=False):
duration: timedelta
reason: str
until: datetime


class _MuteTimeConverter(Converter):
Expand Down Expand Up @@ -57,6 +58,8 @@ async def convert(self, ctx: commands.Context, argument: str) -> _MuteTime:
)
try:
result["duration"] = duration = timedelta(**time_data)
result["until"] = ctx.message.created_at + duration
# Catch if using the timedelta with the current date will also result in an Overflow error
except OverflowError:
raise commands.BadArgument(
_("The time provided is too long; use a more reasonable time.")
Expand Down
65 changes: 41 additions & 24 deletions redbot/cogs/mutes/mutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,13 +1005,20 @@ async def default_mute_time(self, ctx: commands.Context, *, time: Optional[MuteT
await self.config.guild(ctx.guild).default_time.clear()
await ctx.send(_("Default mute time removed."))
else:
data = time.get("duration", {})
if not data:
duration = time.get("duration", None)
if not duration:
return await ctx.send(_("Please provide a valid time format."))
await self.config.guild(ctx.guild).default_time.set(data.total_seconds())
if duration >= timedelta(days=365000):
# prevent setting a default time now that might eventually cause an overflow
# later as the date goes up. 1000 years gives us approximately 8000 more years
# of wiggle room.
return await ctx.send(
_("The time provided is too long; use a more reasonable time.")
)
await self.config.guild(ctx.guild).default_time.set(duration.total_seconds())
await ctx.send(
_("Default mute time set to {time}.").format(
time=humanize_timedelta(timedelta=data)
time=humanize_timedelta(timedelta=duration)
)
)

Expand Down Expand Up @@ -1142,15 +1149,15 @@ async def timeout(
return await ctx.send(_("You cannot mute me."))
if ctx.author in users:
return await ctx.send(_("You cannot mute yourself."))
duration = time_and_reason.get("duration", None)
if duration and duration > timedelta(days=28):
await ctx.send(_(MUTE_UNMUTE_ISSUES["mute_is_too_long"]))
return
until = time_and_reason.get("until", None)
reason = time_and_reason.get("reason", None)
time = ""
until = None
if duration:
until = datetime.now(timezone.utc) + duration
duration = None
if until:
duration = time_and_reason.get("duration")
if duration and duration > timedelta(days=28):
await ctx.send(_(MUTE_UNMUTE_ISSUES["mute_is_too_long"]))
return
length = humanize_timedelta(timedelta=duration)
time = _(" for {length} until {duration}").format(
length=length, duration=discord.utils.format_dt(until)
Expand All @@ -1159,7 +1166,8 @@ async def timeout(
else:
default_duration = await self.config.guild(ctx.guild).default_time()
if default_duration:
until = datetime.now(timezone.utc) + timedelta(seconds=default_duration)
duration = timedelta(seconds=default_duration)
until = ctx.message.created_at + duration
length = humanize_timedelta(seconds=default_duration)
time = _(" for {length} until {duration}").format(
length=length, duration=discord.utils.format_dt(until)
Expand Down Expand Up @@ -1227,12 +1235,12 @@ async def mute(
if not await self._check_for_mute_role(ctx):
return
async with ctx.typing():
duration = time_and_reason.get("duration", None)
until = time_and_reason.get("until", None)
reason = time_and_reason.get("reason", None)
time = ""
until = None
if duration:
until = datetime.now(timezone.utc) + duration
duration = None
if until:
duration = time_and_reason.get("duration")
length = humanize_timedelta(timedelta=duration)
time = _(" for {length} until {duration}").format(
length=length, duration=discord.utils.format_dt(until)
Expand All @@ -1241,7 +1249,8 @@ async def mute(
else:
default_duration = await self.config.guild(ctx.guild).default_time()
if default_duration:
until = datetime.now(timezone.utc) + timedelta(seconds=default_duration)
duration = timedelta(seconds=default_duration)
until = ctx.message.created_at + duration
length = humanize_timedelta(seconds=default_duration)
time = _(" for {length} until {duration}").format(
length=length, duration=discord.utils.format_dt(until)
Expand Down Expand Up @@ -1377,18 +1386,26 @@ async def channel_mute(
if ctx.author in users:
return await ctx.send(_("You cannot mute yourself."))
async with ctx.typing():
duration = time_and_reason.get("duration", None)
until = time_and_reason.get("until", None)
reason = time_and_reason.get("reason", None)
time = ""
until = None
if duration:
until = datetime.now(timezone.utc) + duration
time = _(" until {duration}").format(duration=discord.utils.format_dt(until))
duration = None
if until:
duration = time_and_reason.get("duration")
length = humanize_timedelta(timedelta=duration)
time = _(" for {length} until {duration}").format(
length=length, duration=discord.utils.format_dt(until)
)

else:
default_duration = await self.config.guild(ctx.guild).default_time()
if default_duration:
until = datetime.now(timezone.utc) + timedelta(seconds=default_duration)
time = _(" until {duration}").format(duration=discord.utils.format_dt(until))
duration = timedelta(seconds=default_duration)
until = ctx.message.created_at + duration
length = humanize_timedelta(seconds=default_duration)
time = _(" for {length} until {duration}").format(
length=length, duration=discord.utils.format_dt(until)
)
author = ctx.message.author
channel = ctx.message.channel
if isinstance(channel, discord.Thread):
Expand Down
24 changes: 13 additions & 11 deletions redbot/cogs/mutes/voicemutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,25 @@ async def voice_mute(
if not can_move:
issue_list.append((user, perm_reason))
continue
duration = time_and_reason.get("duration", None)
until = time_and_reason.get("until", None)
reason = time_and_reason.get("reason", None)
time = ""
until = None
if duration:
until = datetime.now(timezone.utc) + duration
time = _(" for {duration}").format(
duration=humanize_timedelta(timedelta=duration)
duration = None
if until:
duration = time_and_reason.get("duration")
length = humanize_timedelta(timedelta=duration)
time = _(" for {length} until {duration}").format(
length=length, duration=discord.utils.format_dt(until)
)

else:
default_duration = await self.config.guild(ctx.guild).default_time()
if default_duration:
until = datetime.now(timezone.utc) + timedelta(seconds=default_duration)
time = _(" for {duration}").format(
duration=humanize_timedelta(
timedelta=timedelta(seconds=default_duration)
)
duration = timedelta(seconds=default_duration)
until = ctx.message.created_at + duration
length = humanize_timedelta(seconds=default_duration)
time = _(" for {length} until {duration}").format(
length=length, duration=discord.utils.format_dt(until)
)
guild = ctx.guild
author = ctx.author
Expand Down

0 comments on commit 0c9c210

Please sign in to comment.