GoldyBot.utility.guilds.guild
1import asyncio 2import json 3import os 4from typing import List 5import nextcord 6import GoldyBot 7from . import config 8 9MODULE_NAME = "GUILD" 10 11GUILD_CONFIG_TEMPLATE_JSON_PATH = GoldyBot.paths.GOLDY_BOT + "/utility/guilds/guild_config_template.json" 12 13class Guild(): 14 """Class that represents a discord guild in Goldy Bot.""" 15 def __init__(self, guild:nextcord.Guild): 16 self.guild = guild 17 self.database:GoldyBot.database.Database = GoldyBot.cache.main_cache_dict["database"] 18 19 self.goldy_config = GoldyBot.config.Config(GoldyBot.files.File(GoldyBot.paths.GOLDY_CONFIG_JSON)) 20 21 self.allowed_guilds = self.goldy_config.read("allowed_guilds") 22 self.config_file = self.get_config_file() 23 24 # Cache this! 25 if self.is_allowed: 26 GoldyBot.cache.main_cache_dict["guilds"][f"{self.code_name}"] = {} 27 GoldyBot.cache.main_cache_dict["guilds"][f"{self.code_name}"]["object"] = self 28 29 async def setup(self): 30 """Setup's a guild in Goldy Bot with the given nextcord guild object. This will create a config and database collection for the guild.""" 31 if self.is_allowed: 32 # Generate config folder for guild of there isn't already. 33 #----------------------------------- 34 if not self.config_exist: 35 # If the config is empty, write the guild config template to it. 36 template = GoldyBot.files.File(GUILD_CONFIG_TEMPLATE_JSON_PATH).read() 37 template_json_obj = json.loads(template) 38 template_json_obj["id"] = self.id 39 template_json_obj["code_name"] = self.code_name 40 template_json_obj["display_name"] = self.guild.name 41 self.config_file.write(json.dumps(template_json_obj)) 42 43 # Create a database collection for the guild if there isn't already. 44 #-------------------------------------------------------------------- 45 if not self.database == None: 46 if not await self.database_exist: 47 await self.database.create_collection(f"{self.code_name} (server)", {"_id":1, 48 "goldy":"I've created this collection automatically for a guild.", 49 "notice":"Feel free to delete this document."}) 50 else: 51 GoldyBot.logging.log("warn", f"[{MODULE_NAME}] Skipping database guild collection creation because database is disabled.") 52 53 GoldyBot.logging.log(f"We ran setup for the guild '{self.code_name}'.") 54 else: 55 GoldyBot.logging.log("warn", f"Setup for '{self.code_name}' did not run because guild is not in goldy bot guild list.") 56 57 @property 58 def id(self): 59 return self.guild.id 60 61 @property 62 def code_name(self): 63 try: 64 return self.allowed_guilds[f"{self.id}"] 65 except KeyError: 66 return None 67 68 @property 69 def nextcord_guild_object(self) -> nextcord.Guild: 70 """Returns back the nextcord guild class you entered before.""" 71 return self.guild 72 73 @property 74 def config(self) -> config.GuildConfig: 75 """Returns guild's config class.""" 76 #TODO: If the config does not exist run guild setup. 77 if self.config_exist: 78 return config.GuildConfig(GoldyBot.config.Config(GoldyBot.files.File(f"{GoldyBot.paths.CONFIG}/{self.code_name}/config.json"))) 79 80 @property 81 def is_allowed(self) -> bool: 82 """Commands Goldy Bot to check whether the guild is allowed to be active.""" 83 try: 84 self.allowed_guilds[f"{self.id}"] # If this fails then guild is not in list. 85 return True 86 except KeyError: 87 return False 88 89 @property 90 def has_config_been_edited(self) -> bool: 91 """Commands Goldy Bot to check if the config of this guild has actually been edited or not. Returns False if config does not exist.""" 92 if self.config_exist: 93 if self.config_file.read() == GoldyBot.files.File(GUILD_CONFIG_TEMPLATE_JSON_PATH).read(): 94 return False 95 else: 96 return True 97 else: 98 return False 99 100 @property 101 def config_exist(self) -> bool: 102 """Commands Goldy Bot to check if the guild exist in config.""" 103 if self.code_name in os.listdir(GoldyBot.paths.CONFIG): 104 if self.config_file.read() in ["", "{}"]: 105 return False 106 return True 107 else: 108 return False 109 110 @property 111 async def database_exist(self) -> bool: 112 """Commands Goldy Bot to check if the guild exist in the database collections.""" 113 if self.database_collection_name in await self.database.list_collection_names(): 114 return True 115 else: 116 return False 117 118 @property 119 def database_collection_name(self) -> str: 120 return f"{self.code_name} (server)" 121 122 def channels(self) -> List[GoldyBot.objects.channel.Channel]: 123 """Returns all channels in the guild. IDK if this method works well lmao, I haven't done much testing.""" 124 channels_list:List[GoldyBot.objects.channel.Channel] = [] 125 for nc_channel in self.guild.channels: 126 channels_list.append(GoldyBot.objects.channel.Channel(None, channel_object=nc_channel)) 127 128 return channels_list 129 130 def get_config_file(self) -> GoldyBot.files.File: 131 # Create guild config folder. 132 GUILD_CONFIG_FOLDER_PATH = GoldyBot.files.File(GoldyBot.paths.CONFIG + f"/{self.code_name}/").file_path 133 134 # Create guild config file. 135 config_file = GoldyBot.files.File(GUILD_CONFIG_FOLDER_PATH + "config.json") 136 137 return config_file 138 139 async def get_members(self, ctx): 140 """Method to get all the discord guild members.""" 141 142 member_list_:List[GoldyBot.Member] = [] 143 for member in self.guild.members: 144 member_list_.append(GoldyBot.objects.member.Member(ctx, member_object=member)) 145 146 return member_list_ 147 148 async def get_channels(self, ctx): 149 """Method to get all the discord guild's channels.""" 150 151 channel_list_:List[GoldyBot.Channel] = [] 152 for channel in self.guild.channels: 153 channel_list_.append(GoldyBot.objects.channel.Channel(ctx, channel_object=channel)) 154 155 return channel_list_
class
Guild:
14class Guild(): 15 """Class that represents a discord guild in Goldy Bot.""" 16 def __init__(self, guild:nextcord.Guild): 17 self.guild = guild 18 self.database:GoldyBot.database.Database = GoldyBot.cache.main_cache_dict["database"] 19 20 self.goldy_config = GoldyBot.config.Config(GoldyBot.files.File(GoldyBot.paths.GOLDY_CONFIG_JSON)) 21 22 self.allowed_guilds = self.goldy_config.read("allowed_guilds") 23 self.config_file = self.get_config_file() 24 25 # Cache this! 26 if self.is_allowed: 27 GoldyBot.cache.main_cache_dict["guilds"][f"{self.code_name}"] = {} 28 GoldyBot.cache.main_cache_dict["guilds"][f"{self.code_name}"]["object"] = self 29 30 async def setup(self): 31 """Setup's a guild in Goldy Bot with the given nextcord guild object. This will create a config and database collection for the guild.""" 32 if self.is_allowed: 33 # Generate config folder for guild of there isn't already. 34 #----------------------------------- 35 if not self.config_exist: 36 # If the config is empty, write the guild config template to it. 37 template = GoldyBot.files.File(GUILD_CONFIG_TEMPLATE_JSON_PATH).read() 38 template_json_obj = json.loads(template) 39 template_json_obj["id"] = self.id 40 template_json_obj["code_name"] = self.code_name 41 template_json_obj["display_name"] = self.guild.name 42 self.config_file.write(json.dumps(template_json_obj)) 43 44 # Create a database collection for the guild if there isn't already. 45 #-------------------------------------------------------------------- 46 if not self.database == None: 47 if not await self.database_exist: 48 await self.database.create_collection(f"{self.code_name} (server)", {"_id":1, 49 "goldy":"I've created this collection automatically for a guild.", 50 "notice":"Feel free to delete this document."}) 51 else: 52 GoldyBot.logging.log("warn", f"[{MODULE_NAME}] Skipping database guild collection creation because database is disabled.") 53 54 GoldyBot.logging.log(f"We ran setup for the guild '{self.code_name}'.") 55 else: 56 GoldyBot.logging.log("warn", f"Setup for '{self.code_name}' did not run because guild is not in goldy bot guild list.") 57 58 @property 59 def id(self): 60 return self.guild.id 61 62 @property 63 def code_name(self): 64 try: 65 return self.allowed_guilds[f"{self.id}"] 66 except KeyError: 67 return None 68 69 @property 70 def nextcord_guild_object(self) -> nextcord.Guild: 71 """Returns back the nextcord guild class you entered before.""" 72 return self.guild 73 74 @property 75 def config(self) -> config.GuildConfig: 76 """Returns guild's config class.""" 77 #TODO: If the config does not exist run guild setup. 78 if self.config_exist: 79 return config.GuildConfig(GoldyBot.config.Config(GoldyBot.files.File(f"{GoldyBot.paths.CONFIG}/{self.code_name}/config.json"))) 80 81 @property 82 def is_allowed(self) -> bool: 83 """Commands Goldy Bot to check whether the guild is allowed to be active.""" 84 try: 85 self.allowed_guilds[f"{self.id}"] # If this fails then guild is not in list. 86 return True 87 except KeyError: 88 return False 89 90 @property 91 def has_config_been_edited(self) -> bool: 92 """Commands Goldy Bot to check if the config of this guild has actually been edited or not. Returns False if config does not exist.""" 93 if self.config_exist: 94 if self.config_file.read() == GoldyBot.files.File(GUILD_CONFIG_TEMPLATE_JSON_PATH).read(): 95 return False 96 else: 97 return True 98 else: 99 return False 100 101 @property 102 def config_exist(self) -> bool: 103 """Commands Goldy Bot to check if the guild exist in config.""" 104 if self.code_name in os.listdir(GoldyBot.paths.CONFIG): 105 if self.config_file.read() in ["", "{}"]: 106 return False 107 return True 108 else: 109 return False 110 111 @property 112 async def database_exist(self) -> bool: 113 """Commands Goldy Bot to check if the guild exist in the database collections.""" 114 if self.database_collection_name in await self.database.list_collection_names(): 115 return True 116 else: 117 return False 118 119 @property 120 def database_collection_name(self) -> str: 121 return f"{self.code_name} (server)" 122 123 def channels(self) -> List[GoldyBot.objects.channel.Channel]: 124 """Returns all channels in the guild. IDK if this method works well lmao, I haven't done much testing.""" 125 channels_list:List[GoldyBot.objects.channel.Channel] = [] 126 for nc_channel in self.guild.channels: 127 channels_list.append(GoldyBot.objects.channel.Channel(None, channel_object=nc_channel)) 128 129 return channels_list 130 131 def get_config_file(self) -> GoldyBot.files.File: 132 # Create guild config folder. 133 GUILD_CONFIG_FOLDER_PATH = GoldyBot.files.File(GoldyBot.paths.CONFIG + f"/{self.code_name}/").file_path 134 135 # Create guild config file. 136 config_file = GoldyBot.files.File(GUILD_CONFIG_FOLDER_PATH + "config.json") 137 138 return config_file 139 140 async def get_members(self, ctx): 141 """Method to get all the discord guild members.""" 142 143 member_list_:List[GoldyBot.Member] = [] 144 for member in self.guild.members: 145 member_list_.append(GoldyBot.objects.member.Member(ctx, member_object=member)) 146 147 return member_list_ 148 149 async def get_channels(self, ctx): 150 """Method to get all the discord guild's channels.""" 151 152 channel_list_:List[GoldyBot.Channel] = [] 153 for channel in self.guild.channels: 154 channel_list_.append(GoldyBot.objects.channel.Channel(ctx, channel_object=channel)) 155 156 return channel_list_
Class that represents a discord guild in Goldy Bot.
Guild(guild: nextcord.guild.Guild)
16 def __init__(self, guild:nextcord.Guild): 17 self.guild = guild 18 self.database:GoldyBot.database.Database = GoldyBot.cache.main_cache_dict["database"] 19 20 self.goldy_config = GoldyBot.config.Config(GoldyBot.files.File(GoldyBot.paths.GOLDY_CONFIG_JSON)) 21 22 self.allowed_guilds = self.goldy_config.read("allowed_guilds") 23 self.config_file = self.get_config_file() 24 25 # Cache this! 26 if self.is_allowed: 27 GoldyBot.cache.main_cache_dict["guilds"][f"{self.code_name}"] = {} 28 GoldyBot.cache.main_cache_dict["guilds"][f"{self.code_name}"]["object"] = self
async def
setup(self):
30 async def setup(self): 31 """Setup's a guild in Goldy Bot with the given nextcord guild object. This will create a config and database collection for the guild.""" 32 if self.is_allowed: 33 # Generate config folder for guild of there isn't already. 34 #----------------------------------- 35 if not self.config_exist: 36 # If the config is empty, write the guild config template to it. 37 template = GoldyBot.files.File(GUILD_CONFIG_TEMPLATE_JSON_PATH).read() 38 template_json_obj = json.loads(template) 39 template_json_obj["id"] = self.id 40 template_json_obj["code_name"] = self.code_name 41 template_json_obj["display_name"] = self.guild.name 42 self.config_file.write(json.dumps(template_json_obj)) 43 44 # Create a database collection for the guild if there isn't already. 45 #-------------------------------------------------------------------- 46 if not self.database == None: 47 if not await self.database_exist: 48 await self.database.create_collection(f"{self.code_name} (server)", {"_id":1, 49 "goldy":"I've created this collection automatically for a guild.", 50 "notice":"Feel free to delete this document."}) 51 else: 52 GoldyBot.logging.log("warn", f"[{MODULE_NAME}] Skipping database guild collection creation because database is disabled.") 53 54 GoldyBot.logging.log(f"We ran setup for the guild '{self.code_name}'.") 55 else: 56 GoldyBot.logging.log("warn", f"Setup for '{self.code_name}' did not run because guild is not in goldy bot guild list.")
Setup's a guild in Goldy Bot with the given nextcord guild object. This will create a config and database collection for the guild.
nextcord_guild_object: nextcord.guild.Guild
Returns back the nextcord guild class you entered before.
has_config_been_edited: bool
Commands Goldy Bot to check if the config of this guild has actually been edited or not. Returns False if config does not exist.
123 def channels(self) -> List[GoldyBot.objects.channel.Channel]: 124 """Returns all channels in the guild. IDK if this method works well lmao, I haven't done much testing.""" 125 channels_list:List[GoldyBot.objects.channel.Channel] = [] 126 for nc_channel in self.guild.channels: 127 channels_list.append(GoldyBot.objects.channel.Channel(None, channel_object=nc_channel)) 128 129 return channels_list
Returns all channels in the guild. IDK if this method works well lmao, I haven't done much testing.
131 def get_config_file(self) -> GoldyBot.files.File: 132 # Create guild config folder. 133 GUILD_CONFIG_FOLDER_PATH = GoldyBot.files.File(GoldyBot.paths.CONFIG + f"/{self.code_name}/").file_path 134 135 # Create guild config file. 136 config_file = GoldyBot.files.File(GUILD_CONFIG_FOLDER_PATH + "config.json") 137 138 return config_file
async def
get_members(self, ctx):
140 async def get_members(self, ctx): 141 """Method to get all the discord guild members.""" 142 143 member_list_:List[GoldyBot.Member] = [] 144 for member in self.guild.members: 145 member_list_.append(GoldyBot.objects.member.Member(ctx, member_object=member)) 146 147 return member_list_
Method to get all the discord guild members.
async def
get_channels(self, ctx):
149 async def get_channels(self, ctx): 150 """Method to get all the discord guild's channels.""" 151 152 channel_list_:List[GoldyBot.Channel] = [] 153 for channel in self.guild.channels: 154 channel_list_.append(GoldyBot.objects.channel.Channel(ctx, channel_object=channel)) 155 156 return channel_list_
Method to get all the discord guild's channels.