Switched to berconpy, implemented listener

This commit is contained in:
2024-06-13 00:07:40 +02:00
parent 7bada37794
commit 1b897a70a9
7 changed files with 205 additions and 364 deletions

51
message_handler.py Normal file
View File

@@ -0,0 +1,51 @@
"""Listens to an RCON server for messages."""
import asyncio
import logging
import math
import berconpy as rcon
IP = "217.72.204.88"
PORT = 2313
PASSWORD = "u1BSlMM4LH0"
log = logging.getLogger("berconpy")
log.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
log.addHandler(handler)
client = rcon.AsyncRCONClient()
@client.dispatch.on_login
async def on_login():
print("on_login")
@client.dispatch.on_message
async def on_message(message: str):
print("on_message:", message)
@client.dispatch.on_command
async def server_response_to_command(response: str):
# this event also includes keep alive commands we send to the server;
# for handling commands, reading the return value of
# `await client.send_command()` is the recommended method
if not response:
return print("on_command: <empty>")
print("on_command:", response)
# Other events are documented in AsyncRCONClient
async def main():
async with client.connect(IP, PORT, PASSWORD):
await asyncio.sleep(math.inf)
if __name__ == "__main__":
asyncio.run(main())