Some nits

Signed-off-by: Etzelia <etzelia@hotmail.com>
pull/1/head
Etzelia 2021-07-25 22:06:04 -05:00
parent a8e12cc8bd
commit 055cc5d967
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
3 changed files with 18 additions and 10 deletions

5
.gitignore vendored 100644
View File

@ -0,0 +1,5 @@
# PyCharm
.idea/
# virtualenv
venv/

View File

@ -12,12 +12,12 @@ SERVER_DIRECTORY="/home/minecraft/server"
STARTUP_SCRIPT="startup.sh" STARTUP_SCRIPT="startup.sh"
# Start the screen if it is not already running # Start the screen if it is not already running
if ! screen -list | grep -q $SCREE_NAME; then if ! screen -list | grep -q $SCREEN_NAME; then
screen -dmS s1_canopy screen -dmS $SCREEN_NAME
fi fi
# Run the startup script in a screen # Run the startup script in a screen
screen -S s1_canopy -p 0 -X stuff "^Mcd $SCREEN_DIRECTORY^M" screen -S $SCREEN_NAME -p 0 -X stuff "^Mcd $SERVER_DIRECTORY^M"
screen -S s1_canopy -p 0 -X stuff "./$STARTUP_SCRIPT^M" screen -S $SCREEN_NAME -p 0 -X stuff "./$STARTUP_SCRIPT^M"
``` ```

View File

@ -9,24 +9,25 @@ parser.add_argument('token', metavar='token', type=str, help='ServerAPI Token')
parser.add_argument('--webhook', metavar='discord_webhook', type=str, help='Discord Webhook URL') parser.add_argument('--webhook', metavar='discord_webhook', type=str, help='Discord Webhook URL')
def send_discord_webhook(url, msg): def send_discord_webhook(url: str, msg: str) -> bool:
request = {"content": msg} request = {"content": msg}
r = requests.post(url, json=request) r = requests.post(url, json=request)
return r.ok
def send_request(url, token, endpoint, request): def send_request(url: str, token: str, endpoint: str, request: dict) -> bool:
headers = {"X-ServerAPI-Token": token} headers = {"X-ServerAPI-Token": token}
r = requests.post(f"{url}{endpoint}/", headers=headers, json=request) r = requests.post(f"{url}{endpoint}/", headers=headers, json=request)
return r.ok return r.ok
def send_command(url, token, command, *args): def send_command(url: str, token: str, command: str, *args) -> bool:
request = {"command": command, "args": list(args)} request = {"command": command, "args": list(args)}
return send_request(url, token, "custom", request) return send_request(url, token, "custom", request)
def send_broadcast(url, token, sender, msg): def send_broadcast(url: str, token: str, sender: str, msg: str) -> bool:
request = {"from": sender, "message": msg} request = {"from": sender, "message": msg}
return send_request(url, token, "broadcast", request) return send_request(url, token, "broadcast", request)
@ -57,7 +58,7 @@ def main():
print("Sending final warnings") print("Sending final warnings")
for i in range(0, 5): for i in range(0, 5):
r = send_command(url, token, "broadcast", "The server is restarting get safe!") r = send_command(url, token, "broadcast", "The server is restarting, get safe!")
if not r: if not r:
print("Failed to send final restart broadcast") print("Failed to send final restart broadcast")
quit(1) quit(1)
@ -65,7 +66,9 @@ def main():
time.sleep(2) time.sleep(2)
if webhook: if webhook:
send_discord_webhook(webhook, "Server is going offline for an automated restarted") r = send_discord_webhook(webhook, "Server is going offline for an automated restart.")
if not r:
print("Failed to send webhook")
print("Restarting") print("Restarting")
r = send_command(url, token, "restart") r = send_command(url, token, "restart")