I know this has been asked about before... I've looked and looked and looked. So many people having this issue are simply bad at OOP but I've stripped it down to the bare essentials to see what is causing this issue and I just cannot figure it out. Some more experienced guidance would be HIGHLY appreciated!
File to run: mybot.py
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
client = commands.Bot(command_prefix="?")
class MyBot():
def __init__(self, bot):
bot.load_extension("mycog")
bot.run(TOKEN, bot=True, reconnect=True)
@client.event
async def on_ready():
print("Bot is listening...")
@client.event
async def on_message(message):
await client.process_commands(message)
if __name__ == "__main__": MyBot(client)
File to load: mycog.py
from discord.ext import commands
import asyncio
class MyCog:
def __init__(self, bot):
self.bot = bot
@commands.command()
async def shutdown(self, ctx):
await self.bot.close()
return None
def setup(client): client.add_cog(MyCog(client))
Bot runs fine, I input command ?shutdown and while it does shut down I get this error...
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x000001BDA9A55EF0>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x000001BDA9BED1C8>, 181630.39)]']
connector: <aiohttp.connector.TCPConnector object at 0x000001BDA9A55F60>
But why? I thought using client.close() or .logout() would properly shut everything down. Is there any way to have everything gracefully exit?
My two cents on this:
First off, the following code reproduces it fine
from discord.ext import commands
client = commands.Bot(command_prefix=">")
@client.command()
async def close(ctx):
await client.close()
client.run('')
Second, the reason I find this happens is because of this here:
https://github.com/Rapptz/discord.py/blob/rewrite/discord/client.py#L443
The closed flag is set before closing the session:
https://github.com/Rapptz/discord.py/blob/rewrite/discord/client.py#L456
This causes the loop that's running to check this, and shutdown before the session closes:
https://github.com/Rapptz/discord.py/blob/rewrite/discord/client.py#L419
A simple fix (I tested and it works for me) for this is to the self._closed.set() to after the await self.http.close()....I'll probably do a simple pull request for it (unless there's some reason that it shouldn't be done this way)
EDIT: As mentioned in the pull request I attempted, it's just not that simple to fix....I'll just leave it at that, at least it's clear what the issue is that causes this
Most helpful comment
My two cents on this:
First off, the following code reproduces it fine
Second, the reason I find this happens is because of this here:
https://github.com/Rapptz/discord.py/blob/rewrite/discord/client.py#L443
The closed flag is set before closing the session:
https://github.com/Rapptz/discord.py/blob/rewrite/discord/client.py#L456
This causes the loop that's running to check this, and shutdown before the session closes:
https://github.com/Rapptz/discord.py/blob/rewrite/discord/client.py#L419
A simple fix (I tested and it works for me) for this is to the
self._closed.set()to after theawait self.http.close()....I'll probably do a simple pull request for it (unless there's some reason that it shouldn't be done this way)EDIT: As mentioned in the pull request I attempted, it's just not that simple to fix....I'll just leave it at that, at least it's clear what the issue is that causes this