From d38cc6a2a2aa848e0d806a782ca97dedb7b1feaa Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 5 Jun 2025 20:43:34 +0000 Subject: [PATCH 1/2] fix placeholder fields --- cogs/logging_cog.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/cogs/logging_cog.py b/cogs/logging_cog.py index 54cf6b9..a5081ce 100644 --- a/cogs/logging_cog.py +++ b/cogs/logging_cog.py @@ -77,12 +77,15 @@ class LoggingCog(commands.Cog): self.header.add_item(ui.TextDisplay(description)) self.container.add_item(self.header) - # Section to hold log fields with no accessory. The API requires a - # valid component type, so use a disabled button with an invisible - # label as a placeholder accessory. + # Section to hold log fields with no accessory. The API requires at + # least one child component, so include an empty text display that + # will be removed when actual fields are added. A disabled button is + # used as the accessory to satisfy Discord's component validation. self.fields_section = ui.Section( accessory=ui.Button(label="\u200b", disabled=True) ) + self._fields_placeholder = ui.TextDisplay("\u200b") + self.fields_section.add_item(self._fields_placeholder) self.container.add_item(self.fields_section) self.container.add_item(ui.Separator(spacing=discord.SeparatorSpacing.small)) @@ -95,6 +98,11 @@ class LoggingCog(commands.Cog): # --- Compatibility helpers --- def add_field(self, name: str, value: str, inline: bool = False): """Mimic Embed.add_field by appending a bolded name/value line.""" + if ( + hasattr(self, "_fields_placeholder") + and self._fields_placeholder in self.fields_section.children + ): + self.fields_section.remove_item(self._fields_placeholder) self.fields_section.add_item(ui.TextDisplay(f"**{name}:** {value}")) def set_footer(self, text: str): @@ -295,12 +303,16 @@ class LoggingCog(commands.Cog): await ctx.send(f"✅ Successfully configured logging to send messages to {channel.mention} via the new webhook `{new_webhook.name}`.") # Test send (optional) try: - test_embed = self._create_log_embed( - "✅ Logging Setup Complete", - f"Logs will now be sent to this channel via the webhook `{new_webhook.name}`.", - color=discord.Color.green(), - ) - await new_webhook.send(view=test_embed, username=webhook_name, avatar_url=self.bot.user.display_avatar.url) + test_view = self._create_log_embed( + "✅ Logging Setup Complete", + f"Logs will now be sent to this channel via the webhook `{new_webhook.name}`.", + color=discord.Color.green(), + ) + await new_webhook.send( + view=test_view, + username=webhook_name, + avatar_url=self.bot.user.display_avatar.url, + ) except Exception as e: log.error(f"Failed to send test message via new webhook for guild {guild.id}: {e}") await ctx.send("⚠️ Could not send a test message via the new webhook, but the URL has been saved.") From e2806adfa2f4da0e6b3b725cf9c2c600f5bc1f71 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 5 Jun 2025 20:49:50 +0000 Subject: [PATCH 2/2] fix log view sections --- cogs/logging_cog.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/cogs/logging_cog.py b/cogs/logging_cog.py index a5081ce..f082d14 100644 --- a/cogs/logging_cog.py +++ b/cogs/logging_cog.py @@ -77,33 +77,35 @@ class LoggingCog(commands.Cog): self.header.add_item(ui.TextDisplay(description)) self.container.add_item(self.header) - # Section to hold log fields with no accessory. The API requires at - # least one child component, so include an empty text display that - # will be removed when actual fields are added. A disabled button is - # used as the accessory to satisfy Discord's component validation. - self.fields_section = ui.Section( - accessory=ui.Button(label="\u200b", disabled=True) - ) - self._fields_placeholder = ui.TextDisplay("\u200b") - self.fields_section.add_item(self._fields_placeholder) - self.container.add_item(self.fields_section) + # Placeholder for future field sections. They are inserted before + # the separator when the first field is added. + self._field_sections: list[ui.Section] = [] - self.container.add_item(ui.Separator(spacing=discord.SeparatorSpacing.small)) + self.separator = ui.Separator(spacing=discord.SeparatorSpacing.small) footer_text = footer or f"Bot ID: {bot.user.id}" + ( f" | User ID: {author.id}" if author else "") self.footer_display = ui.TextDisplay(footer_text) + + self.container.add_item(self.separator) self.container.add_item(self.footer_display) # --- Compatibility helpers --- def add_field(self, name: str, value: str, inline: bool = False): """Mimic Embed.add_field by appending a bolded name/value line.""" - if ( - hasattr(self, "_fields_placeholder") - and self._fields_placeholder in self.fields_section.children - ): - self.fields_section.remove_item(self._fields_placeholder) - self.fields_section.add_item(ui.TextDisplay(f"**{name}:** {value}")) + if not self._field_sections or len(self._field_sections[-1].children) >= 3: + section = ui.Section(accessory=ui.Button(label="\u200b", disabled=True)) + self._insert_field_section(section) + self._field_sections.append(section) + self._field_sections[-1].add_item(ui.TextDisplay(f"**{name}:** {value}")) + + def _insert_field_section(self, section: ui.Section) -> None: + """Insert a field section before the footer separator.""" + self.container.remove_item(self.separator) + self.container.remove_item(self.footer_display) + self.container.add_item(section) + self.container.add_item(self.separator) + self.container.add_item(self.footer_display) def set_footer(self, text: str): """Mimic Embed.set_footer by replacing the footer text display."""