2024-06-24 23:20:03 -04:00
|
|
|
import discord
|
|
|
|
import sqlite3
|
2024-06-28 14:01:48 -04:00
|
|
|
import random
|
2024-07-01 22:30:34 -04:00
|
|
|
import logging
|
|
|
|
import asyncio
|
2024-06-24 23:20:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
class XP:
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
self.db_path = 'data/selena.db'
|
2024-07-01 22:30:34 -04:00
|
|
|
self.logger = logging.getLogger('XP')
|
|
|
|
self.logger.setLevel(logging.DEBUG)
|
|
|
|
handler = logging.FileHandler(filename='log/selena.log', encoding='utf-8', mode='w')
|
|
|
|
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s'))
|
|
|
|
self.logger.addHandler(handler)
|
|
|
|
self.cooldown = 10 # Cooldown time in seconds
|
|
|
|
self.xp_range = (5, 15) # Range of XP that can be earned per message
|
|
|
|
self.user_cooldowns = {}
|
2024-06-24 23:20:03 -04:00
|
|
|
|
2024-07-01 22:30:34 -04:00
|
|
|
self.ensure_table_exists()
|
2024-06-28 15:32:37 -04:00
|
|
|
|
2024-07-01 22:30:34 -04:00
|
|
|
def ensure_table_exists(self):
|
2024-06-28 14:01:48 -04:00
|
|
|
conn = sqlite3.connect(self.db_path)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("""
|
2024-07-01 22:30:34 -04:00
|
|
|
CREATE TABLE IF NOT EXISTS xp (
|
|
|
|
guild_id TEXT NOT NULL,
|
|
|
|
user_id TEXT NOT NULL,
|
|
|
|
xp INTEGER NOT NULL,
|
|
|
|
PRIMARY KEY (guild_id, user_id)
|
|
|
|
);
|
|
|
|
""")
|
2024-06-28 14:01:48 -04:00
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
2024-07-01 22:30:34 -04:00
|
|
|
self.logger.info('XP table ensured in database')
|
2024-06-28 14:01:48 -04:00
|
|
|
|
2024-07-01 22:30:34 -04:00
|
|
|
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 (?, ?, ?)
|
|
|
|
ON CONFLICT(guild_id, user_id)
|
|
|
|
DO UPDATE SET xp = xp + excluded.xp
|
|
|
|
""", (guild_id, user_id, xp))
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
self.logger.debug(f'Added {xp} XP to user {user_id} in guild {guild_id}')
|
2024-06-24 23:20:03 -04:00
|
|
|
|
2024-07-01 22:30:34 -04:00
|
|
|
async def get_xp(self, guild_id, user_id):
|
2024-06-28 14:01:48 -04:00
|
|
|
conn = sqlite3.connect(self.db_path)
|
|
|
|
cursor = conn.cursor()
|
2024-07-01 22:30:34 -04:00
|
|
|
cursor.execute("SELECT xp FROM xp WHERE guild_id = ? AND user_id = ?", (guild_id, user_id))
|
|
|
|
row = cursor.fetchone()
|
2024-06-28 14:01:48 -04:00
|
|
|
conn.close()
|
2024-07-01 22:30:34 -04:00
|
|
|
return row[0] if row else 0
|
2024-06-24 23:20:03 -04:00
|
|
|
|
2024-07-01 22:30:34 -04:00
|
|
|
async def handle_message(self, message):
|
|
|
|
self.logger.debug(f'Received message from user {message.author.id} in guild {message.guild.id}')
|
2024-06-24 23:20:03 -04:00
|
|
|
|
2024-07-01 22:30:34 -04:00
|
|
|
if message.author.bot:
|
|
|
|
self.logger.debug('Message author is a bot, ignoring.')
|
|
|
|
return
|
2024-06-24 23:20:03 -04:00
|
|
|
|
2024-07-01 22:30:34 -04:00
|
|
|
guild_id = str(message.guild.id)
|
|
|
|
user_id = str(message.author.id)
|
|
|
|
|
|
|
|
if user_id in self.user_cooldowns and self.user_cooldowns[user_id] > asyncio.get_event_loop().time():
|
|
|
|
self.logger.debug(f'User {user_id} is on cooldown')
|
|
|
|
return
|
2024-06-24 23:20:03 -04:00
|
|
|
|
2024-07-01 22:30:34 -04:00
|
|
|
xp = random.randint(*self.xp_range)
|
|
|
|
await self.add_xp(guild_id, user_id, xp)
|
|
|
|
self.user_cooldowns[user_id] = asyncio.get_event_loop().time() + self.cooldown
|
|
|
|
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")
|
|
|
|
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()))
|
2024-06-24 23:20:03 -04:00
|
|
|
|
2024-06-28 14:01:48 -04:00
|
|
|
if not tree.get_command("check_xp"):
|
|
|
|
tree.add_command(check_xp_command)
|
2024-07-01 22:30:34 -04:00
|
|
|
|
|
|
|
async def setup_hook(self):
|
|
|
|
self.bot.event(self.handle_message)
|
|
|
|
self.logger.info('XP module setup complete and listener added')
|
|
|
|
|
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
xp = XP(bot)
|
|
|
|
xp.setup(bot.tree)
|
|
|
|
bot.loop.create_task(xp.setup_hook())
|