-
Notifications
You must be signed in to change notification settings - Fork 6
/
command_error.py
90 lines (73 loc) · 3.37 KB
/
command_error.py
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
"""Contains cog classes for any command_error interactions."""
from collections.abc import Sequence
__all__: Sequence[str] = ("CommandErrorCog",)
import contextlib
import logging
from logging import Logger
from typing import Final
import discord
from discord import Forbidden
from discord.ext.commands.errors import CheckAnyFailure
from exceptions import (
CommitteeRoleDoesNotExistError,
GuildDoesNotExistError,
)
from exceptions.base import BaseErrorWithErrorCode
from utils import CommandChecks, TeXBotApplicationContext, TeXBotBaseCog
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: # noqa: E501
"""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):
if CommandChecks.is_interaction_user_in_main_guild_failure(error.checks[0]):
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]):
# noinspection PyUnusedLocal
committee_role_mention: str = "@Committee"
with contextlib.suppress(CommitteeRoleDoesNotExistError):
committee_role_mention = (await self.bot.committee_role).mention
message = f"Only {committee_role_mention} members can run this command."
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): # noqa: E501
command_name: str = (
ctx.command.callback.__name__
if (
hasattr(ctx.command, "callback")
and not ctx.command.callback.__name__.startswith("_")
)
else ctx.command.qualified_name
)
logger.critical(
" ".join(
message_part
for message_part in (
error.original.ERROR_CODE,
f"({command_name})" if command_name in self.ERROR_ACTIVITIES else "",
str(error.original).rstrip(".:"),
)
if message_part
),
)
await self.bot.close()