Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
6c0c43b
do some stuff
MattyTheHacker May 15, 2025
ca1c6c4
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] May 18, 2025
3a66e50
Merge branch 'main' into 479-auto-slow-mode
MattyTheHacker May 18, 2025
a29067d
Fix some stuff
MattyTheHacker May 18, 2025
2c18202
fix mypy error
MattyTheHacker May 18, 2025
867e775
Implement the task
MattyTheHacker May 18, 2025
886aeb4
Merge branch 'main' into 479-auto-slow-mode
MattyTheHacker May 20, 2025
0e5ba44
Merge branch 'main' into 479-auto-slow-mode
MattyTheHacker May 21, 2025
688627a
Implement message rate method
MattyTheHacker May 23, 2025
d0125f0
improve disablement message
MattyTheHacker May 23, 2025
f681a65
improve disablement message
MattyTheHacker May 23, 2025
dce5dff
basic functionality
MattyTheHacker May 23, 2025
9211020
Merge branch 'main' into 479-auto-slow-mode
MattyTheHacker May 27, 2025
27d269e
Merge branch 'main' into 479-auto-slow-mode
MattyTheHacker May 27, 2025
4286023
Merge main into 479-auto-slow-mode
cssbhamdev Jun 12, 2025
a2ca2ff
Merge main into 479-auto-slow-mode
cssbhamdev Jun 13, 2025
9fbee1f
Merge main into 479-auto-slow-mode
cssbhamdev Jun 13, 2025
d39918b
Merge main into 479-auto-slow-mode
cssbhamdev Jun 14, 2025
1a6ff75
Allow committee-elect to update actions (and appear in auto-complete)…
Thatsmusic99 Jun 15, 2025
08f963b
Merge main into 479-auto-slow-mode
cssbhamdev Jun 15, 2025
2c12a00
Merge main into 479-auto-slow-mode
cssbhamdev Jun 15, 2025
a9da921
Merge main into 479-auto-slow-mode
cssbhamdev Jun 15, 2025
eb681ea
Merge main into 479-auto-slow-mode
cssbhamdev Jun 15, 2025
b8beaca
Merge main into 479-auto-slow-mode
cssbhamdev Jun 16, 2025
429df0d
Merge main into 479-auto-slow-mode
cssbhamdev Jun 16, 2025
459b5c9
Merge main into 479-auto-slow-mode
cssbhamdev Jun 17, 2025
f9f48c2
Merge main into 479-auto-slow-mode
cssbhamdev Jun 19, 2025
03b7e12
Merge main into 479-auto-slow-mode
cssbhamdev Jun 19, 2025
78ac0bc
Merge main into 479-auto-slow-mode
cssbhamdev Jun 22, 2025
6ca1183
Merge main into 479-auto-slow-mode
cssbhamdev Jun 24, 2025
0c8b9ac
Merge main into 479-auto-slow-mode
cssbhamdev Jun 24, 2025
6e7c672
Merge main into 479-auto-slow-mode
cssbhamdev Jun 24, 2025
6be3820
Merge main into 479-auto-slow-mode
cssbhamdev Jun 25, 2025
0629715
Merge main into 479-auto-slow-mode
cssbhamdev Jun 30, 2025
1377367
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Jun 30, 2025
98411b8
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Jul 2, 2025
c53fbd8
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Jul 2, 2025
7dcfa71
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Jul 3, 2025
9efc15f
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Jul 3, 2025
b01c0e2
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Jul 3, 2025
1a09d4e
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Jul 4, 2025
f79b98f
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Jul 4, 2025
d471cf9
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Jul 4, 2025
dc02aff
Merge branch 'main' into 479-auto-slow-mode
MattyTheHacker Aug 8, 2025
068d3bf
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 9, 2025
47e5f6d
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 11, 2025
34105c6
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 11, 2025
21e513c
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 12, 2025
9967a1d
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 18, 2025
1ca1dd8
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 26, 2025
43a2569
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 26, 2025
72a8648
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 28, 2025
3d841a0
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 29, 2025
a3f4282
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 29, 2025
9d902c9
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 29, 2025
889bc37
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Aug 30, 2025
3be7ec2
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Sep 2, 2025
bb963d6
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Sep 2, 2025
e6d3da0
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Sep 4, 2025
6799d9f
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Sep 4, 2025
9c4f404
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Sep 5, 2025
d510888
Merge main into 479-auto-slow-mode
automatic-pr-updater[bot] Sep 5, 2025
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
5 changes: 5 additions & 0 deletions cogs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
CommitteeHandoverCommandCog,
)
from .archive import ArchiveCommandCog
from .auto_slow_mode import AutomaticSlowModeCommandCog, AutomaticSlowModeTaskCog
from .check_su_platform_authorisation import (
CheckSUPlatformAuthorisationCommandCog,
CheckSUPlatformAuthorisationTaskCog,
Expand Down Expand Up @@ -56,6 +57,8 @@
"AnnualRolesResetCommandCog",
"AnnualYearChannelsIncrementCommandCog",
"ArchiveCommandCog",
"AutomaticSlowModeCommandCog",
"AutomaticSlowModeTaskCog",
"CheckSUPlatformAuthorisationCommandCog",
"CheckSUPlatformAuthorisationTaskCog",
"ClearRemindersBacklogTaskCog",
Expand Down Expand Up @@ -98,6 +101,8 @@ def setup(bot: "TeXBot") -> None:
AnnualRolesResetCommandCog,
AnnualYearChannelsIncrementCommandCog,
ArchiveCommandCog,
AutomaticSlowModeCommandCog,
AutomaticSlowModeTaskCog,
ClearRemindersBacklogTaskCog,
CommandErrorCog,
CommitteeActionsTrackingSlashCommandsCog,
Expand Down
136 changes: 136 additions & 0 deletions cogs/auto_slow_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"""Module to handle automatic slow mode for Discord channels."""

import logging
from typing import TYPE_CHECKING, override

import discord
from discord.ext import tasks

from config import settings
from utils import CommandChecks, TeXBotBaseCog
from utils.error_capture_decorators import capture_guild_does_not_exist_error

if TYPE_CHECKING:
from collections.abc import Sequence
from logging import Logger
from typing import Final

from utils import TeXBot, TeXBotApplicationContext


__all__: "Sequence[str]" = ("AutomaticSlowModeCommandCog", "AutomaticSlowModeTaskCog")


logger: "Final[Logger]" = logging.getLogger("TeX-Bot")


class AutomaticSlowModeBaseCog(TeXBotBaseCog):
"""Base class for automatic slow mode functionality."""

async def calculate_message_rate(self, channel: discord.TextChannel) -> int:
"""
Calculate the message rate for a given channel.

Returns the number of messages per minute, rounded to the nearest integer.
This is based on the previous 5 minutes of messages.
"""
from datetime import UTC, datetime, timedelta

Check failure on line 37 in cogs/auto_slow_mode.py

View workflow job for this annotation

GitHub Actions / ruff-lint

Ruff (PLC0415)

cogs/auto_slow_mode.py:37:9: PLC0415 `import` should be at the top-level of a file

# TODO: Make the time period user configurable. # noqa: FIX002

count = len(
[
message
async for message in channel.history(
after=datetime.now(UTC) - timedelta(minutes=5),
oldest_first=False,
limit=None,
)
]
)

logger.debug(
"Channel: %s | Message count in last 5 minutes: %d | Rate: %d",
channel.name,
count,
round(count / 5),
)

return round(count / 5)


class AutomaticSlowModeTaskCog(AutomaticSlowModeBaseCog):
"""Task to handle automatic slow mode for Discord channels."""

@override
def __init__(self, bot: "TeXBot") -> None:
"""Start all task managers when this cog is initialised."""
if settings["AUTO_SLOW_MODE"]:
_ = self.auto_slow_mode_task.start()

super().__init__(bot)

@override
def cog_unload(self) -> None:
"""
Unload-hook that ends all running tasks whenever the cog is unloaded.

This may be run dynamically or when the bot closes.
"""
self.auto_slow_mode_task.cancel()

@tasks.loop(seconds=60)
@capture_guild_does_not_exist_error
async def auto_slow_mode_task(self) -> None:
"""Task to automatically adjust slow mode in channels."""
import time

Check failure on line 86 in cogs/auto_slow_mode.py

View workflow job for this annotation

GitHub Actions / ruff-lint

Ruff (PLC0415)

cogs/auto_slow_mode.py:86:9: PLC0415 `import` should be at the top-level of a file

time.process_time()
for channel in self.bot.get_all_channels():
if isinstance(channel, discord.TextChannel):
message_rate: int = await self.calculate_message_rate(channel)
if message_rate > 5:
await channel.edit(
slowmode_delay=10, reason="TeX-Bot auto slow mode enabled."
)
await channel.edit(slowmode_delay=0, reason="TeX-Bot auto slow mode disabled.")
logger.debug("Time taken to calculate message rate: %s seconds", time.process_time())

@auto_slow_mode_task.before_loop
async def before_tasks(self) -> None:
"""Pre-execution hook, preventing any tasks from executing before the bot is ready."""
await self.bot.wait_until_ready()


class AutomaticSlowModeCommandCog(AutomaticSlowModeBaseCog):
"""Cog to handle automatic slow mode for Discord channels."""

@discord.slash_command( # type: ignore[misc, no-untyped-call]
name="toggle-auto-slow-mode",
description="Enable or disable automatic slow mode.",
)
@CommandChecks.check_interaction_user_has_committee_role
@CommandChecks.check_interaction_user_in_main_guild
async def toggle_auto_slow_mode( # type: ignore[misc]
self,
ctx: "TeXBotApplicationContext",
) -> None:
"""Enable or disable automatic slow mode for a channel."""
# NOTE: This should be replaced when the settings improved in PR #221
if settings["AUTO_SLOW_MODE"]:
settings["AUTO_SLOW_MODE"] = False
await ctx.respond(
"Automatic slow mode is now disabled."
"If you would like to keep it disabled, please remember to "
"update the deployment variables as well. "
"If you do not, it will be re-enabled when the bot restarts.",
)
return

settings["AUTO_SLOW_MODE"] = True
await ctx.respond(
"Automatic slow mode is now enabled."
"If you would like to keep it enabled, please remember to "
"update the deployment variables as well. "
"If you do not, it will be disabled again when the bot restarts.",
)
15 changes: 15 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,20 @@ def _setup_auto_add_committee_to_threads(cls) -> None:
raw_auto_add_committee_to_threads in TRUE_VALUES
)

@classmethod
def _setup_auto_slow_mode(cls) -> None:
raw_auto_slow_mode: str = str(
os.getenv("AUTO_SLOW_MODE", "True"),
).lower()

if raw_auto_slow_mode not in TRUE_VALUES | FALSE_VALUES:
INVALID_AUTO_SLOW_MODE_MESSAGE: Final[str] = (
"AUTO_SLOW_MODE must be a boolean value."
)
raise ImproperlyConfiguredError(INVALID_AUTO_SLOW_MODE_MESSAGE)

cls._settings["AUTO_SLOW_MODE"] = raw_auto_slow_mode in TRUE_VALUES

@classmethod
def _setup_env_variables(cls) -> None:
"""
Expand Down Expand Up @@ -978,6 +992,7 @@ def _setup_env_variables(cls) -> None:
cls._setup_moderation_document_url()
cls._setup_strike_performed_manually_warning_location()
cls._setup_auto_add_committee_to_threads()
cls._setup_auto_slow_mode()
except ImproperlyConfiguredError as improper_config_error:
webhook_config_logger.error(improper_config_error.message) # noqa: TRY400
raise improper_config_error from improper_config_error
Expand Down
Loading