FEAT: Started working on Knucklebones
FIX: Corrected an issue with the database and XP (it was creating an extra table) REF: Moved Destiny2 module to the social modules (makes sense since it's not fully interactable)
This commit is contained in:
@ -24,37 +24,57 @@ class XP:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS xp (
|
||||
CREATE TABLE IF NOT EXISTS guild_xp (
|
||||
guild_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
xp INTEGER NOT NULL,
|
||||
level INTEGER NOT NULL,
|
||||
PRIMARY KEY (guild_id, user_id)
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
self.logger.info('XP table ensured in database')
|
||||
self.logger.info('guild_xp table ensured in database')
|
||||
|
||||
async def add_xp(self, guild_id, user_id, xp):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
INSERT INTO xp (guild_id, user_id, xp)
|
||||
VALUES (?, ?, ?)
|
||||
INSERT INTO guild_xp (guild_id, user_id, xp, level)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(guild_id, user_id)
|
||||
DO UPDATE SET xp = xp + excluded.xp
|
||||
""", (guild_id, user_id, xp))
|
||||
""", (guild_id, user_id, xp, 0))
|
||||
cursor.execute("SELECT xp, level FROM guild_xp WHERE guild_id = ? AND user_id = ?", (guild_id, user_id))
|
||||
row = cursor.fetchone()
|
||||
conn.commit()
|
||||
conn.close()
|
||||
self.logger.debug(f'Added {xp} XP to user {user_id} in guild {guild_id}')
|
||||
|
||||
if row:
|
||||
total_xp = row[0]
|
||||
level = row[1]
|
||||
new_level = self.calculate_level(total_xp)
|
||||
if new_level > level:
|
||||
await self.update_level(guild_id, user_id, new_level)
|
||||
|
||||
async def update_level(self, guild_id, user_id, level):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("UPDATE guild_xp SET level = ? WHERE guild_id = ? AND user_id = ?", (level, guild_id, user_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
self.logger.info(f'User {user_id} in guild {guild_id} leveled up to {level}')
|
||||
|
||||
def calculate_level(self, xp):
|
||||
return int(xp ** 0.5) # Simple leveling formula, can be adjusted
|
||||
|
||||
async def get_xp(self, guild_id, user_id):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT xp FROM xp WHERE guild_id = ? AND user_id = ?", (guild_id, user_id))
|
||||
cursor.execute("SELECT xp, level FROM guild_xp WHERE guild_id = ? AND user_id = ?", (guild_id, user_id))
|
||||
row = cursor.fetchone()
|
||||
conn.close()
|
||||
return row[0] if row else 0
|
||||
return row if row else (0, 0)
|
||||
|
||||
async def handle_message(self, message):
|
||||
self.logger.debug(f'Received message from user {message.author.id} in guild {message.guild.id}')
|
||||
@ -76,12 +96,12 @@ class XP:
|
||||
self.logger.info(f'Added {xp} XP to user {user_id} in guild {guild_id}')
|
||||
|
||||
def setup(self, tree: discord.app_commands.CommandTree):
|
||||
@tree.command(name="check_xp", description="Check your XP")
|
||||
@tree.command(name="check_xp", description="Check your XP and level")
|
||||
async def check_xp_command(interaction: discord.Interaction):
|
||||
user_id = str(interaction.user.id)
|
||||
guild_id = str(interaction.guild.id)
|
||||
xp = await self.get_xp(guild_id, user_id)
|
||||
await interaction.response.send_message(embed=discord.Embed(description=f"You have {xp} XP.", color=discord.Color.green()))
|
||||
xp, level = await self.get_xp(guild_id, user_id)
|
||||
await interaction.response.send_message(embed=discord.Embed(description=f"You have {xp} XP and are at level {level}.", color=discord.Color.green()))
|
||||
|
||||
if not tree.get_command("check_xp"):
|
||||
tree.add_command(check_xp_command)
|
||||
@ -95,3 +115,4 @@ def setup(bot):
|
||||
xp = XP(bot)
|
||||
xp.setup(bot.tree)
|
||||
bot.loop.create_task(xp.setup_hook())
|
||||
bot.xp_module = xp
|
||||
|
Reference in New Issue
Block a user