-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
121 lines (98 loc) · 4.42 KB
/
bot.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import discord
import subprocess
import re
role_id = 838848407296409600
token_file = open('token.txt', 'r')
token = token_file.read()
token_file.close()
def server_check():
# Checks if the server is already up
ps = subprocess.run(['ps', '-a'], stdout=subprocess.PIPE).stdout.decode('utf-8')
if 'java' in ps:
return True
else:
return False
def get_java_pid():
ps = subprocess.run(['ps', '-a'], stdout=subprocess.PIPE).stdout.decode('utf-8')
for line in ps.splitlines():
if 'java' in line:
return line.strip().split(' ')[0]
server_up = server_check()
server = None
async def read_output(channel):
#await channel.send('Server starting!')
while True:
if not server:
break
for line in iter(server.stdout.readline, b''):
matched = re.search('Done \(d{1,}.\d{1,3}s\)! For help, type "help"', line.decode('utf-8'))
print(line.decode('utf-8'))
if matched:
channel.send('Server started.')
if not server:
break
class discordClient(discord.Client):
async def send_message(self, channel, message):
await channel.send(message)
async def update_activity(self):
discord_state = 'Unknown'
if server_up:
discord_state = 'up'
else:
discord_state = 'down'
await self.change_presence(activity=discord.Activity(name='Server is {}!'.format(discord_state), type=0))
async def on_ready(self):
print('Ready')
await self.update_activity()
async def on_message(self, msg):
if msg.content.startswith('mc!'):
if role_id in [role.id for role in msg.author.roles]:
global server_up
global server
command = msg.content[3:].lower()
if command == 'start':
if server_up == True:
await msg.channel.send('Could not start server, server is already started/is starting!')
else:
if server_check():
await msg.channel.send('Server is currently attempting to start or shut down, please try again later.')
else:
server = subprocess.Popen(['/home/minecraft_server/fabric/start.sh'], stdout=subprocess.PIPE)
global thread
#loop = asyncio.get_event_loop()
#thread = await loop.run_in_executor(ThreadPoolExecutor(), read_output(msg.channel))
server_up = True
await self.update_activity()
await msg.channel.send('Sent message to server to start.')
elif command == 'stop':
if server_up == False:
await msg.channel.send('Could not stop server, server is offline.')
else:
if not server_check():
await msg.channel.send('Server stopped instantly!')
server_up = False
await self.update_activity()
else:
await msg.channel.send('Sending message to server to stop.')
subprocess.run(['kill', get_java_pid()])
server_up = False
await self.update_activity()
if server:
server.kill()
server.wait()
server.returncode
server = None
#Ensure command closes
elif command[0:2] == 'say':
server.communicate('<Discord> [{}] {}\n'.format(msg.author.name,command[4:]))
elif command == 'status':
status = None
if server_up == True:
status = 'up'
else:
status = 'down'
await msg.channel.send('The server is {}!'.format(status))
else:
await msg.channel.send('You do not have adequate permissions to use fabric commands.')
client = discordClient()
client.run(token)