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/framework/e b/framework/e deleted file mode 100644 index 8b137891..00000000 --- a/framework/e +++ /dev/null @@ -1 +0,0 @@ - diff --git a/framework/isobot/colors.py b/framework/isobot/colors.py new file mode 100644 index 00000000..fe87c5ab --- /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' diff --git a/framework/isobot/currency.py b/framework/isobot/currency.py new file mode 100644 index 00000000..c01905d0 --- /dev/null +++ b/framework/isobot/currency.py @@ -0,0 +1,51 @@ +import json +import discord + +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. + + 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}]") 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() diff --git a/main.py b/main.py index 53522731..f6e39937 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 * +import framework.isobot.currency # Slash option types: # sub command: 1 @@ -77,33 +79,7 @@ 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) +currency_unused = framework.isobot.currency.CurrencyAPI(f'{wdir}/database/currency.json') # Initialize part of the framework (Currency) #Events @client.event