Просмотр исходного кода

Error handler replies to errant command attempts

master
Rocketsoup 4 лет назад
Родитель
Сommit
c980d2617c
1 измененных файлов: 18 добавлений и 0 удалений
  1. 18
    0
      rocketbot.py

+ 18
- 0
rocketbot.py Просмотреть файл

5
 Author: Ian Albert (@rocketsoup)
5
 Author: Ian Albert (@rocketsoup)
6
 Date: 2021-11-11
6
 Date: 2021-11-11
7
 """
7
 """
8
+import traceback
8
 from discord import Intents
9
 from discord import Intents
9
 from discord.ext import commands
10
 from discord.ext import commands
10
 
11
 
33
 	def __init__(self, command_prefix, **kwargs):
34
 	def __init__(self, command_prefix, **kwargs):
34
 		super().__init__(command_prefix, **kwargs)
35
 		super().__init__(command_prefix, **kwargs)
35
 
36
 
37
+	async def on_command_error(self, context: commands.Context, exception):
38
+		if context.guild is None or \
39
+				context.message.channel is None or \
40
+				context.message.author.bot:
41
+			return
42
+		if not context.message.author.permissions_in(context.message.channel).ban_members:
43
+			# Don't tell non-mods about errors
44
+			return
45
+		if isinstance(exception, (commands.errors.CommandError, )):
46
+			# Reply with the error message for ordinary command errors
47
+			await context.message.reply(
48
+				f'{CONFIG["failure_emoji"]} {exception}',
49
+				mention_author=False)
50
+			return
51
+		# Stack trace everything else
52
+		traceback.print_exception(type(exception), exception, exception.__traceback__)
53
+
36
 intents = Intents.default()
54
 intents = Intents.default()
37
 intents.messages = True  # pylint: disable=assigning-non-slot
55
 intents.messages = True  # pylint: disable=assigning-non-slot
38
 intents.members = True  # pylint: disable=assigning-non-slot
56
 intents.members = True  # pylint: disable=assigning-non-slot

Загрузка…
Отмена
Сохранить