Skip to content

Command preparation and sync fails when bot is started on an unmanaged event loop #1138

Open
@Starz0r

Description

@Starz0r

Summary

When passing a event loop to the bot constructor, any command preparation will fail.

Reproduction Steps

  1. Construct a bot.
  2. Pass in a newly created event loop
  3. Annotate a function for a slash command
  4. Run the bot

Minimal Reproducible Code

from typing import Final
import asyncio
import sys
import disnake

EVLOOP: Final[asyncio.AbstractEventLoop] = asyncio.new_event_loop()
DISCORD_CLIENT: commands.InteractionBot(loop=EVLOOP)

@DISCORD_CLIENT.slash_command()
async def ping(ctx: disnake.ApplicationCommandInteraction) -> None:
    await ctx.send("pong!")

async def main():
    await DISCORD_CLIENT.start("PUT A TOKEN HERE", reconnect=True)

if __name__ == "__main__":
    sys.exit(EVLOOP.run_until_complete(main()))

Expected Results

Command preparation and sync to complete successfully.

Actual Results

Command preparations and sync does not run or finish as the tasks are bound to the wrong event loop.

Intents

None

System Information

- Python v3.9.1-final
- disnake v2.9.1-final
    - disnake importlib.metadata: v2.9.1
- aiohttp v3.9.1
- system info: Windows 10 10.0.19041 AMD64

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

My personal traceback in question:

INFO:disnake.client:logging in using static token
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='disnake: app_command_preparation' coro=<InteractionBotBase._prepare_application_commands() done, defined at C:\my_virtualenv\lib\site-packages\disnake\ext\commands\interaction_bot_base.py:879> exception=RuntimeError("Task <Task pending name='disnake: app_command_preparation' coro=<InteractionBotBase._prepare_application_commands() running at C:\\my_virtualenv\\lib\\site-packages\\disnake\\ext\\commands\\interaction_bot_base.py:884>> got Future <Future pending> attached to a different loop")>
Traceback (most recent call last):
  File "C:\my_virtualenv\lib\site-packages\disnake\ext\commands\interaction_bot_base.py", line 884, in _prepare_application_commands
    await self.wait_until_first_connect()
  File "C:\my_virtualenv\lib\site-packages\disnake\client.py", line 1517, in wait_until_first_connect
    await self._first_connect.wait()
  File "C:\python3.9.1\lib\asyncio\locks.py", line 226, in wait
    await fut
RuntimeError: Task <Task pending name='disnake: app_command_preparation' coro=<InteractionBotBase._prepare_application_commands() running at C:\my_virtualenv\lib\site-packages\disnake\ext\commands\interaction_bot_base.py:884>> got Future <Future pending> attached to a different loop
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-10' coro=<CommonBotBase._fill_owners() done, defined at C:\my_virtualenv\lib\site-packages\disnake\ext\commands\common_bot_base.py:92> exception=RuntimeError("Task <Task pending name='Task-10' coro=<CommonBotBase._fill_owners() running at C:\\my_virtualenv\\lib\\site-packages\\disnake\\ext\\commands\\common_bot_base.py:96>> got Future <Future pending> attached to a different loop")>
Traceback (most recent call last):
  File "C:\my_virtualenv\lib\site-packages\disnake\ext\commands\common_bot_base.py", line 96, in _fill_owners
    await self.wait_until_first_connect()  # type: ignore
  File "C:\my_virtualenv\lib\site-packages\disnake\client.py", line 1517, in wait_until_first_connect
    await self._first_connect.wait()
  File "C:\python3.9.1\lib\asyncio\locks.py", line 226, in wait
    await fut
RuntimeError: Task <Task pending name='Task-10' coro=<CommonBotBase._fill_owners() running at C:\my_virtualenv\lib\site-packages\disnake\ext\commands\common_bot_base.py:96>> got Future <Future pending> attached to a different loop
DEBUG:disnake.http:GET https://discord.com/api/v10/users/@me with None has returned 200
DEBUG:disnake.http:A rate limit bucket has been exhausted (bucket: None:None:/users/@me, retry: 0.001).

The Bad News: Slash commands don't sync, which is unfortunate, but…

The Good News: The bot doesn't crash! Which is good! And if the commands were already created and synced by another working instance, then those commands will still dispatch properly if someone calls them!

NOTE: Using DISCORD_CLIENT.run(...) does not error, but I need to be able to manage my own event loop for this bot, and since it's a probable parameter you can pass it, I'd expect that this would be supported as well?

Metadata

Metadata

Assignees

No one assigned

    Labels

    unconfirmed bugSomething might not be working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions