GoldyBot.cache
1from __future__ import annotations 2 3import GoldyBot 4from nextcord.ext import commands 5from typing import Dict, List 6 7MODULE_NAME = "CACHE" 8 9main_cache_dict = { 10 "client" : None, 11 12 "goldy_class" : None, 13 14 "database" : None, 15 16 "modules" : { 17 18 }, 19 20 "internal_modules" : { 21 "goldy" : { 22 "extensions" : { 23 "core" : { 24 "commands" : [] 25 } 26 } 27 } 28 }, 29 30 "guilds" : { 31 32 }, 33 34 "events" : { 35 "on_message" : [], 36 "on_join": [], 37 "on_leave": [] 38 }, 39 40 "bot_stop" : False 41} 42 43def database() -> GoldyBot.database.Database|None: 44 """Returns current database instance.""" 45 return main_cache_dict["database"] 46 47def client() -> commands.Bot|None: 48 """Returns the nextcord client from cache.""" 49 return main_cache_dict["client"] 50 51def goldy_class() -> GoldyBot.Goldy|None: 52 """Returns the Goldy Bot main control class from cache.""" 53 return main_cache_dict["goldy_class"] 54 55def events() -> Dict[str, List[function]]: 56 return main_cache_dict["events"] 57 58class FindGuilds(): 59 """A class dedicated to finding guilds from cache.""" 60 def __init__(self, goldy_config:GoldyBot.config.Config=None): 61 if goldy_config == None: 62 self.goldy_config = GoldyBot.config.Config(GoldyBot.files.File(GoldyBot.paths.GOLDY_CONFIG_JSON)) 63 else: 64 self.goldy_config = goldy_config 65 66 def find_object_by_id(self, guild_id): 67 """Searches cache and returns guild object of found guild.""" 68 try: 69 guild_code_name = self.goldy_config.read("allowed_guilds")[f"{guild_id}"] 70 guild_object:GoldyBot.utility.guilds.guild.Guild = main_cache_dict["guilds"][f"{guild_code_name}"]["object"] 71 #TODO: #23 Add return None if guild is not found. 72 return guild_object 73 except KeyError: 74 return None 75 76 def find_object_by_code_name(self, guild_code_name:str): 77 """This is faster than finding the guild by id.""" 78 try: 79 guild_object:GoldyBot.utility.guilds.guild.Guild = main_cache_dict["guilds"][f"{guild_code_name}"]["object"] 80 #TODO: #23 Add return None if guild is not found. 81 return guild_object 82 except KeyError: 83 return None 84 85class FindModules(): 86 """A class dedicated to finding loaded Goldy Bot modules from cache.""" 87 def __init__(self): 88 pass 89 90 def find_object_by_module_name(self, module_name:str): 91 """A fast way to grab the class object of a module from Goldy Bot cache.""" 92 GoldyBot.logging.log(f"[{MODULE_NAME}] Finding module '{module_name}' by name...") 93 try: 94 module_object:GoldyBot.modules.Module = main_cache_dict["modules"][f"{module_name}"]["object"] 95 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the module '{module_name}'!") 96 return module_object 97 except KeyError: 98 try: 99 module_object:GoldyBot.modules.Module = main_cache_dict["internal_modules"][f"{module_name}"]["object"] 100 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the module '{module_name}'!") 101 return module_object 102 except KeyError: 103 GoldyBot.logging.log("error", f"[{MODULE_NAME}] Failed to find the module '{module_name}'.") 104 return None 105 106class FindExtensions(): 107 """A class dedicated to finding loaded Goldy Bot extensions from cache.""" 108 def __init__(self): 109 pass 110 111 def find_object_by_extension_name(self, extension_name:str): 112 """A fast way to grab the class object of a extension from Goldy Bot cache.""" 113 GoldyBot.logging.log(f"[{MODULE_NAME}] Finding extension '{extension_name}' by name...") 114 for module in main_cache_dict["modules"]: 115 try: 116 extension_object:GoldyBot.ext.extensions.Extension = main_cache_dict["modules"][f"{module}"]["extensions"][f"{extension_name}"]["object"] 117 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the extension '{extension_name}'!") 118 return extension_object 119 except KeyError: 120 pass 121 122 for module in main_cache_dict["internal_modules"]: 123 try: 124 extension_object:GoldyBot.ext.extensions.Extension = main_cache_dict["internal_modules"][f"{module}"]["extensions"][f"{extension_name}"]["object"] 125 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the extension '{extension_name}'!") 126 return extension_object 127 except KeyError: 128 pass 129 130 GoldyBot.logging.log("error", f"[{MODULE_NAME}] Failed to find the extension '{extension_name}'.") 131 return None
44def database() -> GoldyBot.database.Database|None: 45 """Returns current database instance.""" 46 return main_cache_dict["database"]
Returns current database instance.
def
client() -> nextcord.ext.commands.bot.Bot | None:
48def client() -> commands.Bot|None: 49 """Returns the nextcord client from cache.""" 50 return main_cache_dict["client"]
Returns the nextcord client from cache.
52def goldy_class() -> GoldyBot.Goldy|None: 53 """Returns the Goldy Bot main control class from cache.""" 54 return main_cache_dict["goldy_class"]
Returns the Goldy Bot main control class from cache.
def
events() -> 'Dict[str, List[function]]':
class
FindGuilds:
59class FindGuilds(): 60 """A class dedicated to finding guilds from cache.""" 61 def __init__(self, goldy_config:GoldyBot.config.Config=None): 62 if goldy_config == None: 63 self.goldy_config = GoldyBot.config.Config(GoldyBot.files.File(GoldyBot.paths.GOLDY_CONFIG_JSON)) 64 else: 65 self.goldy_config = goldy_config 66 67 def find_object_by_id(self, guild_id): 68 """Searches cache and returns guild object of found guild.""" 69 try: 70 guild_code_name = self.goldy_config.read("allowed_guilds")[f"{guild_id}"] 71 guild_object:GoldyBot.utility.guilds.guild.Guild = main_cache_dict["guilds"][f"{guild_code_name}"]["object"] 72 #TODO: #23 Add return None if guild is not found. 73 return guild_object 74 except KeyError: 75 return None 76 77 def find_object_by_code_name(self, guild_code_name:str): 78 """This is faster than finding the guild by id.""" 79 try: 80 guild_object:GoldyBot.utility.guilds.guild.Guild = main_cache_dict["guilds"][f"{guild_code_name}"]["object"] 81 #TODO: #23 Add return None if guild is not found. 82 return guild_object 83 except KeyError: 84 return None
A class dedicated to finding guilds from cache.
FindGuilds(goldy_config: GoldyBot.config.Config = None)
def
find_object_by_id(self, guild_id):
67 def find_object_by_id(self, guild_id): 68 """Searches cache and returns guild object of found guild.""" 69 try: 70 guild_code_name = self.goldy_config.read("allowed_guilds")[f"{guild_id}"] 71 guild_object:GoldyBot.utility.guilds.guild.Guild = main_cache_dict["guilds"][f"{guild_code_name}"]["object"] 72 #TODO: #23 Add return None if guild is not found. 73 return guild_object 74 except KeyError: 75 return None
Searches cache and returns guild object of found guild.
def
find_object_by_code_name(self, guild_code_name: str):
77 def find_object_by_code_name(self, guild_code_name:str): 78 """This is faster than finding the guild by id.""" 79 try: 80 guild_object:GoldyBot.utility.guilds.guild.Guild = main_cache_dict["guilds"][f"{guild_code_name}"]["object"] 81 #TODO: #23 Add return None if guild is not found. 82 return guild_object 83 except KeyError: 84 return None
This is faster than finding the guild by id.
class
FindModules:
86class FindModules(): 87 """A class dedicated to finding loaded Goldy Bot modules from cache.""" 88 def __init__(self): 89 pass 90 91 def find_object_by_module_name(self, module_name:str): 92 """A fast way to grab the class object of a module from Goldy Bot cache.""" 93 GoldyBot.logging.log(f"[{MODULE_NAME}] Finding module '{module_name}' by name...") 94 try: 95 module_object:GoldyBot.modules.Module = main_cache_dict["modules"][f"{module_name}"]["object"] 96 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the module '{module_name}'!") 97 return module_object 98 except KeyError: 99 try: 100 module_object:GoldyBot.modules.Module = main_cache_dict["internal_modules"][f"{module_name}"]["object"] 101 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the module '{module_name}'!") 102 return module_object 103 except KeyError: 104 GoldyBot.logging.log("error", f"[{MODULE_NAME}] Failed to find the module '{module_name}'.") 105 return None
A class dedicated to finding loaded Goldy Bot modules from cache.
def
find_object_by_module_name(self, module_name: str):
91 def find_object_by_module_name(self, module_name:str): 92 """A fast way to grab the class object of a module from Goldy Bot cache.""" 93 GoldyBot.logging.log(f"[{MODULE_NAME}] Finding module '{module_name}' by name...") 94 try: 95 module_object:GoldyBot.modules.Module = main_cache_dict["modules"][f"{module_name}"]["object"] 96 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the module '{module_name}'!") 97 return module_object 98 except KeyError: 99 try: 100 module_object:GoldyBot.modules.Module = main_cache_dict["internal_modules"][f"{module_name}"]["object"] 101 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the module '{module_name}'!") 102 return module_object 103 except KeyError: 104 GoldyBot.logging.log("error", f"[{MODULE_NAME}] Failed to find the module '{module_name}'.") 105 return None
A fast way to grab the class object of a module from Goldy Bot cache.
class
FindExtensions:
107class FindExtensions(): 108 """A class dedicated to finding loaded Goldy Bot extensions from cache.""" 109 def __init__(self): 110 pass 111 112 def find_object_by_extension_name(self, extension_name:str): 113 """A fast way to grab the class object of a extension from Goldy Bot cache.""" 114 GoldyBot.logging.log(f"[{MODULE_NAME}] Finding extension '{extension_name}' by name...") 115 for module in main_cache_dict["modules"]: 116 try: 117 extension_object:GoldyBot.ext.extensions.Extension = main_cache_dict["modules"][f"{module}"]["extensions"][f"{extension_name}"]["object"] 118 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the extension '{extension_name}'!") 119 return extension_object 120 except KeyError: 121 pass 122 123 for module in main_cache_dict["internal_modules"]: 124 try: 125 extension_object:GoldyBot.ext.extensions.Extension = main_cache_dict["internal_modules"][f"{module}"]["extensions"][f"{extension_name}"]["object"] 126 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the extension '{extension_name}'!") 127 return extension_object 128 except KeyError: 129 pass 130 131 GoldyBot.logging.log("error", f"[{MODULE_NAME}] Failed to find the extension '{extension_name}'.") 132 return None
A class dedicated to finding loaded Goldy Bot extensions from cache.
def
find_object_by_extension_name(self, extension_name: str):
112 def find_object_by_extension_name(self, extension_name:str): 113 """A fast way to grab the class object of a extension from Goldy Bot cache.""" 114 GoldyBot.logging.log(f"[{MODULE_NAME}] Finding extension '{extension_name}' by name...") 115 for module in main_cache_dict["modules"]: 116 try: 117 extension_object:GoldyBot.ext.extensions.Extension = main_cache_dict["modules"][f"{module}"]["extensions"][f"{extension_name}"]["object"] 118 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the extension '{extension_name}'!") 119 return extension_object 120 except KeyError: 121 pass 122 123 for module in main_cache_dict["internal_modules"]: 124 try: 125 extension_object:GoldyBot.ext.extensions.Extension = main_cache_dict["internal_modules"][f"{module}"]["extensions"][f"{extension_name}"]["object"] 126 GoldyBot.logging.log(f"[{MODULE_NAME}] Found the extension '{extension_name}'!") 127 return extension_object 128 except KeyError: 129 pass 130 131 GoldyBot.logging.log("error", f"[{MODULE_NAME}] Failed to find the extension '{extension_name}'.") 132 return None
A fast way to grab the class object of a extension from Goldy Bot cache.