forked from Pechente/minecraft-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
129 lines (103 loc) · 3.33 KB
/
index.php
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
122
123
124
125
126
127
128
129
<?php
$start = microtime(true);
include "config.php";
include "bot/Telegram.php";
require "query/MinecraftQuery.php";
require "query/MinecraftQueryException.php";
include "lang/" . $config["lang"] . ".php";
use xPaw\MinecraftQuery;
use xPaw\MinecraftQueryException;
$live_playerlist = get_live_playerlist();
$cached_playerlist = get_cached_playerlist();
$message_to_chat = "";
// Compare live playerlist to cached playerlist. If there is a change, generate a message
if ($live_playerlist != $cached_playerlist) {
$message_to_chat = generate_message($live_playerlist, $cached_playerlist);
}
// If there's a message, post it
if ($message_to_chat) {
post_message_to_chat($message_to_chat);
echo $message_to_chat; // also echo the result for easier debugging without chat spam
}
// When done, cache current playerlist
cache_playerlist($live_playerlist);
// Display execution time. Useful for adjusting the interval to call this script
$execution_time = microtime(true) - $start;
echo "\r\nExecution time: " . $execution_time;
function get_live_playerlist()
{
global $config;
$Query = new MinecraftQuery();
try {
$Query->Connect($config["server_url"], $config["server_port"]);
$playerlist = $Query->GetPlayers();
$playerlist = !is_array($playerlist) ? [] : $playerlist;
sort($playerlist);
return $playerlist;
} catch (MinecraftQueryException $e) {
echo $e->getMessage();
}
}
function get_cached_playerlist()
{
return unserialize(file_get_contents("cache"));
}
function cache_playerlist($playerlist)
{
file_put_contents("cache", serialize($playerlist));
}
function generate_message($live_playerlist, $cached_playerlist)
{
global $lang;
// The playerlist is an empty string if no players joined but we ALWAYS need an array for the following functions
$live_playerlist_array = is_array($live_playerlist) ? $live_playerlist : [];
$cached_playerlist_array = is_array($cached_playerlist)
? $cached_playerlist
: [];
$players_joined = array_diff(
$live_playerlist_array,
$cached_playerlist_array
);
$players_disconnected = array_diff(
$cached_playerlist_array,
$live_playerlist_array
);
$message = "";
foreach ($players_joined as $player_joined) {
$message .= $player_joined . " " . $lang["joined"] . " ";
}
foreach ($players_disconnected as $player_disconnected) {
$message .= $player_disconnected . " " . $lang["disconnected"] . " ";
}
$player_count = count($live_playerlist_array);
switch ($player_count) {
case 0:
$message .= $lang["no_players_connected"];
break;
case 1:
$message .= $lang["one_player_connected"];
break;
default:
$message .= $player_count . " " . $lang["players_connected"];
}
return $message;
}
function post_message_to_chat($message)
{
global $config;
$telegram = new Telegram($config["bot_token"]);
$content = ["chat_id" => $config["chat_id"], "text" => $message];
if (isset($config["reply_to_message_id"]) && $config["reply_to_message_id"] != "") {
$content["reply_to_message_id"] = $config["reply_to_message_id"];
}
$result = $telegram->sendMessage($content);
if ($result["error_code"] >= 400)
{
echo "Failed to send telegram message (" . $result["error_code"] . ").";
echo "Message: " . $result["description"];
echo "";
echo "Full Response:";
var_dump($result);
}
}
?>