mirror of
https://github.com/wlinator/luminara.git
synced 2024-10-02 18:23:12 +00:00
Add /item command
This commit is NOT unstable because there are no commands to influence this inventory system yet.
This commit is contained in:
parent
8a4707bdcc
commit
b4bb4b8b50
8 changed files with 168 additions and 44 deletions
|
@ -1,15 +1,15 @@
|
|||
{
|
||||
"apple": {
|
||||
"id_name": "apple",
|
||||
"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.",
|
||||
"quote": "The rarest item in Racu, you literally can't get this.",
|
||||
"type": "collectable"
|
||||
},
|
||||
"tester_badge": {
|
||||
"id_name": "tester_badge",
|
||||
"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",
|
||||
|
|
|
@ -20,7 +20,7 @@ class Inventory:
|
|||
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))
|
||||
database.execute_query(query, (self.user_id, item.id, self.user_id, item.id, abs(quantity)))
|
||||
|
||||
def get_inventory(self):
|
||||
query = "SELECT item_id, quantity FROM inventory WHERE user_id = ? AND quantity > 0"
|
||||
|
@ -32,15 +32,9 @@ class Inventory:
|
|||
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
|
||||
query = "SELECT COALESCE(quantity, 0) FROM inventory WHERE user_id = ? AND item_id = ?"
|
||||
result = database.select_query_one(query, (self.user_id, item.id))
|
||||
return result
|
||||
|
|
59
data/Item.py
59
data/Item.py
|
@ -1,16 +1,16 @@
|
|||
import json
|
||||
import sqlite3
|
||||
|
||||
from db import database
|
||||
|
||||
|
||||
class Item:
|
||||
def __init__(self, item_id):
|
||||
self.item_id = item_id
|
||||
self.id = item_id
|
||||
|
||||
data = self.get_item_data()
|
||||
print(data)
|
||||
|
||||
self.id_name = data[0]
|
||||
self.name = data[0]
|
||||
self.display_name = data[1]
|
||||
self.description = data[2]
|
||||
self.image_url = data[3]
|
||||
|
@ -20,12 +20,22 @@ class Item:
|
|||
|
||||
def get_item_data(self):
|
||||
query = """
|
||||
SELECT id_name, display_name, description, image_url, emote_id, quote, type
|
||||
SELECT name, display_name, description, image_url, emote_id, quote, type
|
||||
FROM item
|
||||
WHERE item_id = ?
|
||||
WHERE id = ?
|
||||
"""
|
||||
|
||||
return database.select_query(query, (self.item_id,))[0]
|
||||
data = database.select_query(query, (self.id,))[0]
|
||||
return data
|
||||
|
||||
def get_quantity(self, author_id):
|
||||
query = """
|
||||
SELECT COALESCE((SELECT quantity FROM inventory WHERE user_id = ? AND item_id = ?), 0) AS quantity
|
||||
"""
|
||||
|
||||
quantity = database.select_query_one(query, (author_id, self.id))
|
||||
|
||||
return quantity
|
||||
|
||||
@staticmethod
|
||||
def insert_items():
|
||||
|
@ -33,20 +43,43 @@ class Item:
|
|||
items_data = json.load(file)
|
||||
|
||||
for index, (item_id, item_data) in enumerate(items_data.items(), start=1):
|
||||
id_name = item_data["id_name"]
|
||||
name = item_data["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"]
|
||||
item_type = item_data["type"]
|
||||
|
||||
query = """
|
||||
INSERT OR IGNORE INTO item
|
||||
(item_id, id_name, display_name, description, image_url, emote_id, quote, type)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
"""
|
||||
INSERT OR REPLACE INTO item
|
||||
(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))
|
||||
(index, name, display_name, description, image_url, emote_id, quote, item_type))
|
||||
|
||||
print("Items inserted into the database successfully.")
|
||||
|
||||
@staticmethod
|
||||
def get_all_item_names():
|
||||
query = "SELECT display_name FROM item"
|
||||
|
||||
try:
|
||||
item_names = []
|
||||
items = database.select_query(query)
|
||||
|
||||
for item in items:
|
||||
item_names.append(item[0])
|
||||
|
||||
return item_names
|
||||
|
||||
except sqlite3.Error:
|
||||
print(sqlite3.Error)
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def get_item_by_display_name(display_name):
|
||||
query = "SELECT id FROM item WHERE display_name = ?"
|
||||
item_id = database.select_query_one(query, (display_name,))
|
||||
return Item(item_id)
|
||||
|
|
|
@ -19,8 +19,8 @@ CREATE TABLE IF NOT EXISTS currency (
|
|||
|
||||
item_table = """
|
||||
CREATE TABLE IF NOT EXISTS item (
|
||||
item_id INTEGER PRIMARY KEY,
|
||||
id_name TEXT,
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT,
|
||||
display_name TEXT,
|
||||
description TEXT,
|
||||
image_url TEXT,
|
||||
|
@ -37,7 +37,7 @@ CREATE TABLE IF NOT EXISTS inventory (
|
|||
quantity INTEGER,
|
||||
|
||||
PRIMARY KEY (user_id, item_id),
|
||||
FOREIGN KEY (item_id) REFERENCES item (item_id)
|
||||
FOREIGN KEY (item_id) REFERENCES item (id)
|
||||
)
|
||||
"""
|
||||
|
||||
|
|
|
@ -37,7 +37,15 @@ class EconomyCog(commands.Cog):
|
|||
cash_balance = ctx_currency.cash
|
||||
special_balance = ctx_currency.special
|
||||
|
||||
await ctx.respond(embed=economy_embeds.currency_balance(ctx, cash_balance, special_balance))
|
||||
embed = discord.Embed(
|
||||
color=discord.Color.embed_background(),
|
||||
description=f"**Cash**: {cash_balance_name}{cash_balance}\n"
|
||||
f"**{special_balance_name.capitalize()}**: {special_balance}"
|
||||
)
|
||||
embed.set_author(name=f"{ctx.author.name}'s wallet", icon_url=ctx.author.avatar.url)
|
||||
embed.set_footer(text=f"Level up to earn {special_balance_name}!")
|
||||
|
||||
await ctx.respond(embed=embed)
|
||||
|
||||
@commands.slash_command(
|
||||
name="give",
|
||||
|
|
|
@ -5,7 +5,6 @@ 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
|
||||
|
||||
|
@ -33,10 +32,12 @@ class InventoryCog(commands.Cog):
|
|||
inventory = Inventory(ctx.author.id)
|
||||
inventory_dict = inventory.get_inventory()
|
||||
|
||||
currency = Currency(ctx.author.id)
|
||||
balance = currency.cash
|
||||
description = "You don't have any items!" if inventory_dict == {} else None
|
||||
|
||||
embed = discord.Embed(description=f"**Balance: ${balance}**")
|
||||
embed = discord.Embed(
|
||||
color=discord.Color.embed_background(),
|
||||
description=description
|
||||
)
|
||||
embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
|
||||
|
||||
for item, quantity in inventory_dict.items():
|
||||
|
@ -44,6 +45,7 @@ class InventoryCog(commands.Cog):
|
|||
embed.add_field(name=f"{emote} {item.display_name.capitalize()}",
|
||||
value=f"*— Amount: `{quantity}`*",
|
||||
inline=False)
|
||||
embed.set_footer(text="for more info do /item")
|
||||
|
||||
await ctx.respond(embed=embed)
|
||||
|
||||
|
|
97
modules/item.py
Normal file
97
modules/item.py
Normal file
|
@ -0,0 +1,97 @@
|
|||
import json
|
||||
import os
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from data.Inventory import Inventory
|
||||
from data.Item import Item
|
||||
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 ItemCog(commands.Cog):
|
||||
def __init__(self, sbbot):
|
||||
self.bot = sbbot
|
||||
|
||||
@commands.slash_command(
|
||||
name="item",
|
||||
description="View the information about a specific item.",
|
||||
guild_only=True
|
||||
)
|
||||
@commands.check(universal.beta_check)
|
||||
async def item_command(self, ctx, *, item: discord.Option(choices=Item.get_all_item_names())):
|
||||
# create item object from choice
|
||||
item = Item.get_item_by_display_name(item)
|
||||
amount = item.get_quantity(ctx.author.id)
|
||||
shop_cost = "N/A"
|
||||
sell_value = "N/A"
|
||||
|
||||
emote = self.bot.get_emoji(item.emote_id)
|
||||
|
||||
amount_string = f"You have this item {amount} time"
|
||||
|
||||
if amount > 1:
|
||||
amount_string += "s"
|
||||
elif amount < 1:
|
||||
amount_string = f"You don't have this item"
|
||||
|
||||
if item.quote is None or item.quote == "":
|
||||
description = amount_string
|
||||
else:
|
||||
description = f"> *{item.quote}*\n\n{amount_string}"
|
||||
|
||||
embed = discord.Embed(
|
||||
color=discord.Color.embed_background(),
|
||||
title=f"{emote} {item.display_name}",
|
||||
description=description
|
||||
)
|
||||
embed.add_field(name="Value", value=f"`/shop` cost: **{shop_cost}**\n`/sell` value: **{sell_value}**",
|
||||
inline=False)
|
||||
embed.add_field(name="Description", value=item.description, inline=False)
|
||||
embed.set_thumbnail(url=item.image_url)
|
||||
embed.set_footer(text=f"type: {item.type}")
|
||||
|
||||
return await ctx.respond(embed=embed)
|
||||
|
||||
items = discord.SlashCommandGroup(name="items")
|
||||
|
||||
@items.command(
|
||||
name="gift",
|
||||
description="Award items to someone."
|
||||
)
|
||||
@commands.check(universal.owner_check)
|
||||
async def gift(self, ctx, *,
|
||||
item: discord.Option(choices=Item.get_all_item_names()),
|
||||
user: discord.Option(discord.Member),
|
||||
quantity: discord.Option(int)):
|
||||
|
||||
try:
|
||||
item = Item.get_item_by_display_name(item)
|
||||
target_inventory = Inventory(user.id)
|
||||
target_inventory.add_item(item, quantity)
|
||||
|
||||
except Exception as e:
|
||||
await ctx.channel.respond("Something went wrong. Check console.", ephemeral=True)
|
||||
print(e)
|
||||
return
|
||||
|
||||
embed = discord.Embed(
|
||||
color=discord.Color.embed_background(),
|
||||
description=f"Awarded **{abs(quantity)} {item.name}** to {user.name}."
|
||||
)
|
||||
|
||||
await ctx.respond(embed=embed)
|
||||
|
||||
|
||||
def setup(sbbot):
|
||||
sbbot.add_cog(ItemCog(sbbot))
|
|
@ -16,16 +16,6 @@ with open("config/economy.json") as file:
|
|||
json_data = json.load(file)
|
||||
|
||||
|
||||
def currency_balance(ctx, cash_balance, special_balance):
|
||||
embed = discord.Embed(
|
||||
description=f"**Cash**: {cash_balance_name}{cash_balance}\n"
|
||||
f"**{special_balance_name.capitalize()}**: {special_balance}"
|
||||
)
|
||||
embed.set_author(name=f"{ctx.author.name}'s wallet", icon_url=ctx.author.avatar.url)
|
||||
embed.set_footer(text=f"Level up to earn {special_balance_name}!")
|
||||
return embed
|
||||
|
||||
|
||||
def award(user, currency, amount):
|
||||
reward = f"{amount}"
|
||||
if currency == "cash_balance":
|
||||
|
|
Loading…
Reference in a new issue