0

Hyello I'm new to coding and I'm trying to make this discord bot say "Azu-bot offline" when it goes from being online to offline in discord.

Here is the code that I have for this event

@client.event
async def on_disconnect():
    code_channel = client.get_channel(channel id)
    await code_channel.send("Azu-bot offline")

and where it says channel id is the actual id of the channel.

The problem is that when I kill the terminal to make the bot go offline it never sends the message so I need help.

I also have one event that makes it say "Azu-bot online" when it gets online which works whenever I run it.

Maybe the problem is that the terminal that I kill to make the bot go offline is the same one that is running the code that makes it send the "Azu-bot offline" message which means since the terminal is dead the code that detects the offline event is no longer running so it won't send. If so then I don't know how to fix that.

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48

1 Answers1

2

Have a look at this answer https://stackoverflow.com/a/56478987/13335890 It has the code for a shutdown command. Might be useful to you. Here is the same code in python

@client.command(aliases=["quit"])
@commands.has_permissions(administrator=True)
async def close(ctx):
    await client.close()
    print("Bot Closed")  # This is optional, but it is there to tell you.

Send a offline message before client.close()