mirror of
https://github.com/wlinator/luminara.git
synced 2024-10-02 20:43:12 +00:00
First inventory push - unstable
This push is completely unstable, the inventory command will also be set as "beta command", making it impossible for anyone to use.
This commit is contained in:
parent
eb11990da5
commit
d24309602c
6 changed files with 198 additions and 0 deletions
20
config/default_items.json
Normal file
20
config/default_items.json
Normal file
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"apple": {
|
||||
"id_name": "apple",
|
||||
"display_name": "apple",
|
||||
"description": "This is just an apple.. An item used for testing.",
|
||||
"image_url": "https://i.imgur.com/ykQO3sH.png",
|
||||
"emote_id": 1121491265658306692,
|
||||
"quote": "The rarest item in Racu, you *literally* can't get this.",
|
||||
"type": "collectable"
|
||||
},
|
||||
"tester_badge": {
|
||||
"id_name": "tester_badge",
|
||||
"display_name": "tester badge",
|
||||
"description": "A badge awarded to the people who helped make Racu.",
|
||||
"image_url": "https://i.imgur.com/Z8zQXuQ.png",
|
||||
"emote_id": 1121491902370418869,
|
||||
"quote": "Thank you for the support <3",
|
||||
"type": "collectable"
|
||||
}
|
||||
}
|
46
data/Inventory.py
Normal file
46
data/Inventory.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
from data import Item
|
||||
from db import database
|
||||
|
||||
|
||||
class Inventory:
|
||||
def __init__(self, user_id):
|
||||
self.user_id = user_id
|
||||
|
||||
def add_item(self, item: Item.Item, quantity=1):
|
||||
"""
|
||||
Adds an item with a specific count (default 1) to the database, if there are
|
||||
no records of this user having that item yet, it will just add a record with quantity=quantity.
|
||||
:param item:
|
||||
:param quantity:
|
||||
:return:
|
||||
"""
|
||||
|
||||
query = """
|
||||
INSERT OR REPLACE INTO inventory (user_id, item_id, quantity)
|
||||
VALUES (?, ?, COALESCE((SELECT quantity FROM inventory WHERE user_id = ? AND item_id = ?), 0) + ?)
|
||||
"""
|
||||
|
||||
database.execute_query(query, (self.user_id, item.item_id, self.user_id, item.item_id, quantity))
|
||||
|
||||
def get_inventory(self):
|
||||
query = "SELECT item_id, quantity FROM inventory WHERE user_id = ? AND quantity > 0"
|
||||
results = database.select_query(query, (self.user_id,))
|
||||
|
||||
items_dict = {}
|
||||
for row in results:
|
||||
item_id, quantity = row
|
||||
item = Item.Item(item_id)
|
||||
items_dict[item] = quantity
|
||||
|
||||
print(items_dict)
|
||||
return items_dict
|
||||
|
||||
def get_item_quantity(self, item: Item.Item):
|
||||
query = "SELECT quantity FROM inventory WHERE user_id = ? AND item_id = ?"
|
||||
|
||||
result = database.select_query_one(query, (self.user_id, item.item_id))
|
||||
|
||||
if result:
|
||||
return result[0]
|
||||
|
||||
return 0
|
52
data/Item.py
Normal file
52
data/Item.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
import json
|
||||
|
||||
from db import database
|
||||
|
||||
|
||||
class Item:
|
||||
def __init__(self, item_id):
|
||||
self.item_id = item_id
|
||||
|
||||
data = self.get_item_data()
|
||||
print(data)
|
||||
|
||||
self.id_name = data[0]
|
||||
self.display_name = data[1]
|
||||
self.description = data[2]
|
||||
self.image_url = data[3]
|
||||
self.emote_id = data[4]
|
||||
self.quote = data[5]
|
||||
self.type = data[6]
|
||||
|
||||
def get_item_data(self):
|
||||
query = """
|
||||
SELECT id_name, display_name, description, image_url, emote_id, quote, type
|
||||
FROM item
|
||||
WHERE item_id = ?
|
||||
"""
|
||||
|
||||
return database.select_query(query, (self.item_id,))[0]
|
||||
|
||||
@staticmethod
|
||||
def insert_items():
|
||||
with open("config/default_items.json", 'r') as file:
|
||||
items_data = json.load(file)
|
||||
|
||||
for index, (item_id, item_data) in enumerate(items_data.items(), start=1):
|
||||
id_name = item_data["id_name"]
|
||||
display_name = item_data["display_name"]
|
||||
description = item_data["description"]
|
||||
image_url = item_data["image_url"]
|
||||
emote_id = item_data["emote_id"]
|
||||
quote = item_data["quote"]
|
||||
type = item_data["type"]
|
||||
|
||||
query = """
|
||||
INSERT OR IGNORE INTO item
|
||||
(item_id, id_name, display_name, description, image_url, emote_id, quote, type)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
"""
|
||||
database.execute_query(query,
|
||||
(index, id_name, display_name, description, image_url, emote_id, quote, type))
|
||||
|
||||
print("Items inserted into the database successfully.")
|
26
db/tables.py
26
db/tables.py
|
@ -17,6 +17,30 @@ CREATE TABLE IF NOT EXISTS currency (
|
|||
)
|
||||
"""
|
||||
|
||||
item_table = """
|
||||
CREATE TABLE IF NOT EXISTS item (
|
||||
item_id INTEGER PRIMARY KEY,
|
||||
id_name TEXT,
|
||||
display_name TEXT,
|
||||
description TEXT,
|
||||
image_url TEXT,
|
||||
emote_id INTEGER,
|
||||
quote TEXT,
|
||||
type TEXT
|
||||
)
|
||||
"""
|
||||
|
||||
inventory_table = """
|
||||
CREATE TABLE IF NOT EXISTS inventory (
|
||||
user_id INTEGER,
|
||||
item_id INTEGER,
|
||||
quantity INTEGER,
|
||||
|
||||
PRIMARY KEY (user_id, item_id),
|
||||
FOREIGN KEY (item_id) REFERENCES item (item_id)
|
||||
)
|
||||
"""
|
||||
|
||||
dailies_table = """
|
||||
CREATE TABLE IF NOT EXISTS dailies (
|
||||
id INTEGER PRIMARY KEY,
|
||||
|
@ -64,6 +88,8 @@ CREATE TABLE IF NOT EXISTS stats_duel (
|
|||
def sync_database():
|
||||
database.execute_query(xp_table)
|
||||
database.execute_query(currency_table)
|
||||
database.execute_query(item_table)
|
||||
database.execute_query(inventory_table)
|
||||
database.execute_query(dailies_table)
|
||||
database.execute_query(stats_bj)
|
||||
database.execute_query(stats_slots)
|
||||
|
|
2
main.py
2
main.py
|
@ -16,6 +16,7 @@ from dotenv import load_dotenv
|
|||
import db.tables
|
||||
import sb_tools.resources
|
||||
from config import json_loader
|
||||
from data.Item import Item
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
load_dotenv('.env')
|
||||
|
@ -37,6 +38,7 @@ async def on_ready():
|
|||
# wait until the bot is ready
|
||||
# then sync the sqlite3 database
|
||||
db.tables.sync_database()
|
||||
Item.insert_items()
|
||||
|
||||
"""
|
||||
https://docs.pycord.dev/en/stable/api/events.html#discord.on_ready
|
||||
|
|
52
modules/InventoryCog.py
Normal file
52
modules/InventoryCog.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
import json
|
||||
import os
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from data.Currency import Currency
|
||||
from data.Inventory import Inventory
|
||||
from sb_tools import universal
|
||||
|
||||
load_dotenv('.env')
|
||||
|
||||
active_blackjack_games = {}
|
||||
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
|
||||
cash_balance_name = os.getenv("CASH_BALANCE_NAME")
|
||||
|
||||
with open("config/economy.json") as file:
|
||||
json_data = json.load(file)
|
||||
|
||||
|
||||
class InventoryCog(commands.Cog):
|
||||
def __init__(self, sbbot):
|
||||
self.bot = sbbot
|
||||
|
||||
@commands.slash_command(
|
||||
name="inventory",
|
||||
description="Display your inventory.",
|
||||
guild_only=True
|
||||
)
|
||||
@commands.check(universal.beta_check)
|
||||
async def inventory(self, ctx):
|
||||
inventory = Inventory(ctx.author.id)
|
||||
inventory_dict = inventory.get_inventory()
|
||||
|
||||
currency = Currency(ctx.author.id)
|
||||
balance = currency.cash
|
||||
|
||||
embed = discord.Embed(description=f"**Balance: ${balance}**")
|
||||
embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
|
||||
|
||||
for item, quantity in inventory_dict.items():
|
||||
emote = self.bot.get_emoji(item.emote_id)
|
||||
embed.add_field(name=f"{emote} {item.display_name.capitalize()}",
|
||||
value=f"*— Amount: `{quantity}`*",
|
||||
inline=False)
|
||||
|
||||
await ctx.respond(embed=embed)
|
||||
|
||||
|
||||
def setup(sbbot):
|
||||
sbbot.add_cog(InventoryCog(sbbot))
|
Loading…
Reference in a new issue