-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path_error_tools.py
More file actions
107 lines (91 loc) · 4.81 KB
/
_error_tools.py
File metadata and controls
107 lines (91 loc) · 4.81 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
98
99
100
101
102
103
104
105
106
107
import discord
import traceback
from typing import Optional, TYPE_CHECKING
from discord.ext import commands
from lib.structs import GitBotEmbed
from lib.structs.discord.context import GitBotContext
from gidgethub import BadRequest, QueryError
if TYPE_CHECKING:
from lib.structs import GitBot
def silenced(ctx: GitBotContext, error) -> bool:
return bool(getattr(ctx, f'__silence_{ctx.bot.mgr.to_snake_case(error.__class__.__name__)}_error__', False))
async def respond_to_command_doesnt_exist(ctx: GitBotContext, error: commands.CommandNotFound) -> discord.Message | None:
if len(ctx.message.content) == (len(ctx.prefix) + 3) and 'gud' in ctx.message.content:
await ctx.message.add_reaction('🤣')
return
ctx.fmt.set_prefix('errors command_not_found')
embed: GitBotEmbed = GitBotEmbed(
color=0x0384fc,
title=ctx.l.errors.command_not_found.title,
description=ctx.fmt('description',
f'```haskell\n{str(ctx.prefix).strip()} '
f'{(closest_existing_command_from_error(ctx.bot, error))}```'),
footer=ctx.l.errors.command_not_found.footer
)
return await ctx.send(embed=embed)
async def log_error_in_discord(ctx: GitBotContext, error: Exception, _actual=None) -> Optional[discord.Message]:
guild_id: str = str(ctx.guild.id) if not isinstance(ctx.channel, discord.DMChannel) else 'DM'
ping_owner: bool = False
add_location: bool = True
if ctx.command:
embed: discord.Embed = discord.Embed(
color=0xda4353,
title=f'Error in `{ctx.command}` command'
)
message: str = ctx.bot.mgr.sanitize_codeblock_content(str(error))
tb: str = ctx.bot.mgr.sanitize_codeblock_content(format_tb(error.__traceback__))
sanitized_args: str = ctx.bot.mgr.sanitize_codeblock_content(format_args(ctx.args))
sanitized_kwargs: str = ctx.bot.mgr.sanitize_codeblock_content(format_kwargs(ctx.kwargs))
embed.add_field(name='Message', value=f'```{message}```')
embed.add_field(name='Traceback', value=f'```{tb}```')
embed.add_field(name='Arguments',
value=f'```properties\nargs={sanitized_args}\nkwargs={sanitized_kwargs}```')
elif isinstance(error, commands.CommandNotFound):
error_text: str = ctx.bot.mgr.sanitize_codeblock_content(str(error))
embed: GitBotEmbed = GitBotEmbed(
color=0x0384fc,
title='Nonexistent command!',
description=f'```{error_text}```',
footer='Closest existing command: ' + closest_existing_command_from_error(ctx.bot, error)
)
elif isinstance(error, (BadRequest, QueryError)):
embed: GitBotEmbed = GitBotEmbed(
color=0xda4353,
title='GitHub API Error!',
footer='The logs may contain more information.'
)
api_response: str = ctx.bot.mgr.sanitize_codeblock_content(str(error))
code_location: str = ctx.bot.mgr.sanitize_codeblock_content(ctx.gh_query_debug.code_location)
embed.add_field(name='API Response', value=f'```diff\n- {api_response}```')
embed.add_field(name='Code Location', value=f'```{code_location}```')
if ctx.gh_query_debug.additional_info:
additional_info: str = ctx.bot.mgr.sanitize_codeblock_content(ctx.gh_query_debug.additional_info)
embed.add_field(name='Additional Info', value=f'```{additional_info}```')
if ctx.gh_query_debug.status_code is not None:
embed.add_field(name='Status Code', value=f'```c\n{error.status_code}```')
ping_owner: bool = True
add_location: bool = False
else:
return
if add_location:
embed.add_field(name='Location', value=f'**Guild ID:** `{guild_id}`\n**Author ID:** `{ctx.author.id}`')
return await ctx.bot.error_log_channel.send(f'<@{ctx.bot.mgr.env.owner_id}>' if ping_owner else None, embed=embed)
def closest_existing_command_from_error(bot: 'GitBot', error: commands.CommandNotFound | str) -> str:
return str(bot.mgr.get_closest_match_from_iterable(
(error := error.args[0])[error.index('"') + 1:error.rindex('"')],
filter(lambda cmd: cmd not in bot.mgr.env.hidden_commands, map(str, bot.walk_commands()))))
def format_tb(tb) -> str:
return '\n\n'.join([i.strip() for i in traceback.format_tb(tb, -5)])
def format_args(args: list) -> str:
if args:
for i, arg in enumerate(args):
if repr(arg).startswith('<cogs'):
args[i]: str = repr(arg).split()[0].strip('<')
elif 'Context' in repr(arg):
args[i]: str = 'ctx'
return f'[{", ".join(args)}]'
return 'No arguments'
def format_kwargs(kwargs: dict) -> str:
if kwargs:
return 'dict(' + ', '.join([f"{k}=\'{v}\'" for k, v in kwargs.items()]) + ')'
return 'No keyword arguments'