2024-06-21 15:01:11 -04:00
|
|
|
# Flake8:noqa: E501
|
2024-06-21 13:38:24 -04:00
|
|
|
import discord
|
|
|
|
from discord import app_commands
|
|
|
|
import sqlite3
|
|
|
|
import logging
|
|
|
|
import random
|
2024-06-21 15:01:11 -04:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from modules.data.db import initialize_db, get_connection
|
2024-06-21 13:38:24 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
initialize_db()
|
|
|
|
|
|
|
|
|
|
|
|
class CurrencyModule:
|
2024-06-21 15:01:11 -04:00
|
|
|
COOLDOWN_PERIOD = timedelta(minutes=5) # Set the cooldown period here
|
|
|
|
|
2024-06-21 13:38:24 -04:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
self.add_commands()
|
|
|
|
|
|
|
|
def add_commands(self):
|
|
|
|
@app_commands.command(
|
2024-06-21 18:38:15 -04:00
|
|
|
name="earn_kibble", description="Earn Kibble"
|
2024-06-21 13:38:24 -04:00
|
|
|
)
|
2024-06-21 18:38:15 -04:00
|
|
|
async def earn_kibble(interaction: discord.Interaction):
|
2024-06-21 13:38:24 -04:00
|
|
|
await interaction.response.defer()
|
|
|
|
try:
|
|
|
|
conn = get_connection()
|
|
|
|
cursor = conn.cursor()
|
2024-06-21 15:01:11 -04:00
|
|
|
user_id = str(interaction.user.id)
|
|
|
|
|
|
|
|
# Check cooldown
|
|
|
|
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])
|
|
|
|
if datetime.now() - last_earned < CurrencyModule.COOLDOWN_PERIOD:
|
|
|
|
await interaction.followup.send(
|
|
|
|
embed=discord.Embed(
|
2024-06-21 18:38:15 -04:00
|
|
|
title="Earn Kibble",
|
2024-06-21 15:01:11 -04:00
|
|
|
description="You are on cooldown. Please try again later.",
|
|
|
|
color=discord.Color.red()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
conn.close()
|
2024-06-21 18:38:15 -04:00
|
|
|
logger.info(f"User {user_id} attempted to earn Kibble but is on cooldown.")
|
2024-06-21 15:01:11 -04:00
|
|
|
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))
|
2024-06-21 13:38:24 -04:00
|
|
|
conn.commit()
|
2024-06-21 15:01:11 -04:00
|
|
|
cursor.execute('SELECT balance FROM currency WHERE user_id = ?', (user_id,))
|
2024-06-21 13:38:24 -04:00
|
|
|
new_balance = cursor.fetchone()[0]
|
|
|
|
conn.close()
|
|
|
|
await interaction.followup.send(
|
|
|
|
embed=discord.Embed(
|
2024-06-21 18:38:15 -04:00
|
|
|
title="Earn Kibble",
|
|
|
|
description=f"You have earned {amount} Kibble. Your new balance is {new_balance}.",
|
2024-06-21 13:38:24 -04:00
|
|
|
color=discord.Color.green()
|
|
|
|
)
|
|
|
|
)
|
2024-06-21 18:38:15 -04:00
|
|
|
logger.info(f"User {user_id} earned {amount} Kibble. New balance: {new_balance}")
|
2024-06-21 13:38:24 -04:00
|
|
|
except Exception as e:
|
|
|
|
await interaction.followup.send(f"An error occurred: {e}")
|
2024-06-21 18:38:15 -04:00
|
|
|
logger.error(f"Error in earn_kibble command: {e}")
|
2024-06-21 13:38:24 -04:00
|
|
|
|
|
|
|
@app_commands.command(
|
2024-06-21 18:38:15 -04:00
|
|
|
name="check_balance", description="Check your Kibble balance"
|
2024-06-21 13:38:24 -04:00
|
|
|
)
|
|
|
|
async def check_balance(interaction: discord.Interaction):
|
|
|
|
await interaction.response.defer()
|
|
|
|
try:
|
|
|
|
conn = get_connection()
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute('SELECT balance FROM currency WHERE user_id = ?', (str(interaction.user.id),))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
conn.close()
|
|
|
|
if result:
|
|
|
|
await interaction.followup.send(
|
|
|
|
embed=discord.Embed(
|
|
|
|
title="Check Balance",
|
2024-06-21 18:38:15 -04:00
|
|
|
description=f"Your current balance is {result[0]} Kibble.",
|
2024-06-21 13:38:24 -04:00
|
|
|
color=discord.Color.green()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
logger.info(f"User {interaction.user.id} checked balance: {result[0]}")
|
|
|
|
else:
|
|
|
|
await interaction.followup.send(
|
|
|
|
embed=discord.Embed(
|
|
|
|
title="Check Balance",
|
2024-06-21 18:38:15 -04:00
|
|
|
description="You have no Kibble.",
|
2024-06-21 13:38:24 -04:00
|
|
|
color=discord.Color.red()
|
|
|
|
)
|
|
|
|
)
|
2024-06-21 18:38:15 -04:00
|
|
|
logger.info(f"User {interaction.user.id} has no Kibble.")
|
2024-06-21 13:38:24 -04:00
|
|
|
except Exception as e:
|
|
|
|
await interaction.followup.send(f"An error occurred: {e}")
|
|
|
|
logger.error(f"Error in check_balance command: {e}")
|
|
|
|
|
2024-06-21 18:38:15 -04:00
|
|
|
self.bot.tree.add_command(earn_kibble)
|
2024-06-21 13:38:24 -04:00
|
|
|
self.bot.tree.add_command(check_balance)
|
|
|
|
|
|
|
|
|
|
|
|
async def setup(bot):
|
|
|
|
CurrencyModule(bot)
|