fix(pterodactyl): don't depend on a website to host images when i can bundle image files in the cog itself
Some checks failed
Actions / Build Documentation (MkDocs) (push) Successful in 30s
Actions / Lint Code (Ruff & Pylint) (push) Failing after 54s

This commit is contained in:
Seaswimmer 2024-08-26 17:46:42 -04:00
parent 1c0f12660b
commit 1244d5a941
Signed by: cswimr
GPG key ID: 3813315477F26F82
3 changed files with 26 additions and 10 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

View file

@ -22,7 +22,7 @@ class Pterodactyl(commands.Cog):
__author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"] __author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"]
__git__ = "https://www.coastalcommits.com/cswimr/SeaCogs" __git__ = "https://www.coastalcommits.com/cswimr/SeaCogs"
__version__ = "2.0.1" __version__ = "2.0.2"
__documentation__ = "https://seacogs.coastalcommits.com/pterodactyl/" __documentation__ = "https://seacogs.coastalcommits.com/pterodactyl/"
def __init__(self, bot: Red): def __init__(self, bot: Red):

View file

@ -1,12 +1,14 @@
# pylint: disable=cyclic-import # pylint: disable=cyclic-import
import json import json
import re import re
from typing import Optional, Union from pathlib import Path
from typing import Optional, Tuple, Union
import aiohttp import aiohttp
import discord import discord
import websockets import websockets
from pydactyl import PterodactylClient from pydactyl import PterodactylClient
from redbot.core.data_manager import bundled_data_path
from redbot.core.utils.chat_formatting import bold, pagify from redbot.core.utils.chat_formatting import bold, pagify
from pterodactyl.config import config from pterodactyl.config import config
@ -78,7 +80,12 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
if join_message: if join_message:
if chat_channel is not None: if chat_channel is not None:
if coginstance.bot.embed_requested(chat_channel): if coginstance.bot.embed_requested(chat_channel):
await chat_channel.send(embed=await generate_join_leave_embed(join_message, True)) embed, img = await generate_join_leave_embed(coginstance=coginstance, username=chat_message['username'],join=True)
if img:
with open(img, 'rb') as file:
await chat_channel.send(embed=embed, file=file)
else:
await chat_channel.send(embed=embed)
else: else:
await chat_channel.send(f"{join_message} joined the game", allowed_mentions=discord.AllowedMentions.none()) await chat_channel.send(f"{join_message} joined the game", allowed_mentions=discord.AllowedMentions.none())
@ -86,7 +93,12 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
if leave_message: if leave_message:
if chat_channel is not None: if chat_channel is not None:
if coginstance.bot.embed_requested(chat_channel): if coginstance.bot.embed_requested(chat_channel):
await chat_channel.send(embed=await generate_join_leave_embed(leave_message, False)) embed, img = await generate_join_leave_embed(coginstance=coginstance, username=chat_message['username'],join=False)
if img:
with open(img, 'rb') as file:
await chat_channel.send(embed=embed, file=file)
else:
await chat_channel.send(embed=embed)
else: else:
await chat_channel.send(f"{leave_message} left the game", allowed_mentions=discord.AllowedMentions.none()) await chat_channel.send(f"{leave_message} left the game", allowed_mentions=discord.AllowedMentions.none())
@ -219,29 +231,33 @@ async def send_chat_discord(coginstance: Pterodactyl, username: str, message: st
else: else:
logger.warning("Chat channel not set. Skipping sending chat message to Discord") logger.warning("Chat channel not set. Skipping sending chat message to Discord")
async def generate_join_leave_embed(username: str, join: bool) -> discord.Embed: async def generate_join_leave_embed(coginstance: Pterodactyl, username: str, join: bool) -> Tuple[discord.Embed, Optional[Union[str, Path]]]:
embed = discord.Embed() embed = discord.Embed()
embed.color = discord.Color.green() if join else discord.Color.red() embed.color = discord.Color.green() if join else discord.Color.red()
embed.description = await config.join_msg() if join else await config.leave_msg() embed.description = await config.join_msg() if join else await config.leave_msg()
info = await get_info(username) info = await get_info(username)
if info: if info:
img = None
embed.set_author(name=username, icon_url=info['data']['player']['avatar']) embed.set_author(name=username, icon_url=info['data']['player']['avatar'])
else: else:
embed.set_author(name=username, icon_url='https://seafsh.cc/u/j3AzqQ.png') img = bundled_data_path(coginstance) / "unknown.png"
embed.set_author(name=username, icon_url='attachment://unknown.png')
embed.timestamp = discord.utils.utcnow() embed.timestamp = discord.utils.utcnow()
return embed return embed, img
async def generate_achievement_embed(username: str, achievement: str, challenge: bool) -> discord.Embed: async def generate_achievement_embed(coginstance: Pterodactyl, username: str, achievement: str, challenge: bool) -> Tuple[discord.Embed, Optional[Union[str, Path]]]:
embed = discord.Embed() embed = discord.Embed()
embed.color = discord.Color.from_str('#a800a7') if challenge else discord.Color.from_str('#54fb54') embed.color = discord.Color.from_str('#a800a7') if challenge else discord.Color.from_str('#54fb54')
embed.description = f"{bold(username)} has {'completed the challenge' if challenge else 'made the advancement'} {bold(achievement)}" embed.description = f"{bold(username)} has {'completed the challenge' if challenge else 'made the advancement'} {bold(achievement)}"
info = await get_info(username) info = await get_info(username)
if info: if info:
img = None
embed.set_author(name=username, icon_url=info['data']['player']['avatar']) embed.set_author(name=username, icon_url=info['data']['player']['avatar'])
else: else:
embed.set_author(name=username, icon_url='https://seafsh.cc/u/j3AzqQ.png') img = bundled_data_path(coginstance) / "unknown.png"
embed.set_author(name=username, icon_url='attachment://unknown.png')
embed.timestamp = discord.utils.utcnow() embed.timestamp = discord.utils.utcnow()
return embed return embed, img
def mask_ip(string: str) -> str: def mask_ip(string: str) -> str:
def check(match: re.Match[str]): def check(match: re.Match[str]):