Skip to content

Commit

Permalink
finish the help commands
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 committed Jul 24, 2024
1 parent cc2aa89 commit 37baf08
Showing 1 changed file with 77 additions and 3 deletions.
80 changes: 77 additions & 3 deletions bot/cogs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

if TYPE_CHECKING:
from cogs.tickets import Tickets

from rodhaj import Rodhaj


Expand Down Expand Up @@ -232,6 +233,44 @@ async def get_config(self) -> Optional[GuildWebhook]:
return GuildWebhook(bot=self.bot, **dict(rows))


class ConfigHelpEntry(msgspec.Struct, frozen=True):
key: str
default: str
description: str
examples: list[str]
notes: list[str]


class ConfigEntryEmbed(Embed):
def __init__(self, entry: ConfigHelpEntry, **kwargs):
super().__init__(**kwargs)
self.title = entry.key
self.description = entry.description
self.add_field(name="Default", value=entry.default, inline=False)
self.add_field(name="Example(s)", value="\n".join(entry.examples), inline=False)
self.add_field(
name="Notes",
value="\n".join(f"- {note}" for note in entry.notes) or None,
inline=False,
)


class ConfigHelpPageSource(menus.ListPageSource):
async def format_page(self, menu: ConfigHelpPages, entry: ConfigHelpEntry):
embed = ConfigEntryEmbed(entry=entry)

maximum = self.get_max_pages()
if maximum > 1:
embed.set_footer(text=f"Page {menu.current_page + 1}/{maximum}")
return embed


class ConfigHelpPages(RoboPages):
def __init__(self, entries: list[ConfigHelpEntry], *, ctx: GuildContext):
super().__init__(ConfigHelpPageSource(entries, per_page=1), ctx=ctx)
self.embed = discord.Embed()


class ConfigPageSource(menus.AsyncIteratorPageSource):
def __init__(self, entries: dict[str, Any], active: Optional[bool] = None):
super().__init__(self.config_iterator(entries), per_page=20)
Expand Down Expand Up @@ -300,15 +339,16 @@ def disambiguate(self, argument: str, keys: list[str]) -> str:
return f"Key not found. Did you mean...\n{close_keys}"

async def convert(self, ctx: GuildContext, argument: str) -> str:
lowered = argument.lower()
cog: Optional[Config] = ctx.bot.get_cog("Config") # type: ignore

if not cog:
raise RuntimeError("Unable to get Config cog")

if argument not in cog.config_keys:
raise commands.BadArgument(self.disambiguate(argument, cog.config_keys))
if lowered not in cog.config_keys:
raise commands.BadArgument(self.disambiguate(lowered, cog.config_keys))

return argument
return lowered


class ConfigValueConverter(commands.Converter):
Expand Down Expand Up @@ -750,6 +790,34 @@ async def config_options(
pages = ConfigPages(guild_settings.to_dict(), ctx=ctx, active=flags.active)
await pages.start()

@is_manager()
@commands.guild_only()
@config.group(name="help", aliases=["info"])
async def config_help(
self, ctx: GuildContext, option: Annotated[str, ConfigKeyConverter]
) -> None:
"""Shows help information for different configuration options"""
# Because we are using the converter, all options are guaranteed to be correct
embed = ConfigEntryEmbed(
ConfigHelpEntry(key=option, **self.options_help[option])
)
await ctx.send(embed=embed)

@is_manager()
@commands.guild_only()
@config_help.command(name="all")
async def config_help_all(self, ctx: GuildContext):
"""Shows all possible help information for all configurations"""
# We need to separate this since we are using the key converter. If it is an invalid option, it passes back None,
# thus causing it to show all entries. This isn't that useful when you just made one mistake.
# Modmail handles this differently by internally looking for the key, and giving an whole embed list of possible options
converted = [
ConfigHelpEntry(key=key, **item)
for key, item in self.options_help.all().items()
]
pages = ConfigHelpPages(entries=converted, ctx=ctx)
await pages.start()

@is_manager()
@commands.guild_only()
@config.command(name="set-age")
Expand Down Expand Up @@ -853,6 +921,12 @@ async def on_config_toggle_error(
):
await self._handle_error(error, ctx=ctx)

@config_help.error
async def on_config_help_error(
self, ctx: GuildContext, error: commands.CommandError
):
await self._handle_error(error, ctx=ctx)

@is_manager()
@commands.guild_only()
@config.group(name="prefix", fallback="info")
Expand Down

0 comments on commit 37baf08

Please sign in to comment.