FIX: XP finally works/tracks (won't be earned on the site bot, just development)
This commit is contained in:
parent
4f065d368b
commit
c6daeb425e
28
main.py
28
main.py
@ -8,71 +8,86 @@ TOKEN = config['DISCORD_TOKEN']
|
|||||||
GUILD_ID = config['GUILD_ID']
|
GUILD_ID = config['GUILD_ID']
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
intents.messages = True
|
intents.message_content = True
|
||||||
|
|
||||||
|
|
||||||
class Selena(discord.Client):
|
class Selena(discord.Client):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(intents=intents)
|
super().__init__(intents=intents)
|
||||||
self.tree = discord.app_commands.CommandTree(self)
|
self.tree = discord.app_commands.CommandTree(self)
|
||||||
self.twitch = None
|
self.xp_module = None # Initialize as None
|
||||||
self.load_modules()
|
self.load_modules()
|
||||||
|
|
||||||
async def setup_hook(self):
|
async def setup_hook(self):
|
||||||
logging.info("Setting up modules...")
|
logging.info("Setting up modules...")
|
||||||
self.tree.copy_global_to(guild=discord.Object(id=GUILD_ID))
|
self.tree.copy_global_to(guild=discord.Object(id=GUILD_ID))
|
||||||
await self.tree.sync(guild=discord.Object(id=GUILD_ID))
|
await self.tree.sync(guild=discord.Object(id=GUILD_ID))
|
||||||
|
logging.info("Modules setup and commands synchronized")
|
||||||
|
# Call setup_hook for xp_module here
|
||||||
|
if self.xp_module:
|
||||||
|
await self.xp_module.setup_hook()
|
||||||
|
|
||||||
def load_modules(self):
|
def load_modules(self):
|
||||||
if config['modules']['currency']['enabled']:
|
if config['modules']['currency']['enabled']:
|
||||||
from modules.user.currency import Currency
|
from modules.user.currency import Currency
|
||||||
currency = Currency(self)
|
currency = Currency(self)
|
||||||
currency.setup(self.tree)
|
currency.setup(self.tree)
|
||||||
|
logging.info("Currency module loaded")
|
||||||
|
|
||||||
if config['modules']['xp']['enabled']:
|
if config['modules']['xp']['enabled']:
|
||||||
from modules.user.xp import XP
|
from modules.user.xp import XP
|
||||||
xp = XP(self)
|
xp = XP(self)
|
||||||
xp.setup(self.tree)
|
xp.setup(self.tree)
|
||||||
|
self.xp_module = xp # Set the xp_module attribute
|
||||||
|
logging.info("XP module loaded")
|
||||||
|
|
||||||
if config['modules']['birthday']['enabled']:
|
if config['modules']['birthday']['enabled']:
|
||||||
from modules.user.birthday import Birthday
|
from modules.user.birthday import Birthday
|
||||||
birthday = Birthday(self)
|
birthday = Birthday(self)
|
||||||
birthday.setup(self.tree)
|
birthday.setup(self.tree)
|
||||||
|
logging.info("Birthday module loaded")
|
||||||
|
|
||||||
if config['modules']['destiny2']['enabled']:
|
if config['modules']['destiny2']['enabled']:
|
||||||
from modules.games.destiny2 import Destiny2
|
from modules.games.destiny2 import Destiny2
|
||||||
destiny2 = Destiny2(self)
|
destiny2 = Destiny2(self)
|
||||||
destiny2.setup(self.tree)
|
destiny2.setup(self.tree)
|
||||||
|
logging.info("Destiny 2 module loaded")
|
||||||
|
|
||||||
if config['modules']['music']['enabled']:
|
if config['modules']['music']['enabled']:
|
||||||
from modules.music.music import Music
|
from modules.music.music import Music
|
||||||
music = Music(self)
|
music = Music(self)
|
||||||
music.setup(self.tree)
|
music.setup(self.tree)
|
||||||
|
logging.info("Music module loaded")
|
||||||
|
|
||||||
if config['modules']['youtube']['enabled']:
|
if config['modules']['youtube']['enabled']:
|
||||||
from modules.social.youtube import YouTube
|
from modules.social.youtube import YouTube
|
||||||
youtube = YouTube(self)
|
youtube = YouTube(self)
|
||||||
youtube.setup(self.tree)
|
youtube.setup(self.tree)
|
||||||
|
logging.info("YouTube module loaded")
|
||||||
|
|
||||||
if config['modules']['twitch']['enabled']:
|
if config['modules']['twitch']['enabled']:
|
||||||
from modules.social.twitch import Twitch
|
from modules.social.twitch import Twitch
|
||||||
twitch = Twitch(self)
|
twitch = Twitch(self)
|
||||||
twitch.setup(self.tree)
|
twitch.setup(self.tree)
|
||||||
|
logging.info("Twitch module loaded")
|
||||||
|
|
||||||
if config['modules']['update']['enabled']:
|
if config['modules']['update']['enabled']:
|
||||||
from modules.admin.update import Update
|
from modules.admin.update import Update
|
||||||
update = Update(self)
|
update = Update(self)
|
||||||
update.setup(self.tree)
|
update.setup(self.tree)
|
||||||
|
logging.info("Update module loaded")
|
||||||
|
|
||||||
if config['modules']['data_privacy']['enabled']:
|
if config['modules']['data_privacy']['enabled']:
|
||||||
from modules.admin.data_privacy import DataPrivacy
|
from modules.admin.data_privacy import DataPrivacy
|
||||||
data_privacy = DataPrivacy(self)
|
data_privacy = DataPrivacy(self)
|
||||||
data_privacy.setup(self.tree)
|
data_privacy.setup(self.tree)
|
||||||
|
logging.info("Data Privacy module loaded")
|
||||||
|
|
||||||
if config['modules']['terms_privacy']['enabled']:
|
if config['modules']['terms_privacy']['enabled']:
|
||||||
from modules.admin.terms_privacy import TermsPrivacy
|
from modules.admin.terms_privacy import TermsPrivacy
|
||||||
terms_privacy = TermsPrivacy(self)
|
terms_privacy = TermsPrivacy(self)
|
||||||
terms_privacy.setup(self.tree)
|
terms_privacy.setup(self.tree)
|
||||||
|
logging.info("Terms and Privacy module loaded")
|
||||||
|
|
||||||
|
|
||||||
bot = Selena()
|
bot = Selena()
|
||||||
@ -82,4 +97,13 @@ bot = Selena()
|
|||||||
async def on_ready():
|
async def on_ready():
|
||||||
logging.info(f'{bot.user.name} has connected to Discord!')
|
logging.info(f'{bot.user.name} has connected to Discord!')
|
||||||
|
|
||||||
|
|
||||||
|
@bot.event
|
||||||
|
async def on_message(message):
|
||||||
|
logging.debug(f"Message from {message.author}: {message.content}")
|
||||||
|
if message.author == bot.user:
|
||||||
|
return
|
||||||
|
if bot.xp_module:
|
||||||
|
await bot.xp_module.handle_message(message)
|
||||||
|
|
||||||
bot.run(TOKEN)
|
bot.run(TOKEN)
|
||||||
|
@ -1,72 +1,97 @@
|
|||||||
# xp.py
|
|
||||||
import discord
|
import discord
|
||||||
from discord import app_commands
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import random
|
import random
|
||||||
|
import logging
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
class XP:
|
class XP:
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.db_path = 'data/selena.db'
|
self.db_path = 'data/selena.db'
|
||||||
|
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 = {}
|
||||||
|
|
||||||
def calculate_level(self, xp):
|
self.ensure_table_exists()
|
||||||
level = 1
|
|
||||||
while xp >= 5 * (level ** 2) + 50 * level + 100:
|
|
||||||
level += 1
|
|
||||||
return level
|
|
||||||
|
|
||||||
async def add_xp(self, interaction: discord.Interaction):
|
|
||||||
guild_id = str(interaction.guild_id)
|
|
||||||
user_id = str(interaction.user.id)
|
|
||||||
earned_xp = random.randint(5, 15)
|
|
||||||
|
|
||||||
|
def ensure_table_exists(self):
|
||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
INSERT INTO guild_xp (guild_id, user_id, xp, level)
|
CREATE TABLE IF NOT EXISTS xp (
|
||||||
VALUES (?, ?, ?, 1)
|
guild_id TEXT NOT NULL,
|
||||||
ON CONFLICT(guild_id, user_id)
|
user_id TEXT NOT NULL,
|
||||||
DO UPDATE SET xp = xp + ?
|
xp INTEGER NOT NULL,
|
||||||
""", (guild_id, user_id, earned_xp, earned_xp))
|
PRIMARY KEY (guild_id, user_id)
|
||||||
conn.commit()
|
);
|
||||||
cursor.execute("SELECT xp, level FROM guild_xp WHERE guild_id = ? AND user_id = ?", (guild_id, user_id))
|
""")
|
||||||
xp, level = cursor.fetchone()
|
|
||||||
new_level = self.calculate_level(xp)
|
|
||||||
if new_level > level:
|
|
||||||
cursor.execute("UPDATE guild_xp SET level = ? WHERE guild_id = ? AND user_id = ?", (new_level, guild_id, user_id))
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
self.logger.info('XP table ensured in database')
|
||||||
|
|
||||||
await interaction.response.send_message(f"You earned {earned_xp} XP! You now have {xp} XP and are level {new_level}.", ephemeral=False)
|
async def add_xp(self, guild_id, user_id, xp):
|
||||||
|
|
||||||
async def check_xp(self, interaction: discord.Interaction):
|
|
||||||
guild_id = str(interaction.guild_id)
|
|
||||||
user_id = str(interaction.user.id)
|
|
||||||
|
|
||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("SELECT xp, level FROM guild_xp WHERE guild_id = ? AND user_id = ?", (guild_id, user_id))
|
cursor.execute("""
|
||||||
result = cursor.fetchone()
|
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()
|
conn.close()
|
||||||
|
self.logger.debug(f'Added {xp} XP to user {user_id} in guild {guild_id}')
|
||||||
|
|
||||||
if result:
|
async def get_xp(self, guild_id, user_id):
|
||||||
xp, level = result
|
conn = sqlite3.connect(self.db_path)
|
||||||
await interaction.response.send_message(f"You have {xp} XP and are level {level}.", ephemeral=False)
|
cursor = conn.cursor()
|
||||||
else:
|
cursor.execute("SELECT xp FROM xp WHERE guild_id = ? AND user_id = ?", (guild_id, user_id))
|
||||||
await interaction.response.send_message("You don't have any XP yet.", ephemeral=False)
|
row = cursor.fetchone()
|
||||||
|
conn.close()
|
||||||
|
return row[0] if row else 0
|
||||||
|
|
||||||
def setup(self, tree: app_commands.CommandTree):
|
async def handle_message(self, message):
|
||||||
@tree.command(name="earn_xp", description="Earn XP")
|
self.logger.debug(f'Received message from user {message.author.id} in guild {message.guild.id}')
|
||||||
async def earn_xp_command(interaction: discord.Interaction):
|
|
||||||
await self.add_xp(interaction)
|
|
||||||
|
|
||||||
@tree.command(name="check_xp", description="Check your XP and level")
|
if message.author.bot:
|
||||||
|
self.logger.debug('Message author is a bot, ignoring.')
|
||||||
|
return
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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):
|
async def check_xp_command(interaction: discord.Interaction):
|
||||||
await self.check_xp(interaction)
|
user_id = str(interaction.user.id)
|
||||||
|
guild_id = str(interaction.guild.id)
|
||||||
if not tree.get_command("earn_xp"):
|
xp = await self.get_xp(guild_id, user_id)
|
||||||
tree.add_command(earn_xp_command)
|
await interaction.response.send_message(embed=discord.Embed(description=f"You have {xp} XP.", color=discord.Color.green()))
|
||||||
|
|
||||||
if not tree.get_command("check_xp"):
|
if not tree.get_command("check_xp"):
|
||||||
tree.add_command(check_xp_command)
|
tree.add_command(check_xp_command)
|
||||||
|
|
||||||
|
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())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user