Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit a207fc6

Browse files
committed
Add before and after invoke events
Also clean up demo bot logging and bump version
1 parent 2b2975b commit a207fc6

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

demo_bot.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,16 @@ async def on_slash_permissions():
156156
stop.add_perm(client.app_info.owner, True, None)
157157
await client.register_permissions()
158158

159+
@client.event
160+
async def on_before_slash_command_invoke(ctx: slash.Context):
161+
logger.info('User %s running /%s', ctx.author, ctx.command)
162+
159163
# show extension logs
160164
logger = logging.getLogger('discord.ext.slash')
161165
logger.setLevel(logging.DEBUG)
162166
logger.addHandler(logging.StreamHandler())
167+
logger.handlers[0].setFormatter(logging.Formatter(
168+
'{levelname}\t{asctime} {message}', style='{'))
163169

164170
token = os.environ['DISCORD_TOKEN'].strip()
165171

discord/ext/slash/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async def repeat( # command name
8585
'SlashBot'
8686
]
8787

88-
__version__ = '0.9.0'
88+
__version__ = '0.9.1'
8989

9090
class SlashWarning(UserWarning):
9191
""":mod:`discord.ext.slash`-specific warning type."""
@@ -1359,6 +1359,7 @@ async def on_interaction_create(self, event: dict):
13591359
raise commands.CommandNotFound(
13601360
f'No command {event["data"]["name"]!r} found by any critera')
13611361
ctx = await cmd.coro.__annotations__[cmd._ctx_arg](self, cmd, event)
1362+
self.dispatch('before_slash_command_invoke', ctx)
13621363
try:
13631364
await ctx.command.invoke(ctx)
13641365
except commands.CommandError as exc:
@@ -1370,6 +1371,8 @@ async def on_interaction_create(self, event: dict):
13701371
raise commands.CommandInvokeError(exc) from exc
13711372
except commands.CommandInvokeError as exc2:
13721373
self.dispatch('command_error', ctx, exc2)
1374+
else:
1375+
self.dispatch('after_slash_command_invoke', ctx)
13731376

13741377
async def on_slash_permissions(self):
13751378
await self.register_permissions()

docs/discord-ext-slash.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,13 @@ Events
8484
opportunity to register dynamic permissions in code before pushing to the
8585
API. If overriding using @:meth:`discord.Client.event`, you must await
8686
:meth:`-SlashBot.register_permissions` at the end of the event handler.
87-
See ``/stop`` in ``demo_bot.py`` for an example.
87+
See ``/stop`` in ``demo_bot.py`` for an example.
88+
89+
.. function:: on_before_slash_command_invoke(ctx: Context)
90+
91+
Triggered immediately before a slash command is invoked, for logging etc.
92+
93+
.. function:: on_after_slash_command_invoke(ctx: Context)
94+
95+
Triggered immediately after a *successful* slash command invocation.
96+
Failed invocations will trigger :func:`discord.on_command_error` instead.

0 commit comments

Comments
 (0)