From ddaf8ce12cd4f8ace3aca10a11ab39b880317beb Mon Sep 17 00:00:00 2001 From: TheKrol Date: Fri, 5 Jul 2024 14:33:32 -0400 Subject: [PATCH] Make winerror paginated (#1015) --- techsupport_bot/commands/winerror.py | 63 +++++++++++++++++++++------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/techsupport_bot/commands/winerror.py b/techsupport_bot/commands/winerror.py index 5fc940aa..be34294c 100644 --- a/techsupport_bot/commands/winerror.py +++ b/techsupport_bot/commands/winerror.py @@ -7,6 +7,7 @@ from typing import TYPE_CHECKING, Self import discord +import ui from core import auxiliary, cogs from discord import app_commands @@ -96,7 +97,7 @@ async def winerror( upper_sixteen = padded_hex_code[2:6] try: if int(upper_sixteen[0], 16) > 7: - facility_code = int(upper_sixteen, 16) - 0x8000 + facility_code = int(upper_sixteen, 16) & 0x1FFF else: severity = "SUCCESS (0)" facility_code = upper_sixteen @@ -137,27 +138,37 @@ async def winerror( await interaction.response.send_message(embed=embed) return - embed = discord.Embed() - embed.title = "Windows error search results" - embed.description = f"Search results for `{search_term}`" - embed.color = discord.Color.blue() - - cat_count = 1 - # For every category, add a category header and then + embeds = [] + await interaction.response.defer(ephemeral=False) # loop through all errors in the category - for category in categories: - embed.add_field( - name=f"Category {cat_count}", value=category.name, inline=False + for category_index, category in enumerate(categories): + embed = self.generate_blank_embed( + search_term=search_term, + category_index=(category_index + 1), + category_name=category.name, ) - cat_count += 1 - for error in category.errors: + for error_index, error in enumerate(category.errors): + if error_index % 10 == 0 and error_index > 0: + if error_index + 1 == len(category.errors): + continue + embeds.append(embed) + # Create a new embed for the next set of fields + embed = self.generate_blank_embed( + search_term=search_term, + category_index=(category_index + 1), + category_name=category.name, + ) embed.add_field( name=f"{error.name} - {error.source}", value=error.description, inline=False, ) + embeds.append(embed) - await interaction.response.send_message(embed=embed) + # Initialize PaginateView with necessary arguments + await ui.PaginateView().send( + interaction.channel, interaction.user, embeds, interaction + ) def handle_hresult_errors( self: Self, trunc_hex_code: int, severity: str, facility_code: int @@ -327,3 +338,27 @@ def pad_hex(self: Self, hex_code_input: str) -> str: return "0xFFFF" return "0x" + hex_code_input[2:].zfill(8) + + def generate_blank_embed( + self: Self, search_term: str, category_index: int, category_name: str + ) -> discord.Embed: + """Generating an embed for winerror + + Args: + search_term (str): The search term user defined + category_index (int): The categories index for winerror + category_name (str): The name of the categories winerror is searching + + Returns: + discord.Embed: Embed with a title description and category + """ + embed = discord.Embed() + embed.title = "Windows error search results" + embed.description = f"Search results for `{search_term}`" + embed.color = discord.Color.blue() + + embed.add_field( + name=f"Category {category_index}", value=category_name, inline=False + ) + + return embed