-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (75 loc) · 2.63 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import discord
import os
import boto3
from dotenv import load_dotenv
load_dotenv()
ec2 = boto3.client('ec2')
client = discord.Client()
TOKEN = os.getenv('TOKEN')
SERVER_NAME = os.getenv('SERVER_NAME')
class ServerAlreadyRunning(Exception):
'''Raised when Instance is already running'''
pass
class ServerAlreadyStopped(Exception):
'''Raised when Instance has already stopped'''
pass
def describe_instance():
instance = ec2.describe_instances(
Filters=[
{
'Name': 'tag:Name',
'Values': [
SERVER_NAME
]
}
]
)
return {
"state": instance['Reservations'][0]['Instances'][0]['State']['Name'],
"id": instance['Reservations'][0]['Instances'][0]['InstanceId']
}
def stop_instance():
instance = describe_instance()
if instance['state'] == 'stopped':
raise ServerAlreadyStopped('Instance is already stopped.')
else:
return ec2.stop_instances(InstanceIds=[instance['id']])
def start_instance():
instance = describe_instance()
if instance['state'] == 'running':
raise ServerAlreadyRunning('Instance is already running.')
else:
return ec2.start_instances(InstanceIds=[instance['id']])
def get_status():
instance = describe_instance()
return instance['state']
@ client.event
async def on_ready():
print(print('We have logged in as {0.user}'.format(client)))
@ client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!server'):
command = message.content.split(' ')
if len(command) < 2:
await message.channel.send("Available commands:\n\nstart - Starts the server.\nstop - Stops the server.\nstatus - Displays the current server status.")
elif command[1] == "start":
try:
start_instance()
except ServerAlreadyRunning:
await message.channel.send("The server is already running!")
else:
await message.channel.send("Starting Valheim server...")
elif command[1] == "stop":
try:
stop_instance()
except ServerAlreadyStopped:
await message.channel.send("The server is already stopped!")
else:
await message.channel.send("Stopping Valheim server...")
elif command[1] == "status":
await message.channel.send(F"The server is currently: {get_status()}")
else:
await message.channel.send("Unrecognized command.")
client.run(os.getenv('TOKEN'))