Skip to content
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
16 changes: 12 additions & 4 deletions cogs/command_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,28 @@ async def on_application_command_error(
if isinstance(error, discord.ApplicationCommandInvokeError) and isinstance(
error.original, GuildDoesNotExistError
):
command_name: str = (
command_name: str | None = (
ctx.command.callback.__name__
if (
hasattr(ctx.command, "callback")
ctx.command
and hasattr(ctx.command, "callback")
and not ctx.command.callback.__name__.startswith("_")
)
else ctx.command.qualified_name
else (ctx.command.qualified_name if ctx.command else None)
)
logger.critical(
" ".join(
message_part
for message_part in (
error.original.ERROR_CODE,
f"({command_name})" if command_name in self.ERROR_ACTIVITIES else "",
(
f"({command_name})"
if (
command_name is not None
and command_name in self.ERROR_ACTIVITIES
)
else ""
),
str(error.original).rstrip(".:"),
)
if message_part
Expand Down
18 changes: 12 additions & 6 deletions cogs/induct.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from exceptions import (
ApplicantRoleDoesNotExistError,
CommitteeRoleDoesNotExistError,
DiscordMemberNotInMainGuildError,
GuestRoleDoesNotExistError,
GuildDoesNotExistError,
MemberRoleDoesNotExistError,
Expand Down Expand Up @@ -188,8 +189,8 @@ async def _perform_induction(
main_guild: discord.Guild = self.bot.main_guild
guest_role: discord.Role = await self.bot.guest_role

induction_user: discord.User | None = await self.bot.get_or_fetch_user(
induction_member_id
induction_user: discord.User | None = await self.bot.get_or_fetch(
discord.User, induction_member_id
)
if not induction_user:
await ctx.respond(
Expand All @@ -201,12 +202,15 @@ async def _perform_induction(
)
return

induction_member: discord.Member | None = main_guild.get_member(induction_member_id)
if not induction_member:
try:
induction_member: discord.Member = await self.bot.get_main_guild_member(
induction_user
)
except DiscordMemberNotInMainGuildError:
await ctx.respond(
(
":information_source: No changes made. User cannot be inducted "
"because they have left the server or are not cached "
"because they are not in the server "
":information_source:"
),
ephemeral=True,
Expand Down Expand Up @@ -265,7 +269,9 @@ async def _perform_induction(
applicant_role, reason=INDUCT_AUDIT_MESSAGE
)

tex_emoji: discord.Emoji | None = self.bot.get_emoji(743218410409820213)
tex_emoji: discord.GuildEmoji | discord.AppEmoji | None = self.bot.get_emoji(
743218410409820213
)
if not tex_emoji:
tex_emoji = discord.utils.get(main_guild.emojis, name="TeX")

Expand Down
8 changes: 4 additions & 4 deletions cogs/kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ class ConfirmKillView(View):
@discord.ui.button(
label="SHUTDOWN", style=discord.ButtonStyle.red, custom_id="shutdown_confirm"
)
async def confirm_shutdown_button_callback( # type: ignore[misc]
self, _: discord.Button, interaction: discord.Interaction
async def confirm_shutdown_button_callback(
self, _: discord.ui.Button["ConfirmKillView"], interaction: discord.Interaction
) -> None:
"""When the shutdown button is pressed, delete the message."""
logger.debug('"Confirm" button pressed. %s', interaction)

@discord.ui.button(
label="CANCEL", style=discord.ButtonStyle.grey, custom_id="shutdown_cancel"
)
async def cancel_shutdown_button_callback( # type: ignore[misc]
self, _: discord.Button, interaction: discord.Interaction
async def cancel_shutdown_button_callback(
self, _: discord.ui.Button["ConfirmKillView"], interaction: discord.Interaction
) -> None:
"""When the cancel button is pressed, delete the message."""
logger.debug('"Cancel" button pressed. %s', interaction)
Expand Down
4 changes: 3 additions & 1 deletion cogs/make_applicant.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ async def _perform_make_applicant(
await applicant_member.add_roles(applicant_role, reason=AUDIT_MESSAGE)
logger.debug("Applicant role given to user %s", applicant_member)

tex_emoji: discord.Emoji | None = self.bot.get_emoji(743218410409820213)
tex_emoji: discord.GuildEmoji | discord.AppEmoji | None = self.bot.get_emoji(
743218410409820213
)
if not tex_emoji:
tex_emoji = discord.utils.get(main_guild.emojis, name="TeX")

Expand Down
8 changes: 6 additions & 2 deletions cogs/send_introduction_reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,12 @@ async def send_error(
style=discord.ButtonStyle.red,
emoji=discord.PartialEmoji.from_str(emoji.emojize(":no_good:", language="alias")),
)
async def opt_out_introduction_reminders_button_callback( # type: ignore[misc]
self, button: discord.Button, interaction: discord.Interaction
async def opt_out_introduction_reminders_button_callback(
self,
button: discord.ui.Button[
"SendIntroductionRemindersTaskCog.OptOutIntroductionRemindersView"
],
interaction: discord.Interaction,
) -> None:
"""
Set the opt-in/out flag depending on the status of the button.
Expand Down
34 changes: 33 additions & 1 deletion cogs/stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ async def channel_stats(
# NOTE: Shortcut accessors are placed at the top of the function so that the exceptions they raise are displayed before any further errors may be sent
main_guild: discord.Guild = self.bot.main_guild

channel_id: int = ctx.channel_id
if not ctx.channel or isinstance(
ctx.channel, (discord.CategoryChannel, discord.ForumChannel)
):
await self.command_send_error(
ctx, message="This command cannot be used in category/forum channels."
)
return
Comment thread
MattyTheHacker marked this conversation as resolved.
Resolved

channel_id: int | None = ctx.channel_id

if str_channel_id:
if not re.fullmatch(r"\A\d{17,20}\Z", str_channel_id):
Expand Down Expand Up @@ -157,6 +165,14 @@ async def server_stats(self, ctx: "TeXBotApplicationContext") -> None:
main_guild: discord.Guild = self.bot.main_guild
guest_role: discord.Role = await self.bot.guest_role

if not ctx.channel or isinstance(
ctx.channel, (discord.CategoryChannel, discord.ForumChannel)
):
await self.command_send_error(
ctx, message="This command cannot be used in category/forum channels."
)
return
Comment thread
MattyTheHacker marked this conversation as resolved.
Resolved

await ctx.defer(ephemeral=True)

message_counts: Mapping[str, Mapping[str, int]] = await get_server_message_counts(
Expand Down Expand Up @@ -237,6 +253,14 @@ async def user_stats(self, ctx: "TeXBotApplicationContext") -> None:
interaction_member: discord.Member = await self.bot.get_main_guild_member(ctx.user)
guest_role: discord.Role = await self.bot.guest_role

if not ctx.channel or isinstance(
ctx.channel, (discord.CategoryChannel, discord.ForumChannel)
):
await self.command_send_error(
ctx, message="This command cannot be used in category/forum channels."
)
return
Comment thread
MattyTheHacker marked this conversation as resolved.
Resolved

if guest_role not in interaction_member.roles:
await self.command_send_error(
ctx,
Expand Down Expand Up @@ -314,6 +338,14 @@ async def left_member_stats(self, ctx: "TeXBotApplicationContext") -> None:
# NOTE: Shortcut accessors are placed at the top of the function so that the exceptions they raise are displayed before any further errors may be sent
main_guild: discord.Guild = self.bot.main_guild

if not ctx.channel or isinstance(
ctx.channel, (discord.CategoryChannel, discord.ForumChannel)
):
await self.command_send_error(
ctx, message="This command cannot be used in category/forum channels."
)
return
Comment thread
MattyTheHacker marked this conversation as resolved.
Resolved

await ctx.defer(ephemeral=True)

left_member_counts: dict[str, int] = {
Expand Down
75 changes: 50 additions & 25 deletions cogs/strike.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class ConfirmStrikeMemberView(View):
@discord.ui.button(
label="Yes", style=discord.ButtonStyle.red, custom_id="yes_strike_member"
)
async def yes_strike_member_button_callback( # type: ignore[misc]
self, _: discord.Button, interaction: discord.Interaction
async def yes_strike_member_button_callback(
self, _: discord.ui.Button["ConfirmStrikeMemberView"], interaction: discord.Interaction
) -> None:
"""
Delete the message associated with the view when the Yes button is pressed.
Expand All @@ -113,8 +113,8 @@ async def yes_strike_member_button_callback( # type: ignore[misc]
@discord.ui.button(
label="No", style=discord.ButtonStyle.grey, custom_id="no_strike_member"
)
async def no_strike_member_button_callback( # type: ignore[misc]
self, _: discord.Button, interaction: discord.Interaction
async def no_strike_member_button_callback(
self, _: discord.ui.Button["ConfirmStrikeMemberView"], interaction: discord.Interaction
) -> None:
"""
Delete the message associated with the view when the No button is pressed.
Expand All @@ -136,8 +136,10 @@ class ConfirmManualModerationView(View):
@discord.ui.button(
label="Yes", style=discord.ButtonStyle.red, custom_id="yes_manual_moderation_action"
)
async def yes_manual_moderation_action_button_callback( # type: ignore[misc]
self, _: discord.Button, interaction: discord.Interaction
async def yes_manual_moderation_action_button_callback(
self,
_: discord.ui.Button["ConfirmManualModerationView"],
interaction: discord.Interaction,
) -> None:
"""
Delete the message associated with the view when the Yes button is pressed.
Expand All @@ -156,8 +158,10 @@ async def yes_manual_moderation_action_button_callback( # type: ignore[misc]
@discord.ui.button(
label="No", style=discord.ButtonStyle.grey, custom_id="no_manual_moderation_action"
)
async def no_manual_moderation_action_button_callback( # type: ignore[misc]
self, _: discord.Button, interaction: discord.Interaction
async def no_manual_moderation_action_button_callback(
self,
_: discord.ui.Button["ConfirmManualModerationView"],
interaction: discord.Interaction,
) -> None:
"""
Delete the message associated with the view when the No button is pressed.
Expand All @@ -180,8 +184,10 @@ class ConfirmStrikesOutOfSyncWithBanView(View):
@discord.ui.button(
label="Yes", style=discord.ButtonStyle.red, custom_id="yes_out_of_sync_ban_member"
)
async def yes_out_of_sync_ban_member_button_callback( # type: ignore[misc]
self, _: discord.Button, interaction: discord.Interaction
async def yes_out_of_sync_ban_member_button_callback(
self,
_: discord.ui.Button["ConfirmStrikesOutOfSyncWithBanView"],
interaction: discord.Interaction,
) -> None:
"""
Delete the message associated with the view when the Yes button is pressed.
Expand All @@ -200,8 +206,10 @@ async def yes_out_of_sync_ban_member_button_callback( # type: ignore[misc]
@discord.ui.button(
label="No", style=discord.ButtonStyle.grey, custom_id="no_out_of_sync_ban_member"
)
async def no_out_of_sync_ban_member_button_callback( # type: ignore[misc]
self, _: discord.Button, interaction: discord.Interaction
async def no_out_of_sync_ban_member_button_callback(
self,
_: discord.ui.Button["ConfirmStrikesOutOfSyncWithBanView"],
interaction: discord.Interaction,
) -> None:
"""
Delete the message associated with the view when the No button is pressed.
Expand Down Expand Up @@ -229,7 +237,7 @@ class BaseStrikeCog(TeXBotBaseCog):
SUGGESTED_ACTIONS: "Final[Mapping[int, str]]" = {1: "time-out", 2: "kick", 3: "ban"} # noqa: RUF012

async def _send_strike_user_message(
self, strike_user: discord.User | discord.Member, member_strikes: DiscordMemberStrikes
self, strike_user: discord.abc.Messageable, member_strikes: DiscordMemberStrikes
) -> None:
try:
await strike_user.send(
Expand Down Expand Up @@ -264,7 +272,7 @@ async def _send_strike_user_message(
async def _confirm_perform_moderation_action(
self,
message_sender_component: "MessageSavingSenderComponent",
interaction_user: discord.User,
interaction_user: discord.User | discord.Member,
strike_user: discord.Member,
confirm_strike_message: str,
actual_strike_amount: int,
Expand Down Expand Up @@ -315,14 +323,14 @@ async def _confirm_perform_moderation_action(
async def _confirm_increase_strike(
self,
message_sender_component: "MessageSavingSenderComponent",
interaction_user: discord.User,
interaction_user: discord.User | discord.Member,
strike_user: discord.User | discord.Member,
member_strikes: DiscordMemberStrikes,
button_callback_channel: discord.TextChannel | discord.DMChannel,
*,
perform_action: bool,
) -> None:
if perform_action and isinstance(strike_user, discord.User):
if perform_action and not isinstance(strike_user, discord.Member):
STRIKE_USER_TYPE_ERROR_MESSAGE: Final[str] = (
"Cannot perform moderation action on non-guild member."
)
Expand Down Expand Up @@ -401,6 +409,12 @@ async def _command_perform_strike(
Also calls the process of performing the appropriate moderation action,
given the new number of strikes that the member has.
"""
if not isinstance(ctx.channel, (discord.TextChannel, discord.DMChannel)):
await self.command_send_error(
ctx, message="This command can only be used in text channels or DMs."
)
return

if strike_member.bot:
await self.command_send_error(
ctx,
Expand Down Expand Up @@ -453,7 +467,9 @@ async def get_confirmation_message_channel(
) from fetch_log_channel_error

raw_user: discord.User | None = (
self.bot.get_user(user.id) if isinstance(user, discord.Member) else user
user
if isinstance(user, discord.User)
else await self.bot.get_or_fetch(discord.User, user.id)
)
if not raw_user:
raise StrikeTrackingError
Expand Down Expand Up @@ -504,8 +520,12 @@ async def _confirm_manual_add_strike( # noqa: PLR0915
async for _audit_log_entry in main_guild.audit_logs(
after=discord.utils.utcnow() - datetime.timedelta(minutes=1), action=action
)
if _audit_log_entry.target.id
== strike_user.id # NOTE: IDs are checked here rather than the objects themselves as the audit log provides an unusual object type in some cases.
if (
_audit_log_entry.target
and (
_audit_log_entry.target.id == strike_user.id
) # NOTE: IDs are checked here rather than the objects themselves as the audit log provides an unusual object type in some cases.
)
)
except (StopIteration, StopAsyncIteration):
logger.debug("Printing 5 most recent audit logs:")
Expand Down Expand Up @@ -720,7 +740,9 @@ async def _confirm_manual_add_strike( # noqa: PLR0915
return

if button_interaction.data["custom_id"] == "yes_manual_moderation_action": # type: ignore[index, typeddict-item]
interaction_user: discord.User | None = self.bot.get_user(applied_action_user.id)
interaction_user: discord.User | None = await self.bot.get_or_fetch(
discord.User, applied_action_user.id
)
if not interaction_user:
raise StrikeTrackingError

Expand Down Expand Up @@ -750,11 +772,14 @@ async def on_member_update(self, before: discord.Member, after: discord.Member)

audit_log_entry: discord.AuditLogEntry
async for audit_log_entry in main_guild.audit_logs(limit=5):
FOUND_CORRECT_AUDIT_LOG_ENTRY: bool = audit_log_entry.target.id == after.id and (
audit_log_entry.action
== discord.AuditLogAction.auto_moderation_user_communication_disabled
)
if FOUND_CORRECT_AUDIT_LOG_ENTRY:
if (
audit_log_entry.target
and (audit_log_entry.target.id == after.id)
and (
audit_log_entry.action
== discord.AuditLogAction.auto_moderation_user_communication_disabled
)
):
await self._confirm_manual_add_strike(
strike_user=after, action=audit_log_entry.action
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ main = [
"matplotlib>=3.10,<3.11",
"mplcyberpunk>=0.7",
"parsedatetime>=2.6",
"py-cord>=2.6,<2.7",
"py-cord>=2.8",
Comment thread
MattyTheHacker marked this conversation as resolved.
Resolved
"python-dotenv>=1.0",
"python-logging-discord-handler>=0.1",
"typed_classproperties>=1.2",
Expand Down
1 change: 0 additions & 1 deletion stubs/discord/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ from .team import *
from .template import *
from .threads import *
from .user import *
from .voice_client import *
from .webhook import *
from .welcome_screen import *
from .widget import *
Expand Down
2 changes: 1 addition & 1 deletion utils/message_sender_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def delete(self) -> None:
await self.sent_message.delete()

else:
await self.sent_message.delete_original_message()
await self.sent_message.delete_original_response()


class ChannelMessageSender(MessageSavingSenderComponent):
Expand Down
Loading