Skip to content

bot.before_invoke function runs twice with slash sub commands causing a whole bunch of issues #2134

Closed
@Revnoplex

Description

@Revnoplex

Summary

the bot.before_invoke function gets called twice during the invocation of a slash sub command

Reproduction Steps

This seems to only occur on master and not stable 2.4.1

  1. Have a pre-invocation function with the @bot.before_invoke decorator
  2. Have any slash command group
  3. add a print statement to the pre-invocation function or add context.defer()
  4. print statement will print twice or InteractionResponded will be raised

Minimal Reproducible Code

# in main file
#...

client: bridge.Bot = bridge.Bot(command_prefix=config.prefix, intents=intents, description=bot_desc, help_command=information.RevnobotHelp3(), debug_guilds=config.slash_guilds,max_messages=10**3, enable_debug_events=config.debug_mode)

# ...

@client.before_invoke
async def pre_invocation(ctx):
    print("i executed")

# in cog 

test_group = discord.SlashCommandGroup('test-group', "Hello Worlds")

@test_group.command(name="test1", description="Hello World 1")
@commands.cooldown(**config.default_cooldown_options)
@commands.bot_has_permissions(send_messages=True, attach_files=True)
async def test_1(self, ctx: discord.ApplicationContext):
    await ctx.respond("Hello World 1")

Expected Results

i executed should only print once meaning

Actual Results

i executed prints twice
or the following traceback if you use ctx.defer()

  File "Revnobot/venv/lib/python3.11/site-packages/discord/commands/core.py", line 131, in wrapped
    ret = await coro(arg)
          ^^^^^^^^^^^^^^^

  File "Revnobot/venv/lib/python3.11/site-packages/discord/commands/core.py", line 1363, in _invoke
    await command.invoke(ctx)

  File "Revnobot/venv/lib/python3.11/site-packages/discord/commands/core.py", line 378, in invoke
    await self.prepare(ctx)

  File "Revnobot/venv/lib/python3.11/site-packages/discord/commands/core.py", line 310, in prepare
    await self.call_before_hooks(ctx)

  File "Revnobot/venv/lib/python3.11/site-packages/discord/commands/core.py", line 535, in call_before_hooks
    await hook(ctx)

  File "Revnobot/main.py", line 116, in pre_invocation
    await utils.pre_invocation(ctx)

  File "Revnobot/utils.py", line 147, in pre_invocation
    await ctx.defer()

  File "Revnobot/venv/lib/python3.11/site-packages/discord/ext/bridge/context.py", line 113, in defer
    return await self._defer(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "Revnobot/venv/lib/python3.11/site-packages/discord/ext/bridge/context.py", line 149, in _defer
    return await self._get_super("defer")(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "Revnobot/venv/lib/python3.11/site-packages/discord/interactions.py", line 705, in defer
    raise InteractionResponded(self._parent)

Intents

all

System Information

  • Python v3.11.3-final
  • py-cord v2.4.1-final
  • aiohttp v3.8.4
  • system info: Linux 6.3.6-arch1-1 Update README.rst #1 SMP PREEMPT_DYNAMIC Mon, 05 Jun 2023 15:12:57 +0000

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions