Skip to content

Commit 836b652

Browse files
Improve text_channel and member fetch from ID processing (#535)
Signed-off-by: Matt Norton <matt@carrotmanmatt.com> Co-authored-by: automatic-pr-updater[bot] <217796550+automatic-pr-updater[bot]@users.noreply.github.com>
1 parent 2702944 commit 836b652

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

utils/tex_bot.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
if TYPE_CHECKING:
3030
from collections.abc import Awaitable, Sequence
3131
from logging import Logger
32-
from typing import Final, NoReturn
32+
from typing import Final, LiteralString, NoReturn
3333

3434
from utils import AllChannelTypes
3535

@@ -108,7 +108,7 @@ async def committee_role(self) -> discord.Role:
108108
109109
Raises `CommitteeRoleDoesNotExist` if the role does not exist.
110110
"""
111-
if not self._committee_role or not self._guild_has_role(self._committee_role):
111+
if not self._committee_role or not self._main_guild_has_role(self._committee_role):
112112
self._committee_role = discord.utils.get(
113113
await self.main_guild.fetch_roles(),
114114
name="Committee",
@@ -131,7 +131,7 @@ async def committee_elect_role(self) -> discord.Role:
131131
"""
132132
COMMITTEE_ELECT_ROLE_NEEDS_FETCHING: Final[bool] = bool(
133133
not self._committee_elect_role
134-
or not self._guild_has_role(self._committee_elect_role),
134+
or not self._main_guild_has_role(self._committee_elect_role),
135135
)
136136
if COMMITTEE_ELECT_ROLE_NEEDS_FETCHING:
137137
self._committee_elect_role = discord.utils.get(
@@ -156,7 +156,7 @@ async def guest_role(self) -> discord.Role:
156156
157157
Raises `GuestRoleDoesNotExist` if the role does not exist.
158158
"""
159-
if not self._guest_role or not self._guild_has_role(self._guest_role):
159+
if not self._guest_role or not self._main_guild_has_role(self._guest_role):
160160
self._guest_role = discord.utils.get(
161161
await self.main_guild.fetch_roles(),
162162
name="Guest",
@@ -179,7 +179,7 @@ async def member_role(self) -> discord.Role:
179179
180180
Raises `MemberRoleDoesNotExist` if the role does not exist.
181181
"""
182-
if not self._member_role or not self._guild_has_role(self._member_role):
182+
if not self._member_role or not self._main_guild_has_role(self._member_role):
183183
self._member_role = discord.utils.get(self.main_guild.roles, name="Member")
184184
self._member_role = discord.utils.get(
185185
await self.main_guild.fetch_roles(),
@@ -201,7 +201,7 @@ async def archivist_role(self) -> discord.Role:
201201
202202
Raises `ArchivistRoleDoesNotExist` if the role does not exist.
203203
"""
204-
if not self._archivist_role or not self._guild_has_role(self._archivist_role):
204+
if not self._archivist_role or not self._main_guild_has_role(self._archivist_role):
205205
self._archivist_role = discord.utils.get(
206206
await self.main_guild.fetch_roles(),
207207
name="Archivist",
@@ -219,7 +219,7 @@ async def applicant_role(self) -> discord.Role:
219219
220220
The applicant role allows users to see the specific applicant channels.
221221
"""
222-
if not self._applicant_role or not self._guild_has_role(self._applicant_role):
222+
if not self._applicant_role or not self._main_guild_has_role(self._applicant_role):
223223
self._applicant_role = discord.utils.get(
224224
await self.main_guild.fetch_roles(),
225225
name="Applicant",
@@ -240,8 +240,8 @@ async def roles_channel(self) -> discord.TextChannel:
240240
241241
Raises `RolesChannelDoesNotExist` if the channel does not exist.
242242
"""
243-
if not self._roles_channel or not self._guild_has_channel(self._roles_channel):
244-
self._roles_channel = await self._fetch_text_channel("roles")
243+
if not self._roles_channel or not self._main_guild_has_channel(self._roles_channel):
244+
self._roles_channel = await self._fetch_main_guild_text_channel("roles")
245245

246246
if not self._roles_channel:
247247
raise RolesChannelDoesNotExistError
@@ -255,8 +255,10 @@ async def general_channel(self) -> discord.TextChannel:
255255
256256
Raises `GeneralChannelDoesNotExist` if the channel does not exist.
257257
"""
258-
if not self._general_channel or not self._guild_has_channel(self._general_channel):
259-
self._general_channel = await self._fetch_text_channel("general")
258+
if not self._general_channel or not self._main_guild_has_channel(
259+
self._general_channel
260+
):
261+
self._general_channel = await self._fetch_main_guild_text_channel("general")
260262

261263
if not self._general_channel:
262264
raise GeneralChannelDoesNotExistError
@@ -272,9 +274,10 @@ async def rules_channel(self) -> discord.TextChannel:
272274
273275
Raises `RulesChannelDoesNotExist` if the channel does not exist.
274276
"""
275-
if not self._rules_channel or not self._guild_has_channel(self._rules_channel):
277+
if not self._rules_channel or not self._main_guild_has_channel(self._rules_channel):
276278
self._rules_channel = (
277-
self.main_guild.rules_channel or await self._fetch_text_channel("welcome")
279+
self.main_guild.rules_channel
280+
or await self._fetch_main_guild_text_channel("welcome")
278281
)
279282

280283
if not self._rules_channel:
@@ -390,13 +393,15 @@ def group_moderation_contact(self) -> str:
390393
def _check_guild_accessible(self, guild_id: int) -> bool:
391394
return bool(discord.utils.get(self.guilds, id=guild_id))
392395

393-
def _guild_has_role(self, role: discord.Role) -> bool:
396+
def _main_guild_has_role(self, role: discord.Role) -> bool:
394397
return bool(discord.utils.get(self.main_guild.roles, id=role.id))
395398

396-
def _guild_has_channel(self, channel: discord.TextChannel) -> bool:
399+
def _main_guild_has_channel(self, channel: discord.TextChannel) -> bool:
397400
return bool(discord.utils.get(self.main_guild.text_channels, id=channel.id))
398401

399-
async def _fetch_text_channel(self, name: str) -> discord.TextChannel | None:
402+
async def _fetch_main_guild_text_channel(
403+
self, name: "LiteralString"
404+
) -> discord.TextChannel | None:
400405
text_channel: AllChannelTypes | None = discord.utils.get(
401406
await self.main_guild.fetch_channels(),
402407
name=name,
@@ -485,7 +490,7 @@ async def get_member_from_str_id(self, str_member_id: str) -> discord.Member:
485490
Raises `ValueError` if the provided ID does not represent any member
486491
of your group's Discord guild.
487492
"""
488-
str_member_id = str_member_id.replace("<@", "").replace(">", "")
493+
str_member_id = re.sub(r"\A\s*(<@)(.*)(?(1)>|)\s*\Z", r"\2", str_member_id)
489494

490495
if not re.fullmatch(r"\A\d{17,20}\Z", str_member_id):
491496
INVALID_USER_ID_MESSAGE: Final[str] = f"'{str_member_id}' is not a valid user ID."

0 commit comments

Comments
 (0)