Add Pterodactyl cog #19
1 changed files with 9 additions and 0 deletions
|
@ -111,22 +111,29 @@ class Pterodactyl(commands.Cog):
|
||||||
return ansi_escape.sub('', text)
|
return ansi_escape.sub('', text)
|
||||||
|
|
||||||
def check_if_chat_message(self, text: str) -> Union[bool, dict]:
|
def check_if_chat_message(self, text: str) -> Union[bool, dict]:
|
||||||
|
self.logger.debug("Checking if message is a chat message")
|
||||||
regex = self.config.chat_regex()
|
regex = self.config.chat_regex()
|
||||||
match: Optional[re.Match[str]] = re.match(regex, text)
|
match: Optional[re.Match[str]] = re.match(regex, text)
|
||||||
if match:
|
if match:
|
||||||
|
self.logger.debug("Message is a chat message")
|
||||||
return {"time": match.group(1), "username": match.group(2), "message": match.group(3)}
|
return {"time": match.group(1), "username": match.group(2), "message": match.group(3)}
|
||||||
|
self.logger.debug("Message is not a chat message")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def get_info(self, username: str) -> Optional[dict]:
|
async def get_info(self, username: str) -> Optional[dict]:
|
||||||
|
self.logger.debug("Retrieving player info for %s", username)
|
||||||
endpoint = await self.config.endpoint()
|
endpoint = await self.config.endpoint()
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(f"https://playerdb.co/api/player/{endpoint}/{username}") as response:
|
async with session.get(f"https://playerdb.co/api/player/{endpoint}/{username}") as response:
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
|
self.logger.debug("Player info retrieved for %s", username)
|
||||||
return await response.json()
|
return await response.json()
|
||||||
else:
|
else:
|
||||||
|
self.logger.error("Failed to retrieve player info for %s: %s", username, response.status)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def send_chat_discord(self, username: str, message: str, avatar_url: str) -> None:
|
async def send_chat_discord(self, username: str, message: str, avatar_url: str) -> None:
|
||||||
|
self.logger.debug("Sending chat message to Discord")
|
||||||
channel = self.bot.get_channel(await self.config.chat_channel())
|
channel = self.bot.get_channel(await self.config.chat_channel())
|
||||||
if channel is not None:
|
if channel is not None:
|
||||||
webhooks = await channel.webhooks()
|
webhooks = await channel.webhooks()
|
||||||
|
@ -134,6 +141,8 @@ class Pterodactyl(commands.Cog):
|
||||||
if webhook is None:
|
if webhook is None:
|
||||||
webhook = await channel.create_webhook(name="Pterodactyl Chat")
|
webhook = await channel.create_webhook(name="Pterodactyl Chat")
|
||||||
await webhook.send(content=message, username=username, avatar_url=avatar_url)
|
await webhook.send(content=message, username=username, avatar_url=avatar_url)
|
||||||
|
self.logger.debug("Chat message sent to Discord")
|
||||||
|
self.logger.debug("Chat channel not set. Skipping sending chat message to Discord")
|
||||||
|
|
||||||
def get_task(self):
|
def get_task(self):
|
||||||
return self.bot.loop.create_task(self.establish_websocket_connection(), name="Pterodactyl Websocket Connection")
|
return self.bot.loop.create_task(self.establish_websocket_connection(), name="Pterodactyl Websocket Connection")
|
||||||
|
|
Loading…
Reference in a new issue