Skip to content

Commit 58f79f0

Browse files
committed
Small fixes
1 parent 8718f49 commit 58f79f0

31 files changed

Lines changed: 161 additions & 287 deletions

cogs/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"StartupCog",
7676
"StatsCommandsCog",
7777
"StrikeCommandCog",
78-
"StrikeUserCommandCog",
78+
"StrikeContextCommandsCog",
7979
"WriteRolesCommandCog",
8080
"setup",
8181
)

cogs/command_error.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
"""Contains cog classes for any command_error interactions."""
22

3-
import contextlib
43
import logging
54
from typing import TYPE_CHECKING
65

76
import discord
87
from discord import Forbidden
98
from discord.ext.commands.errors import CheckAnyFailure
109

11-
from exceptions import (
12-
CommitteeRoleDoesNotExistError,
13-
GuildDoesNotExistError,
14-
)
10+
from exceptions import GuildDoesNotExistError
1511
from exceptions.base import BaseErrorWithErrorCode
1612
from utils import CommandChecks, TeXBotBaseCog
1713

@@ -59,11 +55,10 @@ async def on_application_command_error(
5955
)
6056

6157
elif CommandChecks.is_interaction_user_has_committee_role_failure(error.checks[0]):
62-
# noinspection PyUnusedLocal
63-
committee_role_mention: str = "@Committee"
64-
with contextlib.suppress(CommitteeRoleDoesNotExistError):
65-
committee_role_mention = (await self.bot.committee_role).mention
66-
message = f"Only {committee_role_mention} members can run this command."
58+
message = (
59+
f"Only {await self.bot.get_mention_string(self.bot.committee_role)} "
60+
"members can run this command."
61+
)
6762

6863
await self.command_send_error(
6964
ctx,

cogs/delete_all.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ async def _delete_all(
3131
ctx: "TeXBotApplicationContext", delete_model: type["AsyncBaseModel"]
3232
) -> None:
3333
"""Perform the actual deletion process of all instances of the given model class."""
34-
# noinspection PyProtectedMember
3534
await delete_model._default_manager.all().adelete()
3635

3736
delete_model_instances_name_plural: str = (

cogs/edit_message.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626

2727
class EditMessageCommandCog(TeXBotBaseCog):
28-
# noinspection SpellCheckingInspection
2928
"""Cog class that defines the "/edit-message" command and its call-back method."""
3029

3130
@staticmethod
@@ -50,7 +49,6 @@ async def autocomplete_get_text_channels(
5049

5150
return await TeXBotBaseCog.autocomplete_get_text_channels(ctx)
5251

53-
# noinspection SpellCheckingInspection
5452
@discord.slash_command( # type: ignore[no-untyped-call, misc]
5553
name="edit-message",
5654
description="Edits a message sent by TeX-Bot to the value supplied.",

cogs/induct.py

Lines changed: 53 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
GuestRoleDoesNotExistError,
1616
GuildDoesNotExistError,
1717
MemberRoleDoesNotExistError,
18-
RolesChannelDoesNotExistError,
19-
RulesChannelDoesNotExistError,
2018
)
2119
from utils import (
2220
CommandChecks,
@@ -77,59 +75,60 @@ async def on_member_update(self, before: discord.Member, after: discord.Member)
7775
await IntroductionReminderOptOutMember.objects.aget(discord_id=before.id)
7876
).adelete()
7977

80-
async for message in after.history():
78+
reminder_message: discord.Message
79+
async for reminder_message in after.history():
8180
MESSAGE_IS_INTRODUCTION_REMINDER: bool = bool(
82-
("joined the " in message.content)
83-
and (" Discord guild but have not yet introduced" in message.content)
84-
and message.author.bot
81+
("joined the " in reminder_message.content)
82+
and (" Discord guild but have not yet introduced" in reminder_message.content)
83+
and reminder_message.author.bot
8584
)
8685
if MESSAGE_IS_INTRODUCTION_REMINDER:
87-
await message.delete(
86+
await reminder_message.delete(
8887
reason="Delete introduction reminders after member is inducted.",
8988
)
9089

91-
# noinspection PyUnusedLocal
92-
rules_channel_mention: str = "**`#welcome`**"
93-
with contextlib.suppress(RulesChannelDoesNotExistError):
94-
rules_channel_mention = (await self.bot.rules_channel).mention
95-
96-
# noinspection PyUnusedLocal
97-
roles_channel_mention: str = "**`#roles`**"
98-
with contextlib.suppress(RolesChannelDoesNotExistError):
99-
roles_channel_mention = (await self.bot.roles_channel).mention
100-
101-
user_type: Literal["guest", "member"] = "guest"
102-
with contextlib.suppress(MemberRoleDoesNotExistError):
103-
if await self.bot.member_role in after.roles:
104-
user_type = "member"
105-
90+
user_type: Literal["guest", "member"]
10691
try:
107-
await after.send(
92+
user_type = "member" if await self.bot.member_role in after.roles else "guest"
93+
except MemberRoleDoesNotExistError:
94+
user_type = "guest"
95+
96+
messages_to_send: list[str] = [
97+
(
10898
f"**Congrats on joining the {self.bot.group_short_name} Discord server "
10999
f"as a {user_type}!** "
110100
"You now have access to communicate in all the public channels.\n\n"
111101
"Some things to do to get started:\n"
112-
f"1. Check out our rules in {rules_channel_mention}\n"
113-
f"2. Head to {roles_channel_mention} and click on the icons to get "
114-
"optional roles like pronouns and year groups\n"
102+
f"1. Check out our rules in {
103+
await self.bot.get_mention_string(self.bot.rules_channel)
104+
}\n"
105+
f"2. Head to {
106+
await self.bot.get_mention_string(self.bot.roles_channel)
107+
} and click on the icons to get optional roles like pronouns and year groups\n"
115108
"3. Change your nickname to whatever you wish others to refer to you as "
116109
"(You can do this by right-clicking your name in the members-list "
117-
'to the right & selecting "Edit Server Profile").',
110+
'to the right & selecting "Edit Server Profile").'
111+
),
112+
]
113+
114+
if user_type != "member":
115+
messages_to_send.append(
116+
f"You can also get yourself an annual membership "
117+
f"to {self.bot.group_full_name} for only £5! "
118+
f"Just head to {settings['PURCHASE_MEMBERSHIP_URL']}. "
119+
"You'll get awesome perks like a free T-shirt:shirt:, "
120+
"access to member only events:calendar_spiral: and a cool green name on "
121+
f"the {self.bot.group_short_name} Discord server:green_square:! "
122+
f"Checkout all the perks at {settings['MEMBERSHIP_PERKS_URL']}",
118123
)
119-
if user_type != "member":
120-
await after.send(
121-
f"You can also get yourself an annual membership "
122-
f"to {self.bot.group_full_name} for only £5! "
123-
f"Just head to {settings['PURCHASE_MEMBERSHIP_URL']}. "
124-
"You'll get awesome perks like a free T-shirt:shirt:, "
125-
"access to member only events:calendar_spiral: and a cool green name on "
126-
f"the {self.bot.group_short_name} Discord server:green_square:! "
127-
f"Checkout all the perks at {settings['MEMBERSHIP_PERKS_URL']}",
128-
)
124+
125+
try:
126+
message_to_send: str
127+
for message_to_send in messages_to_send:
128+
await after.send(message_to_send)
129129
except discord.Forbidden:
130130
logger.info(
131-
"Failed to open DM channel to user %s so no welcome message was sent.",
132-
after,
131+
"Failed to open DM channel to user %s so no welcome message was sent.", after
133132
)
134133

135134

@@ -227,34 +226,28 @@ async def _perform_induction(
227226
return
228227

229228
if not silent:
230-
general_channel: discord.TextChannel = await self.bot.general_channel
231-
232-
# noinspection PyUnusedLocal
233-
roles_channel_mention: str = "**`#roles`**"
234-
with contextlib.suppress(RolesChannelDoesNotExistError):
235-
roles_channel_mention = (await self.bot.roles_channel).mention
236-
237-
await general_channel.send(
229+
await (await self.bot.general_channel).send(
238230
f"{await self.get_random_welcome_message(induction_member)} :tada:\n"
239-
f"Remember to grab your roles in {roles_channel_mention} "
240-
"and say hello to everyone here! :wave:",
231+
f"Remember to grab your roles in {
232+
await self.bot.get_mention_string(self.bot.roles_channel)
233+
} and say hello to everyone here! :wave:",
241234
)
242235

243236
await induction_member.add_roles(
244237
guest_role,
245238
reason=INDUCT_AUDIT_MESSAGE,
246239
)
247240

248-
# noinspection PyUnusedLocal
249-
applicant_role: discord.Role | None = None
250-
with contextlib.suppress(ApplicantRoleDoesNotExistError):
251-
applicant_role = await ctx.bot.applicant_role
252-
253-
if applicant_role and applicant_role in induction_member.roles:
254-
await induction_member.remove_roles(
255-
applicant_role,
256-
reason=INDUCT_AUDIT_MESSAGE,
257-
)
241+
try:
242+
applicant_role: discord.Role = await ctx.bot.applicant_role
243+
except ApplicantRoleDoesNotExistError:
244+
pass
245+
else:
246+
if applicant_role in induction_member.roles:
247+
await induction_member.remove_roles(
248+
applicant_role,
249+
reason=INDUCT_AUDIT_MESSAGE,
250+
)
258251

259252
tex_emoji: discord.Emoji | None = self.bot.get_emoji(743218410409820213)
260253
if not tex_emoji:
@@ -365,7 +358,7 @@ async def induct( # type: ignore[misc]
365358

366359

367360
class InductContextCommandsCog(BaseInductCog):
368-
"""Cog class that defines the context-menu induction commands & their call-back methods."""
361+
"""Cog class to define the context-menu induction commands and their call-back methods."""
369362

370363
@discord.user_command(name="Induct User") # type: ignore[no-untyped-call, misc]
371364
@CommandChecks.check_interaction_user_has_committee_role
@@ -434,7 +427,6 @@ async def non_silent_message_induct( # type: ignore[misc]
434427
class EnsureMembersInductedCommandCog(TeXBotBaseCog):
435428
"""Cog class that defines the "/ensure-members-inducted" command and call-back method."""
436429

437-
# noinspection SpellCheckingInspection
438430
@discord.slash_command( # type: ignore[no-untyped-call, misc]
439431
name="ensure-members-inducted",
440432
description="Ensures all users with the @Member role also have the @Guest role.",

cogs/kill.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Contains cog classes for any killing interactions."""
22

3-
import contextlib
43
import logging
54
from typing import TYPE_CHECKING
65

@@ -64,9 +63,11 @@ async def kill(self, ctx: "TeXBotApplicationContext") -> None: # type: ignore[m
6463
The "kill" command shuts down TeX-Bot,
6564
but only after the user has confirmed that this is the action they wish to take.
6665
"""
67-
committee_role: discord.Role | None = None
68-
with contextlib.suppress(CommitteeRoleDoesNotExistError):
66+
committee_role: discord.Role | None
67+
try:
6968
committee_role = await self.bot.committee_role
69+
except CommitteeRoleDoesNotExistError:
70+
committee_role = None
7071

7172
response: discord.Message | discord.Interaction = await ctx.respond(
7273
content=(

cogs/make_member.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Contains cog classes for any make_member interactions."""
22

3-
import contextlib
43
import logging
54
import re
65
from typing import TYPE_CHECKING
@@ -13,11 +12,7 @@
1312

1413
from config import settings
1514
from db.core.models import GroupMadeMember
16-
from exceptions import (
17-
ApplicantRoleDoesNotExistError,
18-
CommitteeRoleDoesNotExistError,
19-
GuestRoleDoesNotExistError,
20-
)
15+
from exceptions import ApplicantRoleDoesNotExistError, GuestRoleDoesNotExistError
2116
from utils import CommandChecks, TeXBotBaseCog
2217

2318
if TYPE_CHECKING:
@@ -78,10 +73,8 @@
7873

7974

8075
class MakeMemberCommandCog(TeXBotBaseCog):
81-
# noinspection SpellCheckingInspection
8276
"""Cog class that defines the "/makemember" command and its call-back method."""
8377

84-
# noinspection SpellCheckingInspection
8578
@discord.slash_command( # type: ignore[no-untyped-call, misc]
8679
name="makemember",
8780
description=(
@@ -118,7 +111,7 @@ class MakeMemberCommandCog(TeXBotBaseCog):
118111
parameter_name="group_member_id",
119112
)
120113
@CommandChecks.check_interaction_user_in_main_guild
121-
async def make_member(self, ctx: "TeXBotApplicationContext", group_member_id: str) -> None: # type: ignore[misc] # noqa: PLR0915
114+
async def make_member(self, ctx: "TeXBotApplicationContext", group_member_id: str) -> None: # type: ignore[misc]
122115
"""
123116
Definition & callback response of the "make_member" command.
124117
@@ -152,24 +145,17 @@ async def make_member(self, ctx: "TeXBotApplicationContext", group_member_id: st
152145
)
153146
return
154147

155-
GROUP_MEMBER_ID_IS_ALREADY_USED: Final[
156-
bool
157-
] = await GroupMadeMember.objects.filter(
148+
if await GroupMadeMember.objects.filter(
158149
hashed_group_member_id=GroupMadeMember.hash_group_member_id(
159150
group_member_id, self.bot.group_member_id_type
160151
)
161-
).aexists()
162-
if GROUP_MEMBER_ID_IS_ALREADY_USED:
163-
# noinspection PyUnusedLocal
164-
committee_mention: str = "committee"
165-
with contextlib.suppress(CommitteeRoleDoesNotExistError):
166-
committee_mention = (await self.bot.committee_role).mention
167-
152+
).aexists():
168153
await ctx.followup.send(
169154
content=(
170155
":information_source: No changes made. This student ID has already "
171-
f"been used. Please contact a {committee_mention} member if this is "
172-
"an error. :information_source:"
156+
f"been used. Please contact a {
157+
await self.bot.get_mention_string(self.bot.committee_role)
158+
} member if this is an error. :information_source:"
173159
),
174160
ephemeral=True,
175161
)
@@ -278,11 +264,11 @@ async def make_member(self, ctx: "TeXBotApplicationContext", group_member_id: st
278264
guest_role,
279265
reason='TeX Bot slash-command: "/makemember"',
280266
)
281-
282-
# noinspection PyUnusedLocal
283-
applicant_role: discord.Role | None = None
284-
with contextlib.suppress(ApplicantRoleDoesNotExistError):
267+
applicant_role: discord.Role | None
268+
try:
285269
applicant_role = await ctx.bot.applicant_role
270+
except ApplicantRoleDoesNotExistError:
271+
applicant_role = None
286272

287273
if applicant_role and applicant_role in interaction_member.roles:
288274
await interaction_member.remove_roles(

cogs/remind_me.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ async def remind_me( # type: ignore[misc]
215215
216216
The "remind_me" command responds with the given message after the specified time.
217217
"""
218-
# noinspection PyTypeChecker
219218
parsed_time: tuple[time.struct_time, int] = parsedatetime.Calendar().parseDT(
220219
delay,
221220
tzinfo=timezone.get_current_timezone(),
@@ -289,7 +288,7 @@ def __init__(self, bot: "TeXBot") -> None:
289288
@override
290289
def cog_unload(self) -> None:
291290
"""
292-
Unload hook that ends all running tasks whenever the tasks cog is unloaded.
291+
Unload-hook that ends all running tasks whenever the tasks cog is unloaded.
293292
294293
This may be run dynamically or when the bot closes.
295294
"""
@@ -333,7 +332,6 @@ async def clear_reminders_backlog(self) -> None:
333332
await reminder.adelete()
334333
continue
335334

336-
# noinspection PyUnresolvedReferences
337335
channel: discord.PartialMessageable = self.bot.get_partial_messageable(
338336
reminder.channel_id,
339337
type=reminder.channel_type,

0 commit comments

Comments
 (0)