66from discord .ext import commands
77from typing import List
88from server import get_players_online , setup_player
9-
9+ from PIL import Image
10+ from io import BytesIO
11+ import os
1012
1113class McStatus (commands .Cog , name = config .MCSTATUS_COG_NAME ):
1214 """Commands which give info about the server's status"""
1315 def __init__ (self , bot : commands .Bot ):
1416 self ._bot = bot
1517
16-
1718 @staticmethod
18- def _create_online_embed (players : List [str ]) -> discord .Embed :
19+ def _create_embed_player_image (previous_images , next_image ):
20+ image1 = previous_images .convert ("RGBA" )
21+ image2 = next_image .convert ("RGBA" )
22+ gap = 10
23+
24+ (width1 , height1 ) = image1 .size
25+ (width2 , height2 ) = image2 .size
26+
27+ result_width = width1 + gap + width2
28+ result_height = max (height1 , height2 )
29+
30+ # Create a new image with RGBA mode to support transparency
31+ result = Image .new ('RGBA' , (result_width , result_height ), (255 , 255 , 255 , 0 ))
32+ result .paste (im = image1 , box = (0 , 0 ))
33+
34+ # Extract the alpha channel of the second image
35+ image2_alpha = image2 .split ()[- 1 ]
36+ result .paste (im = image2 , box = (width1 + gap , 0 ), mask = image2_alpha )
37+
38+ # Save the merged image as PNG to preserve transparency
39+ return result
40+
41+ def _create_online_embed (self , players : List [tuple [str , Image .Image ]]) -> discord .Embed :
1942 """
2043 Creates an embed for the !online command
2144
2245 :param players: List of players currently online
2346 :return: discord.Embed to be sent by the !online command
2447 """
25- print (players )
48+ file = None
49+
2650 if players :
27- players_formatted = '\n ' .join (players )
51+ script_dir = os .path .dirname (os .path .abspath (__file__ ))
52+ file_path = os .path .join (script_dir , "temp.png" )
53+
54+ usernames = []
55+ for player in players :
56+ usernames .append (player [0 ])
57+
58+ image = players [0 ][1 ]
59+ if len (players ) > 1 :
60+ for player in players [1 :]:
61+ image = self ._create_embed_player_image (image , player [1 ])
62+
63+
64+ image .save (file_path )
65+
66+ players_formatted = '\n ' .join (usernames )
2867 players_formatted = f'```\n { players_formatted } \n ```'
2968
3069 embed = discord .Embed (title = 'The following players are online!' ,
3170 color = discord .Color .green (),
3271 description = players_formatted )
72+ embed .set_footer (text = "This message will disappear when outdated" )
73+
74+ file = discord .File (file_path , filename = "image.png" )
75+ embed .set_image (url = "attachment://image.png" )
76+
77+ os .remove (file_path )
3378 else :
3479 embed = discord .Embed (title = 'No one is online!' ,
3580 color = discord .Color .red ())
3681
37- return embed
38-
82+ return embed , file
3983
4084 @commands .command (name = 'online' , description = config .ONLINE_DESCRIPTION , help = config .ONLINE_HELP )
4185 async def _online (self , ctx : commands .Context ):
@@ -44,16 +88,19 @@ async def _online(self, ctx: commands.Context):
4488
4589 :param ctx: Message context sent by the Discord API
4690 """
47- original_message = await ctx .send ('Loading...' )
91+ loading_message = await ctx .send ('Loading...' )
4892 try :
49- players = get_players_online ()
93+ _loop_cog = self ._bot .get_cog ('LoopedTasks' )
94+ players = _loop_cog .online_player_cache
95+
96+ embed , file = self ._create_online_embed (players )
97+ # await original_message.edit(content = None, embed = embed)
98+ await loading_message .delete ()
99+ original_message = await ctx .send (content = None , embed = embed , file = file )
50100
51- embed = self ._create_online_embed (players )
52- await original_message .edit (content = None , embed = embed )
53101
54- loop_cog = self ._bot .get_cog ('LoopedTasks' )
55- loop_cog .add_message_to_delete (config .DELETION_COOLDOWN , original_message )
56- loop_cog .add_message_to_delete (config .DELETION_COOLDOWN , ctx .message )
102+ _loop_cog .add_message_to_previous (original_message )
103+ _loop_cog .add_message_to_previous (ctx .message )
57104 except Exception as exc :
58105 print (f"ERROR during !online: { exc } " )
59106 await original_message .edit (content = config .ONLINE_ERROR )
0 commit comments