async def currency_check(self): """Update the currency API data on fixed intervals.""" await self.wait_until_ready() print("Starting the currency data update loop...") timer_data = custom_json.load( "bot\\database\\currency_api\\timer.json") current_time = time.time() if "next_check" not in timer_data or timer_data[ "next_check"] < current_time: timer_data["interval"] = timer_data["default_interval"] timer_data["next_check"] = current_time await Client.get_latest_currency_data(timer_data) else: cache.CURRENCY_DATA = custom_json.load( "bot\\database\\currency_api\\data.json") timer_data["interval"] = timer_data["next_check"] - current_time while not self.is_closed(): await asyncio.sleep(timer_data["interval"]) timer_data["interval"] = timer_data["default_interval"] await Client.get_latest_currency_data(timer_data)
def add_new_keys(self): """Add new language keys from the default language.""" default_language = definitions.LANGUAGES[default_values.LANGUAGE_ID] new_keys = [] for key in default_language.keys: if key not in self.keys: new_keys.append(key) for key in new_keys: directory = strings.modify_filepath( definitions.DEFAULT_LANGUAGE_KEY_LOCATIONS[key], "_NEW_", { "original": default_values.LANGUAGE_ID, "new": self.obj_id }) key_dict = custom_json.load(directory) if key_dict is None: key_dict = {} key_dict[key] = default_language.keys[key] custom_json.save(key_dict, directory, compact=False) for special_file in Language.special_files: if special_file == "commands.json": continue lang_dict = custom_json.load( os.path.join(self.directory, special_file)) if lang_dict is None: lang_dict = {} default_dict = custom_json.load( os.path.join(default_language.directory, special_file)) new_lang_dict = custom_json.load( os.path.join(self.directory, "_NEW_" + special_file)) if new_lang_dict is None: new_lang_dict = {} changed_dict = False for key in default_dict: if key not in lang_dict and key not in new_lang_dict: changed_dict = True new_lang_dict[key] = default_dict[key] if changed_dict: custom_json.save(new_lang_dict, os.path.join(self.directory, "_NEW_" + special_file), compact=False)
async def create_object_from_database(row): """Create GlobalData object from a database row.""" command_data = None if row is not None: command_data = custom_json.load(row[0]) return GlobalData(command_data=command_data)
def delete_obsolete_keys(self): """Remove keys that no longer exist in the default language.""" default_language = definitions.LANGUAGES[default_values.LANGUAGE_ID] for root, dirs, files in os.walk(self.directory): for name in files: full_dir = os.path.join(root, name) relative_dir = full_dir.replace(self.directory, "") if relative_dir[0] == "\\": relative_dir = relative_dir[1:] file_dict, changed_dict, changed_dir, new_dict, new_dir = get_files( full_dir) found_obsolete_key = False found_changed_obsolete_key = False found_new_obsolete_key = False if relative_dir not in Language.special_files: for key in list(file_dict.keys()): if key not in default_language.keys: found_obsolete_key = True del file_dict[key] if key in changed_dict: found_changed_obsolete_key = True del changed_dict[key] for key in list(new_dict.keys()): if key not in default_language.keys: found_new_obsolete_key = True del new_dict[key] elif relative_dir != "commands.json": default_dict = custom_json.load( os.path.join(default_language.directory, name)) for key in list(file_dict.keys()): if key not in default_dict: del file_dict[key] found_obsolete_key = True if key in changed_dict: del changed_dict[key] found_changed_obsolete_key = True for key in list(new_dict.keys()): if key not in default_dict: del new_dict[key] found_new_obsolete_key = True if found_obsolete_key: custom_json.save(file_dict, full_dir, compact=False) if found_changed_obsolete_key: custom_json.save(changed_dict, changed_dir, compact=False) if found_new_obsolete_key: custom_json.save(new_dict, new_dir, compact=False)
def create_object_from_database(row): """Create UserData object from a database row.""" if row is None: return None return UserData(prefix=row[0], language_id=row[1], command_data=custom_json.load(row[2]))
def get_files(full_dir): """Get dictionaries from files.""" file_dict = custom_json.load(full_dir) if file_dict is None: file_dict = {} changed_dir = strings.modify_filepath(full_dir, "_CHANGED_") changed_dict = custom_json.load(changed_dir) if changed_dict is None: changed_dict = {} new_dir = strings.modify_filepath(full_dir, "_NEW_") new_dict = custom_json.load(new_dir) if new_dict is None: new_dict = {} return file_dict, changed_dict, changed_dir, new_dict, new_dir
def __init__(self, prefix=None, language_id=None, shortcuts=None, command_data=None): """Object initialisation.""" super().__init__(prefix, language_id, shortcuts) self.command_data = command_data if self.command_data is None: self.command_data = custom_json.load(definitions.USER_COMMAND_DATA)
def synchronise_commands(): """Make command language data synchronised with existing commands.""" for language in definitions.LANGUAGES.values(): command_file_dir = os.path.join(language.directory, "commands.json") language_command_dict = custom_json.load(command_file_dir) if language_command_dict is None: language_command_dict = {} if synchronise_command_layer(language_command_dict, language.obj_id): custom_json.save(language_command_dict, command_file_dir, compact=False)
def __init__(self, prefix=None, language_id=None, shortcuts=None, command_data=None, max_dice=None): """Object initialisation.""" super().__init__(prefix, language_id, shortcuts) self.command_data = command_data self.max_dice = max_dice self.guild_call = GuildCall(None, False) if self.command_data is None: self.command_data = custom_json.load( definitions.DEFAULT_COMMAND_DATA)
def __init__(self, command_data=None): """Initialise object.""" self.command_data = command_data if self.command_data is None: self.command_data = custom_json.load(definitions.USER_COMMAND_DATA)
def update_config_data(): """ Update the config data. Updates the previously saved config data to be compatible with new versions of Mami. Called on startup. """ channels = database_functions.select_all_channels() for channel_id in channels: channel = channels[channel_id] channel_command_data = channel.command_data.sub_commands channel_default_command_data = custom_json.load( definitions.DEFAULT_COMMAND_DATA).sub_commands synchronise_command_data(channel_default_command_data, channel_command_data) if (channel.language_id is not None and channel.language_id not in definitions.LANGUAGES): channel.language_id = None database_functions.synchronise_channel_update(channel_id, channel) categories = database_functions.select_all_categories() for category_id in categories: category = categories[category_id] category_command_data = category.command_data.sub_commands category_default_command_data = custom_json.load( definitions.DEFAULT_COMMAND_DATA, ).sub_commands synchronise_command_data(category_default_command_data, category_command_data) if (category.language_id is not None and category.language_id not in definitions.LANGUAGES): category.language_id = None database_functions.synchronise_category_update(category_id, category) guilds = database_functions.select_all_guilds() for guild_id in guilds: guild = guilds[guild_id] guild_command_data = guild.command_data.sub_commands guild_default_command_data = custom_json.load( definitions.GUILD_COMMAND_DATA).sub_commands synchronise_command_data(guild_default_command_data, guild_command_data) if (guild.language_id is not None and guild.language_id not in definitions.LANGUAGES): guild.language_id = None database_functions.synchronise_guild_update(guild_id, guild) users = database_functions.select_all_users() for user_id in users: user = users[user_id] user_command_data = user.command_data.sub_commands user_default_command_data = custom_json.load( definitions.USER_COMMAND_DATA).sub_commands synchronise_command_data(user_default_command_data, user_command_data) if (user.language_id is not None and user.language_id not in definitions.LANGUAGES): user.language_id = None database_functions.synchronise_user_update(user_id, user)