FEAT: Added XP System, need to test and adjust it

REF: Adjusted the Twitch Module to be on a 5 minute cooldown
DOC: Adjusted related code for the new XP system in db and config
This commit is contained in:
Dan 2024-06-21 22:14:26 -04:00
parent cc8a1ea630
commit 4d9214e378
4 changed files with 92 additions and 1 deletions

View File

@ -19,6 +19,7 @@ YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
ENABLED_MODULES = [ ENABLED_MODULES = [
"modules.media.spotify_module", "modules.media.spotify_module",
"modules.user.birthday_module", "modules.user.birthday_module",
"modules.user.xp_module",
"modules.money.currency_module", "modules.money.currency_module",
"modules.social.twitch_module", "modules.social.twitch_module",
"modules.social.youtube_module", "modules.social.youtube_module",

View File

@ -41,6 +41,13 @@ def initialize_db():
last_video_id TEXT last_video_id TEXT
) )
''') ''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS user_xp (
user_id INTEGER PRIMARY KEY,
xp INTEGER,
level INTEGER
)
''')
conn.commit() conn.commit()
conn.close() conn.close()

View File

@ -75,7 +75,7 @@ class TwitchModule:
logger.error(f"Error checking live status for {twitch_name}: {e}", exc_info=True) logger.error(f"Error checking live status for {twitch_name}: {e}", exc_info=True)
conn.close() conn.close()
await asyncio.sleep(120) # Check every 2 minutes for testing purposes await asyncio.sleep(300) # Check every 5 minutes
def add_commands(self): def add_commands(self):
@app_commands.command( @app_commands.command(

83
modules/user/xp_module.py Normal file
View File

@ -0,0 +1,83 @@
import discord
from discord import app_commands
import logging
from modules.data.db import get_connection
from datetime import datetime, timedelta
logger = logging.getLogger(__name__)
class XPModule:
def __init__(self, bot):
self.bot = bot
self.cooldown = {} # Dictionary to store cooldowns
self.add_commands()
async def on_message(self, message):
if message.author.bot:
return
user_id = message.author.id
now = datetime.now()
if user_id in self.cooldown:
if now < self.cooldown[user_id]:
return # User is on cooldown
self.give_xp(user_id, 10) # Give 10 XP for a message
self.cooldown[user_id] = now + timedelta(seconds=60) # 1 minute cooldown
def give_xp(self, user_id, xp):
conn = get_connection()
c = conn.cursor()
c.execute('SELECT xp, level FROM user_xp WHERE user_id = ?', (user_id,))
row = c.fetchone()
if row:
current_xp, current_level = row
new_xp = current_xp + xp
new_level = current_level
# Level up logic
if new_xp >= self.xp_for_next_level(current_level):
new_level += 1
new_xp = new_xp - self.xp_for_next_level(current_level)
logger.info(f"User {user_id} leveled up to {new_level}")
c.execute('UPDATE user_xp SET xp = ?, level = ? WHERE user_id = ?', (new_xp, new_level, user_id))
else:
c.execute('INSERT INTO user_xp (user_id, xp, level) VALUES (?, ?, ?)', (user_id, xp, 1))
conn.commit()
conn.close()
def xp_for_next_level(self, level):
return 100 * level # Example leveling curve
def add_commands(self):
@app_commands.command(name='xp', description='Check your XP and level')
async def check_xp(interaction: discord.Interaction):
user_id = interaction.user.id
conn = get_connection()
c = conn.cursor()
c.execute('SELECT xp, level FROM user_xp WHERE user_id = ?', (user_id,))
row = c.fetchone()
conn.close()
if row:
xp, level = row
await interaction.response.send_message(f"You have {xp} XP and are level {level}.")
else:
await interaction.response.send_message("You have no XP yet.")
self.bot.tree.add_command(check_xp)
async def setup(bot):
xp_module = XPModule(bot)
@bot.event
async def on_message(message):
if not message.author.bot: # Ensure the bot doesn't earn XP
await xp_module.on_message(message)
await bot.process_commands(message) # Process commands after the XP check