0

There is my code i tried to make a leveling system but the level command wont work unless i take out the on_message part

from nextcord.ext import commands, tasks
import nextcord
import requests
import asyncio
import aiosqlite
from webserver import keep_alive
import random
import datetime
import psutil
from PIL import Image 
from easy_pil import *
from io import BytesIO
from googleapiclient.discovery import build

# key
key = "my key >:("
# key 

def get_prefix(bot, message):
  with open('prefixes.json', 'r') as f:
    prefixes = json.load(f)
  return prefixes[str(message.guild.id)]

bot = commands.Bot(command_prefix=get_prefix, intents=nextcord.Intents.all(), case_insensitive=True)

@bot.event
async def on_ready():
  print(f"Good job. Connected to {bot.user}")
  bot.db = await aiosqlite.connect("level.db")
  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.db.commit()
  print("LEVEL Database up!")



@bot.command(name="level")
async def level(ctx, member: nextcord.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 level or not xp:
      await cursor.execute("INSERT INTO levels (level, xp, user, guild) VALUES (?, ?, ?, ?)", (0, 0, member.id, ctx.guild.id,))

    try:
      xp = xp[0]
      level = level[0]
    except TypeError:
      xp = 0
      level = 0

    user_data = {
      "name": f"{member.name}#{member.discriminator}",
      "xp": xp,
      "level": level,
      "next_level_xp": 100,
      "percentage": xp,
    }
    background = Editor("pic2.jpg")
    profile_picture = await load_image_async(str(member.avatar.url))
    profile = Editor(profile_picture).resize((150,150)).circle_image() 
    
    poppins = Font.poppins(size=40)
    poppins_small = Font.poppins(size=30)
    
    
    background.paste(profile,(30,30))
    
    background.rectangle((30,220), width=650, height=40,color="#FFFFFF")
    background.bar((30,220),max_width=650, height=40,percentage=user_data['percentage'],color="#800080",radius=20,)
    background.text((200,40), user_data['name'],font= poppins ,color="#FFFFFF")

    background.rectangle((200,100), width=350, height=2,fill="#FFFFFF")
    background.text(
      (200,130),
      f"Level - {user_data['level']} | XP - {user_data['xp']}/{user_data['next_level_xp']}",
      font = poppins_small,
      color="#FFFFFF"
    )

    file = nextcord.File(fp=background.image_bytes, filename="levelcard.png")
    await ctx.send(file=file)


@bot.event
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 (?, ?, ?, ?)", (0, 0, author.id, guild.id,))

    try:
      xp = xp[0]
      level = level[0]
    except TypeError:
      xp = 0
      level = 0
      
    if level < 5:
      xp += random.randint(1,6)
      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(1,6)
      await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?",(xp, author.id, guild.id,))
    if xp >=100:
      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,))
      await message.channel.send("gg")
  await bot.db.commit()

bot.run(key)
  • Does this answer your question? [Why does on\_message stop commands from working?](https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working) – TheFungusAmongUs Sep 04 '22 at 20:02

0 Answers0