Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Discord bot is online

I'm trying to make it so that only one instance of my bot can connect to Discord at a time and that the other connects only if the other one isn't connected. How could I achieve this? I'm using Discord.py. Also, if possible, I'd like it to work across multiple machines.

like image 335
ComboDev Avatar asked Oct 26 '22 13:10

ComboDev


2 Answers

If you're asking what I think you're asking, that is, the bot should only be allowed to have one version of itself running on the machine at any one time, then this should work for all cases where you only want to have one of a script running at once.

One way we can do this is by having the script create a "lock" file, and exiting if said file already exists. Just remember to delete it when we're finished, even if the bot crashed. (There are likely better ways to handle errors here and your bot code itself should do its best to handle errors that the bot may generate. For the most part discord.py will just keep going even when there are errors. This will just get the serious bot-crashing stuff and makes sure you can see what happened, while still gracefully closing and ensuring the lock file is deleted.)

import discord
from discord.ext import commands
import os  # for file interactions
import traceback
# etc.

bot = commands.Bot(description="Hard Lander's lovely bot", command_prefix="!")
@bot.event
async def on_ready():
    print("I'm ready to go!")
    print(f"Invite link: https://discordapp.com/oauth2/authorize?client_id={bot.user.id}&scope=bot&permissions=8")

def main():
    bot.run("TOKEN")

if __name__ == '__main__':
    running_file = "running.txt"
    if os.path.isfile(running_file):  # check if the "lock" file exists
        print("Bot already running!")
        exit()  # close this instance having taken no action. 
    else:
        with open(running_file, 'w') as f:
            f.write("running")
    try:  # catch anything that crashes the bot
        main()
    except:  # print out the error properly 
        print(traceback.format_exc())
    finally:  # delete the lock file regardless of it it crashed or closed naturally. 
        os.unlink(running_File)
        
like image 86
ch4rl1e97 Avatar answered Nov 15 '22 07:11

ch4rl1e97


A simple way to do this to run the second bot after the bot.run() in the first bot. Why? Because of the fact that it's blocking any other code that comes after it unless the bot stopped.

so I would suggest this:

bot.run("Your first bot token here")

import secondbot # you may use another method to run the bot script

Another way to do this is to make a third script for the second bot, this script will run periodically to check the first bot status (in this case, offline status), if the first bot status is offline, it will turn on the second bot. I'm not recommending this method for your case because you may want to change your first bot presence to offline which will make the third script think the bot goes offline.

Edit: After writing this answer I found a method of Bot called "is_closed" wich return bool wether or not the bot connected to the websocket, the link is here

like image 34
hard lander Avatar answered Nov 15 '22 05:11

hard lander