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

Add msl methods #314

Draft
wants to merge 51 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
35d0493
add MSL methods
MattyTheHacker Aug 18, 2024
247a011
actually do it this time
MattyTheHacker Aug 18, 2024
e4ad85b
add anyio
MattyTheHacker Aug 18, 2024
5365811
improve referencing
MattyTheHacker Aug 18, 2024
2b174c3
implement is_user_member
MattyTheHacker Aug 18, 2024
d758e20
make urls class variable
MattyTheHacker Aug 18, 2024
8192c85
refactor
MattyTheHacker Aug 18, 2024
7831001
yeet the classes
MattyTheHacker Aug 18, 2024
da09b7b
Merge branch 'main' into add-msl-methods
MattyTheHacker Aug 18, 2024
fca2bcb
refactor with config changes
MattyTheHacker Aug 18, 2024
81117f3
more
MattyTheHacker Aug 18, 2024
0eae636
refactor makemember
MattyTheHacker Aug 18, 2024
7de0d21
update comment
MattyTheHacker Aug 18, 2024
10d49ae
add await and remove old link
MattyTheHacker Aug 18, 2024
8f3256e
not working yet
MattyTheHacker Aug 19, 2024
fb8d6a0
refactor
MattyTheHacker Aug 22, 2024
87b19ec
yeet
MattyTheHacker Aug 22, 2024
60a4cb2
Merge branch 'main' into add-msl-methods
MattyTheHacker Aug 22, 2024
2c1122c
Merge branch 'main' into add-msl-methods
MattyTheHacker Aug 24, 2024
67872d7
refactor
MattyTheHacker Aug 24, 2024
6cd4016
build out activities
MattyTheHacker Aug 24, 2024
cf57587
Merge branch 'main' into add-msl-methods
MattyTheHacker Aug 27, 2024
7c7de5d
update deps
MattyTheHacker Aug 27, 2024
d346560
start finances
MattyTheHacker Aug 29, 2024
293d471
Merge branch 'main' into add-msl-methods
MattyTheHacker Aug 30, 2024
c18994e
add some stuff
MattyTheHacker Aug 30, 2024
1597ea6
not implemented
MattyTheHacker Aug 31, 2024
4415d36
formatting
MattyTheHacker Sep 1, 2024
71571d3
Merge branch 'main' into add-msl-methods
MattyTheHacker Sep 5, 2024
edce5da
update lock
MattyTheHacker Sep 5, 2024
0c35949
Merge branch 'main' into add-msl-methods
MattyTheHacker Sep 6, 2024
6002c63
Merge branch 'main' into add-msl-methods
MattyTheHacker Sep 11, 2024
7d3d89f
update deps
MattyTheHacker Sep 11, 2024
6ed18ff
minor finance work
MattyTheHacker Sep 12, 2024
9459613
update deps
MattyTheHacker Sep 12, 2024
798d191
Merge branch 'main' into add-msl-methods
MattyTheHacker Sep 17, 2024
2d798c4
update deps
MattyTheHacker Sep 17, 2024
4f5760e
Merge branch 'main' into add-msl-methods
MattyTheHacker Sep 25, 2024
c7cfd8d
Update dependencies
MattyTheHacker Sep 25, 2024
9cc67b9
add todo
MattyTheHacker Oct 2, 2024
aeaaf8a
Add caching to member list fetching
MattyTheHacker Oct 4, 2024
d39008d
Merge branch 'main' into add-msl-methods
MattyTheHacker Oct 7, 2024
7bb1861
update dependencies
MattyTheHacker Oct 7, 2024
900d911
not working yet
MattyTheHacker Oct 14, 2024
d949e17
Merge branch 'main' into add-msl-methods
MattyTheHacker Oct 14, 2024
c49cb42
update deps
MattyTheHacker Oct 14, 2024
af27c5c
Merge branch 'main' into add-msl-methods
MattyTheHacker Oct 16, 2024
9b656e3
update deps
MattyTheHacker Oct 16, 2024
3c8d312
ruff can suck my nuts
MattyTheHacker Oct 16, 2024
6e73fe2
Merge branch 'main' into add-msl-methods
MattyTheHacker Oct 19, 2024
921041e
update deps
MattyTheHacker Oct 19, 2024
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
63 changes: 3 additions & 60 deletions cogs/make_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
from logging import Logger
from typing import Final

import aiohttp
import bs4
import discord
from bs4 import BeautifulSoup
from django.core.exceptions import ValidationError

from config import settings
Expand All @@ -26,6 +23,7 @@
GuestRoleDoesNotExistError,
)
from utils import CommandChecks, TeXBotApplicationContext, TeXBotBaseCog
from utils.msl import is_student_id_member

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

Expand Down Expand Up @@ -65,8 +63,6 @@
".ASPXAUTH": settings["MEMBERS_LIST_AUTH_SESSION_COOKIE"],
}

REQUEST_URL: Final[str] = settings["MEMBERS_LIST_URL"]


class MakeMemberCommandCog(TeXBotBaseCog):
# noinspection SpellCheckingInspection
Expand Down Expand Up @@ -163,62 +159,9 @@ async def make_member(self, ctx: TeXBotApplicationContext, group_member_id: str)
)
return

guild_member_ids: set[str] = set()

http_session: aiohttp.ClientSession = aiohttp.ClientSession(
headers=REQUEST_HEADERS,
cookies=REQUEST_COOKIES,
)
async with http_session, http_session.get(REQUEST_URL) as http_response:
response_html: str = await http_response.text()

MEMBER_HTML_TABLE_IDS: Final[frozenset[str]] = frozenset(
{
"ctl00_Main_rptGroups_ctl05_gvMemberships",
"ctl00_Main_rptGroups_ctl03_gvMemberships",
"ctl00_ctl00_Main_AdminPageContent_rptGroups_ctl03_gvMemberships",
"ctl00_ctl00_Main_AdminPageContent_rptGroups_ctl05_gvMemberships",
},
)
table_id: str
for table_id in MEMBER_HTML_TABLE_IDS:
parsed_html: bs4.Tag | bs4.NavigableString | None = BeautifulSoup(
response_html,
"html.parser",
).find(
"table",
{"id": table_id},
)

if parsed_html is None or isinstance(parsed_html, bs4.NavigableString):
continue

guild_member_ids.update(
row.contents[2].text
for row in parsed_html.find_all(
"tr",
{"class": ["msl_row", "msl_altrow"]},
)
)

guild_member_ids.discard("")
guild_member_ids.discard("\n")
guild_member_ids.discard(" ")

if not guild_member_ids:
if not await is_student_id_member(student_id=group_member_id):
await self.command_send_error(
ctx,
error_code="E1041",
logging_message=OSError(
"The guild member IDs could not be retrieved from "
"the MEMBERS_LIST_URL.",
),
)
return

if group_member_id not in guild_member_ids:
await self.command_send_error(
ctx,
ctx=ctx,
message=(
f"You must be a member of {self.bot.group_full_name} "
"to use this command.\n"
Expand Down
21 changes: 10 additions & 11 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,20 +378,19 @@ def _setup_roles_messages(cls) -> None:
cls._settings["ROLES_MESSAGES"] = set(messages_dict["roles_messages"]) # type: ignore[call-overload]

@classmethod
def _setup_members_list_url(cls) -> None:
raw_members_list_url: str | None = os.getenv("MEMBERS_LIST_URL")
def _setup_msl_organisation_id(cls) -> None:
raw_msl_organisation_id: str | None = os.getenv("MSL_ORGANISATION_ID")

MEMBERS_LIST_URL_IS_VALID: Final[bool] = bool(
raw_members_list_url
and validators.url(raw_members_list_url),
MSL_ORGANISATION_ID_IS_VALID: Final[bool] = bool(
raw_msl_organisation_id
and re.fullmatch(r"\A\d{4}\Z", raw_msl_organisation_id),
)
if not MEMBERS_LIST_URL_IS_VALID:
INVALID_MEMBERS_LIST_URL_MESSAGE: Final[str] = (
"MEMBERS_LIST_URL must be a valid URL."
if not MSL_ORGANISATION_ID_IS_VALID:
raise ImproperlyConfiguredError(
message="MSL_ORGANISATION_ID must be a 4-digit number.",
)
raise ImproperlyConfiguredError(INVALID_MEMBERS_LIST_URL_MESSAGE)

cls._settings["MEMBERS_LIST_URL"] = raw_members_list_url
cls._settings["MSL_ORGANISATION_ID"] = raw_msl_organisation_id

@classmethod
def _setup_members_list_auth_session_cookie(cls) -> None:
Expand Down Expand Up @@ -708,7 +707,7 @@ def _setup_env_variables(cls) -> None:
cls._setup_ping_command_easter_egg_probability()
cls._setup_welcome_messages()
cls._setup_roles_messages()
cls._setup_members_list_url()
cls._setup_msl_organisation_id()
cls._setup_members_list_auth_session_cookie()
cls._setup_membership_perks_url()
cls._setup_purchase_membership_url()
Expand Down
Loading