Skip to content

Commit

Permalink
Revamped Bedwars stats command to use PIL lib. Also fixed description…
Browse files Browse the repository at this point in the history
… to player_skin
  • Loading branch information
KevinTrinh1227 committed Oct 22, 2023
1 parent 2549298 commit e176e1c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
Binary file added assets/backgrounds/bedwars_stats.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/outputs/bedwars_stats.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 56 additions & 1 deletion commands/bedwars_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,38 @@
import json
import os
from dotenv import load_dotenv
from PIL import Image, ImageFont, ImageDraw
from io import BytesIO
import requests


# Open the JSON file and read in the data
with open('config.json') as json_file:
data = json.load(json_file)

embed_color = int(data["general"]["embed_color"].strip("#"), 16) #convert hex color to hexadecimal format

font_title = ImageFont.truetype("./assets/fonts/Minecraft.ttf", 17)
font_stat = ImageFont.truetype("./assets/fonts/Minecraft.ttf", 16)

class bedwarsstats(commands.Cog):
def __init__(self, client):
self.client = client


#bedwars stats command
@commands.hybrid_command(aliases=["bedwarstats", "bwstat", "bws"], brief="bws [Minecraft User Name]", description="View a players Bedwars Stats", with_app_command=True)
async def bedwars(self, ctx, *, username):

def center(x, text, font):
text = str(text)
try:
_, _, text_width, _ = ImageDraw.Draw(Image.new("RGB", (1, 1))).textbbox((0, 0), text, font=font)
centered_x = x - (text_width / 2)
return centered_x
except Exception as e:
print(f"Error in center function: {e}")
return x

try:#if player exist it will work

#load in .env variables
Expand Down Expand Up @@ -61,6 +76,45 @@ async def bedwars(self, ctx, *, username):
#other statistics
bedwars_level = hydata["player"]["achievements"]["bedwars_level"] #bedwars level


background_image = Image.open("./assets/backgrounds/bedwars_stats.png")
front_response = requests.get(f"https://visage.surgeplay.com/full/300/{uuid}.png?y=-50")
front_skin = Image.open(BytesIO(front_response.content))


# Paste the downloaded image onto the background
background_image.paste(front_skin, (437, 38), front_skin)

draw = ImageDraw.Draw(background_image)
draw.text((20,18), f"{username}'s Bedwars Stats", (255, 255, 255), font=font_title)
draw.text((20,307), f"{ctx.guild.name} | Powered by: Hycord.net", (255, 255, 255), font=font_title)
draw.text((center(525, f"Lvl. {bedwars_level}", font_stat),13), f"Lvl. {bedwars_level}", (255, 255, 255), font=font_stat)

# wins row
draw.text((center(70, bedwars_wins, font_stat), 85), f"{bedwars_wins}", (255, 255, 255), font=font_stat)
draw.text((center(222, bedwars_losses, font_stat), 85), f"{bedwars_losses}", (255, 255, 255), font=font_stat)
draw.text((center(360, bedwars_wlr, font_stat), 85), f"{bedwars_wlr}", (255, 255, 255), font=font_stat)

# finals row
draw.text((center(70, bedwars_final_kills, font_stat), 142), f"{bedwars_final_kills}", (255, 255, 255), font=font_stat)
draw.text((center(222, bedwars_final_deaths, font_stat), 142), f"{bedwars_final_deaths}", (255, 255, 255), font=font_stat)
draw.text((center(360, bedwars_fkdr, font_stat), 142), f"{bedwars_fkdr}", (255, 255, 255), font=font_stat)

# beds stats row
draw.text((center(70, bedwars_beds_broken, font_stat), 203), f"{bedwars_beds_broken}", (255, 255, 255), font=font_stat)
draw.text((center(222, bedwars_beds_lost, font_stat), 203), f"{bedwars_beds_lost}", (255, 255, 255), font=font_stat)
draw.text((center(360, bedwars_bed_bblr, font_stat), 203), f"{bedwars_bed_bblr}", (255, 255, 255), font=font_stat)

# normal kills
draw.text((center(70, bedwars_kills, font_stat), 262), f"{bedwars_kills}", (255, 255, 255), font=font_stat)
draw.text((center(222, bedwars_deaths, font_stat), 262), f"{bedwars_deaths}", (255, 255, 255), font=font_stat)
draw.text((center(360, bedwars_kdr, font_stat), 262), f"{bedwars_kdr}", (255, 255, 255), font=font_stat)

background_image.save("./assets/outputs/bedwars_stats.png") # save the img

await ctx.send(file=discord.File("./assets/outputs/bedwars_stats.png"))

"""
#player embed
embed = discord.Embed(
title = f"**[{bedwars_level}✫] {username}'s Bedwars Stats**",
Expand All @@ -86,6 +140,7 @@ async def bedwars(self, ctx, *, username):
embed.set_footer(text = f"Requested by {ctx.author}", icon_url=ctx.author.avatar.url)
await ctx.send(embed=embed)
"""
except: #if player does not exist
embed = discord.Embed(
title = f"User Does Not Exist",
Expand Down
2 changes: 1 addition & 1 deletion commands/player_skin.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def skin(self, ctx, *, username):


# player skin download feature
@commands.hybrid_command(aliases=["downloadskin"], brief="skindownload [Minecraft IGN]", description="Downloads a specific player's skin", with_app_command=True)
@commands.hybrid_command(aliases=["downloadskin"], brief="skindownload [Minecraft IGN]", description="Gets a specific player's skin download link", with_app_command=True)
@commands.cooldown(1, 10, commands.BucketType.user) # 1 use for every 10 seconds.
async def skindownload(self, ctx, *, username):

Expand Down

0 comments on commit e176e1c

Please sign in to comment.