FIX: Made the currency and xp guild-based now

This commit is contained in:
Dan
2024-06-28 14:01:48 -04:00
parent 55ede2d2cf
commit e39833a98b
2 changed files with 93 additions and 148 deletions

View File

@@ -1,79 +1,59 @@
# currency.py
import discord
from discord import app_commands
import random
import sqlite3
from datetime import datetime, timedelta
import random
class Currency:
def __init__(self, bot):
self.bot = bot
self.db_path = 'data/selena.db'
self._init_db()
self.cooldowns = {}
def _init_db(self):
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
user_id TEXT PRIMARY KEY,
kibble INTEGER DEFAULT 0,
xp INTEGER DEFAULT 0,
level INTEGER DEFAULT 1
)
''')
conn.commit()
def _get_user_kibble(self, user_id):
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('SELECT kibble FROM users WHERE user_id = ?', (user_id,))
row = cursor.fetchone()
return row[0] if row else 0
def _update_user_kibble(self, user_id, amount):
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('INSERT OR IGNORE INTO users (user_id, kibble) VALUES (?, ?)', (user_id, 0))
cursor.execute('UPDATE users SET kibble = kibble + ? WHERE user_id = ?', (amount, user_id))
conn.commit()
async def earn_kibble(self, interaction: discord.Interaction):
guild_id = str(interaction.guild_id)
user_id = str(interaction.user.id)
now = datetime.utcnow()
earned = random.randint(1, 10)
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()
if user_id in self.cooldowns:
if now < self.cooldowns[user_id]:
retry_after = (self.cooldowns[user_id] - now).total_seconds()
await interaction.response.send_message(
f'{interaction.user.mention}, you are on a cooldown. Please wait {retry_after:.2f} seconds before using this command again.',
ephemeral=True
)
return
self.cooldowns[user_id] = now + timedelta(seconds=30)
amount = random.choices([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100], [0.01, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0.01])[0]
self._update_user_kibble(user_id, amount)
await interaction.response.send_message(f'{interaction.user.mention} earned {amount} Kibble!')
await interaction.response.send_message(f"You earned {earned} Kibble! You now have {balance} Kibble.", ephemeral=False)
async def balance(self, interaction: discord.Interaction):
guild_id = str(interaction.guild_id)
user_id = str(interaction.user.id)
balance = self._get_user_kibble(user_id)
await interaction.response.send_message(f'{interaction.user.mention} has {balance} Kibble.')
def setup(self, tree):
tree.add_command(app_commands.Command(
name='earn_kibble',
description='Earn Kibble',
callback=self.earn_kibble
))
tree.add_command(app_commands.Command(
name='balance',
description='Check your Kibble balance',
callback=self.balance
))
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(bot):
bot.tree.add_cog(Currency(bot))
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)
if not tree.get_command("earn_kibble"):
tree.add_command(earn_kibble_command)
if not tree.get_command("balance"):
tree.add_command(balance_command)