async def get_latest_currency_data(timer_data): """ Get the latest currency data from the API. Also updates the timer and saves the updated currency data. """ timer_data["next_check"] += timer_data["default_interval"] custom_json.save(timer_data, "bot\\database\\currency_api\\timer.json") cache.CURRENCY_DATA = {} api_key = secrets.CURRENCY_API_KEY async with aiohttp.ClientSession() as session: url = "http://data.fixer.io/api/latest?access_key=" + api_key async with session.get(url) as request: rates = await request.json() cache.CURRENCY_DATA["timestamp"] = rates["timestamp"] cache.CURRENCY_DATA["currencies"] = {} for key in rates["rates"]: cache.CURRENCY_DATA["currencies"][key] = { "code": key, "rate": rates["rates"][key] } url = "http://data.fixer.io/api/symbols?access_key=" + api_key async with session.get(url) as request: names = await request.json() for key in names["symbols"]: cache.CURRENCY_DATA["currencies"][key]["name"] = names[ "symbols"][key] custom_json.save(cache.CURRENCY_DATA, "bot\\database\\currency_api\\data.json")
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 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 update_global_command_data(context): """Update command data for global data.""" statement = """ UPDATE global SET command_data = ? """ variables = (custom_json.save(context.global_data.command_data), ) definitions.DATABASE_CURSOR.execute(statement, variables) definitions.DATABASE_CONNECTION.commit()
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)
async def update_command_data(context, platform): """Update command data for selected platform.""" statement = """ UPDATE {table} SET command_data = ? WHERE id = ? """.format(table=PLURAL[platform]) platform_data = getattr(context, platform + "_data") platform_id = await get_id_from_platform(context.message, platform) variables = (custom_json.save(platform_data.command_data), platform_id) definitions.DATABASE_CURSOR.execute(statement, variables) definitions.DATABASE_CONNECTION.commit()
async def update_user(context): """Update the user.""" statement = """ UPDATE users SET prefix = ?, language = ?, command_data = ? WHERE id = ? """ variables = (context.user_data.prefix, context.user_data.language_id, custom_json.save(context.user_data.command_data), context.message.author.id) definitions.DATABASE_CURSOR.execute(statement, variables) definitions.DATABASE_CONNECTION.commit()
async def update_guild(context): """Update the guild.""" statement = """ UPDATE guilds SET prefix = ?, language = ?, command_data = ?, max_dice = ? WHERE id = ? """ variables = (context.guild_data.prefix, context.guild_data.language_id, custom_json.save(context.guild_data.command_data), context.guild_data.max_dice, context.message.guild.id) definitions.DATABASE_CURSOR.execute(statement, variables) definitions.DATABASE_CONNECTION.commit()
def synchronise_user_update(user_id, user): """ Update the user when synchronising data. Update the relevant user data at startup when making sure there are no compatibility issues with the data stored and the current Mami version. """ statement = """ UPDATE users SET language = ?, command_data = ? WHERE id = ? """ variables = (user.language_id, custom_json.save(user.command_data), user_id) definitions.DATABASE_CURSOR.execute(statement, variables) definitions.DATABASE_CONNECTION.commit()
async def update_category(context): """Update the category.""" statement = """ UPDATE categories SET prefix = ?, language = ?, command_data = ?, max_dice = ? WHERE id = ? """ variables = (context.category_data.prefix, context.category_data.language_id, custom_json.save(context.category_data.command_data), context.category_data.max_dice, context.message.channel.category_id) definitions.DATABASE_CURSOR.execute(statement, variables) definitions.DATABASE_CONNECTION.commit()
def reveal_changed_keys(self, changed_keys): """Let translators know of keys that have been changed.""" for root, dirs, files in os.walk(self.directory): for name in files: if name.startswith("_NEW_") or name.startswith("_CHANGED_"): continue 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) has_changed_keys = False has_new_changed_keys = False if relative_dir not in Language.special_files: for key, value in changed_keys["default"].items(): if key in file_dict: changed_dict[key] = value has_changed_keys = True elif key in new_dict: new_dict[key] = value has_new_changed_keys = True elif relative_dir == "languages.json" and changed_keys[ "languages"]: for key, value in changed_keys["languages"].items(): if key in file_dict: changed_dict[key] = value has_changed_keys = True elif key in new_dict: new_dict[key] = value has_new_changed_keys = True elif relative_dir == "permissions.json" and changed_keys[ "permissions"]: for key, value in changed_keys["permissions"].items(): if key in file_dict: changed_dict[key] = value has_changed_keys = True elif key in new_dict: new_dict[key] = value has_new_changed_keys = True elif relative_dir == "units.json" and changed_keys["units"]: for key, value in changed_keys["units"].items(): if key in file_dict: changed_dict[key] = value has_changed_keys = True elif key in new_dict: new_dict[key] = value has_new_changed_keys = True if has_changed_keys: custom_json.save(changed_dict, changed_dir, compact=False) has_changes = True if has_new_changed_keys: custom_json.save(new_dict, new_dir, compact=False) has_changes = True
def initialise_commands(): """Initialise all commands.""" definitions.COMMANDS = Command( sub_commands={ "currency": Command(obj_id="currency", sub_commands={ "convert": Command(obj_id="convert", action=currency.convert, arguments=[ Argument(obj_id="amount", modification=numbers.to_float), Argument(obj_id="base", modification=currency_codes. valid_currency), Argument(obj_id="target", modification=currency_codes. valid_currency) ]), "search": Command(obj_id="search", action=currency.search, arguments=[Argument(obj_id="search_term")]), "display": Command(obj_id="display", action=currency.view_all) }), "dice": Command(obj_id="dice", action=dice.throw, arguments=[ Argument(obj_id="no_of_dice", modification=numbers.valid_no_of_dice), Argument(obj_id="first_value", modification=numbers.integer), Argument(obj_id="second_value", modification=numbers.integer) ]), "guild_call": Command(obj_id="guild_call", sub_commands={ "connect": Command( obj_id="connect", pre_check=pre_checks.is_not_connecting_guild_call, action=guild_call.connect_guild_call), "disconnect": Command(obj_id="disconnect", pre_check=pre_checks. is_connecting_or_connected_guild_call, action=guild_call.disconnect_guild_call) }), "info": Command(obj_id="info", sub_commands={ "command": Command(obj_id="command", action=info.get_command_info, arguments=[ Argument(obj_id="command", modification=command_names. command_to_command_object) ]), "member": Command(obj_id="member", pre_check=pre_checks.in_guild, action=info.guild_member_info, arguments=[ Argument(obj_id="member", modification=users_and_roles. valid_member) ]) }), "runes": Command(obj_id="runes", sub_commands={ "archaic": Command(obj_id="archaic", action=runes.translate_archaic, arguments=[Argument(obj_id="text")]), "modern": Command(obj_id="modern", action=runes.translate_modern, arguments=[Argument(obj_id="text")]), "musical": Command(obj_id="musical", action=runes.translate_musical, arguments=[Argument(obj_id="text")]) }), "settings": Command( obj_id="settings", sub_commands={ "category": Command( obj_id="category", pre_check=pre_checks.in_category, sub_commands={ "command_rules": Command( obj_id="command_rules", default_permissions=["administrator"], sub_commands={ "add": Command( obj_id="add", sub_commands={ "inclusionary": Command( obj_id="inclusionary", action=settings. add_inclusionary_category_command_rule, arguments=[ Argument( obj_id="commands", modification= command_names. commands_to_category_inclusionary_command_rules ), Argument( obj_id="rule", modification= users_and_roles. member_role_permission) ], unlimited_arguments=True), "exclusionary": Command( obj_id="exclusionary", action=settings. add_exclusionary_category_command_rule, arguments=[ Argument( obj_id="commands", modification= command_names. commands_to_category_exclusionary_command_rules ), Argument(obj_id="rule", modification= users_and_roles. member_role) ], unlimited_arguments=True) }), "remove": Command( obj_id="remove", action=settings. remove_category_command_rule, arguments=[ Argument( obj_id="commands", modification=command_names. commands_to_category_command_data ), Argument( obj_id="rule", modification=users_and_roles. member_role_permission) ], unlimited_arguments=True), "display": Command( obj_id="display", action=settings. display_category_command_rules, arguments=[ Argument( obj_id="command", modification=command_names. command_to_non_empty_category_command_rules ) ]) }), "language": Command( obj_id="language", sub_commands={ "reset": Command( obj_id="reset", pre_check=pre_checks. category_has_language, action=settings. reset_category_language, default_permissions=["administrator"]), "set": Command( obj_id="set", action=settings.set_category_language, arguments=[ Argument(obj_id="language", modification=languages. language_id) ], default_permissions=["administrator"]) }), "shortcut": Command( obj_id="shortcut", related_commands=[["shortcut"]], sub_commands={ "add": Command( obj_id="add", related_commands=[["shortcut"]], action=shortcuts.add_category_shortcut, arguments=[ Argument( obj_id="name", modification=shortcut_names. is_not_category_shortcut_name, ), Argument(obj_id="content") ]), "delete": Command(obj_id="delete", related_commands=[["shortcut"]], action=shortcuts. delete_category_shortcut, arguments=[ Argument( obj_id="name", modification=shortcut_names .is_category_shortcut_name) ], default_permissions=[ "manage_messages" ]), "display": Command(obj_id="display", related_commands=[["shortcut"]], pre_check=pre_checks. category_has_shortcuts, action=shortcuts. display_category_shortcuts) }) }), "channel": Command( obj_id="channel", pre_check=pre_checks.in_guild, sub_commands={ "command_rules": Command( obj_id="command_rules", default_permissions=["administrator"], sub_commands={ "add": Command( obj_id="add", sub_commands={ "inclusionary": Command( obj_id="inclusionary", action=settings. add_inclusionary_channel_command_rule, arguments=[ Argument( obj_id="commands", modification= command_names. commands_to_channel_inclusionary_command_rules ), Argument( obj_id="rule", modification= users_and_roles. member_role_permission) ], unlimited_arguments=True), "exclusionary": Command( obj_id="exclusionary", action=settings. add_exclusionary_channel_command_rule, arguments=[ Argument( obj_id="commands", modification= command_names. commands_to_channel_exclusionary_command_rules ), Argument(obj_id="rule", modification= users_and_roles. member_role) ], unlimited_arguments=True) }), "remove": Command( obj_id="remove", action=settings. remove_channel_command_rule, arguments=[ Argument( obj_id="commands", modification=command_names. commands_to_channel_command_data ), Argument( obj_id="rule", modification=users_and_roles. member_role_permission) ], unlimited_arguments=True), "display": Command( obj_id="display", action=settings. display_channel_command_rules, arguments=[ Argument( obj_id="command", modification=command_names. command_to_non_empty_channel_command_rules ) ]) }), "language": Command( obj_id="language", sub_commands={ "reset": Command( obj_id="reset", pre_check=pre_checks. channel_has_language, action=settings.reset_channel_language, default_permissions=["administrator"]), "set": Command( obj_id="set", action=settings.set_channel_language, arguments=[ Argument(obj_id="language", modification=languages. language_id) ], default_permissions=["administrator"]) }), "shortcut": Command( obj_id="shortcut", related_commands=[["shortcut"]], sub_commands={ "add": Command( obj_id="add", related_commands=[["shortcut"]], action=shortcuts.add_channel_shortcut, arguments=[ Argument( obj_id="name", modification=shortcut_names. is_not_channel_shortcut_name, ), Argument(obj_id="content") ]), "delete": Command(obj_id="delete", related_commands=[["shortcut"]], action=shortcuts. delete_channel_shortcut, arguments=[ Argument( obj_id="name", modification=shortcut_names .is_channel_shortcut_name) ], default_permissions=[ "manage_messages" ]), "display": Command(obj_id="display", related_commands=[["shortcut"]], pre_check=pre_checks. channel_has_shortcuts, action=shortcuts. display_channel_shortcuts) }) }), "guild": Command( obj_id="guild", pre_check=pre_checks.in_guild, sub_commands={ "command_rules": Command( obj_id="command_rules", default_permissions=["administrator"], sub_commands={ "add": Command( obj_id="add", sub_commands={ "inclusionary": Command( obj_id="inclusionary", action=settings. add_inclusionary_guild_command_rule, arguments=[ Argument( obj_id="commands", modification= command_names. commands_to_guild_inclusionary_command_rules ), Argument( obj_id="rule", modification= users_and_roles. member_role_permission) ], unlimited_arguments=True), "exclusionary": Command( obj_id="exclusionary", action=settings. add_exclusionary_guild_command_rule, arguments=[ Argument( obj_id="commands", modification= command_names. commands_to_guild_exclusionary_command_rules ), Argument(obj_id="rule", modification= users_and_roles. member_role) ], unlimited_arguments=True) }), "remove": Command( obj_id="remove", action=settings. remove_guild_command_rule, arguments=[ Argument( obj_id="commands", modification=command_names. commands_to_guild_command_data ), Argument( obj_id="rule", modification=users_and_roles. member_role_permission) ], unlimited_arguments=True), "display": Command( obj_id="display", action=settings. display_guild_command_rules, arguments=[ Argument( obj_id="command", modification=command_names. command_to_non_empty_guild_command_rules ) ]) }), "language": Command( obj_id="language", sub_commands={ "reset": Command( obj_id="reset", pre_check=pre_checks. guild_has_language, action=settings.reset_guild_language, default_permissions=["administrator"]), "set": Command( obj_id="set", action=settings.set_guild_language, arguments=[ Argument(obj_id="language", modification=languages. language_id) ], default_permissions=["administrator"]) }), "shortcut": Command( obj_id="shortcut", related_commands=[["shortcut"]], sub_commands={ "add": Command( obj_id="add", related_commands=[["shortcut"]], action=shortcuts.add_guild_shortcut, arguments=[ Argument( obj_id="name", modification=shortcut_names. is_not_guild_shortcut_name, ), Argument(obj_id="content") ]), "delete": Command( obj_id="delete", related_commands=[["shortcut"]], action=shortcuts.delete_guild_shortcut, arguments=[ Argument( obj_id="name", modification=shortcut_names. is_guild_shortcut_name) ], default_permissions=[ "manage_messages" ]), "display": Command(obj_id="display", related_commands=[["shortcut"]], pre_check=pre_checks. guild_has_shortcuts, action=shortcuts. display_guild_shortcuts) }) }), "user": Command( obj_id="user", sub_commands={ "language": Command( obj_id="language", sub_commands={ "reset": Command( obj_id="reset", pre_check=pre_checks.user_has_language, action=settings.reset_user_language, default_permissions=["administrator"]), "set": Command( obj_id="set", action=settings.set_user_language, arguments=[ Argument(obj_id="language", modification=languages. language_id) ], default_permissions=["administrator"]) }), "shortcut": Command( obj_id="shortcut", related_commands=[["shortcut"]], sub_commands={ "add": Command( obj_id="add", related_commands=[["shortcut"]], action=shortcuts.add_user_shortcut, arguments=[ Argument( obj_id="name", modification=shortcut_names. is_not_user_shortcut_name, ), Argument(obj_id="content") ]), "delete": Command( obj_id="delete", related_commands=[["shortcut"]], action=shortcuts.delete_user_shortcut, arguments=[ Argument( obj_id="name", modification=shortcut_names. is_user_shortcut_name) ]), "display": Command(obj_id="display", related_commands=[["shortcut"]], pre_check=pre_checks. user_has_shortcuts, action=shortcuts. display_user_shortcuts) }) }) }), "shortcut": Command(obj_id="shortcut", related_commands=[["settings", "category", "shortcut"], ["settings", "channel", "shortcut"], ["settings", "guild", "shortcut"], ["settings", "user", "shortcut"]], action=shortcuts.use_shortcut, arguments=[ Argument(obj_id="name", modification=shortcut_names.any_shortcut) ]) }) guild_command_data, default_command_data, user_command_data = definitions.COMMANDS.initialise_commands( ) definitions.GUILD_COMMAND_DATA = custom_json.save(guild_command_data) definitions.DEFAULT_COMMAND_DATA = custom_json.save(default_command_data) definitions.USER_COMMAND_DATA = custom_json.save(user_command_data)