Get Started - Learn How To Make Your Bot!GuidesCode SnippetsGet methodsPost methodsWeb APIUseful linksList of all currently Free ItemsList of EmotesHighrise Bot SDK Changelog
Concept
Itโs a bummer when you let your bot running before going to sleep only to wake up and see that it crashed a few minutes after you closed your eyes, who canโt relate to that? But donโt give up, everything has a solution and for this case is pretty simple. For today, I will show how to make you bot reconnect after crashing.
Keep in mind that you have to investigate why your bot is crashing, this is not a solution, just a way around the problem. ๐
Required Libraries
- highrise-bot-sdk
- time
Code
Hereโs what we will need to import into our bot:
from highrise.__main__ import * import time
Why we are importing from the
__main__
file? Thatโs because thereโs where the magic happens, whe are use to perform the command highrise <file_name>:<class_name> <room_id> <token_key>
, well, this command is handled by __main__
file and it is what makes the bot run, there for is where the bot starts and stops.To re-run the code after crashing we can use a very simple trick, that is to make it run in a loop. To that letโs create a new file called
run_bot.py
:Perfect, now we need to write our code, the bot runs based on a few informations:
Bot file name
Bot class name
Room Id
Bot Token
Now letโs create those variables in our code:
from highrise.__main__ import * import time bot_file_name = "bot" bot_class_name = "Bot" room_id = "61186670bd5272731251a06f" bot_token = "96cfd73016e193a33f335eab464fbff3c621ea6eb666516c13e58e7646666097"
Ok, now we have to create a
BotDefinition
object, this object will be used by the run
function from the SDK. Note that the bot file and class name will be used in the BotDefinition
to get the BaseBot
class attribute from the Bot
code:from highrise.__main__ import * import time bot_file_name = "bot" bot_class_name = "Bot" room_id = "61186670bd5272731251a06f" bot_token = "96cfd73016e193a33f335eab464fbff3c621ea6eb666516c13e58e7646666097" my_bot = BotDefinition(getattr(import_module(bot_file_name), bot_class_name)(), room_id, bot_token)
After it we can proceed to make a infite loop and use the function to run the bot from the
__main__
file within a try
& except
block to make sure that, if the bot crashes, the error will be handled and it will proceed to run again after a few seconds:from highrise.__main__ import * import time bot_file_name = "bot" bot_class_name = "Bot" room_id = "61186670bd5272731251a06f" bot_token = "96cfd73016e193a33f335eab464fbff3c621ea6eb666516c13e58e7646666097" my_bot = BotDefinition(getattr(import_module(bot_file_name), bot_class_name)(), room_id, bot_token) while True: try: definitions = [my_bot] arun(main(definitions)) except Exception as e: print(f"An exception occourred: {e}") time.sleep(5)
After it, your code should be something like this one:
Now your bot will reconnect if your code crashes! To run your bot all you need to do is write in your terminal the following:
py run_bot.py
An additional tip that I will give is that, inside of the
__main__
file you can change the line 169 from:
frame = await ws.receive(READ_TIMEOUT) if frame.type in [WSMsgType.CLOSE, WSMsgType.CLOSED]: print( f"ERROR connection with ID: {session_metadata.connection_id} closed." ) # Close frame ka_task.cancel() return
to:
frame = await ws.receive(READ_TIMEOUT) if frame.type in [WSMsgType.CLOSE, WSMsgType.CLOSED]: print( f"ERROR connection with ID: {session_metadata.connection_id} closed." ) # Close frame ka_task.cancel() raise ConnectionResetError
This will make the bot reconnect instead of closing in case of TIMEOUT errors.
ย