I'm working on a bot using `discord.py. The bot makes/deletes several channels, and connects to a SQLite database. If the bot were to crash, I want it to
- Destroy all temporary voice channels it created.
- Disconnect from the SQL database.
Here's the shutdown coroutine:
async def shutdown(self):
print("Shutting down Canvas...")
for ch in self.active_channels:
await client.delete_channel(ch)
self.db.close()
Things I've tried:
# Canv is the interface between the bot and the data we're collecting
atexit.register(canv.shutdown)
bot.run(TOKEN)
try:
bot.loop.run_until_complete(bot.start(TOKEN))
except KeyboardInterrupt or InterruptedError:
bot.loop.run_until_complete(canv.shutdown())
finally:
bot.loop.close()
from async_generator import asynccontextmanager
@asynccontextmanager
async def cleanup_context_manager():
try:
yield
finally:
await canv.shutdown()
with cleanup_context_manager():
bot.run(TOKEN)
None of these run canv.shutdown()
, which is an asyncio.coroutine
. How do ensure this code gets run on every type of exit?
I used this post for some info, and I think it's the closest to what I want.