1

I am coding a discord bot in discord.py. I want it to change the role of the bot to show it's offline when the code is stopped. Here's the relevant code:

import atexit, signal
class MyClient(discord.Client):
 async def exit_handler(self):
 print("stopped!")
 guild = self.get_guild(SERVER_ID)
 role = utils.get(guild.roles, name="Bot Offline")
 member = guild.get_member(BOT_USER_ID)
 await member.add_roles(role)
atexit.register(client.exit_handler)
signal.signal(signal.SIGTERM, client.exit_handler)
signal.signal(signal.SIGINT, client.exit_handler)

exit_handler isn't running when stopped with vsCode. I did not get an error message. I Tried running it in IDLE as well.

11
  • do you get error message? Show it in question (not in comments) Is this working when you run without VS Code? Maybe VS Code use this atexit to catch exit and it can block access. Or maybe atexit is executed when all objects are already removed from memory. Or maybe atexit can't run w async. I think discord has own function to run something when yo close connection. Probably you have to create function on_close() Commented Dec 6, 2023 at 14:47
  • python - Is there a way to run an asyncio coroutine using atexit? - Stack Overflow Commented Dec 6, 2023 at 14:50
  • asyncio-atexit · PyPI Commented Dec 6, 2023 at 14:51
  • @furas do you mean that async def on_close() would execute on close? Commented Dec 6, 2023 at 21:36
  • When you stop a program with VSCode, you don't get SIGTERM or SIGINT - the code just literally stops dead. Commented Dec 6, 2023 at 21:42

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.