From 407e69c0715eca59b2f7782bc2375a9a3fc58a44 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:53:05 +0530 Subject: [PATCH 1/5] Add framework module to fetch shop information --- framework/isobot/shop.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 framework/isobot/shop.py diff --git a/framework/isobot/shop.py b/framework/isobot/shop.py new file mode 100644 index 00000000..d7ea9158 --- /dev/null +++ b/framework/isobot/shop.py @@ -0,0 +1,27 @@ +"""Framework module used to fetch information on isobot's item shop.""" + +# Imports +import json + +# Functions +class ShopData: + """Initializes the isobot shop.""" + def __init__(self, db_path: str): + self.db_path = db_path + with open(db_path, 'r') as f: self.db = json.load(f) + + def get_item_ids(self) -> list: + """Fetches and returns all of the shop item ids.""" + json_list = list() + for h in self.db: json_list.append(str(h)) + return json_list + + def get_item_names(self) -> list: + """Fetches and returns all of the shop item names.""" + json_list = list() + for h in self.db: json_list.append(str(h["stylized name"])) + return json_list + + def get_raw_data(self) -> dict: + """Fetches the entire raw shop data, in a `dict` format.""" + return self.db From ab8d7583614ac30d5f424bc2b2e46cc1d74e807e Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:56:55 +0530 Subject: [PATCH 2/5] Add framework modulle to modify the items db --- framework/isobot/db/items.py | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 framework/isobot/db/items.py diff --git a/framework/isobot/db/items.py b/framework/isobot/db/items.py new file mode 100644 index 00000000..9cefa879 --- /dev/null +++ b/framework/isobot/db/items.py @@ -0,0 +1,53 @@ +"""The framework module library used for managing isobot's items database.""" +# Imports +import json +from framework.isobot.shop import ShopData + +# Variables +shop = ShopData("config/shop.json") +shopitem = shop.get_item_ids() + +# Functions +class Items(): + """Used to initialize the items database.""" + def __init__(self): + print("[framework/db/Levelling] Items db library initialized.") + + def load(self) -> dict: + """Fetches and returns the latest data from the items database.""" + with open("database/items.json", 'r', encoding="utf8") as f: db = json.load(f) + return db + + def save(self, data: dict) -> int: + """Dumps all cached data to your local machine.""" + with open("database/items.json", 'w+', encoding="utf8") as f: json.dump(data, f, indent=4) + return 0 + + def generate(self, user_id: int) -> int: + """Generates a new database key for the specified user id in the items database.""" + items = self.load() + if str(user_id) not in items: items[str(user_id)] = {} + for z in shopitem: + if z not in items[str(user_id)]: + items[str(user_id)][str(z)] = 0 + self.save(items) + return 0 + + def add_item(self, user_id: int, item: str, *, quantity: int = 1) -> int: + """Adds an item of choice to a specific user id.""" + items = self.load() + items[str(user_id)][item] += quantity + self.save() + return 0 + + def remove_item(self, user_id: int, item: str, *, quantity: int = 1) -> int: + """Removes an item of choice from a specific user id.""" + items = self.load() + items[str(user_id)][item] += quantity + self.save() + return 0 + + def fetch_item_count(self, user_id: int, item: str) -> int: + """Fetches and returns the amount of a specific item owned by the user.""" + items = self.load() + return items[str(user_id)][item] From 5e269e8c8941d5ffa09d01fadcc9eadbfdb70f6d Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 20 Oct 2023 21:04:33 +0530 Subject: [PATCH 3/5] Update to support new items db library --- main.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 38e5bd45..3e5c78ee 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ from math import floor from random import randint from framework.isobot import currency, colors, settings -from framework.isobot.db import levelling +from framework.isobot.db import levelling, items from discord import ApplicationContext, option from discord.ext import commands from cogs.economy import new_userdat @@ -25,7 +25,6 @@ #Variables client = discord.Bot() color = discord.Color.random() -with open('database/items.json', 'r', encoding="utf-8") as f: items = json.load(f) with open('config/shop.json', 'r', encoding="utf-8") as f: shopitem = json.load(f) with open('database/presence.json', 'r', encoding="utf-8") as f: presence = json.load(f) with open('config/commands.json', 'r', encoding="utf-8") as f: commandsdb = json.load(f) @@ -35,7 +34,6 @@ #Pre-Initialization Commands def save(): """Dumps all cached data to the local databases.""" - with open('database/items.json', 'w+', encoding="utf-8") as e: json.dump(items, e, indent=4) with open('database/presence.json', 'w+', encoding="utf-8") as e: json.dump(presence, e, indent=4) with open('database/automod.json', 'w+', encoding="utf-8") as e: json.dump(automod_config, e, indent=4) @@ -58,6 +56,7 @@ def save(): currency = currency.CurrencyAPI("database/currency.json", "logs/currency.log") settings = settings.Configurator() levelling = levelling.Levelling() +items = items.Items() # Theme Loader themes = False # True: enables themes; False: disables themes; @@ -99,8 +98,7 @@ async def on_message(ctx): create_isocoin_key(ctx.author.id) new_userdat(ctx.author.id) settings.generate(ctx.author.id) - if str(ctx.author.id) not in items: - items[str(ctx.author.id)] = {} + items.generate(ctx.author.id) levelling.generate(ctx.author.id) if str(ctx.guild.id) not in automod_config: automod_config[str(ctx.guild.id)] = { @@ -113,9 +111,6 @@ async def on_message(ctx): } } } - for z in shopitem: - if z in items[str(ctx.author.id)]: pass - else: items[str(ctx.author.id)][str(z)] = 0 save() uList = list() if str(ctx.guild.id) in presence: From 94a79750b8de16dd50048774c64aa194d871c6df Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 20 Oct 2023 21:06:14 +0530 Subject: [PATCH 4/5] Fix variable type incompatibility --- cogs/economy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/economy.py b/cogs/economy.py index 2df084d8..7b1b1fa8 100644 --- a/cogs/economy.py +++ b/cogs/economy.py @@ -563,7 +563,7 @@ async def deposit(self, ctx: ApplicationContext, amount): elif int(amount) > currency.get_wallet(ctx.author.id): return await ctx.respond('The amount to deposit must not be more than what you have in your wallet!', ephemeral=True) currency.deposit(ctx.author.id, int(amount)) localembed = discord.Embed(title="Deposit successful", description=f"You deposited `{amount}` coin(s) to your bank account.", color=color) - localembed.add_field(name="You previously had", value=f"`{currency.get_bank(ctx.author.id) - amount} coins` in your bank account") + localembed.add_field(name="You previously had", value=f"`{currency.get_bank(ctx.author.id) - int(amount)} coins` in your bank account") localembed.add_field(name="Now you have", value=f"`{currency.get_bank(ctx.author.id)} coins` in your bank account") await ctx.respond(embed=localembed) @@ -581,7 +581,7 @@ async def withdraw(self, ctx: ApplicationContext, amount): elif int(amount) > currency.get_bank(ctx.author.id): return await ctx.respond('The amount to withdraw must not be more than what you have in your bank account!', ephemeral=True) currency.withdraw(ctx.author.id, int(amount)) localembed = discord.Embed(title="Withdraw successful", description=f"You withdrew `{amount}` coin(s) from your bank account.", color=color) - localembed.add_field(name="You previously had", value=f"`{currency.get_wallet(ctx.author.id) - amount} coins` in your wallet") + localembed.add_field(name="You previously had", value=f"`{currency.get_wallet(ctx.author.id) - int(amount)} coins` in your wallet") localembed.add_field(name="Now you have", value=f"`{currency.get_wallet(ctx.author.id)} coins` in your wallet") await ctx.respond(embed=localembed) From c904b9d860100dc2487d842f79d0f552ed5da810 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 20 Oct 2023 21:08:58 +0530 Subject: [PATCH 5/5] Add support for new ShopData system --- cogs/economy.py | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/cogs/economy.py b/cogs/economy.py index 7b1b1fa8..726e9085 100644 --- a/cogs/economy.py +++ b/cogs/economy.py @@ -8,33 +8,19 @@ import utils.logger import asyncio import framework.isobot.currency +from framework.isobot.shop import ShopData from framework.isobot.db import levelling from random import randint from discord import option, ApplicationContext from discord.ext import commands -# Classes -class ShopData: - def __init__(self, db_path: str): - self.db_path = db_path - with open(db_path, 'r') as f: self.config = json.load(f) - - def get_item_ids(self) -> list: - json_list = list() - for h in self.config: json_list.append(str(h)) - return json_list - - def get_item_names(self) -> list: - json_list = list() - for h in self.config: json_list.append(str(h["stylized name"])) - return json_list - # Variables color = discord.Color.random() currency = framework.isobot.currency.CurrencyAPI("database/currency.json", "logs/currency.log") levelling = levelling.Levelling() shop_data = ShopData("config/shop.json") all_item_ids = shop_data.get_item_ids() +shopitem = shop_data.get_raw_data() jobs = [ "Discord mod", "YouTuber", @@ -46,7 +32,6 @@ def get_item_names(self) -> list: ] with open("database/items.json", 'r') as f: items = json.load(f) -with open("config/shop.json", 'r') as f: shopitem = json.load(f) with open("database/user_data.json", 'r') as f: userdat = json.load(f) def save():