Open Starz0r opened 10 months ago
If i remember correctly, disnake uses asyncio.get_event_loop
/asyncio.get_running_loop
in certain parts, which means that the loop you passed via loop=
may be a different loop than the one returned by the above-mentioned functions. Use asyncio.set_event_loop(EVLOOP)
before the sys.exit(...)
line to ensure the loops match. Not sure whether this is a "bug", though.
Did you get any solution? I have the same problem, when I use bot.start my commands don't sync
this here worked for me
import asyncio, disnake
from disnake.ext import commands
EVLOOP = asyncio.new_event_loop()
asyncio.set_event_loop(EVLOOP)
bot = commands.Bot(
command_prefix=commands.when_mentioned,
intents=disnake.Intents.all(),
)
@bot.slash_command(description='test')
async def test(inter: disnake.ApplicationCommandInteraction) -> None:
await inter.response.send_message('working')
async def main():
EVLOOP = asyncio.get_event_loop()
server_task = asyncio.create_task('run your server')
discord_bot_task = asyncio.create_task(bot.start(''))
await asyncio.gather(socketio_task, discord_bot_task)
EVLOOP.run_until_complete(main())
Took me awhile to get back to this. To be honest, I somewhat forgot until I had to come back to maintaining my bot.
My problem with the library is that it should save whatever event loop it was spawned on and only run tasks on that, which isn't the case. If I need to call a Disnake function in another event loop, I assume that internally it will be shunted off to the correct place, but it isn't. I solved my issue by wrapping calls like channel.send(...)
in a Task
with the correct event loop attached, and sending that off to asyncio.run_coroutine_threadsafe(...)
. Personally, I don't think I should need to take these steps, but perhaps this could just be a limitation of asyncio
in Python in general?
Summary
When passing a event loop to the bot constructor, any command preparation will fail.
Reproduction Steps
Minimal Reproducible Code
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
Checklist
Additional Context
My personal traceback in question:
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?