Add Pterodactyl cog #19

Merged
cswimr merged 139 commits from pterodactyl into main 2024-03-02 00:07:42 -05:00
3 changed files with 89 additions and 14 deletions
Showing only changes of commit 2fbd8cde9e - Show all commits

View file

@ -13,9 +13,12 @@ def register_config(config_obj: Config) -> None:
current_status='', current_status='',
chat_regex=r"\[(\d{2}:\d{2}:\d{2})\sINFO\]:\s<(\w+)>\s(.*)", chat_regex=r"\[(\d{2}:\d{2}:\d{2})\sINFO\]:\s<(\w+)>\s(.*)",
server_regex=r"^\[\d{2}:\d{2}:\d{2} INFO\]: \[Not Secure\] \[.*\] (.*)", server_regex=r"^\[\d{2}:\d{2}:\d{2} INFO\]: \[Not Secure\] \[.*\] (.*)",
join_regex=r"^\[\d{2}:\d{2}:\d{2} INFO\]: ([^<\n]+) joined the game$",
chat_command='tellraw @a ["",{"text":".$U ","color":".$C"},{"text":" (DISCORD): ","color":"blue"},{"text":".$M","color":"white"}]', chat_command='tellraw @a ["",{"text":".$U ","color":".$C"},{"text":" (DISCORD): ","color":"blue"},{"text":".$M","color":"white"}]',
api_endpoint="minecraft", api_endpoint="minecraft",
chat_channel=None, chat_channel=None,
startup_msg='Server started!', startup_msg='Server started!',
shutdown_msg='Server stopped!', shutdown_msg='Server stopped!',
join_msg='Welcome to the server! 👋',
leave_msg='Goodbye! 👋',
) )

View file

@ -130,18 +130,6 @@ class Pterodactyl(commands.Cog):
await config.chat_channel.set(channel.id) await config.chat_channel.set(channel.id)
await ctx.send(f"Chat channel set to {channel.mention}") await ctx.send(f"Chat channel set to {channel.mention}")
@pterodactyl_config_chat.command(name = "regex")
async def pterodactyl_config_chat_regex(self, ctx: commands.Context, *, regex: str = None) -> None:
"""Set the regex pattern to match chat messages on the server.
See [documentation]() for more information."""
#TODO - fix this link
if regex is None:
regex = await config.chat_regex()
return await ctx.send(f"Chat regex is currently set to:\n{box(regex, 'regex')}")
await config.chat_regex.set(regex)
await ctx.send(f"Chat regex set to:\n{box(regex, 'regex')}")
@pterodactyl_config_chat.command(name = "command") @pterodactyl_config_chat.command(name = "command")
async def pterodactyl_config_chat_command(self, ctx: commands.Context, *, command: str = None) -> None: async def pterodactyl_config_chat_command(self, ctx: commands.Context, *, command: str = None) -> None:
"""Set the command that will be used to send messages from Discord. """Set the command that will be used to send messages from Discord.
@ -155,8 +143,24 @@ class Pterodactyl(commands.Cog):
await config.chat_command.set(command) await config.chat_command.set(command)
await ctx.send(f"Chat command set to:\n{box(command, 'json')}") await ctx.send(f"Chat command set to:\n{box(command, 'json')}")
@pterodactyl_config_chat.command(name = "server") @pterodactyl_config.group(name = "regex")
async def pterodactyl_config_chat_server(self, ctx: commands.Context, *, regex: str = None) -> None: async def pterodactyl_config_regex(self, ctx: commands.Context) -> None:
"""Set regex patterns."""
@pterodactyl_config_regex.command(name = "chat")
async def pterodactyl_config_regex_chat(self, ctx: commands.Context, *, regex: str = None) -> None:
"""Set the regex pattern to match chat messages on the server.
See [documentation]() for more information."""
#TODO - fix this link
if regex is None:
regex = await config.chat_regex()
return await ctx.send(f"Chat regex is currently set to:\n{box(regex, 'regex')}")
await config.chat_regex.set(regex)
await ctx.send(f"Chat regex set to:\n{box(regex, 'regex')}")
@pterodactyl_config_regex.command(name = "server")
async def pterodactyl_config_regex_server(self, ctx: commands.Context, *, regex: str = None) -> None:
"""Set the regex pattern to match server messages on the server. """Set the regex pattern to match server messages on the server.
See [documentation]() for more information.""" See [documentation]() for more information."""
@ -188,3 +192,21 @@ class Pterodactyl(commands.Cog):
return await ctx.send(f"Shutdown message is currently set to: {message}") return await ctx.send(f"Shutdown message is currently set to: {message}")
await config.shutdown_msg.set(message) await config.shutdown_msg.set(message)
await ctx.send(f"Shutdown message set to: {message}") await ctx.send(f"Shutdown message set to: {message}")
@pterodactyl_config_messages.command(name = "join")
async def pterodactyl_config_messages_join(self, ctx: commands.Context, *, message: str = None) -> None:
"""Set the message that will be sent when a user joins the server. This is only shown in embeds."""
if message is None:
message = await config.join_msg()
return await ctx.send(f"Join message is currently set to: {message}")
await config.join_msg.set(message)
await ctx.send(f"Join message set to: {message}")
@pterodactyl_config_messages.command(name = "leave")
async def pterodactyl_config_messages_leave(self, ctx: commands.Context, *, message: str = None) -> None:
"""Set the message that will be sent when a user leaves the server. This is only shown in embeds."""
if message is None:
message = await config.leave_msg()
return await ctx.send(f"Leave message is currently set to: {message}")
await config.leave_msg.set(message)
await ctx.send(f"Leave message set to: {message}")

View file

@ -67,6 +67,24 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
if channel is not None: if channel is not None:
await channel.send(server_message if len(server_message) < 2000 else server_message[:1997] + '...') 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': if json.loads(message)['event'] == 'status':
old_status = await config.current_status() old_status = await config.current_status()
current_status = json.loads(message)['args'][0] 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") logger.debug("Message is not a chat message")
return False 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]: async def get_info(username: str) -> Optional[dict]:
logger.debug("Retrieving player info for %s", username) logger.debug("Retrieving player info for %s", username)
endpoint = await config.api_endpoint() 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") logger.debug("Chat message sent to Discord")
else: else:
logger.debug("Chat channel not set. Skipping sending chat message to Discord") 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