Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 39 additions & 3 deletions cogs/make_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

import logging
import re
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, override

import discord
from discord.ext import tasks
from django.core.exceptions import ValidationError

from config import settings
from db.core.models import GroupMadeMember
from exceptions import ApplicantRoleDoesNotExistError, GuestRoleDoesNotExistError
from utils import CommandChecks, TeXBotBaseCog
from utils.msl import fetch_community_group_members_count, is_id_a_community_group_member
from utils.error_capture_decorators import capture_guild_does_not_exist_error
from utils.msl import (
fetch_community_group_members_count,
is_id_a_community_group_member,
update_group_member_list_cache,
)

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

from utils import TeXBotApplicationContext
from utils import TeXBot, TeXBotApplicationContext


__all__: "Sequence[str]" = ("MakeMemberCommandCog", "MemberCountCommandCog")
Expand Down Expand Up @@ -225,3 +231,33 @@ async def member_count(self, ctx: "TeXBotApplicationContext") -> None:
f"{await fetch_community_group_members_count()} members! :tada:"
)
)


Comment thread
CarrotManMatt marked this conversation as resolved.
Outdated
class FetchMemberListOnStartupTaskCog(TeXBotBaseCog):
Comment thread
MattyTheHacker marked this conversation as resolved.
Outdated
"""Cog class that defines a startup task to fetch the member list."""

@override
def __init__(self, bot: "TeXBot") -> None:
"""Start all task managers when this cog is initialised."""
_ = self.fetch_member_list_on_startup_task.start()
super().__init__(bot)
Comment thread
CarrotManMatt marked this conversation as resolved.
Outdated

@override
def cog_unload(self) -> None:
"""Cancel all task managers when this cog is unloaded."""
self.fetch_member_list_on_startup_task.cancel()

@tasks.loop(count=1)
@capture_guild_does_not_exist_error
async def fetch_member_list_on_startup_task(self) -> None:
"""Fetch the member list from the community group on bot startup."""
if await update_group_member_list_cache():
logger.info("Successfully updated the community group member list cache.")
return

logger.warning("Failed to update the community group member list cache on startup.")

@fetch_member_list_on_startup_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()
2 changes: 2 additions & 0 deletions utils/msl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
fetch_community_group_members_count,
fetch_community_group_members_list,
is_id_a_community_group_member,
update_group_member_list_cache,
)

if TYPE_CHECKING:
Expand All @@ -15,4 +16,5 @@
"fetch_community_group_members_count",
"fetch_community_group_members_list",
"is_id_a_community_group_member",
"update_group_member_list_cache",
)
14 changes: 14 additions & 0 deletions utils/msl/memberships.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"fetch_community_group_members_count",
"fetch_community_group_members_list",
"is_id_a_community_group_member",
"update_group_member_list_cache",
)


Expand Down Expand Up @@ -122,3 +123,16 @@ async def is_id_a_community_group_member(member_id: int) -> bool:
async def fetch_community_group_members_count() -> int:
"""Return the total number of members in your community group."""
return len(await fetch_community_group_members_list())


async def update_group_member_list_cache() -> bool:
"""Update the cached community group member list."""
try:
await fetch_community_group_members_list()
except MSLMembershipError as msl_membership_error:
logger.debug(
"Failed to update community group member list cache: %s", msl_membership_error
)
return False
else:
Comment thread
MattyTheHacker marked this conversation as resolved.
Outdated
return True
Comment thread
MattyTheHacker marked this conversation as resolved.
Outdated
Loading