Repository navigation
Expand file tree
/
Copy pathcommand_error.py
More file actions
97 lines (81 loc) · 3.66 KB
/
Copy pathcommand_error.py
File metadata and controls
97 lines (81 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Contains cog classes for any command_error interactions."""
__all__: Sequence[str] = ("CommandErrorCog",)
import logging
lazy from collections.abc import Sequence
lazy from logging import Logger
lazy from typing import Final
lazy import discord
lazy from discord import Forbidden
lazy from discord.ext.commands.errors import CheckAnyFailure
from utils import TeXBotBaseCog
lazy from exceptions import GuildDoesNotExistError
lazy from exceptions.base import BaseErrorWithErrorCode
lazy from utils import CommandChecks, TeXBotApplicationContext
logger: Final[Logger] = logging.getLogger("TeX-Bot")
class CommandErrorCog(TeXBotBaseCog):
"""Cog class that defines additional code to execute upon a command error."""
@TeXBotBaseCog.listener()
async def on_application_command_error(
self, ctx: TeXBotApplicationContext, error: discord.ApplicationCommandError
) -> None:
"""Log any major command errors in the logging channel & stderr."""
error_code: str | None = None
message: str | None = "Please contact a committee member."
logging_message: str | BaseException | None = None
if isinstance(error, discord.ApplicationCommandInvokeError):
message = None
logging_message = (
None if isinstance(error.original, GuildDoesNotExistError) else error.original
)
if isinstance(error.original, Forbidden):
error_code = "E1044"
elif isinstance(error.original, BaseErrorWithErrorCode):
error_code = error.original.ERROR_CODE
elif isinstance(error, CheckAnyFailure):
# TODO: Remove type ignore comments once #349 is resolved # noqa: FIX002
if CommandChecks.is_interaction_user_in_main_guild_failure(error.checks[0]): # type: ignore[arg-type]
message = (
f"You must be a member of the {self.bot.group_short_name} Discord server "
"to use this command."
)
elif CommandChecks.is_interaction_user_has_committee_role_failure(error.checks[0]): # type: ignore[arg-type]
message = (
f"Only {await self.bot.get_mention_string(self.bot.committee_role)} "
"members can run this command."
)
else:
logging_message = error
await self.command_send_error(
ctx, error_code=error_code, message=message, logging_message=logging_message
)
if isinstance(error, discord.ApplicationCommandInvokeError) and isinstance(
error.original, GuildDoesNotExistError
):
command_name: str | None = (
ctx.command.callback.__name__
if (
ctx.command
and hasattr(ctx.command, "callback")
and not ctx.command.callback.__name__.startswith("_")
)
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 is not None
and command_name in self.ERROR_ACTIVITIES
)
else ""
),
str(error.original).rstrip(".:"),
)
if message_part
)
)
await self.bot.close()