Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Palworld + opengsq Update #131

Merged
merged 5 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions discordgsm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,13 @@ def query_server_modal(game: GamedigGame, locale: Locale):
modal.remove_item(query_param['port'])
query_param['port']._value = '0'
elif game['id'] == 'teamspeak3':
query_extra['voice_port'] = TextInput(label='Voice Port', placeholder='Voice port', default=9987)
query_extra['voice_port'] = TextInput(label='Voice Port', placeholder='Voice port', default='9987')
modal.add_item(query_extra['voice_port'])
elif game['id'] == 'palworld':
query_extra['api_port'] = TextInput(label='REST API Port', default='8213')
query_extra['admin_password'] = TextInput(label='Admin Password', placeholder='admin')
modal.add_item(query_extra['api_port'])
modal.add_item(query_extra['admin_password'])
elif game['id'] == 'tmnf':
query_extra['username'] = TextInput(label='Username', placeholder='Query Username', default="User")
query_extra['password'] = TextInput(label='Password', placeholder='Query Password', default="User")
Expand Down Expand Up @@ -305,7 +310,6 @@ async def modal_on_submit(interaction: Interaction):
return
except database.ServerNotFoundError:
pass

# Query the server
try:
result = await gamedig.run({**{'type': game_id}, **params})
Expand Down
43 changes: 21 additions & 22 deletions discordgsm/protocols/palworld.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
import os
import time
from typing import TYPE_CHECKING

import aiohttp
from opengsq.protocol_socket import Socket
import opengsq

from discordgsm.protocols.protocol import Protocol

if TYPE_CHECKING:
from discordgsm.gamedig import GamedigResult


class Palworld(Protocol):
name = "palworld"

async def query(self):
host, port = str(self.kv["host"]), int(str(self.kv["port"]))
ip = await Socket.gethostbyname(host)

base_url = os.getenv('OPENGSQ_MASTER_SERVER_URL', 'https://master-server.opengsq.com/').rstrip('/')
url = f"{base_url}/palworld/search?host={ip}&port={port}"
host, port, api_port, admin_password = (
str(self.kv["host"]),
int(str(self.kv["port"])),
int(str(self.kv["api_port"])),
str(self.kv["admin_password"]),
)
palworld = opengsq.Palworld(host, api_port, "admin", admin_password, self.timeout)
start = time.time()

async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response.raise_for_status()
data: dict = await response.json()
ping = int((time.time() - start) * 1000)

data = await palworld.get_status()
ping = int((time.time() - start) * 1000)
if data.server_name:
status = "online"
else:
status = "offline"
result: GamedigResult = {
"name": data.get("name", ""),
"map": data.get("map_name", ""),
"password": data.get("is_password", False),
"numplayers": data.get("current_players", 0),
"status": status,
"name": data.server_name,
"map": None,
"password": False,
"numplayers": data.num_players,
"numbots": 0,
"maxplayers": data.get("max_players", 0),
"maxplayers": data.max_players,
"players": None,
"bots": None,
"connect": f"{host}:{port}",
"ping": ping,
"raw": data,
"raw": data.__dict__,
}

return result
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ backports.zoneinfo==0.2.1;python_version<"3.9"
discord.py==2.3.2
flask[async]==3.0.3
gunicorn==22.0.0
opengsq==3.1.0
opengsq==3.2.0
psycopg2-binary==2.9.9
pymongo==4.7.3
python-dotenv==1.0.1
Expand Down