From 7e0e0052893ec10aff1429d3dd2b1e84cf557848 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Thu, 25 Jul 2024 13:20:04 -0400 Subject: [PATCH 1/6] Adds a crude debug command --- techsupport_bot/commands/debug.py | 102 ++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 techsupport_bot/commands/debug.py diff --git a/techsupport_bot/commands/debug.py b/techsupport_bot/commands/debug.py new file mode 100644 index 00000000..a0e355e6 --- /dev/null +++ b/techsupport_bot/commands/debug.py @@ -0,0 +1,102 @@ +"""The channel slowmode modification extension +Holds only a single slash command""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Self + +import discord +from core import auxiliary, cogs +from discord import app_commands + +if TYPE_CHECKING: + import bot + + +async def setup(bot: bot.TechSupportBot) -> None: + """Registers the slowmode cog + + Args: + bot (bot.TechSupportBot): The bot to register the cog to + """ + await bot.add_cog(Debugger(bot=bot)) + + +class Debugger(cogs.BaseCog): + """The cog that holds the slowmode commands and helper functions + + Attrs: + debug_group (app_commands.Group): The group for the /debug commands + """ + + debug_group = app_commands.Group( + name="debug", description="...", extras={"module": "debug"} + ) + + @app_commands.check(auxiliary.bot_admin_check_interaction) + @debug_group.command( + name="message", + description="Searches and displays all the message properties", + extras={ + "module": "debug", + }, + ) + async def debug_message( + self: Self, + interaction: discord.Interaction, + channel: discord.abc.GuildChannel, + id: str, + ) -> None: + """Modifies slowmode on a given channel + + Args: + interaction (discord.Interaction): The interaction that called this command + seconds (int): The seconds to change the slowmode to. 0 will disable slowmode + channel (discord.abc.GuildChannel, optional): If specified, the channel to modify + slowmode on. Defaults to the channel the command was invoked in. + """ + message = await channel.fetch_message(str(id)) + properties_string = "" + + for attribute in dir(message): + if not attribute.startswith("_"): + value = getattr(message, attribute) + temp_string = f"**{attribute}:** {value}\n" + if temp_string.startswith(f"**{attribute}:** None: + """Modifies slowmode on a given channel + + Args: + interaction (discord.Interaction): The interaction that called this command + seconds (int): The seconds to change the slowmode to. 0 will disable slowmode + channel (discord.abc.GuildChannel, optional): If specified, the channel to modify + slowmode on. Defaults to the channel the command was invoked in. + """ + properties_string = "" + + for attribute in dir(member): + if not attribute.startswith("_"): + value = getattr(member, attribute) + temp_string = f"**{attribute}:** {value}\n" + if temp_string.startswith(f"**{attribute}:** Date: Mon, 19 May 2025 19:29:42 -0400 Subject: [PATCH 2/6] Make some changes --- techsupport_bot/commands/debug.py | 125 +++++++++++++++++++++++------- 1 file changed, 99 insertions(+), 26 deletions(-) diff --git a/techsupport_bot/commands/debug.py b/techsupport_bot/commands/debug.py index a0e355e6..d239d937 100644 --- a/techsupport_bot/commands/debug.py +++ b/techsupport_bot/commands/debug.py @@ -1,11 +1,15 @@ -"""The channel slowmode modification extension -Holds only a single slash command""" +""" +This is a development and issue tracking command designed to dump all attributes +of a given object type. +Current supported is message, member and channel +""" from __future__ import annotations from typing import TYPE_CHECKING, Self import discord +import ui from core import auxiliary, cogs from discord import app_commands @@ -44,7 +48,7 @@ class Debugger(cogs.BaseCog): async def debug_message( self: Self, interaction: discord.Interaction, - channel: discord.abc.GuildChannel, + channel: discord.TextChannel, id: str, ) -> None: """Modifies slowmode on a given channel @@ -55,19 +59,12 @@ async def debug_message( channel (discord.abc.GuildChannel, optional): If specified, the channel to modify slowmode on. Defaults to the channel the command was invoked in. """ + await interaction.response.defer(ephemeral=False) message = await channel.fetch_message(str(id)) - properties_string = "" + embeds = build_debug_embed(message) - for attribute in dir(message): - if not attribute.startswith("_"): - value = getattr(message, attribute) - temp_string = f"**{attribute}:** {value}\n" - if temp_string.startswith(f"**{attribute}:** None: + """Modifies slowmode on a given channel + + Args: + interaction (discord.Interaction): The interaction that called this command + seconds (int): The seconds to change the slowmode to. 0 will disable slowmode + channel (discord.abc.GuildChannel): + """ + await interaction.response.defer(ephemeral=False) + embeds = build_debug_embed(channel) + view = ui.PaginateView() + await view.send(interaction.channel, interaction.user, embeds, interaction) + + +def build_debug_embed(object: object): + """Builds a list of embeds, with each one at a max of 4000 characters + This will be every attribute of the given object. + + Args: + object (object): A discord object that needs to be explored + """ + all_strings = [] + properties_string = "" + + for attribute in dir(object): + if not attribute.startswith("_"): + try: + value = getattr(object, attribute) + except AttributeError: + continue + + temp_string = f"**{attribute}:** {value}\n" + if temp_string.startswith(f"**{attribute}:** 1500: + all_strings.append(properties_string) + properties_string = "" + + properties_string += print_string + + all_strings.append(properties_string) + + embeds = [] + for string in all_strings: + embeds.append(discord.Embed(description=string[:4000])) + + return embeds + + +def format_attribute_chunks(attribute: str, value: str) -> list[str]: + def make_chunk_label(index: int, total: int) -> str: + return f"**{attribute} ({index}):** " if total > 1 else f"**{attribute}:** " + + max_length = 750 + # First, determine the prefix length assuming worst-case (e.g., "attribute (10): ") + temp_prefix = f"**{attribute} (999):** " # Conservative estimation + chunk_size = max_length - len(temp_prefix) - 1 # Reserve space for prefix and \n + + # Create raw chunks of value + raw_chunks = [value[i : i + chunk_size] for i in range(0, len(value), chunk_size)] + total_chunks = len(raw_chunks) + + # Format each chunk with appropriate prefix + return [ + f"{make_chunk_label(i + 1, total_chunks)}{chunk}\n" + for i, chunk in enumerate(raw_chunks) + ] From ca9dbd3bd46896e052021790f3b4e0cb5c8394e4 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Mon, 9 Jun 2025 10:19:12 -0400 Subject: [PATCH 3/6] Formatting --- techsupport_bot/commands/debug.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/techsupport_bot/commands/debug.py b/techsupport_bot/commands/debug.py index d239d937..37de827a 100644 --- a/techsupport_bot/commands/debug.py +++ b/techsupport_bot/commands/debug.py @@ -18,7 +18,7 @@ async def setup(bot: bot.TechSupportBot) -> None: - """Registers the slowmode cog + """Registers the debugger cog Args: bot (bot.TechSupportBot): The bot to register the cog to @@ -156,12 +156,22 @@ def build_debug_embed(object: object): def format_attribute_chunks(attribute: str, value: str) -> list[str]: + """This makes a simple paginated split fields, to break up long attributes + + Args: + attribute (str): The name of the attribute to be formatted. + value (str): The string representation of the attribute + + Returns: + list[str]: A list of attributes, split if needed + """ + def make_chunk_label(index: int, total: int) -> str: return f"**{attribute} ({index}):** " if total > 1 else f"**{attribute}:** " max_length = 750 - # First, determine the prefix length assuming worst-case (e.g., "attribute (10): ") - temp_prefix = f"**{attribute} (999):** " # Conservative estimation + + temp_prefix = f"**{attribute} (999):** " chunk_size = max_length - len(temp_prefix) - 1 # Reserve space for prefix and \n # Create raw chunks of value From e1b4a6abed7cabc2a720e720b126cc6d04f2c58e Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Mon, 9 Jun 2025 10:25:41 -0400 Subject: [PATCH 4/6] Formatting --- techsupport_bot/commands/debug.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/techsupport_bot/commands/debug.py b/techsupport_bot/commands/debug.py index 37de827a..03cd6c8c 100644 --- a/techsupport_bot/commands/debug.py +++ b/techsupport_bot/commands/debug.py @@ -29,7 +29,7 @@ async def setup(bot: bot.TechSupportBot) -> None: class Debugger(cogs.BaseCog): """The cog that holds the slowmode commands and helper functions - Attrs: + Attributes: debug_group (app_commands.Group): The group for the /debug commands """ @@ -51,13 +51,12 @@ async def debug_message( channel: discord.TextChannel, id: str, ) -> None: - """Modifies slowmode on a given channel + """Searches for a message by ID in the given channel. Args: interaction (discord.Interaction): The interaction that called this command - seconds (int): The seconds to change the slowmode to. 0 will disable slowmode - channel (discord.abc.GuildChannel, optional): If specified, the channel to modify - slowmode on. Defaults to the channel the command was invoked in. + channel (discord.TextChannel): The channel to find the message in + id (str): The ID of the message to search for """ await interaction.response.defer(ephemeral=False) message = await channel.fetch_message(str(id)) @@ -77,13 +76,11 @@ async def debug_message( async def debug_member( self: Self, interaction: discord.Interaction, member: discord.Member ) -> None: - """Modifies slowmode on a given channel + """Displays attributes for a member of the guild where the command was run. Args: interaction (discord.Interaction): The interaction that called this command - seconds (int): The seconds to change the slowmode to. 0 will disable slowmode - channel (discord.abc.GuildChannel, optional): If specified, the channel to modify - slowmode on. Defaults to the channel the command was invoked in. + member (discord.Member): The member to search for information on """ await interaction.response.defer(ephemeral=False) embeds = build_debug_embed(member) @@ -101,12 +98,11 @@ async def debug_member( async def debug_channel( self: Self, interaction: discord.Interaction, channel: discord.abc.GuildChannel ) -> None: - """Modifies slowmode on a given channel + """Displays attributes for a channel of the guild where the command was run. Args: interaction (discord.Interaction): The interaction that called this command - seconds (int): The seconds to change the slowmode to. 0 will disable slowmode - channel (discord.abc.GuildChannel): + channel (discord.abc.GuildChannel): The channel to search for information on """ await interaction.response.defer(ephemeral=False) embeds = build_debug_embed(channel) @@ -114,12 +110,15 @@ async def debug_channel( await view.send(interaction.channel, interaction.user, embeds, interaction) -def build_debug_embed(object: object): +def build_debug_embed(object: object) -> list[discord.Embed]: """Builds a list of embeds, with each one at a max of 4000 characters This will be every attribute of the given object. Args: object (object): A discord object that needs to be explored + + Returns: + list[discord.Embed]: A list of embeds to be displayed in a paginated display """ all_strings = [] properties_string = "" From 59fbd8cf5fee6c3271830197532889ac3b00afe0 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Mon, 9 Jun 2025 10:29:08 -0400 Subject: [PATCH 5/6] More formatting --- techsupport_bot/commands/debug.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/techsupport_bot/commands/debug.py b/techsupport_bot/commands/debug.py index 03cd6c8c..1d87b20c 100644 --- a/techsupport_bot/commands/debug.py +++ b/techsupport_bot/commands/debug.py @@ -33,7 +33,7 @@ class Debugger(cogs.BaseCog): debug_group (app_commands.Group): The group for the /debug commands """ - debug_group = app_commands.Group( + debug_group: app_commands.Group = app_commands.Group( name="debug", description="...", extras={"module": "debug"} ) From 826b1d82738708cc6662903bf0871063fc7a5f86 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Mon, 9 Jun 2025 10:32:41 -0400 Subject: [PATCH 6/6] Even more formatting --- techsupport_bot/commands/debug.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/techsupport_bot/commands/debug.py b/techsupport_bot/commands/debug.py index 1d87b20c..071bcc74 100644 --- a/techsupport_bot/commands/debug.py +++ b/techsupport_bot/commands/debug.py @@ -110,12 +110,12 @@ async def debug_channel( await view.send(interaction.channel, interaction.user, embeds, interaction) -def build_debug_embed(object: object) -> list[discord.Embed]: +def build_debug_embed(discord_object: object) -> list[discord.Embed]: """Builds a list of embeds, with each one at a max of 4000 characters This will be every attribute of the given object. Args: - object (object): A discord object that needs to be explored + discord_object (object): A discord object that needs to be explored Returns: list[discord.Embed]: A list of embeds to be displayed in a paginated display @@ -123,10 +123,10 @@ def build_debug_embed(object: object) -> list[discord.Embed]: all_strings = [] properties_string = "" - for attribute in dir(object): + for attribute in dir(discord_object): if not attribute.startswith("_"): try: - value = getattr(object, attribute) + value = getattr(discord_object, attribute) except AttributeError: continue