mirror of
https://github.com/SantaSpeen/Rcon-VK-Bot.git
synced 2026-04-17 13:49:55 +00:00
Add logger, new cmd "!online"
Code optimization
This commit is contained in:
37
main.py
37
main.py
@@ -1,17 +1,23 @@
|
||||
import json
|
||||
import traceback
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
import vk
|
||||
from mcrcon import MCRcon
|
||||
|
||||
print("Starting..")
|
||||
|
||||
def log(text, lvl=0):
|
||||
print(f"[{datetime.now()}] [{['INFO ', 'ERROR'][lvl]}] {text}")
|
||||
|
||||
|
||||
log("Starting..")
|
||||
with open('config.json') as f:
|
||||
config = json.load(f)
|
||||
|
||||
vk = vk.API(access_token=config['vk']['token'], v=5.131)
|
||||
group_id = vk.groups.getById()[0]['id']
|
||||
print(f"Group id: {group_id}")
|
||||
log(f"Group id: {group_id}")
|
||||
admins = config['vk']['admins']
|
||||
|
||||
host = config['rcon']['host']
|
||||
@@ -28,7 +34,7 @@ def fix_rcon_text(_srt):
|
||||
_srt[i + 1] = ''
|
||||
_srt = ''.join(_srt)
|
||||
except Exception as e:
|
||||
print(f"fix_rcon_text ERROR with: {_srt}")
|
||||
log(f"fix_rcon_text ERROR with: {_srt}", 1)
|
||||
_srt = f'CRITICAL ERROR: {e}'
|
||||
return _srt
|
||||
|
||||
@@ -38,7 +44,7 @@ def rcon(cmd):
|
||||
with MCRcon(host, password, port) as mcr:
|
||||
return fix_rcon_text(mcr.command(cmd))
|
||||
except Exception as e:
|
||||
print(f"RCON ERROR with command: {cmd}")
|
||||
log(f"RCON ERROR with command: {cmd}", 1)
|
||||
print(traceback.format_exc())
|
||||
return f"Rcon error: {e}"
|
||||
|
||||
@@ -53,14 +59,25 @@ def write(peer_id, message):
|
||||
messages = (len(message) // 4095)
|
||||
for i in range(1, messages + 1):
|
||||
if i > 30:
|
||||
log("Found very long message...", 1)
|
||||
break
|
||||
write(peer_id, message[:4095*i])
|
||||
vk.messages.send(message=message, peer_id=peer_id, random_id=0)
|
||||
else:
|
||||
vk.messages.send(message=message, peer_id=peer_id, random_id=0)
|
||||
|
||||
|
||||
def rcon_cmd_handle(cmd, from_id, peer_id, _write=True):
|
||||
answer = rcon(cmd)
|
||||
log(f"User: {from_id} in Chat: {peer_id} use RCON cmd: \"{cmd}\", with answer: \"{answer}\"")
|
||||
if _write:
|
||||
write(peer_id, f"RCON:\n{answer}")
|
||||
else:
|
||||
return answer
|
||||
|
||||
|
||||
def main():
|
||||
server, key, ts = get_lp_server()
|
||||
print("Listening..")
|
||||
log("Listening..")
|
||||
while True:
|
||||
lp = requests.get(f'{server}?act=a_check&key={key}&ts={ts}&wait=25').json()
|
||||
try:
|
||||
@@ -75,9 +92,13 @@ def main():
|
||||
text = message['text']
|
||||
if from_id in admins:
|
||||
if text.startswith(".rcon "):
|
||||
write(peer_id, f"rcon: {rcon(text[6:])}")
|
||||
rcon_cmd_handle(text[6:], from_id, peer_id)
|
||||
elif text.startswith(".wl "):
|
||||
write(peer_id, f"Ok\n{rcon(f'whitelist add {text[4:]}')}")
|
||||
rcon_cmd_handle(f'whitelist add {text[4:]}', from_id, peer_id)
|
||||
if text == "!online":
|
||||
text = rcon_cmd_handle('list', from_id, peer_id, False).replace("\n", "")
|
||||
now = text[10:11]
|
||||
write(peer_id, f"Сейчас играют {now} человек" + "" if now == "0" else f": {text[43:]}")
|
||||
|
||||
ts = lp.get('ts')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user