-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.py
145 lines (110 loc) · 4.47 KB
/
help.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from discord.ext import commands
from utils.util import Pag
class Help(commands.Cog, name="Help command"):
def __init__(self, bot):
self.bot = bot
self.cmds_per_page = 10
@staticmethod
def get_command_signature(command: commands.Command, ctx: commands.Context):
aliases = "|".join(command.aliases)
cmd_invoke = f"[{command.name}|{aliases}]" if command.aliases else command.name
full_invoke = command.qualified_name.replace(command.name, "")
signature = f"{ctx.prefix}{full_invoke}{cmd_invoke} {command.signature}"
return signature
async def return_filtered_commands(self, walkable, ctx, hide_hidden=True):
filtered = []
for c in walkable.walk_commands():
try:
if hide_hidden and c.hidden:
# command is hidden
continue
# elif c.parent:
# # Command is a subcommand
# continue
await c.can_run(ctx)
filtered.append(c)
except commands.CommandError:
continue
return self.return_sorted_commands(filtered)
@staticmethod
def return_sorted_commands(commandList):
return sorted(commandList, key=lambda x: x.qualified_name)
async def setup_help_pag(self, ctx, entity=None, title=None):
entity = entity or self.bot
title = title or self.bot.description
pages = []
if isinstance(entity, commands.Command):
filtered_commands = (
list(set(entity.all_commands.values()))
if hasattr(entity, "all_commands")
else []
)
filtered_commands.insert(0, entity)
else:
hide_hidden = (
True
if ctx.author.id not in (271612318947868673, 158063324699951104)
else False
)
filtered_commands = await self.return_filtered_commands(
entity, ctx, hide_hidden
)
for i in range(0, len(filtered_commands), self.cmds_per_page):
next_commands = filtered_commands[i : i + self.cmds_per_page]
command_entry = ""
for cmd in next_commands:
desc = cmd.short_doc or cmd.description
signature = self.get_command_signature(cmd, ctx)
extra = [
"Has parent command"
if getattr(cmd, "parent", False) is True
else None,
"Has subcommands" if hasattr(cmd, "all_commands") else None,
"Marked as hidden" if cmd.hidden else None,
]
extra = list(filter(lambda x: x is not None, extra))
aliases = "|".join(cmd.aliases)
if isinstance(entity, commands.Command):
entry = f"• **__{cmd.name}__**\n```\n{signature}\n```\n"
if desc:
entry += f"{desc}\n"
else:
entry = f"• **{cmd.qualified_name}**"
if aliases:
entry += f" -`{aliases}`"
entry += "\n"
if desc:
entry += f"{desc}\n"
if extra:
entry += f" {' | '.join(extra)}\n"
entry += "\n"
command_entry += entry
pages.append(command_entry)
await Pag(
title=title,
colour=0xDD9323,
entries=pages,
length=1,
).start(ctx)
@commands.Cog.listener()
async def on_ready(self):
print(f"{self.__class__.__name__}: Ready")
@commands.command(name="help", aliases=["commands"])
async def help_command(self, ctx, *, entity=None):
"""
Sends a paginated help command or help for an existing entity.
"""
if not entity:
await self.setup_help_pag(ctx, title=f"{self.bot.user.name}'s commands")
else:
cog = self.bot.get_cog(entity)
if cog:
await self.setup_help_pag(ctx, cog, f"{cog.qualified_name}'s commands")
else:
command = self.bot.get_command(entity)
if command:
await self.setup_help_pag(ctx, command, command.name)
else:
await ctx.send("Entity not found.")
def setup(bot):
bot.add_cog(Help(bot))