|
1 | | -from discord import Color, Embed, Interaction, Member, app_commands |
2 | | -from discord.ext.commands import Cog |
3 | | -from humanize import intcomma |
4 | | -from tabulate import tabulate |
| 1 | +from collections import defaultdict |
| 2 | + |
| 3 | +from discord import Interaction, Member, app_commands |
| 4 | +from discord.ext.commands import GroupCog |
5 | 5 |
|
6 | 6 | from bot.main import ActBot |
7 | 7 | from bot.ui import EmbedX |
8 | | -from db.actor import Actor |
9 | | -from db.item import Item |
10 | | -from db.main import ActToml |
11 | | -from utils.misc import numsign |
12 | 8 |
|
13 | 9 |
|
14 | 10 | # ---------------------------------------------------------------------------------------------------- |
15 | 11 | # * Help Cog |
16 | 12 | # ---------------------------------------------------------------------------------------------------- |
17 | | -class HelpCog(Cog, description="Provide help and information interface."): |
| 13 | +class HelpCog( |
| 14 | + GroupCog, group_name="help", description="Provide help and information interface." |
| 15 | +): |
18 | 16 | def __init__(self, bot: ActBot): |
19 | 17 | self.bot = bot |
20 | 18 |
|
21 | 19 | # ---------------------------------------------------------------------------------------------------- |
22 | | - # * Commands |
23 | | - # ---------------------------------------------------------------------------------------------------- |
24 | | - @app_commands.command(description="View all commands") |
25 | | - async def help(self, interaction: Interaction): |
26 | | - all_cmds = self.bot.tree.get_commands() |
27 | | - embed = Embed( |
28 | | - title=f"🤖 {self.bot.title} v{self.bot.version}", |
29 | | - description=(f"{self.bot.description}\n\n"), |
30 | | - color=Color.blue(), |
| 20 | + |
| 21 | + @app_commands.command(description="Learn about the bot") |
| 22 | + async def about(self, interaction: Interaction): |
| 23 | + embed = EmbedX.info( |
| 24 | + emoji="🤖", |
| 25 | + title=f"{self.bot.title} v{self.bot.version}", |
| 26 | + description=self.bot.description, |
31 | 27 | ) |
32 | | - for cmd in all_cmds: |
33 | | - # Check if command has administrator requirement in default_permissions |
34 | | - admin_required = False |
35 | | - if hasattr(cmd, "default_permissions") and cmd.default_permissions: |
36 | | - admin_required = cmd.default_permissions.administrator |
37 | | - if not admin_required or ( |
38 | | - isinstance(interaction.user, Member) |
39 | | - and interaction.user.guild_permissions.administrator |
40 | | - ): |
41 | | - embed.add_field( |
42 | | - name=f"/{cmd.name}", |
43 | | - value=( |
44 | | - cmd.description |
45 | | - if not isinstance(cmd, app_commands.commands.ContextMenu) |
46 | | - else "" |
47 | | - ), |
48 | | - inline=False, |
49 | | - ) |
50 | 28 | if self.bot.user: |
51 | 29 | embed.set_thumbnail(url=self.bot.user.display_avatar) |
52 | | - await interaction.response.send_message(embed=embed, ephemeral=True) |
| 30 | + await interaction.response.send_message( |
| 31 | + embed=embed, |
| 32 | + ephemeral=True, |
| 33 | + ) |
53 | 34 |
|
54 | | - # ---------------------------------------------------------------------------------------------------- |
55 | | - # * Table |
56 | | - # ---------------------------------------------------------------------------------------------------- |
57 | | - @app_commands.guild_only |
58 | | - @app_commands.command(description="View table of data") |
59 | | - @app_commands.choices( |
60 | | - data=[ |
61 | | - app_commands.Choice(name="📦 Items", value="items"), |
62 | | - app_commands.Choice(name="🏅 Levels", value="levels"), |
63 | | - app_commands.Choice(name="🏆 Ranks", value="ranks"), |
64 | | - app_commands.Choice(name="🎭 Roles", value="roles"), |
65 | | - ] |
| 35 | + @app_commands.command( |
| 36 | + description="View a comprehensive list of all available commands" |
66 | 37 | ) |
67 | | - async def table( |
68 | | - self, |
69 | | - interaction: Interaction, |
70 | | - data: app_commands.Choice[str], |
71 | | - min: int = 0, |
72 | | - max: int = 10, |
73 | | - ): |
74 | | - await interaction.response.defer() |
75 | | - output = " " |
76 | | - match data.value: |
77 | | - case "items": |
| 38 | + async def commands(self, interaction: Interaction): |
| 39 | + # Initialize embed |
| 40 | + embed = EmbedX.info(emoji="🔷", title=f"Commands") |
78 | 41 |
|
79 | | - def format_stat_name(name: str): |
80 | | - if "max_" in name: |
81 | | - name = name.replace("max_", "") |
82 | | - return f"M{name[0].upper()}" |
83 | | - return name[0].upper() |
| 42 | + # Group commands by category/module |
| 43 | + command_groups: dict[str, list[app_commands.Command]] = defaultdict(list) |
| 44 | + for cmd in self.bot.tree.get_commands(): |
| 45 | + # Extract category (you might want to customize this based on your bot's structure) |
| 46 | + category = getattr(cmd, "category", "Root") |
| 47 | + if isinstance(cmd, app_commands.Group): |
| 48 | + category = f"📂 {cmd.name.capitalize()}" |
| 49 | + command_groups[category].append(cmd) |
84 | 50 |
|
85 | | - output = tabulate( |
86 | | - [ |
87 | | - ( |
88 | | - item.name, |
89 | | - " ".join( |
90 | | - f"{numsign(intcomma(value))}{format_stat_name(name)}" |
91 | | - for name, value in item.effective_stats().items() |
92 | | - ), |
93 | | - item.price, |
94 | | - ) |
95 | | - for item in ActToml.load_dict(Item).values() |
96 | | - ], |
97 | | - headers=["Item", "Stats", "Price"], |
98 | | - colalign=["right", "left", "left"], |
99 | | - tablefmt="simple_outline", |
100 | | - ) |
101 | | - case "levels": |
102 | | - output = tabulate( |
103 | | - Actor.level_xp_gold_table(min, max), |
104 | | - headers=["Level", "Experience", "Gold"], |
105 | | - colalign=["right", "left", "left"], |
106 | | - tablefmt="simple_outline", |
107 | | - ) |
108 | | - case "ranks": |
109 | | - output = tabulate( |
110 | | - [(rank.name, elo) for rank, elo in Actor.rank_elo_table(min, max)], |
111 | | - headers=["Rank", "Elo"], |
112 | | - colalign=["right", "left"], |
113 | | - tablefmt="simple_outline", |
114 | | - ) |
115 | | - case "roles": |
116 | | - guild = interaction.guild |
117 | | - roles = ( |
118 | | - sorted(guild.roles, key=lambda role: role.position, reverse=True) |
119 | | - if guild |
120 | | - else [] |
| 51 | + # Check if user is admin for permission filtering |
| 52 | + is_admin = ( |
| 53 | + isinstance(interaction.user, Member) |
| 54 | + and interaction.user.guild_permissions.administrator |
| 55 | + ) |
| 56 | + |
| 57 | + # Build command list for each category |
| 58 | + for category, commands in sorted(command_groups.items()): |
| 59 | + command_lines = [] |
| 60 | + for cmd in sorted(commands, key=lambda x: x.name): |
| 61 | + # Skip admin commands for non-admins |
| 62 | + admin_required = ( |
| 63 | + getattr(cmd, "default_permissions", None) |
| 64 | + and cmd.default_permissions.administrator |
121 | 65 | ) |
122 | | - if roles: |
123 | | - output_list = [] |
124 | | - for role in roles: |
125 | | - output_list.append((role.name, len(role.members))) |
126 | | - output = tabulate( |
127 | | - output_list, |
128 | | - headers=["Role", "Members"], |
129 | | - colalign=["right", "left"], |
130 | | - tablefmt="simple_outline", |
| 66 | + if admin_required and not is_admin: |
| 67 | + continue |
| 68 | + |
| 69 | + # Format command description |
| 70 | + cmd_line = "" |
| 71 | + if isinstance(cmd, app_commands.Group): # Handle command groups |
| 72 | + cmd_line = "\n".join( |
| 73 | + [ |
| 74 | + f"- `/{cmd.name} {subcmd.name}`: {subcmd.description}" |
| 75 | + for subcmd in cmd.commands |
| 76 | + ] |
131 | 77 | ) |
132 | 78 | else: |
133 | | - output = "_No roles found in this server._" |
134 | | - await interaction.followup.send( |
135 | | - embed=EmbedX.info(emoji="", title=data.name, description=f"```{output}```") |
136 | | - ) |
| 79 | + cmd_line = f"- `/{cmd.name}`: {( |
| 80 | + cmd.description |
| 81 | + if not isinstance(cmd, app_commands.ContextMenu) |
| 82 | + else "Context Menu" |
| 83 | + )}" |
| 84 | + |
| 85 | + command_lines.append(cmd_line) |
| 86 | + |
| 87 | + if command_lines: # Only add field if there are visible commands |
| 88 | + embed.add_field( |
| 89 | + name=category, value="\n".join(command_lines), inline=False |
| 90 | + ) |
| 91 | + |
| 92 | + # Send |
| 93 | + if self.bot.user: |
| 94 | + embed.set_thumbnail(url=self.bot.user.display_avatar) |
| 95 | + await interaction.response.send_message(embed=embed, ephemeral=True) |
0 commit comments