feat(pterodactyl): added user join/leave
Some checks failed
Actions / Lint Code (Ruff & Pylint) (pull_request) Failing after 20s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 22s

This commit is contained in:
SeaswimmerTheFsh 2024-03-01 00:23:00 -05:00
parent 7a39c9a75d
commit 2fbd8cde9e
Signed by: cswimr
GPG key ID: B8953EC01E5C4063
3 changed files with 89 additions and 14 deletions

View file

@ -67,6 +67,24 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
if channel is not None:
await channel.send(server_message if len(server_message) < 2000 else server_message[:1997] + '...')
join_message = await check_if_join_message(content)
if join_message:
channel = coginstance.bot.get_channel(await config.chat_channel())
if channel is not None:
if coginstance.bot.embed_requested(channel):
await channel.send(embed=await generate_join_leave_embed(join_message, True))
else:
await channel.send(f"{join_message} joined the game")
leave_message = await check_if_leave_message(content)
if leave_message:
channel = coginstance.bot.get_channel(await config.chat_channel())
if channel is not None:
if coginstance.bot.embed_requested(channel):
await channel.send(embed=await generate_join_leave_embed(leave_message, False))
else:
await channel.send(f"{leave_message} left the game")
if json.loads(message)['event'] == 'status':
old_status = await config.current_status()
current_status = json.loads(message)['args'][0]
@ -134,6 +152,26 @@ async def check_if_chat_message(text: str) -> Union[bool, dict]:
logger.debug("Message is not a chat message")
return False
async def check_if_join_message(text: str) -> Union[bool, str]:
logger.debug("Checking if message is a join message")
regex = await config.join_regex()
match: Optional[re.Match[str]] = re.match(regex, text)
if match:
logger.debug("Message is a join message")
return match.group(1)
logger.debug("Message is not a join message")
return False
async def check_if_leave_message(text: str) -> Union[bool, str]:
logger.debug("Checking if message is a leave message")
regex = await config.leave_regex()
match: Optional[re.Match[str]] = re.match(regex, text)
if match:
logger.debug("Message is a leave message")
return match.group(1)
logger.debug("Message is not a leave message")
return False
async def get_info(username: str) -> Optional[dict]:
logger.debug("Retrieving player info for %s", username)
endpoint = await config.api_endpoint()
@ -157,3 +195,15 @@ async def send_chat_discord(coginstance: Pterodactyl, username: str, message: st
logger.debug("Chat message sent to Discord")
else:
logger.debug("Chat channel not set. Skipping sending chat message to Discord")
async def generate_join_leave_embed(username: str, join: bool) -> discord.Embed:
embed = discord.Embed()
embed.color = discord.Color.green if join else discord.Color.red
embed.description = await config.join_msg() if join else await config.leave_msg()
info = await get_info(username)
if info:
embed.set_author(name=username, icon_url=info['data']['player']['avatar'])
else:
embed.set_author(name=username, icon_url='https://seafsh.cc/u/j3AzqQ.png')
embed.timestamp = discord.utils.utcnow()
return embed