-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (63 loc) · 3.01 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
from server import Server
import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.filters import Command
from config import API_TOKEN, CHAT_ID, IP, PORT
bot = Bot(token=API_TOKEN)
dp = Dispatcher()
server = Server(IP, PORT)
previous_players = []
async def check_server():
global previous_players
while True:
online, max_players = server.get_server_online()
players_list = server.get_server_online_list()
if online is None:
await asyncio.sleep(5)
continue
current_players = [player for player in players_list]
entered_players = set(current_players) - set(previous_players)
exited_players = set(previous_players) - set(current_players)
for player in entered_players:
await bot.send_message(CHAT_ID, f"Игрок {player} зашел на сервер!")
for player in exited_players:
await bot.send_message(CHAT_ID, f"Игрок {player} вышел с сервера!")
previous_players = current_players
await asyncio.sleep(5)
@dp.message(Command("online"))
async def send_online_count(message: types.Message):
online, max_players = server.get_server_online()
if online != "Error":
await message.reply(f"Сейчас на сервере {online} из {max_players} игроков.")
else:
await message.reply("Не удалось получить информацию о количестве игроков.")
@dp.message(Command("ping"))
async def send_online_count(message: types.Message):
ping = server.get_ping()
if ping != "Error":
await message.reply(f"Задержка между клиентом и сервером {ping}ms.")
else:
await message.reply("Не удалось получить информацию.")
@dp.message(Command("players"))
async def send_online_count(message: types.Message):
players = server.get_server_online_list()
if len(players) == 0:
await message.reply(f"На сервере нет игроков.")
elif players != ["Error"]:
player_list = "\n".join("- " + player for player in players)
await message.reply(f"Список текущих игроков на сервере:\n{player_list}")
else:
await message.reply("Не удалось получить информацию.")
@dp.message(Command("start"))
async def start(message: types.Message):
commands = (
"/online - Узнать текущее количество игроков на сервере\n"
"/ping - Получить задержку между клиентом и сервером\n"
"/players - Посмотреть список текущих игроков на сервере"
)
await message.reply(f"Добро пожаловать! Вот доступные команды:\n{commands}")
async def main():
asyncio.ensure_future(check_server())
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())