public
imalexnord
read
Discord-Hetzner-Bot
No description yet
Languages
Repository composition by tracked source files.
Python
100%
Create file
Wiki Documentation
Clone
https://nobgit.com/user/imalexnord/discord-hetzner-bot.git
ssh://[email protected]:2222/user/imalexnord/discord-hetzner-bot.git
Commit
Added location infos and possibly fixed some bugs
4a1e1f5
game.py | 22 ++++++++++++++++++----
gameserver.py | 10 +++++++---
2 files changed, 25 insertions(+), 7 deletions(-)
Diff
diff --git a/game.py b/game.py
index ff5373c..2eb3448 100644
--- a/game.py
+++ b/game.py
@@ -3,12 +3,14 @@ from hcloud.images.domain import Image
from hcloud.servers.domain import Server
from hcloud.server_types.domain import ServerType
from hcloud.volumes.domain import Volume
+from hcloud.locations.domain import Location
import time
+import asyncio
class GameData:
running = False
- def __init__(self, token, name, servertype, snapshot, volume):
+ def __init__(self, token, name, servertype, snapshot, location, volume):
self.name = name
self.servertype = servertype
self.client = Client(token=token)
@@ -20,11 +22,19 @@ class GameData:
imageId = i.data_model.id
self.snapshot = imageId
+ # get id from location
+ locations = self.client.locations.get_all()
+ for i in locations:
+ if i.data_model.name == location:
+ locationId = i.data_model.id
+ self.location = locationId
+
# get id from volume
volumes = self.client.volumes.get_all()
for i in volumes:
if i.data_model.name == volume:
- volumeId = i.data_model.id
+ if i.data_model.location.data_model.id == self.location:
+ volumeId = i.data_model.id
self.volume = volumeId
# check for running server
@@ -36,13 +46,14 @@ class GameData:
self.running = True
print("existing server " + self.server.status)
- def start(self):
+ async def start(self):
if self.running == False:
print("Starting " + self.name)
response = self.client.servers.create(
self.name,
server_type=ServerType(name=self.servertype),
image=Image(self.snapshot),
+ location=Location(self.location),
volumes=[Volume(self.volume)]
)
self.server = response.server;
@@ -56,17 +67,20 @@ class GameData:
else:
print(self.name + " is already running")
- def stop(self):
+ async def stop(self):
if self.running == True:
print("Stopping " + self.name)
self.client.servers.shutdown(self.server)
+ time.sleep(30)
while self.server.status != Server.STATUS_OFF:
print(self.server.status)
time.sleep(5)
serv = self.client.servers.get_by_id(self.server.id)
self.server = serv
print("Server is now stopped")
+ time.sleep(1)
self.client.volumes.detach(Volume(self.volume))
+ time.sleep(1)
self.client.servers.delete(self.server)
self.running = False
self.server = None
diff --git a/gameserver.py b/gameserver.py
index 0171856..b7dd206 100755
--- a/gameserver.py
+++ b/gameserver.py
@@ -4,6 +4,7 @@ from settings import *
from game import *
from discord import Game
import discord
+import sys
client = discord.Client()
@@ -18,7 +19,8 @@ async def on_message(message):
"!ping - simple online check\n"
"!start GAME - starts a server for GAME\n"
"!stop GAME - stops the server for GAME\n"
- "!status [GAME] - prints basic infos about the server for GAME"
+ "!status [GAME] - prints basic infos about the server for GAME\n"
+ "!exit - exit bot script - should restart it when used with systemd service unit"
).format(message)
await client.send_message(message.channel, msg)
elif message.content.startswith(BOT_PREFIX + "ping"):
@@ -35,7 +37,7 @@ async def on_message(message):
if i.name.lower() == cmd[0]:
if i.isRunning() == False:
await client.change_presence(game=Game(name="starting server..."))
- i.start()
+ await i.start()
await client.change_presence(game=Game(name="ready"))
msg = i.status()
await client.send_message(message.channel, msg)
@@ -50,7 +52,7 @@ async def on_message(message):
if i.name.lower() == cmd[0]:
if i.isRunning() == True:
await client.change_presence(game=Game(name="stopping server..."))
- i.stop()
+ await i.stop()
await client.change_presence(game=Game(name="ready"))
msg = i.status()
await client.send_message(message.channel, msg)
@@ -62,6 +64,8 @@ async def on_message(message):
elif i.name.lower() == cmd[0]:
msg = i.status()
await client.send_message(message.channel, msg)
+ elif message.content.startswith(BOT_PREFIX + "exit"):
+ sys.exit("exit requested")
@client.event
async def on_ready():