-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
114 lines (99 loc) · 3.55 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import json
import os
import time
from discord_webhook import DiscordWebhook
from mcstatus import MinecraftServer
file_name = "data.json"
global server
global webhook_url
players_online = 0
player_sample = []
online = False
def init():
global server
global webhook_url
if os.path.exists(file_name):
with open(file_name) as json_file:
data = json.load(json_file)
if "serverName" in data and "url" in data:
server_name = data["serverName"]
webhook_url = data["url"]
else:
server_name = input("please enter the ip of the server")
webhook_url = input("please enter the url of the discord webhook")
data = {"serverName": server_name, "url": webhook_url}
with open(file_name, 'w') as outfile:
json.dump(data, outfile)
print(F"server ip: {server_name}")
print(F"webhook url: {webhook_url}")
server = MinecraftServer.lookup(server_name)
try:
global players_online
global player_sample
global online
status = server.status()
players_online = status.players.online
player_sample = status.players.sample
online = True
print(f"player online: {players_online}")
except Exception as e:
print(e)
print("connecting failed")
def send_webhook(text, player=None):
if player is None:
print(text)
webhook = DiscordWebhook(url=webhook_url, content=text, rate_limit_retry=True)
else:
print(player.name + text)
webhook = DiscordWebhook(url=webhook_url, content=text, rate_limit_retry=True, username=player.name,
avatar_url="https://minotar.net/bust/" + player.id)
webhook.execute()
def send_player_join_or_leave(player, message):
send_webhook(message, player=player)
def send_player_change(new_players):
global player_sample
if new_players is not None:
for player in new_players:
player_online = False
if player_sample is not None:
for oldPlayer in player_sample:
if oldPlayer.id == player.id:
player_online = True
if not player_online:
send_player_join_or_leave(player, " has joined the server")
if player_sample is not None:
for player in player_sample:
player_online = False
if new_players is not None:
for newPlayer in new_players:
if newPlayer.id == player.id:
player_online = True
if not player_online:
send_player_join_or_leave(player, " has left the server")
player_sample = new_players
def start():
global players_online
global player_sample
global online
while True:
try:
status = server.status()
players = status.players.online
players_online = players
send_player_change(status.players.sample)
if players != players_online:
print(f"player online: {players}")
# if not online:
# send_webhook("server online again")
time.sleep(5)
online = True
except Exception as e:
print(e)
print("connecting failed")
# if online:
# send_webhook("server offline")
online = False
time.sleep(5)
if __name__ == '__main__':
init()
start()