From 1748992ed72e522e8eed46acb17ffbcd14cc57a7 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sat, 23 Jul 2022 18:25:48 +0530 Subject: [PATCH 01/10] Add Beta NKA framework --- framework/isobot/colors.py | 7 ++++++ framework/isobot/currency.py | 45 ++++++++++++++++++++++++++++++++++++ framework/logger.py | 39 +++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 framework/isobot/colors.py create mode 100644 framework/isobot/currency.py create mode 100644 framework/logger.py diff --git a/framework/isobot/colors.py b/framework/isobot/colors.py new file mode 100644 index 00000000..9e572f9f --- /dev/null +++ b/framework/isobot/colors.py @@ -0,0 +1,7 @@ +"""Library used for stdout colors.""" +class Colors(): + """Contains general stdout colors.""" + cyan = '\033[96m' + red = '\033[91m' + green = '\033[92m' + end = '\033[0m' \ No newline at end of file diff --git a/framework/isobot/currency.py b/framework/isobot/currency.py new file mode 100644 index 00000000..aae3c799 --- /dev/null +++ b/framework/isobot/currency.py @@ -0,0 +1,45 @@ +import json +import discord +from colors import Colors + +class CurrencyAPI(Colors): + """The isobot API used for managing currency. + + Valid commands: + - add(user, amount) + - remove(user, amount) + - reset(user) + - deposit(user, amount) + - withdraw(user, amount)""" + def __init__(self, db_path:str): + self.db_path = db_path + print(f"[Framework/Loader] {Colors.green}CurrencyAPI initialized.{Colors.end}") + def save(self): + """Saves databases cached on memory.""" + with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4) + def add(self, user:discord.User, amount:int): + """Adds balance to the specified user.""" + currency["wallet"][str(user.id)] += amount + self.save() + def remove(self, user:discord.User, amount:int): + """Removes balance from the specified user.""" + currency["wallet"][str(user.id)] -= amount + self.save() + def reset(self, user:discord.User): + """Resets the specified user's balance.""" + currency["wallet"][str(user.id)] = 0 + currency["bank"][str(user.id)] = 0 + self.save() + print(f"[Framework/CurrencyAPI] Currency data for \"{user.id}\" has been wiped.") + def deposit(self, user:discord.User, amount:int): + """Moves a specified amount of coins to the user's bank.""" + currency["bank"][str(user.id)] += amount + currency["wallet"][str(user.id)] -= amount + save() + print(f"[Framework/CurrencyAPI] Moved {amount} coins to bank. User: {user} [{user.id}]") + def withdraw(self, user:discord.User, amount:int): + """Moves a specified amount of coins to the user's wallet.""" + currency["wallet"][str(user.id)] += amount + currency["bank"][str(user.id)] -= amount + save() + print(f"[Framework/CurrencyAPI] Moved {amount} coins to wallet. User: {user} [{user.id}]") \ No newline at end of file diff --git a/framework/logger.py b/framework/logger.py new file mode 100644 index 00000000..8ff5d3de --- /dev/null +++ b/framework/logger.py @@ -0,0 +1,39 @@ +import time +import datetime +from isobot.colors import Colors + +class Logger(Colors): + """The library used for logging information. + + Valid commands: + - info(text) + - warn(text) + - error(text)""" + def __init__(self, log_path:str, error_path:str): + self.log_path = log_path + self.error_path = error_path + print(f"[Framework/Loader] {Colors.green}Logger initialized.{Colors.end}") + def info(self, text:str, *, nolog=False): + current_time = datetime.datetime.now().strftime("%H:%M:%S") + print(f'[{current_time}/INFO] {text}') + if nolog == True: pass + else: + with open(self.log_path, 'a') as f: + f.write(f'[{current_time}/INFO] {text}\n') + f.close() + def warn(self, text:str, *, nolog=False): + current_time = datetime.datetime.now().strftime("%H:%M:%S") + print(f'[{current_time}/WARN] {text}') + if nolog == True: pass + else: + with open(self.log_path, 'a') as f: + f.write(f'[{current_time}/WARN] {text}\n') + f.close() + def error(self, text:str, *, nolog=False): + current_time = datetime.datetime.now().strftime("%H:%M:%S") + print(f'{Colours.red}[{current_time}/ERROR] {text}{Colours.end}') + if nolog == True: pass + else: + with open(self.error_path, 'a') as f: + f.write(f'[{current_time}/ERROR] {text}\n') + f.close() From 0a12d1a29934348d5672b94310be3c1c599e6066 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sat, 23 Jul 2022 18:33:48 +0530 Subject: [PATCH 02/10] Add imports for framework --- main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.py b/main.py index 53522731..4f1a76a5 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,8 @@ from discord_slash import SlashCommand, SlashContext from discord_slash.utils.manage_commands import create_choice, create_option import api.auth, utils.logger, utils.ping +from framework import * +from framework.isobot import * # Slash option types: # sub command: 1 From cd24234bebc44680aaa290b49a42ef2a96beab74 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sat, 23 Jul 2022 18:35:12 +0530 Subject: [PATCH 03/10] Remove blank file --- framework/e | 1 - 1 file changed, 1 deletion(-) delete mode 100644 framework/e diff --git a/framework/e b/framework/e deleted file mode 100644 index 8b137891..00000000 --- a/framework/e +++ /dev/null @@ -1 +0,0 @@ - From 60c69ceafb602aff050749aac5a68f860163c15d Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sat, 23 Jul 2022 18:36:17 +0530 Subject: [PATCH 04/10] Remove old class `CurrencyAPI` This has been removed as it is now included in the framework. --- main.py | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/main.py b/main.py index 4f1a76a5..6af588be 100644 --- a/main.py +++ b/main.py @@ -79,32 +79,6 @@ class plugins: levelling:bool = False music:bool = False -class CurrencyAPI(colors): - """The isobot API used for managing currency. - - Valid commands: - - add(user, amount) - - remove(user, amount) - - reset(user)""" - - def __init__(self, db_path:str): - self.db_path = db_path - print(f"[Framework/Loader] {colors.green}CurrencyAPI initialized.{colors.end}") - def add(user:discord.User, amount:int): - """Adds balance to the specified user.""" - currency["wallet"][str(user.id)] += amount - save() - def remove(user:discord.User, amount:int): - """Removes balance from the specified user.""" - currency["wallet"][str(user.id)] -= amount - save() - def reset(user:discord.User): - """Resets the specified user's balance.""" - currency["wallet"][str(user.id)] = 0 - currency["bank"][str(user.id)] = 0 - save() - print(f"[Framework/CurrencyAPI] Currency data for \"{user.id}\" has been wiped.") - currency_unused = CurrencyAPI(f'{wdir}/database/currency.json') # Initialize part of the framework (Currency) #Events From a2629b16ee0a832acf7afb116863f99177c3e554 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sat, 23 Jul 2022 18:39:36 +0530 Subject: [PATCH 05/10] Change `CurrencyAPI` class to `currency.CurrencyAPI` for framework support --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 6af588be..3eabf4a4 100644 --- a/main.py +++ b/main.py @@ -79,7 +79,7 @@ class plugins: levelling:bool = False music:bool = False -currency_unused = CurrencyAPI(f'{wdir}/database/currency.json') # Initialize part of the framework (Currency) +currency_unused = currency.CurrencyAPI(f'{wdir}/database/currency.json') # Initialize part of the framework (Currency) #Events @client.event From 3036e8ea7fe8452673bb9120458d2b0965d01522 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sat, 23 Jul 2022 18:42:45 +0530 Subject: [PATCH 06/10] Add newlines at end of files Github got mad at me xd --- framework/isobot/colors.py | 2 +- framework/isobot/currency.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/isobot/colors.py b/framework/isobot/colors.py index 9e572f9f..fe87c5ab 100644 --- a/framework/isobot/colors.py +++ b/framework/isobot/colors.py @@ -4,4 +4,4 @@ class Colors(): cyan = '\033[96m' red = '\033[91m' green = '\033[92m' - end = '\033[0m' \ No newline at end of file + end = '\033[0m' diff --git a/framework/isobot/currency.py b/framework/isobot/currency.py index aae3c799..700e0b26 100644 --- a/framework/isobot/currency.py +++ b/framework/isobot/currency.py @@ -42,4 +42,4 @@ def withdraw(self, user:discord.User, amount:int): currency["wallet"][str(user.id)] += amount currency["bank"][str(user.id)] -= amount save() - print(f"[Framework/CurrencyAPI] Moved {amount} coins to wallet. User: {user} [{user.id}]") \ No newline at end of file + print(f"[Framework/CurrencyAPI] Moved {amount} coins to wallet. User: {user} [{user.id}]") From 1f43fc7728aed01df3700e5629ce6972fe527489 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sat, 23 Jul 2022 18:52:44 +0530 Subject: [PATCH 07/10] Fix attribute error for `CurrencyAPI` class --- .gitignore | 1 + main.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f4e6ef38..f156d0c4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ api/auth.py database/currency.json database/item.json database/warnings.json +*.pyc diff --git a/main.py b/main.py index 3eabf4a4..ac7b56da 100644 --- a/main.py +++ b/main.py @@ -79,7 +79,7 @@ class plugins: levelling:bool = False music:bool = False -currency_unused = currency.CurrencyAPI(f'{wdir}/database/currency.json') # Initialize part of the framework (Currency) +currency_unused = isobot.currency.CurrencyAPI(f'{wdir}/database/currency.json') # Initialize part of the framework (Currency) #Events @client.event From 2e4bdee6a14ea5bdd57d7f8764e0d0072bd16c17 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sat, 23 Jul 2022 18:56:53 +0530 Subject: [PATCH 08/10] Fix framework returning definition errors --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index ac7b56da..40dd5297 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,7 @@ from discord_slash.utils.manage_commands import create_choice, create_option import api.auth, utils.logger, utils.ping from framework import * -from framework.isobot import * +from framework.isobot import currency, colors # Slash option types: # sub command: 1 @@ -79,7 +79,7 @@ class plugins: levelling:bool = False music:bool = False -currency_unused = isobot.currency.CurrencyAPI(f'{wdir}/database/currency.json') # Initialize part of the framework (Currency) +currency_unused = currency.CurrencyAPI(f'{wdir}/database/currency.json') # Initialize part of the framework (Currency) #Events @client.event From 159377d601d08802cc38363e81dff7030302a9c9 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sat, 23 Jul 2022 19:02:23 +0530 Subject: [PATCH 09/10] Resolve framework imports --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 40dd5297..f6e39937 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,7 @@ from discord_slash.utils.manage_commands import create_choice, create_option import api.auth, utils.logger, utils.ping from framework import * -from framework.isobot import currency, colors +import framework.isobot.currency # Slash option types: # sub command: 1 @@ -79,7 +79,7 @@ class plugins: levelling:bool = False music:bool = False -currency_unused = currency.CurrencyAPI(f'{wdir}/database/currency.json') # Initialize part of the framework (Currency) +currency_unused = framework.isobot.currency.CurrencyAPI(f'{wdir}/database/currency.json') # Initialize part of the framework (Currency) #Events @client.event From 56151f22189f2475a7d765bdb76b678abd624d50 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sat, 23 Jul 2022 19:03:38 +0530 Subject: [PATCH 10/10] Replace import with direct class `Colours` The import was causing some weird import errors, so I replaced with a direct class which actually helps. --- framework/isobot/currency.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/framework/isobot/currency.py b/framework/isobot/currency.py index 700e0b26..c01905d0 100644 --- a/framework/isobot/currency.py +++ b/framework/isobot/currency.py @@ -1,6 +1,12 @@ import json import discord -from colors import Colors + +class Colors(): + """Contains general stdout colors.""" + cyan = '\033[96m' + red = '\033[91m' + green = '\033[92m' + end = '\033[0m' class CurrencyAPI(Colors): """The isobot API used for managing currency.