Skip to content
This repository was archived by the owner on Feb 7, 2026. It is now read-only.

Commit 3a3303e

Browse files
authored
Merge pull request #313 from PyBotDevs/items-db-library-2
Migrate items db instances completely to framework module
2 parents 49ceef9 + b5a4598 commit 3a3303e

2 files changed

Lines changed: 42 additions & 51 deletions

File tree

cogs/economy.py

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import asyncio
1010
import framework.isobot.currency
1111
from framework.isobot.shop import ShopData
12-
from framework.isobot.db import levelling
12+
from framework.isobot.db import levelling, items
1313
from random import randint
1414
from discord import option, ApplicationContext
1515
from discord.ext import commands
@@ -18,6 +18,7 @@
1818
color = discord.Color.random()
1919
currency = framework.isobot.currency.CurrencyAPI("database/currency.json", "logs/currency.log")
2020
levelling = levelling.Levelling()
21+
items = items.Items()
2122
shop_data = ShopData("config/shop.json")
2223
all_item_ids = shop_data.get_item_ids()
2324
shopitem = shop_data.get_raw_data()
@@ -31,11 +32,9 @@
3132
"Doctor"
3233
]
3334

34-
with open("database/items.json", 'r') as f: items = json.load(f)
3535
with open("database/user_data.json", 'r') as f: userdat = json.load(f)
3636

3737
def save():
38-
with open("database/items.json", 'w+') as f: json.dump(items, f, indent=4)
3938
with open("database/user_data.json", 'w+') as f: json.dump(userdat, f, indent=4)
4039

4140
# Functions
@@ -84,28 +83,27 @@ async def openlootbox(self, ctx: ApplicationContext, lootbox:str, amount:int):
8483
localembed = discord.Embed(title="You opened a lootbox!", description=f"The amazing rewards of your {lootbox} lootbox behold you...", color=discord.Color.gold())
8584
if lootbox == "normal lootbox":
8685
currency.add(ctx.author.id, normal_loot[0])
87-
items[str(ctx.author.id)][normal_loot[1]] += 1
88-
items[str(ctx.author.id)][normal_loot[2]] += 1
86+
items.add_item(ctx.author.id, normal_loot[1])
87+
items.add_item(ctx.author.id, normal_loot[2])
8988
localembed.add_field(name="Coins gained", value=f"**{normal_loot[0]}** coins", inline=False)
9089
localembed.add_field(name="Items recieved", value=f"You got **1 {normal_loot[1]}**!\nYou got **1 {normal_loot[2]}**!", inline=False)
9190
if lootbox == "large lootbox":
9291
currency.add(ctx.author.id, large_loot[0])
93-
items[str(ctx.author.id)][large_loot[1]] += 1
94-
items[str(ctx.author.id)][large_loot[2]] += 1
95-
items[str(ctx.author.id)][large_loot[3]] += 1
92+
items.add_item(ctx.author.id, large_loot[1])
93+
items.add_item(ctx.author.id, large_loot[2])
94+
items.add_item(ctx.author.id, large_loot[3])
9695
localembed.add_field(name="Coins gained", value=f"**{large_loot[0]}** coins", inline=False)
9796
localembed.add_field(name="Items recieved", value=f"You got **1 {large_loot[1]}**!\nYou got **1 {large_loot[2]}**!\nYou got **1 {large_loot[3]}**!", inline=False)
9897
if lootbox == "special lootbox":
9998
currency.add(ctx.author.id, special_loot[0])
100-
items[str(ctx.author.id)][special_loot[1]] += 1
101-
items[str(ctx.author.id)][special_loot[2]] += 1
102-
items[str(ctx.author.id)][special_loot[3]] += 1
103-
items[str(ctx.author.id)][special_loot[4]] += 1
104-
items[str(ctx.author.id)][special_loot[5]] += 1
99+
items.add_item(ctx.author.id, special_loot[1])
100+
items.add_item(ctx.author.id, special_loot[2])
101+
items.add_item(ctx.author.id, special_loot[3])
102+
items.add_item(ctx.author.id, special_loot[4])
103+
items.add_item(ctx.author.id, special_loot[5])
105104
localembed.add_field(name="Coins gained", value=f"**{special_loot[0]}** coins", inline=False)
106105
localembed.add_field(name="Items recieved", value=f"You got **1 {special_loot[1]}**!\nYou got **1 {special_loot[2]}**!\nYou got **1 {special_loot[3]}**!\nYou got **1 {special_loot[4]}**!\nYou got **1 {special_loot[5]}**!", inline=False)
107106
await ctx.respond(embed=localembed)
108-
save()
109107

110108
@commands.slash_command(
111109
name='beg',
@@ -225,13 +223,12 @@ async def bankrob(self, ctx: ApplicationContext, user:discord.User):
225223
)
226224
@commands.cooldown(1, 30, commands.BucketType.user)
227225
async def hunt(self, ctx: ApplicationContext):
228-
if items[str(ctx.author.id)]['rifle'] == 0: return await ctx.respond("I'd hate to see you hunt with your bare hands. Please buy a hunting rifle from the shop. ||/buy rifle||")
226+
if items.fetch_item_count(ctx.author.id, "rifle") == 0: return await ctx.respond("I'd hate to see you hunt with your bare hands. Please buy a hunting rifle from the shop. ||/buy rifle||")
229227
loot = ['rock', 'ant', 'skunk', 'boar', 'deer', 'dragon', 'nothing', 'died']
230228
choice = random.choice(loot)
231229
if choice != "nothing" and choice != "died":
232-
items[str(ctx.author.id)][choice] += 1
230+
items.add_item(ctx.author.id, choice)
233231
await ctx.respond(f"You found a {choice} while hunting!")
234-
save()
235232
elif (choice == "nothing"): await ctx.respond('You found absolutely **nothing** while hunting.')
236233
elif (choice == "died"):
237234
currency.remove(ctx.author.id, 1000)
@@ -243,12 +240,11 @@ async def hunt(self, ctx: ApplicationContext):
243240
)
244241
@commands.cooldown(1, 45, commands.BucketType.user)
245242
async def fish(self, ctx: ApplicationContext):
246-
if (items[str(ctx.author.id)]['fishingpole'] == 0): return await ctx.respond("I don't think you can fish with your bare hands... or you can just put yo hands in the water bro **giga chad moment**\nAnyway it's just better to buy a fishing pole from the shop. ||/buy fishingpole||")
243+
if items.fetch_item_count(ctx.author.id, "fishingpole") == 0: return await ctx.respond("I don't think you can fish with your bare hands... or you can just put yo hands in the water bro **giga chad moment**\nAnyway it's just better to buy a fishing pole from the shop. ||/buy fishingpole||")
247244
loot = ['shrimp', 'fish', 'rare fish', 'exotic fish', 'jellyfish', 'shark', 'nothing']
248245
choice = random.choice(loot)
249246
if choice != "nothing":
250-
items[str(ctx.author.id)][choice] += 1
251-
save()
247+
items.add_item(ctx.author.id, choice)
252248
await ctx.respond(f'You found a {choice} while hunting!')
253249
else: await ctx.respond('Looks like the fish were weary of your rod. You caught nothing.')
254250

@@ -258,7 +254,7 @@ async def fish(self, ctx: ApplicationContext):
258254
)
259255
@commands.cooldown(1, 45, commands.BucketType.user)
260256
async def dig(self, ctx: ApplicationContext):
261-
if (items[str(ctx.author.id)]['shovel'] == 0): return await ctx.respond("You're too good to have to dig with your bare hands..... at least I hope so. Please buy a shovel from the shop. ||/buy shovel||")
257+
if items.fetch_item_count(ctx.author.id, "shovel") == 0: return await ctx.respond("You're too good to have to dig with your bare hands..... at least I hope so. Please buy a shovel from the shop. ||/buy shovel||")
262258
loot = [
263259
'coins',
264260
'shovel',
@@ -276,8 +272,7 @@ async def dig(self, ctx: ApplicationContext):
276272
currency.add(ctx.author.id, random.randint('1000', '5000'))
277273
await ctx.respond('You went digging and found a bunch of coins. Nice!')
278274
elif choice != "nothing" and choice != "died":
279-
items[str(ctx.author.id)][choice] += 1
280-
save()
275+
items.add_item(ctx.author.id, choice)
281276
await ctx.respond(f'You found a {choice} while digging ')
282277
elif (choice == "nothing"): await ctx.respond('After some time of digging you eventually gave up. You got nothing.')
283278
elif (choice == "died"):
@@ -327,9 +322,8 @@ async def buy(self, ctx: ApplicationContext, name: str, quantity: int=1):
327322
rounded_taxable_amount = math.floor(taxable_amount)
328323
total_amount = amt + rounded_taxable_amount
329324
currency.remove(ctx.author.id, total_amount)
330-
items[str(ctx.author.id)][str(name)] += quantity
325+
items.add_item(ctx.author.id, str(name), quantity=quantity)
331326
currency.treasury_add(rounded_taxable_amount)
332-
save()
333327
localembed = discord.Embed(
334328
title=f'You just bought {quantity} {shopitem[name]["stylized name"]}!',
335329
description=f"**Your Purchase Invoice**\n\nItem: {quantity} {name.lower()}\n---------------\nBase Amount: {amt} coins\nTax: 3%\nTaxable Amount: {taxable_amount} coins\nTaxable Amount (rounded): {rounded_taxable_amount} coins\n**Charged Amount:** {total_amount} coins",
@@ -348,16 +342,15 @@ async def buy(self, ctx: ApplicationContext, name: str, quantity: int=1):
348342
async def sell(self, ctx: ApplicationContext, name: str, quantity: int=1):
349343
try:
350344
if shopitem[name]["sellable"] != True: return await ctx.respond('Dumb, you can\'t sell this item.')
351-
if quantity > items[str(ctx.author.id)][str(name)]: return await ctx.respond('You can\'t sell more than you have.')
352-
items[str(ctx.author.id)][str(name)] -= quantity
345+
if quantity > items.fetch_item_count(ctx.author.id, str(name)): return await ctx.respond('You can\'t sell more than you have.')
346+
items.remove_item(ctx.author.id, str(name), quantity=quantity)
353347
ttl = shopitem[name]["sell price"]*quantity
354348
currency.add(ctx.author.id, int(ttl))
355-
save()
356349
localembed = discord.Embed(title='Item sold', description=f'You successfully sold {quantity} {name} for {ttl} coins!', color=color)
357350
localembed.set_footer(text='Thank you for your business.')
358351
await ctx.respond(embed=localembed)
359352
except KeyError: await ctx.respond('what are you doing that item doesn\'t even exist')
360-
353+
361354
@commands.slash_command(
362355
name="gift",
363356
description="Gifts a (giftable) item to anyone you want"
@@ -368,18 +361,17 @@ async def sell(self, ctx: ApplicationContext, name: str, quantity: int=1):
368361
async def gift(self, ctx: ApplicationContext, user:discord.User, item:str, amount:int=1):
369362
try:
370363
if amount < 1: return await ctx.respond("You can't gift less than 1 of those!", ephemeral=True)
371-
elif items[str(ctx.author.id)][item] < amount: return await ctx.respond("You don't have enough of those to gift!", ephemeral=True)
364+
elif items.fetch_item_count(ctx.author.id, item) < amount: return await ctx.respond("You don't have enough of those to gift!", ephemeral=True)
372365
elif shopitem[item]["giftable"] == False: return await ctx.respond("You can't sell that item!", ephemeral=True)
373-
items[str(user.id)][item] += amount
374-
items[str(ctx.author.id)][item] -= amount
375-
save()
366+
items.add_item(user.id, item, amount=amount)
367+
items.remove_item(ctx.author.id, item, amount=amount)
376368
localembed = discord.Embed(
377369
title="Gift successful!",
378370
description=f"You just gifted {amount} **{item}**s to {user.display_name}!",
379371
color=discord.Color.green()
380372
)
381-
localembed.add_field(name="Now they have", value=f"**{items[str(user.id)][item]} {item}**s")
382-
localembed.add_field(name="and you have", value=f"**{items[str(ctx.author.id)][item]} {item}**s")
373+
localembed.add_field(name="Now they have", value=f"**{items.fetch_item_count(user.id, item)} {item}**s")
374+
localembed.add_field(name="and you have", value=f"**{items.fetch_item_count(ctx.author.id, item)} {item}**s")
383375
await ctx.respond(embed=localembed)
384376
except KeyError as e:
385377
utils.logger.error(e)
@@ -507,7 +499,7 @@ async def donate(self, ctx: ApplicationContext, id:str, amount):
507499
async def scout(self, ctx: ApplicationContext):
508500
if (randint(1, 100) <= 90):
509501
x = randint(550, 2000)
510-
if items[str(ctx.author.id)]['binoculars'] >= 1:
502+
if items.fetch_item_count(ctx.author.id, "binoculars") >= 1:
511503
x *= 1.425
512504
x = math.floor(x)
513505
else: pass
@@ -527,10 +519,9 @@ async def autogrind(self, ctx: ApplicationContext):
527519
ie = shopitem.keys()
528520
items_reward = [random.choice(list(ie)), random.choice(list(ie)), random.choice(list(ie))]
529521
currency.add(ctx.author.id, coins_reward)
530-
items[str(ctx.author.id)][items_reward[0]] += 1
531-
items[str(ctx.author.id)][items_reward[1]] += 1
532-
items[str(ctx.author.id)][items_reward[2]] += 1
533-
save()
522+
items.add_item(ctx.author.id, items_reward[0])
523+
items.add_item(ctx.author.id, items_reward[1])
524+
items.add_item(ctx.author.id, items_reward[2])
534525
localembed = discord.Embed(title="Autogrind has completed!", description=f"**Your rewards**\n\nYou got **{coins_reward}** coins!\nYou got **1 {shopitem[items_reward[0]]['stylized name']}**!\nYou got **1 {shopitem[items_reward[1]]['stylized name']}**!\nYou got **1 {shopitem[items_reward[2]]['stylized name']}!**", color=discord.Color.green())
535526
await ctx.author.send(embed = localembed)
536527

@@ -605,17 +596,17 @@ async def inventory(self, ctx: ApplicationContext, user:discord.User = None):
605596
elif shopitem[x]['collection'] == "power-up": filtered_powerups.append(x)
606597
elif shopitem[x]['collection'] == "lootbox": filtered_lootboxes.append(x)
607598
for g in filtered_utility_items:
608-
if items[str(user.id)][g] != 0:
609-
parsed_utility_items += f"{shopitem[g]['stylized name']} `ID: {g}`: {items[str(user.id)][g]}\n"
599+
if items.fetch_item_count(user.id, g) != 0:
600+
parsed_utility_items += f"{shopitem[g]['stylized name']} `ID: {g}`: {items.fetch_item_count(user.id, g)}\n"
610601
for g in filtered_sellables:
611-
if items[str(user.id)][g] != 0:
612-
parsed_sellables += f"{shopitem[g]['stylized name']} `ID: {g}`: {items[str(user.id)][g]}\n"
602+
if items.fetch_item_count(user.id, g) != 0:
603+
parsed_sellables += f"{shopitem[g]['stylized name']} `ID: {g}`: {items.fetch_item_count(user.id, g)}\n"
613604
for g in filtered_powerups:
614-
if items[str(user.id)][g] != 0:
615-
parsed_powerups += f"{shopitem[g]['stylized name']} `ID: {g}`: {items[str(user.id)][g]}\n"
605+
if items.fetch_item_count(user.id, g) != 0:
606+
parsed_powerups += f"{shopitem[g]['stylized name']} `ID: {g}`: {items.fetch_item_count(user.id, g)}\n"
616607
for g in filtered_lootboxes:
617-
if items[str(user.id)][g] != 0:
618-
parsed_lootboxes += f"{shopitem[g]['stylized name']} `ID: {g}`: {items[str(user.id)][g]}\n"
608+
if items.fetch_item_count(user.id, g) != 0:
609+
parsed_lootboxes += f"{shopitem[g]['stylized name']} `ID: {g}`: {items.fetch_item_count(user.id, g)}\n"
619610
if parsed_utility_items != "": localembed.add_field(name='Utility', value=parsed_utility_items, inline=False)
620611
if parsed_sellables != "": localembed.add_field(name='Sellables', value=parsed_sellables, inline=False)
621612
if parsed_powerups != "": localembed.add_field(name='Power-ups', value=parsed_powerups, inline=False)

framework/isobot/db/items.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ def add_item(self, user_id: int, item: str, *, quantity: int = 1) -> int:
3737
"""Adds an item of choice to a specific user id."""
3838
items = self.load()
3939
items[str(user_id)][item] += quantity
40-
self.save()
40+
self.save(items)
4141
return 0
4242

4343
def remove_item(self, user_id: int, item: str, *, quantity: int = 1) -> int:
4444
"""Removes an item of choice from a specific user id."""
4545
items = self.load()
46-
items[str(user_id)][item] += quantity
47-
self.save()
46+
items[str(user_id)][item] -= quantity
47+
self.save(items)
4848
return 0
4949

5050
def fetch_item_count(self, user_id: int, item: str) -> int:

0 commit comments

Comments
 (0)