REF: Made sure everything conforms to Flake8's standards

FIX: Fixed the Permission issues with logger
FEAT: Changed how the XP system works. Now does a scaling curve with xp being 1-5
This commit is contained in:
Dan
2024-06-22 08:55:26 -04:00
parent 4d9214e378
commit 39603b1e06
12 changed files with 327 additions and 195 deletions

View File

@@ -1,9 +1,10 @@
# Flake8:noqa: E501
import logging
import discord
from discord import app_commands
import sqlite3
import logging
from modules.data.db import initialize_db, get_connection
from modules.data.db import get_connection, initialize_db
logger = logging.getLogger(__name__)
initialize_db()
@@ -15,23 +16,23 @@ class BirthdayModule:
self.add_commands()
def add_commands(self):
@app_commands.command(
name="add_birthday", description="Add your birthday"
)
@app_commands.command(name="add_birthday", description="Add your birthday")
async def add_birthday(interaction: discord.Interaction, date: str):
await interaction.response.defer()
try:
conn = get_connection()
cursor = conn.cursor()
cursor.execute('INSERT OR REPLACE INTO birthdays (user_id, birthday) VALUES (?, ?)',
(str(interaction.user.id), date))
cursor.execute(
"INSERT OR REPLACE INTO birthdays (user_id, birthday) VALUES (?, ?)",
(str(interaction.user.id), date),
)
conn.commit()
conn.close()
await interaction.followup.send(
embed=discord.Embed(
title="Add Birthday",
description=f"Your birthday {date} has been added.",
color=discord.Color.green()
color=discord.Color.green(),
)
)
logger.info(f"Birthday added for user {interaction.user.id}: {date}")
@@ -39,15 +40,16 @@ class BirthdayModule:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in add_birthday command: {e}")
@app_commands.command(
name="view_birthday", description="View your birthday"
)
@app_commands.command(name="view_birthday", description="View your birthday")
async def view_birthday(interaction: discord.Interaction):
await interaction.response.defer()
try:
conn = get_connection()
cursor = conn.cursor()
cursor.execute('SELECT birthday FROM birthdays WHERE user_id = ?', (str(interaction.user.id),))
cursor.execute(
"SELECT birthday FROM birthdays WHERE user_id = ?",
(str(interaction.user.id),),
)
result = cursor.fetchone()
conn.close()
if result:
@@ -55,16 +57,18 @@ class BirthdayModule:
embed=discord.Embed(
title="View Birthday",
description=f"Your birthday is {result[0]}.",
color=discord.Color.green()
color=discord.Color.green(),
)
)
logger.info(f"Birthday viewed for user {interaction.user.id}: {result[0]}")
logger.info(
f"Birthday viewed for user {interaction.user.id}: {result[0]}"
)
else:
await interaction.followup.send(
embed=discord.Embed(
title="View Birthday",
description="You have not set a birthday yet.",
color=discord.Color.red()
color=discord.Color.red(),
)
)
logger.info(f"Birthday not found for user {interaction.user.id}")
@@ -80,14 +84,17 @@ class BirthdayModule:
try:
conn = get_connection()
cursor = conn.cursor()
cursor.execute('DELETE FROM birthdays WHERE user_id = ?', (str(interaction.user.id),))
cursor.execute(
"DELETE FROM birthdays WHERE user_id = ?",
(str(interaction.user.id),),
)
conn.commit()
conn.close()
await interaction.followup.send(
embed=discord.Embed(
title="Remove Birthday",
description="Your birthday has been removed.",
color=discord.Color.green()
color=discord.Color.green(),
)
)
logger.info(f"Birthday removed for user {interaction.user.id}")

View File

@@ -1,8 +1,11 @@
import logging
import random
from datetime import datetime, timedelta
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__)
@@ -12,25 +15,30 @@ class XPModule:
self.bot = bot
self.cooldown = {} # Dictionary to store cooldowns
self.add_commands()
self.setup_event_listeners()
async def on_message(self, message):
logger.debug(f"Received message from {message.author.id}")
if message.author.bot:
logger.debug("Message is from a bot, ignoring")
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
if user_id in self.cooldown and now < self.cooldown[user_id]:
logger.debug(f"User {user_id} is on cooldown, ignoring")
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
xp = random.randint(1, 5) # Award between 1 and 5 XP for a message
logger.debug(f"Awarding {xp} XP to user {user_id}")
self.give_xp(user_id, xp)
self.cooldown[user_id] = now + timedelta(seconds=60) # 1 minute cooldown # noqa: E501
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,))
c.execute("SELECT xp, level FROM user_xp WHERE user_id = ?", (user_id,)) # noqa: E501
row = c.fetchone()
if row:
@@ -39,45 +47,55 @@ class XPModule:
new_level = current_level
# Level up logic
if new_xp >= self.xp_for_next_level(current_level):
while new_xp >= self.xp_for_next_level(new_level):
new_xp -= self.xp_for_next_level(new_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))
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))
c.execute(
"INSERT INTO user_xp (user_id, xp, level) VALUES (?, ?, ?)",
(user_id, xp, 1),
)
conn.commit()
conn.close()
logger.debug(f"Updated XP for user {user_id}")
def xp_for_next_level(self, level):
return 100 * level # Example leveling curve
return int(
100 * (1.5 ** (level - 1))
) # Exponential scaling for XP required to level up
def add_commands(self):
@app_commands.command(name='xp', description='Check your XP and level')
@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,))
c.execute("SELECT xp, level FROM user_xp WHERE user_id = ?", (user_id,)) # noqa: E501
row = c.fetchone()
conn.close()
if row:
xp, level = row
await interaction.response.send_message(f"You have {xp} XP and are level {level}.")
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)
def setup_event_listeners(self):
@self.bot.event
async def on_message(message):
await self.on_message(message)
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
XPModule(bot)