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,11 +1,12 @@
# Flake8:noqa: E501
import discord
from discord import app_commands
import sqlite3
import logging
import random
from datetime import datetime, timedelta
from modules.data.db import initialize_db, get_connection
import discord
from discord import app_commands
from modules.data.db import get_connection, initialize_db
logger = logging.getLogger(__name__)
initialize_db()
@ -19,18 +20,18 @@ class CurrencyModule:
self.add_commands()
def add_commands(self):
@app_commands.command(
name="earn_kibble", description="Earn Kibble"
)
@app_commands.command(name="earn_kibble", description="Earn Kibble")
async def earn_kibble(interaction: discord.Interaction):
await interaction.response.defer()
try:
conn = get_connection()
cursor = conn.cursor()
user_id = str(interaction.user.id)
# Check cooldown
cursor.execute('SELECT last_earned FROM currency WHERE user_id = ?', (user_id,))
cursor.execute(
"SELECT last_earned FROM currency WHERE user_id = ?", (user_id,)
)
result = cursor.fetchone()
if result and result[0]:
last_earned = datetime.fromisoformat(result[0])
@ -39,30 +40,40 @@ class CurrencyModule:
embed=discord.Embed(
title="Earn Kibble",
description="You are on cooldown. Please try again later.",
color=discord.Color.red()
color=discord.Color.red(),
)
)
conn.close()
logger.info(f"User {user_id} attempted to earn Kibble but is on cooldown.")
logger.info(
f"User {user_id} attempted to earn Kibble but is on cooldown."
)
return
amount = random.choices([random.randint(1, 10), 100], [0.99, 0.01])[0]
cursor.execute('INSERT OR IGNORE INTO currency (user_id, balance, last_earned) VALUES (?, ?, ?)',
(user_id, 0, datetime.now().isoformat()))
cursor.execute('UPDATE currency SET balance = balance + ?, last_earned = ? WHERE user_id = ?',
(amount, datetime.now().isoformat(), user_id))
cursor.execute(
"INSERT OR IGNORE INTO currency (user_id, balance, last_earned) VALUES (?, ?, ?)",
(user_id, 0, datetime.now().isoformat()),
)
cursor.execute(
"UPDATE currency SET balance = balance + ?, last_earned = ? WHERE user_id = ?",
(amount, datetime.now().isoformat(), user_id),
)
conn.commit()
cursor.execute('SELECT balance FROM currency WHERE user_id = ?', (user_id,))
cursor.execute(
"SELECT balance FROM currency WHERE user_id = ?", (user_id,)
)
new_balance = cursor.fetchone()[0]
conn.close()
await interaction.followup.send(
embed=discord.Embed(
title="Earn Kibble",
description=f"You have earned {amount} Kibble. Your new balance is {new_balance}.",
color=discord.Color.green()
color=discord.Color.green(),
)
)
logger.info(f"User {user_id} earned {amount} Kibble. New balance: {new_balance}")
logger.info(
f"User {user_id} earned {amount} Kibble. New balance: {new_balance}"
)
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in earn_kibble command: {e}")
@ -75,7 +86,10 @@ class CurrencyModule:
try:
conn = get_connection()
cursor = conn.cursor()
cursor.execute('SELECT balance FROM currency WHERE user_id = ?', (str(interaction.user.id),))
cursor.execute(
"SELECT balance FROM currency WHERE user_id = ?",
(str(interaction.user.id),),
)
result = cursor.fetchone()
conn.close()
if result:
@ -83,16 +97,18 @@ class CurrencyModule:
embed=discord.Embed(
title="Check Balance",
description=f"Your current balance is {result[0]} Kibble.",
color=discord.Color.green()
color=discord.Color.green(),
)
)
logger.info(f"User {interaction.user.id} checked balance: {result[0]}")
logger.info(
f"User {interaction.user.id} checked balance: {result[0]}"
)
else:
await interaction.followup.send(
embed=discord.Embed(
title="Check Balance",
description="You have no Kibble.",
color=discord.Color.red()
color=discord.Color.red(),
)
)
logger.info(f"User {interaction.user.id} has no Kibble.")