qrm2/run.sh

82 lines
1.8 KiB
Bash
Raw Permalink Normal View History

#!/bin/sh
# A wrapper script for painless discord bots.
# v1.2.0
# Copyright (c) 2019 0x5c
# Released under the terms of the MIT license.
# Part of:
# https://github.com/0x5c/quick-bot-no-pain
2024-05-17 12:16:27 -04:00
if [ -n "$VIRTUAL_ENV" ]; then
BOTENV="$VIRTUAL_ENV"
fi
2024-05-17 12:16:27 -04:00
if [ -z "$BOTENV" ]; then
BOTENV='./botenv'
fi
# Argument handling
_PASS_ERRORS=0
_NO_BOTENV=0
2023-12-10 14:45:12 -05:00
while [ -n "$1" ]; do
case $1 in
--pass-errors)
_PASS_ERRORS=1
;;
--no-botenv)
_NO_BOTENV=1
;;
--)
shift
break
;;
esac
shift
done
2022-12-30 15:01:48 -05:00
# If $PYTHON_BIN is not defined, default to 'python3.11'
2023-12-10 14:45:12 -05:00
if [ $_NO_BOTENV -eq 1 ] && [ -z "$PYTHON_BIN" ]; then
2022-12-30 15:01:48 -05:00
PYTHON_BIN='python3.11'
fi
# A function called when the bot exits to decide what to do
code_handling() {
case $err in
0)
echo "$_message: exiting"
exit 0 # The bot whishes to stay alone.
;;
42)
echo "$_message: restarting"
return # The bot whishes to be restarted (returns to the loop).
;;
*)
if [ $_PASS_ERRORS -eq 0 ]; then # The bot crashed and:
echo "$_message: restarting"
return # ...we should return to the loop to restart it.
else
echo "$_message: exiting (--pass-errors)"
exit $err # ...we should just exit and pass the code to our parent (probably a daemon/service manager).
fi
;;
esac
}
echo "$0: Starting bot..."
# The loop
while true; do
if [ $_NO_BOTENV -eq 1 ]; then
2023-12-10 14:45:12 -05:00
"$PYTHON_BIN" main.py "$@"
else
2024-05-17 12:16:27 -04:00
"$BOTENV/bin/python3" main.py "$@"
fi
err=$?
_message="$0: The bot exited with [$err]"
code_handling
done