2024-06-28 14:01:48 -04:00
|
|
|
# currency.py
|
2024-06-24 20:42:30 -04:00
|
|
|
import discord
|
|
|
|
from discord import app_commands
|
|
|
|
import sqlite3
|
2024-06-28 14:01:48 -04:00
|
|
|
import random
|
2024-06-24 20:42:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Currency:
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
self.db_path = 'data/selena.db'
|
|
|
|
|
2024-06-24 23:20:03 -04:00
|
|
|
async def earn_kibble(self, interaction: discord.Interaction):
|
2024-06-28 14:01:48 -04:00
|
|
|
guild_id = str(interaction.guild_id)
|
2024-06-24 23:20:03 -04:00
|
|
|
user_id = str(interaction.user.id)
|
2024-06-28 14:01:48 -04:00
|
|
|
earned = random.randint(1, 10)
|
2024-06-28 15:32:37 -04:00
|
|
|
|
2024-06-28 14:01:48 -04:00
|
|
|
conn = sqlite3.connect(self.db_path)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("""
|
|
|
|
INSERT INTO guild_currency (guild_id, user_id, balance)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
ON CONFLICT(guild_id, user_id)
|
|
|
|
DO UPDATE SET balance = balance + ?
|
|
|
|
""", (guild_id, user_id, earned, earned))
|
|
|
|
conn.commit()
|
|
|
|
cursor.execute("SELECT balance FROM guild_currency WHERE guild_id = ? AND user_id = ?", (guild_id, user_id))
|
|
|
|
balance = cursor.fetchone()[0]
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
await interaction.response.send_message(f"You earned {earned} Kibble! You now have {balance} Kibble.", ephemeral=False)
|
2024-06-24 23:20:03 -04:00
|
|
|
|
|
|
|
async def balance(self, interaction: discord.Interaction):
|
2024-06-28 14:01:48 -04:00
|
|
|
guild_id = str(interaction.guild_id)
|
2024-06-24 23:20:03 -04:00
|
|
|
user_id = str(interaction.user.id)
|
|
|
|
|
2024-06-28 14:01:48 -04:00
|
|
|
conn = sqlite3.connect(self.db_path)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("SELECT balance FROM guild_currency WHERE guild_id = ? AND user_id = ?", (guild_id, user_id))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
balance = result[0] if result else 0
|
|
|
|
await interaction.response.send_message(f"You have {balance} Kibble.", ephemeral=False)
|
|
|
|
|
|
|
|
def setup(self, tree: app_commands.CommandTree):
|
|
|
|
@tree.command(name="earn_kibble", description="Earn Kibble currency")
|
|
|
|
async def earn_kibble_command(interaction: discord.Interaction):
|
|
|
|
await self.earn_kibble(interaction)
|
|
|
|
|
|
|
|
@tree.command(name="balance", description="Check your Kibble balance")
|
|
|
|
async def balance_command(interaction: discord.Interaction):
|
|
|
|
await self.balance(interaction)
|
2024-06-24 20:42:30 -04:00
|
|
|
|
2024-06-28 14:01:48 -04:00
|
|
|
if not tree.get_command("earn_kibble"):
|
|
|
|
tree.add_command(earn_kibble_command)
|
2024-06-24 20:42:30 -04:00
|
|
|
|
2024-06-28 14:01:48 -04:00
|
|
|
if not tree.get_command("balance"):
|
|
|
|
tree.add_command(balance_command)
|