Guest User

Untitled

a guest
Mar 4th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.02 KB | None | 0 0
  1. import os
  2. import discord
  3. from discord.ext import commands
  4. from dotenv import load_dotenv
  5. import aiosqlite
  6. import asyncio
  7. import random
  8. from easy_pil import *
  9. import math
  10.  
  11. intents = discord.Intents.all()
  12. bot = commands.Bot (command_prefix=".", help_command=None, intents=intents)
  13.  
  14. load_dotenv()
  15. TOKEN = os.getenv("TOKEN")
  16.  
  17. @bot.event
  18. async def on_ready():
  19.         print("C'est ok!")
  20.         print("Le bot est connecté au serveur discord")
  21.         setattr(bot, "db", await aiosqlite.connect("level.db"))
  22.         await asyncio.sleep(3)
  23.         async with bot.db.cursor() as cursor:
  24.             await cursor.execute("CREATE TABLE IF NOT EXISTS levels (level INTEGER, xp INTEGER, user INTEGER, guild INTEGER)")
  25.         await bot.tree.sync()
  26.  
  27.  
  28. @bot.listen()
  29. async def on_message(message):
  30.     if message.author.bot:
  31.         return
  32.     author = message.author
  33.     guild = message.guild
  34.     async with bot.db.cursor() as cursor:
  35.         await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (author.id, guild.id,))
  36.         xp = await cursor.fetchone()
  37.         await cursor.execute("SELECT level FROM levels WHERE user = ? AND guild = ?", (author.id, guild.id,))
  38.         level = await cursor.fetchone()
  39.  
  40.         if not xp or not level:
  41.             await cursor.execute ("INSERT INTO levels (level, xp, user, guild) VALUES (?, ?, ?, ?)", (1, 0, author.id, guild.id,))
  42.             await bot.db.commit()
  43.  
  44.         try:
  45.             xp = xp[0]
  46.             level = level[0]
  47.         except TypeError:
  48.             xp= 0
  49.             level= 1
  50.         if level < 4:
  51.             xp += random.randint(1, 5)
  52.             await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (xp, author.id, guild.id,))
  53.         else:
  54.             rand = random.randint(1, (level//4))
  55.             if rand == 1:
  56.                 xp +=random.randint(5, 10)
  57.                 await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (xp, author.id, guild.id,))
  58.        
  59.         if xp >= math.ceil((5*(level ** 3))/2):
  60.            level +=1
  61.            await cursor.execute ("UPDATE levels SET level = ? WHERE user = ? AND guild = ?", (level, author.id, guild.id,))
  62.            await cursor.execute ("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (0, author.id, guild.id,))
  63.            
  64.            userData = {
  65.            "name": f"{message.author.name}",
  66.            "xp": xp,
  67.            "level": level,
  68.            "next_level_up": math.ceil((5*(level ** 3))/2),
  69.            "percentage": xp/math.ceil((5*(level ** 3))/2)*100
  70.            }
  71.            background = Editor(Canvas((900, 300), color="#242424"))
  72.            profile_picture = await load_image_async(str(message.author.avatar.url))
  73.            profile = Editor(profile_picture).resize((150, 150)).rounded_corners(radius=20)
  74.  
  75.            card_right_shape = [(640, 0), (790, 300), (900, 300), (900, 0)]
  76.            card_right_shape_2 = [(650, 0), (800, 300), (900, 300), (900, 0)]
  77.            background.polygon(card_right_shape, color="#d5ca3a").blur("gussian", amount=15)
  78.            background.polygon(card_right_shape_2, color="#181818")
  79.            
  80.            background.paste(profile, (30, 30))
  81.            background.text((200, 40), userData["name"], Font.montserrat(size=38), color="#FFFFFF")
  82.            background.rectangle((200, 80), width=420, height=2, fill="#FFFFFF")
  83.            background.text(
  84.            (200, 110),
  85.            f"Niveau supérieur!",
  86.            font=Font.montserrat(size=40),
  87.            color="#FFFFFF",)
  88.  
  89.            background.text(
  90.            (40, 210),
  91.            f"Félicitation {userData['name']}.",
  92.            font=Font.montserrat(size=28),
  93.            color="#FFFFFF",)
  94.  
  95.            background.text(
  96.            (40, 250),
  97.            f"Tu viens de passer au niveau {userData['level']}.",
  98.            font=Font.montserrat(size=28),
  99.            color="#FFFFFF",)
  100.  
  101.            file = discord.File(fp=background.image_bytes, filename="levelcard.png")
  102.            channel = bot.get_channel(1076827590930731018)
  103.            await channel.send(f"Hé {message.author.mention} \U0001F447")
  104.            await channel.send(file=file)
  105.  
  106.     await bot.db.commit()
  107.  
  108.  
  109.  
  110. # --------------------
  111. # Commande Level
  112. # --------------------
  113.  
  114.  
  115. @bot.hybrid_command(name="level", description="afficher son niveau et son XP actuel")
  116. async def level(ctx, member: discord.Member=None):
  117.     if member is None:
  118.         member=ctx.author
  119.     async with bot.db.cursor() as cursor:
  120.         await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (member.id, ctx.guild.id,))
  121.         xp = await cursor.fetchone()
  122.         await cursor.execute("SELECT level FROM levels WHERE user = ? AND guild = ?", (member.id, ctx.guild.id,))
  123.         level = await cursor.fetchone()
  124.  
  125.     if not xp or not level:
  126.         await cursor.execute ("INSERT INTO levels (level, xp, user, guild) VALUES (?, ?, ?, ?)", (1, 0, member.id, ctx.guild.id,))
  127.         await bot.commit()
  128.  
  129.     try:
  130.         xp = xp[0]
  131.         level = level [0]
  132.     except TypeError:
  133.         xp= 0
  134.         level= 1
  135.  
  136. # -------
  137. # design
  138. # -------
  139.  
  140.     userData = {
  141.         "name": member.name,
  142.         "discriminator": member.discriminator,
  143.         "xp": xp,
  144.         "level": level,
  145.         "next_level_up": math.ceil((5*(level ** 3))/2),
  146.         "percentage": xp/math.ceil((5*(level ** 3))/2)*100
  147.         }
  148.  
  149.     background = Editor(Canvas((900, 300), color="#242424"))
  150.     profile_picture = await load_image_async(str(member.avatar.url))
  151.     profile = Editor(profile_picture).resize((150, 150)).rounded_corners(radius=20)
  152.  
  153.     card_right_shape = [(640, 0), (790, 300), (900, 300), (900, 0)]
  154.     card_right_shape_2 = [(650, 0), (800, 300), (900, 300), (900, 0)]
  155.     background.polygon(card_right_shape, color="#3a7fd5").blur("gussian", amount=15)
  156.     background.polygon(card_right_shape_2, color="#181818")
  157.    
  158.     background.paste(profile, (30, 30))
  159.     background.rectangle((30, 220), width=650, height=60, color="#181818", radius=20)
  160.     background.bar((30, 220), max_width=650, height=60,percentage=userData["percentage"],color="#3a7fd5", radius=20,outline="#181818", stroke_width=8)
  161.     background.text((200, 40), userData["name"], Font.montserrat(size=38), color="#FFFFFF")
  162.     background.text(
  163.         (730, 40),
  164.         f"# {userData['discriminator']}",
  165.         font=Font.montserrat(size=40),
  166.         color="#FFFFFF",
  167.     )
  168.    
  169.     background.rectangle((200, 80), width=420, height=2, fill="#FFFFFF")
  170.     background.text(
  171.         (200, 110),
  172.         f"Niveau: {userData['level']}",
  173.         font=Font.montserrat(size=28),
  174.         color="#FFFFFF",
  175.     )
  176.     background.text(
  177.         (200, 150),
  178.         f"XP: {userData['xp']} / {userData['next_level_up']}",
  179.         font=Font.montserrat(size=28),
  180.         color="#FFFFFF",
  181.     )
  182.  
  183.     file = discord.File(fp=background.image_bytes, filename="levelcard.png")
  184.     channel = bot.get_channel(1076827590930731018)
  185.     await channel.send(f"Hé {ctx.author.mention} \U0001F447")
  186.     await channel.send(file=file)
  187.  
  188. bot.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment