-
-
Notifications
You must be signed in to change notification settings - Fork 717
Implemented optional duration parameter in slowmode command #3331
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor comment, though if others disagree then let me know, it is more a nit based on me not wanting to add dependence onto intricacies of Redis and hoping multiple keys exist referring to the same slowmode delay etc.
bot/exts/moderation/slowmode.py
Outdated
# Stores the expiration timestamp in POSIX format for active slowmodes, keyed by channel ID. | ||
slowmode_expiration_cache = RedisCache() | ||
|
||
# Stores the original slowmode interval by channel ID, allowing its restoration after temporary slowmode expires. | ||
original_slowmode_cache = RedisCache() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given these are both structured and known values, I would maybe consider serializing them together into one store?
This is a nit though and others may disagree, it just appears that we use them more or less in the same place so whilst if we were just working in dict-land this would be preferred when working in a place with an underlying store I prefer it being the same.
bot/exts/moderation/slowmode.py
Outdated
if await self.slowmode_expiration_cache.contains(channel.id): | ||
expiration_time = await self.slowmode_expiration_cache.get(channel.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if await self.slowmode_expiration_cache.contains(channel.id): | |
expiration_time = await self.slowmode_expiration_cache.get(channel.id) | |
expiration_time = await self.slowmode_expiration_cache.get(channel.id, None) | |
if expiration_time is not None: |
bot/exts/moderation/slowmode.py
Outdated
expiration_time = await self.slowmode_expiration_cache.get(channel.id) | ||
expiration_timestamp = discord_timestamp(expiration_time, TimestampFormats.RELATIVE) | ||
await ctx.send( | ||
f"The slowmode delay for {channel.mention} is {humanized_delay} and expires in {expiration_timestamp}." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f"The slowmode delay for {channel.mention} is {humanized_delay} and expires in {expiration_timestamp}." | |
f"The slowmode delay for {channel.mention} is {humanized_delay} and expires {expiration_timestamp}." |
The "in" should be already included in a relative timestamp if I'm not mistaken
Also, should this specify what happens when the slowmode expires? e.g "and will revert to {humanized_original_delay} {expiration_timestamp}"
bot/exts/moderation/slowmode.py
Outdated
if channel.id in COMMONLY_SLOWMODED_CHANNELS: | ||
log.info(f"Recording slowmode change in stats for {channel.name}.") | ||
self.bot.stats.gauge(f"slowmode.{COMMONLY_SLOWMODED_CHANNELS[channel.id]}", slowmode_delay) | ||
if not slowmode_delay <= SLOWMODE_MAX_DELAY: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not slowmode_delay <= SLOWMODE_MAX_DELAY: | |
if slowmode_delay > SLOWMODE_MAX_DELAY: |
bot/exts/moderation/slowmode.py
Outdated
|
||
@slowmode_group.command(name="set", aliases=["s"]) | ||
async def set_slowmode( | ||
self, | ||
ctx: Context, | ||
channel: MessageHolder, | ||
delay: DurationDelta | Literal["0s", "0seconds"], | ||
duration: DurationDelta | None = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Duration
instead of DurationDelta
, which handles the conversion to datetime for you (and change the variable name to something like expiry
to make it less confusing with delay
).
bot/exts/moderation/slowmode.py
Outdated
slowmode_duration = time.relativedelta_to_timedelta(duration).total_seconds() | ||
humanized_duration = time.humanize_delta(duration) | ||
|
||
expiration_time = datetime.now(tz=UTC) + timedelta(seconds=slowmode_duration) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section is then simplified with the usage of Duration
.
bot/exts/moderation/slowmode.py
Outdated
f"{ctx.author} tried to set the slowmode delay of #{channel} to {humanized_delay}, " | ||
"which is not between 0 and 6 hours." | ||
f"{ctx.author} set the slowmode delay for #{channel} to" | ||
f"{humanized_delay} which expires in {humanized_duration}." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f"{humanized_delay} which expires in {humanized_duration}." | |
f"{humanized_delay} which expires {humanized_duration}." |
Same here, can specify what it's going to revert to.
bot/exts/moderation/slowmode.py
Outdated
await channel.edit(slowmode_delay=slowmode_delay) | ||
await ctx.send( | ||
f"{Emojis.check_mark} The slowmode delay for {channel.mention}" | ||
f" is now {humanized_delay} and expires in {expiration_timestamp}." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here
bot/exts/moderation/slowmode.py
Outdated
await channel.send( | ||
f"{Emojis.check_mark} A previously applied slowmode has expired and has been reverted to {slowmode_delay}." | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slowmode changes are usually silent. Unless the mods agreed that this is what they want I would send this message to #mods and/or #mod-log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like mod-log will already pick up when the slowmode delay is modified and sends it as an embed. Should I also send the above to that channel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And if i send it to mod-log, should get_or_fetch_channel
be used here also? Should the mod-log channel be defined as an instance attribute inside init or is it fine to define it inside of the command provided that get_or_fetch_channel
is used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No if it's automatically picked up then great. I'd just send this to #mods
bot/exts/moderation/slowmode.py
Outdated
async def _revert_slowmode(self, channel_id: int) -> None: | ||
original_slowmode = await self.original_slowmode_cache.get(channel_id) | ||
slowmode_delay = time.humanize_delta(seconds=original_slowmode) | ||
channel = self.bot.get_channel(channel_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use get_or_fetch_channel
here
bot/exts/moderation/slowmode.py
Outdated
if channel is None: | ||
channel = ctx.channel | ||
if await self.slowmode_expiration_cache.contains(channel.id): | ||
await self.slowmode_expiration_cache.delete(channel.id) | ||
await self.original_slowmode_cache.delete(channel.id) | ||
self.scheduler.cancel(channel.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it all be already handled in set_slowmode
?
…ches into one object; update and tidy related tests.
Closes #3327.
Implemented an optional
duration
argument for the slowmode command. If the duration argument is present, the intended expiration datetime and the original slowmode interval are saved in redis cache. After the duration is elapsed, the original slowmode interval is grabbed from redis and restored to the channel.The expiration datetime is cached so that it can persist bot restart.