Skip to content
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

Upgrade ruff & fix errors #362

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
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
79 changes: 37 additions & 42 deletions cogs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,8 @@
cogs for each activity.
"""

from collections.abc import Sequence

__all__: Sequence[str] = (
"AnnualRolesResetCommandCog",
"AnnualYearChannelsIncrementCommandCog",
"ArchiveCommandCog",
"ClearRemindersBacklogTaskCog",
"CommandErrorCog",
"CommitteeActionsTrackingSlashCommandsCog",
"CommitteeActionsTrackingContextCommandsCog",
"CommitteeHandoverCommandCog",
"DeleteAllCommandsCog",
"EditMessageCommandCog",
"EnsureMembersInductedCommandCog",
"GetTokenAuthorisationCommandCog",
"InductContextCommandsCog",
"InductSendMessageCog",
"InductSlashCommandCog",
"KillCommandCog",
"MakeApplicantContextCommandsCog",
"MakeApplicantSlashCommandCog",
"MakeMemberCommandCog",
"ManualModerationCog",
"PingCommandCog",
"RemindMeCommandCog",
"SendGetRolesRemindersTaskCog",
"SendIntroductionRemindersTaskCog",
"setup",
"SourceCommandCog",
"StartupCog",
"StatsCommandsCog",
"StrikeCommandCog",
"StrikeUserCommandCog",
"WriteRolesCommandCog",
)


from typing import TYPE_CHECKING

from utils import TeXBot

from .annual_handover_and_reset import (
AnnualRolesResetCommandCog,
AnnualYearChannelsIncrementCommandCog,
Expand Down Expand Up @@ -80,12 +41,46 @@
from .write_roles import WriteRolesCommandCog

if TYPE_CHECKING:
from collections.abc import Iterable
from collections.abc import Iterable, Sequence

from utils import TeXBot, TeXBotBaseCog

from utils import TeXBotBaseCog
__all__: "Sequence[str]" = (
"AnnualRolesResetCommandCog",
"AnnualYearChannelsIncrementCommandCog",
"ArchiveCommandCog",
"ClearRemindersBacklogTaskCog",
"CommandErrorCog",
"CommitteeActionsTrackingContextCommandsCog",
"CommitteeActionsTrackingSlashCommandsCog",
"CommitteeHandoverCommandCog",
"DeleteAllCommandsCog",
"EditMessageCommandCog",
"EnsureMembersInductedCommandCog",
"GetTokenAuthorisationCommandCog",
"InductContextCommandsCog",
"InductSendMessageCog",
"InductSlashCommandCog",
"KillCommandCog",
"MakeApplicantContextCommandsCog",
"MakeApplicantSlashCommandCog",
"MakeMemberCommandCog",
"ManualModerationCog",
"PingCommandCog",
"RemindMeCommandCog",
"SendGetRolesRemindersTaskCog",
"SendIntroductionRemindersTaskCog",
"SourceCommandCog",
"StartupCog",
"StatsCommandsCog",
"StrikeCommandCog",
"StrikeUserCommandCog",
"WriteRolesCommandCog",
"setup",
)


def setup(bot: TeXBot) -> None:
def setup(bot: "TeXBot") -> None:
"""Add all the cogs to the bot, at bot startup."""
cogs: Iterable[type[TeXBotBaseCog]] = (
AnnualRolesResetCommandCog,
Expand Down
52 changes: 28 additions & 24 deletions cogs/annual_handover_and_reset.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
"""Contains cog classes for annual handover and role reset functionality."""

from collections.abc import Sequence

__all__: Sequence[str] = (
"AnnualRolesResetCommandCog",
"AnnualYearChannelsIncrementCommandCog",
"CommitteeHandoverCommandCog",
)

import datetime
import logging
from logging import Logger
from typing import Final
from typing import TYPE_CHECKING

import discord

from db.core.models import GroupMadeMember
from utils import AllChannelTypes, CommandChecks, TeXBotApplicationContext, TeXBotBaseCog
from utils import CommandChecks, TeXBotBaseCog

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

from utils import AllChannelTypes, TeXBotApplicationContext

__all__: "Sequence[str]" = (
"AnnualRolesResetCommandCog",
"AnnualYearChannelsIncrementCommandCog",
"CommitteeHandoverCommandCog",
)

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


class CommitteeHandoverCommandCog(TeXBotBaseCog):
Expand All @@ -30,7 +34,7 @@ class CommitteeHandoverCommandCog(TeXBotBaseCog):
)
@CommandChecks.check_interaction_user_has_committee_role
@CommandChecks.check_interaction_user_in_main_guild
async def committee_handover(self, ctx: TeXBotApplicationContext) -> None:
async def committee_handover(self, ctx: "TeXBotApplicationContext") -> None:
"""
Definition & callback response of the "committee_handover" command.

Expand All @@ -54,7 +58,7 @@ async def committee_handover(self, ctx: TeXBotApplicationContext) -> None:
logger.debug("Running the handover command!")

HANDOVER_AUDIT_MESSAGE: Final[str] = (
f"{ctx.user} used TeX-Bot slash-command: \"/committee-handover\""
f'{ctx.user} used TeX-Bot slash-command: "/committee-handover"'
)

if main_guild.me.top_role.position < committee_role.position:
Expand All @@ -65,7 +69,7 @@ async def committee_handover(self, ctx: TeXBotApplicationContext) -> None:
await initial_response.edit(
content=(
":warning: This command requires TeX-Bot to hold a role higher than "
"that of the \"Committee\" role to perform this action. Operation aborted."
'that of the "Committee" role to perform this action. Operation aborted.'
" :warning:"
),
)
Expand All @@ -91,7 +95,7 @@ async def committee_handover(self, ctx: TeXBotApplicationContext) -> None:
await initial_response.edit(
content=(
":hourglass: Giving committee users access to the #handover channel and "
"removing the \"Committee\" role... :hourglass:"
'removing the "Committee" role... :hourglass:'
),
)

Expand Down Expand Up @@ -129,8 +133,8 @@ async def committee_handover(self, ctx: TeXBotApplicationContext) -> None:

await initial_response.edit(
content=(
":hourglass: Giving committee-elect users the \"Committee\" role "
"and removing their \"Committee-Elect\" role... :hourglass:"
':hourglass: Giving committee-elect users the "Committee" role '
'and removing their "Committee-Elect" role... :hourglass:'
),
)

Expand All @@ -156,7 +160,7 @@ async def committee_handover(self, ctx: TeXBotApplicationContext) -> None:
class AnnualRolesResetCommandCog(TeXBotBaseCog):
"""Cog class that defines the "/annual-roles-reset" command."""

ACADEMIC_YEAR_ROLE_NAMES: Final[frozenset[str]] = frozenset(
ACADEMIC_YEAR_ROLE_NAMES: "Final[frozenset[str]]" = frozenset(
{
"Foundation Year",
"First Year",
Expand All @@ -175,7 +179,7 @@ class AnnualRolesResetCommandCog(TeXBotBaseCog):
)
@CommandChecks.check_interaction_user_has_committee_role
@CommandChecks.check_interaction_user_in_main_guild
async def annual_roles_reset(self, ctx: TeXBotApplicationContext) -> None:
async def annual_roles_reset(self, ctx: "TeXBotApplicationContext") -> None:
"""
Definition & callback response of the "annual_roles_reset" command.

Expand All @@ -193,7 +197,7 @@ async def annual_roles_reset(self, ctx: TeXBotApplicationContext) -> None:
)

ROLE_RESET_AUDIT_MESSAGE: Final[str] = (
f"{ctx.user} used TeX-Bot slash-command: \"/annual_roles_reset\""
f'{ctx.user} used TeX-Bot slash-command: "/annual_roles_reset"'
)

member: discord.Member
Expand Down Expand Up @@ -244,7 +248,7 @@ class AnnualYearChannelsIncrementCommandCog(TeXBotBaseCog):
)
@CommandChecks.check_interaction_user_has_committee_role
@CommandChecks.check_interaction_user_in_main_guild
async def increment_year_channels(self, ctx: TeXBotApplicationContext) -> None:
async def increment_year_channels(self, ctx: "TeXBotApplicationContext") -> None:
"""
Definition and callback response of the "increment_year_channels" command.

Expand All @@ -269,7 +273,7 @@ async def increment_year_channels(self, ctx: TeXBotApplicationContext) -> None:

if final_year_channel:
await initial_message.edit(
content=":hourglass: Archiving \"final-years\" channel... :hourglass:",
content=':hourglass: Archiving "final-years" channel... :hourglass:',
)
archivist_role: discord.Role = await self.bot.archivist_role

Expand Down Expand Up @@ -314,7 +318,7 @@ async def increment_year_channels(self, ctx: TeXBotApplicationContext) -> None:

await initial_message.edit(
content=(
":hourglass: Creating new \"first-years\" channel and setting permissions... "
':hourglass: Creating new "first-years" channel and setting permissions... '
":hourglass:"
),
)
Expand Down
75 changes: 40 additions & 35 deletions cogs/archive.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
"""Contains cog classes for any archival interactions."""

from collections.abc import Sequence

__all__: Sequence[str] = ("ArchiveCommandCog",)


import logging
import re
from collections.abc import Set
from logging import Logger
from typing import Final
from typing import TYPE_CHECKING

import discord

from exceptions import DiscordMemberNotInMainGuildError
from exceptions.base import BaseDoesNotExistError
from utils import (
AllChannelTypes,
CommandChecks,
TeXBotApplicationContext,
TeXBotAutocompleteContext,
TeXBotBaseCog,
)

logger: Final[Logger] = logging.getLogger("TeX-Bot")
if TYPE_CHECKING:
from collections.abc import Sequence
from collections.abc import Set as AbstractSet
from logging import Logger
from typing import Final

from utils import (
AllChannelTypes,
TeXBotApplicationContext,
TeXBotAutocompleteContext,
)

__all__: "Sequence[str]" = ("ArchiveCommandCog",)

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


class ArchiveCommandCog(TeXBotBaseCog):
"""Cog class that defines the "/archive" command and its call-back method."""

@staticmethod
async def autocomplete_get_categories(ctx: TeXBotAutocompleteContext) -> Set[discord.OptionChoice] | Set[str]: # noqa: E501
async def autocomplete_get_categories(
ctx: "TeXBotAutocompleteContext",
) -> "AbstractSet[discord.OptionChoice] | AbstractSet[str]":
"""
Autocomplete callable that generates the set of available selectable categories.

Expand All @@ -53,8 +59,7 @@ async def autocomplete_get_categories(ctx: TeXBotAutocompleteContext) -> Set[dis

return {
discord.OptionChoice(name=category.name, value=str(category.id))
for category
in main_guild.categories
for category in main_guild.categories
if category.permissions_for(interaction_user).is_superset(
discord.Permissions(send_messages=True, view_channel=True),
)
Expand All @@ -74,7 +79,7 @@ async def autocomplete_get_categories(ctx: TeXBotAutocompleteContext) -> Set[dis
)
@CommandChecks.check_interaction_user_has_committee_role
@CommandChecks.check_interaction_user_in_main_guild
async def archive(self, ctx: TeXBotApplicationContext, str_category_id: str) -> None:
async def archive(self, ctx: "TeXBotApplicationContext", str_category_id: str) -> None:
"""
Definition & callback response of the "archive" command.

Expand Down Expand Up @@ -123,64 +128,64 @@ async def archive(self, ctx: TeXBotApplicationContext, str_category_id: str) ->
channel: AllChannelTypes
for channel in category.channels:
try:
CHANNEL_NEEDS_COMMITTEE_ARCHIVING: bool = (
channel.permissions_for(committee_role).is_superset(
discord.Permissions(view_channel=True),
) and not channel.permissions_for(guest_role).is_superset(
discord.Permissions(view_channel=True),
)
CHANNEL_NEEDS_COMMITTEE_ARCHIVING: bool = channel.permissions_for(
committee_role
).is_superset(
discord.Permissions(view_channel=True),
) and not channel.permissions_for(guest_role).is_superset(
discord.Permissions(view_channel=True),
)
CHANNEL_NEEDS_NORMAL_ARCHIVING: bool = (
channel.permissions_for(guest_role).is_superset(
discord.Permissions(view_channel=True),
)
CHANNEL_NEEDS_NORMAL_ARCHIVING: bool = channel.permissions_for(
guest_role
).is_superset(
discord.Permissions(view_channel=True),
)
if CHANNEL_NEEDS_COMMITTEE_ARCHIVING:
await channel.set_permissions(
everyone_role,
reason=f"{interaction_member.display_name} used \"/archive\".",
reason=f'{interaction_member.display_name} used "/archive".',
view_channel=False,
)
await channel.set_permissions(
guest_role,
overwrite=None,
reason=f"{interaction_member.display_name} used \"/archive\".",
reason=f'{interaction_member.display_name} used "/archive".',
)
await channel.set_permissions(
member_role,
overwrite=None,
reason=f"{interaction_member.display_name} used \"/archive\".",
reason=f'{interaction_member.display_name} used "/archive".',
)
await channel.set_permissions(
committee_role,
overwrite=None,
reason=f"{interaction_member.display_name} used \"/archive\".",
reason=f'{interaction_member.display_name} used "/archive".',
)

elif CHANNEL_NEEDS_NORMAL_ARCHIVING:
await channel.set_permissions(
everyone_role,
reason=f"{interaction_member.display_name} used \"/archive\".",
reason=f'{interaction_member.display_name} used "/archive".',
view_channel=False,
)
await channel.set_permissions(
guest_role,
overwrite=None,
reason=f"{interaction_member.display_name} used \"/archive\".",
reason=f'{interaction_member.display_name} used "/archive".',
)
await channel.set_permissions(
member_role,
overwrite=None,
reason=f"{interaction_member.display_name} used \"/archive\".",
reason=f'{interaction_member.display_name} used "/archive".',
)
await channel.set_permissions(
committee_role,
reason=f"{interaction_member.display_name} used \"/archive\".",
reason=f'{interaction_member.display_name} used "/archive".',
view_channel=False,
)
await channel.set_permissions(
archivist_role,
reason=f"{interaction_member.display_name} used \"/archive\".",
reason=f'{interaction_member.display_name} used "/archive".',
view_channel=True,
)

Expand Down
Loading