2019-11-04 12:17:59 -05:00
|
|
|
#!/bin/sh
|
2019-10-06 22:42:44 -04:00
|
|
|
|
|
|
|
# A wrapper script for painless discord bots.
|
2019-11-04 12:17:59 -05:00
|
|
|
# v1.2.0
|
2019-10-06 22:42:44 -04:00
|
|
|
# Copyright (c) 2019 0x5c
|
|
|
|
# Released under the terms of the MIT license.
|
|
|
|
# Part of:
|
|
|
|
# https://github.com/0x5c/quick-bot-no-pain
|
|
|
|
|
|
|
|
|
|
|
|
# If $BOTENV is not defined, default to 'botenv'
|
2019-11-04 12:17:59 -05:00
|
|
|
if [ -z "$BOTENV" ]; then
|
2019-10-06 22:42:44 -04:00
|
|
|
BOTENV='botenv'
|
|
|
|
fi
|
|
|
|
|
2019-11-04 12:17:59 -05:00
|
|
|
|
|
|
|
# Argument handling
|
|
|
|
_PASS_ERRORS=0
|
|
|
|
_NO_BOTENV=0
|
|
|
|
while [ ! -z "$1" ]; do
|
|
|
|
case $1 in
|
|
|
|
--pass-errors)
|
|
|
|
_PASS_ERRORS=1
|
|
|
|
;;
|
|
|
|
--no-botenv)
|
|
|
|
_NO_BOTENV=1
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2021-03-17 23:10:57 -04:00
|
|
|
# If $PYTHON_BIN is not defined, default to 'python3.9'
|
2019-11-04 12:17:59 -05:00
|
|
|
if [ $_NO_BOTENV -eq 1 -a -z "$PYTHON_BIN" ]; then
|
2021-03-17 23:10:57 -04:00
|
|
|
PYTHON_BIN='python3.9'
|
2019-10-06 22:42:44 -04:00
|
|
|
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).
|
|
|
|
;;
|
|
|
|
*)
|
2019-11-04 12:17:59 -05:00
|
|
|
if [ $_PASS_ERRORS -eq 0 ]; then # The bot crashed and:
|
2019-10-06 22:42:44 -04:00
|
|
|
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
|
2019-11-04 12:17:59 -05:00
|
|
|
if [ $_NO_BOTENV -eq 1 ]; then
|
|
|
|
"$PYTHON_BIN" main.py $@
|
|
|
|
else
|
|
|
|
./$BOTENV/bin/python3 main.py $@
|
|
|
|
fi
|
2019-10-06 22:42:44 -04:00
|
|
|
err=$?
|
|
|
|
_message="$0: The bot exited with [$err]"
|
|
|
|
code_handling
|
|
|
|
done
|