-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_process_tracker.py
109 lines (96 loc) · 3.11 KB
/
discord_process_tracker.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
import discord
import os.path
import subprocess
from datetime import datetime
import asyncio
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
message_id = 0
message_channel = ""
config_array = []
is_running = False
# https://stackoverflow.com/a/29275361
def process_exists(process_name):
call = 'TASKLIST', '/FI', 'imagename eq %s' % process_name
# use buildin check_output right away
output = subprocess.check_output(call).decode()
# check in last line for process name
last_line = output.strip().split('\r\n')[-1]
# because Fail message could be translated
return last_line.lower().startswith(process_name.lower())
#
def check_configs():
build_string = "**SERVER STATUS:**\n"
for data in config_array:
exists = process_exists(data[1])
if exists:
build_string += ":green_circle: "
else:
build_string += ":red_circle: "
build_string += data[0] + "\n"
build_string += "\n `Updated " + str(datetime.now()).split(".")[0] + "`"
return build_string
async def run_cycle():
while True:
if not is_running:
await asyncio.sleep(5)
continue
channel = client.get_channel(message_channel)
message = await channel.fetch_message(message_id)
build_string = check_configs()
await message.edit(content=build_string)
await asyncio.sleep(300)
def set_message(id, channel):
global message_id, message_channel
message_id = id
message_channel = channel
def enable_cycle():
global is_running
is_running = True
@client.event
async def on_ready():
print(f'Logged in as {client.user}')
if os.path.isfile("config.txt"):
f = open("config.txt", "r")
lines = f.readlines()
f.close()
config_array.clear()
for line in lines:
s = line.strip()
a = s.split(",")
config_array.append(a)
print(f'Config data: {config_array}')
else:
raise Exception("config.txt not found")
if os.path.isfile("tracker"):
f = open("tracker", "r")
s = f.read()
a = s.split(",")
f.close()
set_message(int(a[0]), int(a[1]))
print(f'Tracker message: {message_id} in {message_channel}')
enable_cycle()
else:
print('Tracker not found: Use $spawn in desired channel to start bot')
client.loop.create_task(run_cycle())
print('Loading complete, process now looping')
@client.event
async def on_message(message):
if message.author == client.user:
set_message(message.id, message.channel.id)
f = open("tracker", "w")
f.write(str(message_id) + "," + str(message_channel))
f.close()
print(f'New tracker message: {message_id} in {message_channel}')
enable_cycle()
return
if message.content.startswith('$spawn'):
await message.channel.send('Setting up...')
if os.path.isfile("token.txt"):
f = open("token.txt", "r")
token = f.read()
f.close()
client.run(token)
else:
raise Exception("token.txt not found")