SHARE
TWEET

Untitled

a guest Oct 21st, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import discord
  2. from discord.ext import commands
  3. import asyncio
  4. import datetime
  5. import sqlite3
  6. import os
  7. import math
  8.  
  9. class LevelingCog(commands.Cog, name = "Leveling"):
  10.     def __init__(self, bot):
  11.         self.bot = bot
  12.  
  13.     @commands.Cog.listener()
  14.     async def on_message(self, message):
  15.         db = sqlite3.connect("leveling.sqlite")
  16.         cursor = db.cursor()
  17.         #grab id of message author
  18.         cursor.execute(f"SELECT user_id FROM levels WHERE guild_id = '{message.author.guild.id}' and user_id = '{message.author.id}'")
  19.         result = cursor.fetchone()
  20.         #if not in db, add them
  21.         if result is None:
  22.             sql = ("INSERT INTO levels(guild_id, user_id, exp, lvl) VALUES(?, ?, ?, ?)")
  23.             val = (message.author.guild.id, message, 2, 0)
  24.             cursor.execute(sql, val)
  25.             db.commit()
  26.         else:              #user_id is index 0, exp is index 1, etc...
  27.             cursor.execute(f"SELECT user_id, exp, lvl FROM levels WHERE guild_id = '{message.author.guild.id}' and user_id = '{message.author.id}'")
  28.             result1 = cursor.fetchone()
  29.             exp = int(result1[1])
  30.             sql = ("UPDATE levels SET exp = ? WHERE guild_id = ? and user_id = ?")
  31.             val = (exp + 2, str(message.guild.id), str(message.author.id))
  32.             cursor.execute(sql, val)
  33.             db.commit()
  34.             #fetch updated results
  35.             cursor.execute(f"SELECT user_id, exp, lvl FROM levels WHERE guild_id = '{message.author.guild.id}' and user_id = '{message.author.id}'")
  36.             result2 = cursor.fetchone()
  37.             #feth initial xp and level
  38.             xp_start = int(result2[1])
  39.             lvl_start = int(result2[2])
  40.             xp_end = math.floor(5 * (lvl_start ** (1/6) + 25 * lvl_start + 100))
  41.             if xp_start > xp_end:
  42.                 await message.channel.send(f"{message.author.mention} has leveled up to level {lvl_start + 1}")
  43.                 sql = ("UPDATE levels SET lvl = ? WHERE guild_id = ? and user_id = ?")
  44.                 val = (lvl_start + 1, str(message.guild.id), str(message.author.id))
  45.                 cursor.execute(sql, val)
  46.                 db.commit()
  47.                 sql = ("UPDATE levels SET exp = ? WHERE guild_id = ? and user_id = ?")
  48.                 val = (0, str(message.guild.id), str(message.author.id))
  49.                 db.commit()
  50.                 cursor.close()
  51.                 db.close()
  52.  
  53.     @commands.command()
  54.     async def rank(self, ctx, user:discord.User=None):
  55.         if user is None:
  56.             db = sqlite3.connect("leveling.sqlite")
  57.             cursor = db.cursor()
  58.             cursor.execute(f"SELECT user_id FROM levels WHERE guild_id = '{ctx.message.author.guild.id}' and user_id = '{ctx.message.author.id}'")
  59.             result = cursor.fetchone()
  60.             if result is None:
  61.                 await ctx.send("That user is not yet ranked")
  62.             else:
  63.                 await ctx.send(f"{user.name} is currently level '{str(result[2])}' and has '{str(result[1])}' XP")
  64.         else:
  65.             db = sqlite3.connect("leveling.sqlite")
  66.             cursor = db.cursor()
  67.             cursor.execute(f"SELECT user_id, exp, lvl FROM levels WHERE guild_id = '{ctx.message.author.guild.id}' and user_id = '{ctx.message.author.id}'")
  68.             result = cursor.fetchone()
  69.             if result is None:
  70.                 await ctx.send("That user is not yet ranked")
  71.             else:
  72.                 await ctx.send(f"{user.name} is currently level '{str(result[2])}' and has '{str(result[1])}' XP")
  73.  
  74.        
  75.  
  76. def setup(bot):
  77.     bot.add_cog(LevelingCog(bot))
  78.     print("Leveling is loaded")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top