command to create a card for the user

#2
Files changed (3) hide show
  1. app.py +74 -0
  2. assets/hugg.png +0 -0
  3. requirements.txt +2 -1
app.py CHANGED
@@ -14,6 +14,9 @@ import csv
14
  from tabulate import tabulate
15
  import logging
16
  import time
 
 
 
17
 
18
  import gradio_client
19
  import gradio as gr
@@ -452,6 +455,77 @@ async def send_message(ctx):
452
  # add emojis for some color
453
  # check if members are still in the server
454
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
455
 
456
  """"""
457
  DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
 
14
  from tabulate import tabulate
15
  import logging
16
  import time
17
+ from PIL import Image, ImageDraw ,ImageFont,ImageOps
18
+ import requests
19
+ from io import BytesIO
20
 
21
  import gradio_client
22
  import gradio as gr
 
455
  # add emojis for some color
456
  # check if members are still in the server
457
 
458
+ def create_progress_bar(w = 800,h = 100 ,progress_percentage=0.25):
459
+ progress = Image.new("RGB", (w,h))
460
+ draw = ImageDraw.Draw(progress)
461
+ draw_progress(20, h*0.5, w-20*3, 25, progress_percentage,draw)
462
+ return progress
463
+ def draw_progress(x, y, width, height, progress_percentage,draw, fg="#FFD21E", fg2="#6B7280"):
464
+ # Draw the background
465
+ draw.rectangle((x+(height/2), y, x+width+(height/2), y+height), fill=fg2, width=10)
466
+ draw.ellipse((x+width, y, x+height+width, y+height), fill=fg2)
467
+ draw.ellipse((x, y, x+height, y+height), fill=fg2)
468
+ width = int(width*progress_percentage)
469
+ # Draw the part of the progress bar that is actually filled
470
+ draw.rectangle((x+(height/2), y, x+width+(height/2), y+height), fill=fg, width=10)
471
+ draw.ellipse((x+width, y, x+height+width, y+height), fill=fg)
472
+ draw.ellipse((x, y, x+height, y+height), fill=fg)
473
+
474
+ def create_base(w=800,h = 300,username = "username",current_xp=25,next_lvl_xp=100):
475
+ base = Image.new("RGB",(w,h))
476
+ draw = ImageDraw.Draw(base)
477
+ # requires a newer version of pillow
478
+ font = ImageFont.load_default(size=50) # big text
479
+ draw.text((20, 20),username,(255,255,255),font=font)
480
+ font = ImageFont.load_default(size=30)
481
+ xp_msg = f"{current_xp}/{next_lvl_xp} XP"
482
+ pad = font.getlength(xp_msg)
483
+ draw.text((w-30-pad, h-50),xp_msg,(255,255,255),font=font)
484
+ return base
485
+
486
+
487
+ async def process_avatar(url,hugg = True):
488
+ response = requests.get(url)
489
+ img = Image.open(BytesIO(response.content))
490
+ # resize image to a square
491
+ img = ImageOps.fit(img, (189, 189))
492
+
493
+ # create circular mask
494
+ mask = Image.new('L', (189, 189), 0)
495
+ draw = ImageDraw.Draw(mask)
496
+ draw.ellipse((0, 0, 189, 189), fill=255)
497
+ # apply mask to image
498
+ img.putalpha(mask)
499
+ if hugg :
500
+ img2 = Image.open("./assets/hugg.png")
501
+ img.paste(img2,(0,0),img2)
502
+ return img
503
+
504
+
505
+ @bot.command(name='card')
506
+ async def card(ctx):
507
+ username=ctx.author.name
508
+ url = ctx.author.display_avatar.url
509
+ # TODO :
510
+ # get the level of a user
511
+ # get next lvl xp
512
+ current_xp = 25
513
+ next_lvl_xp = 100
514
+ progress_percentage = current_xp/next_lvl_xp
515
+ base = create_base(username=username,current_xp=current_xp,next_lvl_xp=next_lvl_xp)
516
+ progress = create_progress_bar(progress_percentage=progress_percentage)
517
+ avatar = await process_avatar(url)
518
+ final = Image.new("RGB",(base.size[0],base.size[1]+progress.size[1]))
519
+ final.paste(base,(0,0))
520
+ final.paste(progress,(0,base.size[1]))
521
+ x_begin = final.size[0]-189-20
522
+ final.paste(avatar,(x_begin,20,x_begin+189,20+189),avatar)
523
+ with BytesIO() as image_binary:
524
+ final.save(image_binary, 'PNG')
525
+ image_binary.seek(0)
526
+ await ctx.send(file=discord.File(fp=image_binary, filename=f'{username}.png'))
527
+
528
+
529
 
530
  """"""
531
  DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
assets/hugg.png ADDED
requirements.txt CHANGED
@@ -2,4 +2,5 @@ discord.py
2
  gradio
3
  gspread==5.12.0
4
  huggingface_hub
5
- tabulate
 
 
2
  gradio
3
  gspread==5.12.0
4
  huggingface_hub
5
+ tabulate
6
+ pillow