-
Notifications
You must be signed in to change notification settings - Fork 50
/
helpall.py
153 lines (131 loc) · 5.07 KB
/
helpall.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
146
147
148
149
150
151
152
153
"""This is a cog for a discord.py bot.
It hides the help command and adds these commands:
helpall show all commands (including all hidden ones)
The commands will output to the current channel or to a dm channel
according to the pm_help kwarg of the bot.
Only users that have an admin role can use the commands.
"""
import itertools
from discord import Embed
from discord.ext import commands
from discord.ext.commands import HelpCommand, DefaultHelpCommand
#pylint: disable=E1101
class myHelpCommand(HelpCommand):
def __init__(self, **options):
super().__init__(**options)
self.paginator = None
self.spacer = "\u1160 " # Invisible Unicode Character to indent lines
async def send_pages(self, header=False, footer=False):
destination = self.get_destination()
embed = Embed(
color=0x2ECC71
)
if header:
embed.set_author(
name=self.context.bot.description,
icon_url=self.context.bot.user.display_avatar
)
for category, entries in self.paginator:
embed.add_field(
name=category,
value=entries,
inline=False
)
if footer:
embed.set_footer(
text='Use felix help <command/category> for more information.'
)
await destination.send(embed=embed)
async def send_bot_help(self, mapping):
ctx = self.context
bot = ctx.bot
def get_category(command):
cog = command.cog
return cog.qualified_name + ':' if cog is not None else 'Help:'
filtered = await self.filter_commands(
bot.commands,
sort=True,
key=get_category
)
to_iterate = itertools.groupby(filtered, key=get_category)
for cog_name, command_grouper in to_iterate:
cmds = sorted(command_grouper, key=lambda c: c.name)
category = f'► {cog_name}'
if len(cmds) == 1:
entries = f'{self.spacer}{cmds[0].name} → {cmds[0].short_doc}'
else:
entries = ''
while len(cmds) > 0:
entries += self.spacer
entries += ' | '.join([cmd.name for cmd in cmds[0:8]])
cmds = cmds[8:]
entries += '\n' if cmds else ''
self.paginator.append((category, entries))
await self.send_pages(header=True, footer=True)
async def send_cog_help(self, cog):
filtered = await self.filter_commands(cog.get_commands(), sort=True)
if not filtered:
await self.context.send(
'No public commands in this cog. Try again with felix helpall.'
)
return
category = f'▼ {cog.qualified_name}'
entries = '\n'.join(
self.spacer +
f'**{command.name}** → {command.short_doc or command.description}'
for command in filtered
)
self.paginator.append((category, entries))
await self.send_pages(footer=True)
async def send_group_help(self, group):
filtered = await self.filter_commands(group.commands, sort=True)
if not filtered:
await self.context.send(
'No public commands in group. Try again with felix helpall.'
)
return
category = f'**{group.name}** - {group.description or group.short_doc}'
entries = '\n'.join(
self.spacer + f'**{command.name}** → {command.short_doc}'
for command in filtered
)
self.paginator.append((category, entries))
await self.send_pages(footer=True)
async def send_command_help(self, command):
signature = self.get_command_signature(command)
helptext = command.help or command.description or 'No help Text'
self.paginator.append(
(signature, helptext)
)
await self.send_pages()
async def prepare_help_command(self, ctx, command=None):
self.paginator = []
await super().prepare_help_command(ctx, command)
class Help(commands.Cog):
def __init__(self, client):
self.client = client
self.client.help_command = myHelpCommand(
command_attrs={
'aliases': ['halp'],
'help': 'Shows help about the bot, a command, or a category'
}
)
async def cog_check(self, ctx):
return self.client.user_is_admin(ctx.author)
def cog_unload(self):
self.client.get_command('help').hidden = False
self.client.help_command = DefaultHelpCommand()
@commands.command(
aliases=['halpall'],
hidden=True
)
async def helpall(self, ctx, *, text=None):
"""Print bot help including all hidden commands"""
self.client.help_command = myHelpCommand(show_hidden=True)
if text:
await ctx.send_help(text)
else:
await ctx.send_help()
self.client.help_command = myHelpCommand()
def setup(client):
client.add_cog(Help(client))