async def display_command_rules(context, arguments, platform): """Display command rules on any platform.""" command_data = arguments[0] message = embeds.PaginatedEmbed( await context.language.get_text( "display_" + platform + "_command_rules_title", {"command": command_data["name"]})) command_rules = command_data["data"].command_rules message.embed.description = await context.language.get_text( "display_" + platform + "_command_rules_" + command_rules.type + "_rules_desc") members = [] for member_id in command_rules.users: member = context.message.guild.get_member(member_id) member_name = None if member is not None: member_name = "{member.display_name} ({member.id})".format(member=member) else: member_name = "[{former_member}] ({id})".format( former_member=await context.language.get_text("former_guild_member"), id=member_id) members.append(member_name) roles = [] for role_id in command_rules.roles: role = context.message.guild.get_role(role_id) role_name = None if role is not None: role_name = "{role.name} ({role.id})".format(role=role) else: role_name = "[{former_role}] ({id})".format( former_role=await context.language.get_text("former_guild_role"), id=role_id) roles.append(role_name) permissions = [] if command_rules.permissions is not None: for permission_code in command_rules.permissions: permissions.append(context.language.permission_names[permission_code]) if members: message.fields.append(embeds.EmbedFieldCollection( members, await context.language.get_text("members_title"))) if roles: message.fields.append(embeds.EmbedFieldCollection( roles, await context.language.get_text("roles_title"))) if permissions: message.fields.append(embeds.EmbedFieldCollection( permissions, await context.language.get_text("permissions_title"))) await message.send(context) return True
async def throw(context, arguments): """Throw the dice.""" no_of_dice = arguments[0] dice_range = arguments[1:] dice_range.sort() dice_throws = [] dice_results = [] while len(dice_throws) < no_of_dice: dice_throw = random.randint(dice_range[0], dice_range[1]) dice_throws.append(dice_throw) dice_results.append("🎲 " + await context.language.format_number(dice_throw)) dice_total = sum(dice_throws) min_total = dice_range[0] * no_of_dice max_total = dice_range[1] * no_of_dice percentage = await numbers.get_percentage(dice_total - min_total, max_total - min_total) formatted_percentage = await context.language.get_text( "percentage", { "number": await context.language.format_number(percentage, decimal_rules=".2f") }) thumbnail = "default" column_amount = 1 if context.desktop_ui: column_amount = 3 thumbnail = None message = embeds.PaginatedEmbed( await context.language.get_text("throw_dice_title"), embeds.EmbedFieldCollection( dice_results, await context.language.get_text("dice_results_title"), column_amount), embeds.EmbedFieldCollection( await context.language.get_text( "dice_results_analysis_content", { "dice_total": await context.language.format_number(dice_total), "min_total": await context.language.format_number(min_total), "max_total": await context.language.format_number(max_total), "percentage": formatted_percentage }), await context.language.get_text("dice_results_analysis_title"))) message.embed.description = await context.language.get_text( "throw_dice_desc", {"user_mention": context.message.author.mention}) await message.send(context, thumbnail=thumbnail) return True
async def display_shortcuts(context, platform_type): """Display all platform shortcuts.""" shortcuts = [] data_object = getattr(context, platform_type + "_data") for shortcut in data_object.shortcuts.values(): shortcuts.append(shortcut.name) shortcuts.sort() shortcut_command = definitions.COMMANDS.get_sub_command_from_path("shortcut") shortcut_cmd_name = await shortcut_command.get_command_string(context) shortcut_instruction = shortcut_cmd_name + " [" + ( await context.language.get_text("display_shortcuts_shortcut_name")) + "]" random_shortcut = random.choice(shortcuts) if " " in random_shortcut: random_shortcut = '"' + random_shortcut + '"' shortcut_example = shortcut_cmd_name + " " + random_shortcut message = embeds.PaginatedEmbed( await context.language.get_text("display_" + platform_type + "_shortcuts_title"), embeds.EmbedFieldCollection( shortcuts, await context.language.get_text("display_shortcuts_subtitle"), 2)) message.embed.description = await context.language.get_text( "display_" + platform_type + "_shortcuts_desc", {"instruction": shortcut_instruction, "example": shortcut_example}) await message.send(context) return True
async def view_all(context, arguments): """Display all currencies.""" currencies = [] columns = 1 thumbnail = True if context.desktop_ui: columns = 2 thumbnail = False for key, value in cache.CURRENCY_DATA["currencies"].items(): currencies.append(key + " - " + value["name"]) currencies.sort() message = embeds.PaginatedEmbed( await context.language.get_text("view_all_currencies_title"), embeds.EmbedFieldCollection( currencies, await context.language.get_text("currencies_title"), columns)) message.embed.description = await context.language.get_text( "view_all_currencies_desc") await message.send(context, thumbnail=thumbnail) return True
async def search(context, arguments): """Search currencies with search terms.""" search_term = arguments[0] currencies = [] columns = 1 thumbnail = True if context.desktop_ui: columns = 2 thumbnail = False search_term_lower = search_term.lower() for key, value in cache.CURRENCY_DATA["currencies"].items(): if search_term_lower in value["name"].lower(): currencies.append(key + " - " + value["name"]) currencies.sort() message = embeds.PaginatedEmbed( await context.language.get_text("search_currencies_title"), embeds.EmbedFieldCollection( currencies, await context.language.get_text("currencies_title"), columns)) message.embed.description = await context.language.get_text( "search_currencies_desc", {"search_term": search_term}) await message.send(context, thumbnail=thumbnail) return True
async def get_info_embed_field(self, context, argument_no): """Get the embed field of the argument for command info.""" localisation = self.localisation[context.language_id] return embeds.EmbedFieldCollection( localisation["help_text"], await context.language.get_text("argument_name_and_number", { "name": localisation["name"], "number": argument_no }))
async def add_dialogue_bonus_fields(context, message, localisation): """Add the bonus fields in the dialogue message.""" for field in localisation["bonus_fields"]: content_list = field["content"] if content_list == "var_all_languages": content_list = await context.language.get_all_languages() message.fields.append( embeds.EmbedFieldCollection(content_list, field["title"], column_amount=2))
async def no_action(context, commands, command_string): """Send a feedback message when the command called has no method to call.""" message = embeds.PaginatedEmbed( await context.language.get_text("no_action_title"), embeds.EmbedFieldCollection(commands, await context.language.get_text("command"))) message.embed.description = "ℹ " + await context.language.get_text( "no_action_desc", {"command": command_string}) await message.send(context)
async def invalid_argument(context, argument, custom_msg=None): """Send invalid argument feedback message.""" message = embeds.PaginatedEmbed( await context.language.get_text("invalid_argument_title"), embeds.EmbedFieldCollection( custom_msg, await context.language.get_text("invalid_argument_custom_msg_title"))) message.embed.description = "❌ " + await context.language.get_text( "invalid_argument_desc", {"arg": argument}) await message.send(context)
async def failed_pre_check(context, custom_msg): """Send a failed pre-check feedback message.""" message = embeds.PaginatedEmbed( await context.language.get_text("failed_pre_check_title"), embeds.EmbedFieldCollection( custom_msg, await context.language.get_text("failed_pre_check_custom_msg_title"))) message.embed.description = ":grey_exclamation: " + await context.language.get_text( "failed_pre_check_desc") await message.send(context)
async def guild_member_info(context, arguments): """Show basic information of a member.""" member = arguments[0] join_time = member.joined_at.strftime( await context.language.get_text("datetime_format")) time_in_guild = await dates.get_time_difference_string(context, member.joined_at) join_data = await context.language.get_text( "datetime_and_time_passed", {"datetime": join_time, "time_passed": time_in_guild}) creation_time = member.created_at.strftime( await context.language.get_text("datetime_format")) time_on_discord = await dates.get_time_difference_string(context, member.created_at) creation_data = await context.language.get_text( "datetime_and_time_passed", {"datetime": creation_time, "time_passed": time_on_discord}) message = embeds.PaginatedEmbed( await context.language.get_text( "member_info_title", {"name": member.display_name}), embeds.EmbedFieldCollection( member.name, await context.language.get_text("member_info_discord_name")), embeds.EmbedFieldCollection( creation_data, await context.language.get_text("member_info_creation_time")), embeds.EmbedFieldCollection( join_data, await context.language.get_text( "member_info_join_time", {"guild": member.guild.name})), embeds.EmbedFieldCollection( member.top_role.name, await context.language.get_text( "member_info_top_role"))) message.embed.description = await context.language.get_text( "member_info_desc", {"member": member.display_name}) colour = member.colour if str(colour) == "#000000": colour = Embed.Empty await message.send(context, thumbnail=member.avatar_url, colour=colour) return True
async def invalid_command_subcommands(context, commands, command_string, last_working_command_string): """ Feedback message. Send a feedback message when an invalid command was called, and the last valid one has sub-commands. """ message = embeds.PaginatedEmbed( await context.language.get_text("invalid_command_title"), embeds.EmbedFieldCollection(commands, await context.language.get_text("command"))) message.embed.description = "❌ " + await context.language.get_text( "invalid_command_desc", { "command": command_string, "last_working": last_working_command_string }) await message.send(context)
async def get_command_info(context, arguments): """Get command info of a specific command.""" command = arguments[0] aliases = await context.language.get_string_list( command.localisation[context.language_id]["names"][1:]) if aliases == "": aliases = await context.language.get_text("no_aliases") platforms = ["category", "channel", "guild", "user", "global"] command_use_times = {} for platform in platforms: platform_cmd_data = getattr(getattr(context, platform + "_data"), "command_data") command_data = await platform_cmd_data.get_object_from_id_path(command.id_path) language_key = "command_" + platform + "_uses_" number = await context.language.format_number(command_data.use_times) number = "**" + number + "**" if command_data.use_times == 1: language_key += "singular" else: language_key += "plural" command_use_times[platform] = await context.language.get_text( language_key, {"times": number}) basic_info = ( "{aliases_title}: **{aliases_content}**\n" "{command_use_times[global]}\n" "{command_use_times[guild]}\n" "{command_use_times[category]}\n" "{command_use_times[channel]}\n" "{command_use_times[user]}").format( aliases_title=await context.language.get_text("command_aliases_title"), aliases_content=aliases, command_use_times=command_use_times) related_commands = "" for command in command.related_commands: if related_commands: related_commands += "\n" related_commands += await command.get_command_string( context, include_prefix=False) sub_commands = await command.get_sub_commands(context, filter_unallowed=False) sub_commands_string = "" for sub_command in sub_commands: if sub_commands_string: sub_commands_string += "\n" sub_command_name = await sub_command.get_command_string( context, include_prefix=False) sub_commands_string += ( sub_command_name.split(".")[-1] + " - " + sub_command.localisation[context.language_id]["description"]) message = embeds.PaginatedEmbed( await context.language.get_text( "command_info_title", {"command": await command.get_command_string(context, include_prefix=False)}), embeds.EmbedFieldCollection( basic_info, await context.language.get_text("basic_command_info_title"))) if sub_commands_string: message.fields.append(embeds.EmbedFieldCollection( sub_commands_string, await context.language.get_text("sub_commands_title"))) if related_commands: message.fields.append(embeds.EmbedFieldCollection( related_commands, await context.language.get_text("related_commands_title"))) for i, argument in enumerate(command.arguments): message.fields.append(await argument.get_info_embed_field(context, i + 1)) message.embed.title = command.localisation[context.language_id]["description"] message.embed.description = command.localisation[context.language_id]["help_text"] await message.send(context) return True