Selaa lähdekoodia

Error handler replies to errant command attempts

master
Rocketsoup 4 vuotta sitten
vanhempi
commit
c980d2617c
1 muutettua tiedostoa jossa 18 lisäystä ja 0 poistoa
  1. 18
    0
      rocketbot.py

+ 18
- 0
rocketbot.py Näytä tiedosto

@@ -5,6 +5,7 @@ for a template).
5 5
 Author: Ian Albert (@rocketsoup)
6 6
 Date: 2021-11-11
7 7
 """
8
+import traceback
8 9
 from discord import Intents
9 10
 from discord.ext import commands
10 11
 
@@ -33,6 +34,23 @@ class Rocketbot(commands.Bot):
33 34
 	def __init__(self, command_prefix, **kwargs):
34 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 54
 intents = Intents.default()
37 55
 intents.messages = True  # pylint: disable=assigning-non-slot
38 56
 intents.members = True  # pylint: disable=assigning-non-slot

Loading…
Peruuta
Tallenna