Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import discord
- from discord.ext import commands
- from dotenv import load_dotenv
- import aiosqlite
- import asyncio
- import random
- from easy_pil import *
- import math
- intents = discord.Intents.all()
- bot = commands.Bot (command_prefix=".", help_command=None, intents=intents)
- load_dotenv()
- TOKEN = os.getenv("TOKEN")
- @bot.event
- async def on_ready():
- print("C'est ok!")
- print("Le bot est connecté au serveur discord")
- setattr(bot, "db", await aiosqlite.connect("level.db"))
- await asyncio.sleep(3)
- async with bot.db.cursor() as cursor:
- await cursor.execute("CREATE TABLE IF NOT EXISTS levels (level INTEGER, xp INTEGER, user INTEGER, guild INTEGER)")
- await bot.tree.sync()
- @bot.listen()
- async def on_message(message):
- if message.author.bot:
- return
- author = message.author
- guild = message.guild
- async with bot.db.cursor() as cursor:
- await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (author.id, guild.id,))
- xp = await cursor.fetchone()
- await cursor.execute("SELECT level FROM levels WHERE user = ? AND guild = ?", (author.id, guild.id,))
- level = await cursor.fetchone()
- if not xp or not level:
- await cursor.execute ("INSERT INTO levels (level, xp, user, guild) VALUES (?, ?, ?, ?)", (1, 0, author.id, guild.id,))
- await bot.db.commit()
- try:
- xp = xp[0]
- level = level[0]
- except TypeError:
- xp= 0
- level= 1
- if level < 4:
- xp += random.randint(1, 5)
- await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (xp, author.id, guild.id,))
- else:
- rand = random.randint(1, (level//4))
- if rand == 1:
- xp +=random.randint(5, 10)
- await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (xp, author.id, guild.id,))
- if xp >= math.ceil((5*(level ** 3))/2):
- level +=1
- await cursor.execute ("UPDATE levels SET level = ? WHERE user = ? AND guild = ?", (level, author.id, guild.id,))
- await cursor.execute ("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (0, author.id, guild.id,))
- userData = {
- "name": f"{message.author.name}",
- "xp": xp,
- "level": level,
- "next_level_up": math.ceil((5*(level ** 3))/2),
- "percentage": xp/math.ceil((5*(level ** 3))/2)*100
- }
- background = Editor(Canvas((900, 300), color="#242424"))
- profile_picture = await load_image_async(str(message.author.avatar.url))
- profile = Editor(profile_picture).resize((150, 150)).rounded_corners(radius=20)
- card_right_shape = [(640, 0), (790, 300), (900, 300), (900, 0)]
- card_right_shape_2 = [(650, 0), (800, 300), (900, 300), (900, 0)]
- background.polygon(card_right_shape, color="#d5ca3a").blur("gussian", amount=15)
- background.polygon(card_right_shape_2, color="#181818")
- background.paste(profile, (30, 30))
- background.text((200, 40), userData["name"], Font.montserrat(size=38), color="#FFFFFF")
- background.rectangle((200, 80), width=420, height=2, fill="#FFFFFF")
- background.text(
- (200, 110),
- f"Niveau supérieur!",
- font=Font.montserrat(size=40),
- color="#FFFFFF",)
- background.text(
- (40, 210),
- f"Félicitation {userData['name']}.",
- font=Font.montserrat(size=28),
- color="#FFFFFF",)
- background.text(
- (40, 250),
- f"Tu viens de passer au niveau {userData['level']}.",
- font=Font.montserrat(size=28),
- color="#FFFFFF",)
- file = discord.File(fp=background.image_bytes, filename="levelcard.png")
- channel = bot.get_channel(1076827590930731018)
- await channel.send(f"Hé {message.author.mention} \U0001F447")
- await channel.send(file=file)
- await bot.db.commit()
- # --------------------
- # Commande Level
- # --------------------
- @bot.hybrid_command(name="level", description="afficher son niveau et son XP actuel")
- async def level(ctx, member: discord.Member=None):
- if member is None:
- member=ctx.author
- async with bot.db.cursor() as cursor:
- await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (member.id, ctx.guild.id,))
- xp = await cursor.fetchone()
- await cursor.execute("SELECT level FROM levels WHERE user = ? AND guild = ?", (member.id, ctx.guild.id,))
- level = await cursor.fetchone()
- if not xp or not level:
- await cursor.execute ("INSERT INTO levels (level, xp, user, guild) VALUES (?, ?, ?, ?)", (1, 0, member.id, ctx.guild.id,))
- await bot.commit()
- try:
- xp = xp[0]
- level = level [0]
- except TypeError:
- xp= 0
- level= 1
- # -------
- # design
- # -------
- userData = {
- "name": member.name,
- "discriminator": member.discriminator,
- "xp": xp,
- "level": level,
- "next_level_up": math.ceil((5*(level ** 3))/2),
- "percentage": xp/math.ceil((5*(level ** 3))/2)*100
- }
- background = Editor(Canvas((900, 300), color="#242424"))
- profile_picture = await load_image_async(str(member.avatar.url))
- profile = Editor(profile_picture).resize((150, 150)).rounded_corners(radius=20)
- card_right_shape = [(640, 0), (790, 300), (900, 300), (900, 0)]
- card_right_shape_2 = [(650, 0), (800, 300), (900, 300), (900, 0)]
- background.polygon(card_right_shape, color="#3a7fd5").blur("gussian", amount=15)
- background.polygon(card_right_shape_2, color="#181818")
- background.paste(profile, (30, 30))
- background.rectangle((30, 220), width=650, height=60, color="#181818", radius=20)
- background.bar((30, 220), max_width=650, height=60,percentage=userData["percentage"],color="#3a7fd5", radius=20,outline="#181818", stroke_width=8)
- background.text((200, 40), userData["name"], Font.montserrat(size=38), color="#FFFFFF")
- background.text(
- (730, 40),
- f"# {userData['discriminator']}",
- font=Font.montserrat(size=40),
- color="#FFFFFF",
- )
- background.rectangle((200, 80), width=420, height=2, fill="#FFFFFF")
- background.text(
- (200, 110),
- f"Niveau: {userData['level']}",
- font=Font.montserrat(size=28),
- color="#FFFFFF",
- )
- background.text(
- (200, 150),
- f"XP: {userData['xp']} / {userData['next_level_up']}",
- font=Font.montserrat(size=28),
- color="#FFFFFF",
- )
- file = discord.File(fp=background.image_bytes, filename="levelcard.png")
- channel = bot.get_channel(1076827590930731018)
- await channel.send(f"Hé {ctx.author.mention} \U0001F447")
- await channel.send(file=file)
- bot.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment