-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelp.py
More file actions
223 lines (176 loc) · 7.23 KB
/
Copy pathHelp.py
File metadata and controls
223 lines (176 loc) · 7.23 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Builtins
import asyncio
from typing import List
# Pip
import discord
from discord.ext import commands
def chunks(parts, size):
"""Yield successive size-sized chunks from parts."""
for i in range(0, len(parts), size):
yield parts[i:i + size]
def helper(ctx) -> List[discord.Embed]:
"""Displays all commands"""
cmds_ = []
cogs = ctx.bot.cogs
for i in cogs:
cmd_ = ctx.bot.get_cog(i).get_commands()
cmd_ = [x for x in cmd_ if not x.hidden]
for x in list(chunks(list(cmd_), 6)):
embed = discord.Embed(colour=discord.Colour.from_rgb(250,0,0))
embed.set_author(name=f"{i} Commands ({len(cmd_)})")
embed.description = ctx.bot.cogs[i].__doc__
for y in x:
embed.add_field(name=f"{y.name} {y.signature}", value=y.help, inline=False)
cmds_.append(embed)
for n, a in enumerate(cmds_):
a.set_footer(
text=f'Page {n+1} of {len(cmds_)} | Type "{ctx.prefix}help <command>" for more information'
)
return cmds_
def cog_helper(cog) -> List[discord.Embed]:
"""Displays commands from a cog"""
name = cog.__class__.__name__
cmds_ = []
cmd = [x for x in cog.get_commands() if not x.hidden]
if not cmd:
return (discord.Embed(colour=discord.Colour.from_rgb(250,0,0),
description=f"{name} commands are hidden.")
.set_author(name="ERROR \N{NO ENTRY SIGN}"))
for i in list(chunks(list(cmd), 6)):
embed = discord.Embed(colour=discord.Colour.from_rgb(250,0,0))
embed.set_author(name=name)
embed.description = cog.__doc__
for x in i:
embed.add_field(name=f"{x.name} {x.signature}", value=x.help, inline=False)
cmds_.append(embed)
for n, a in enumerate(cmds_):
a.set_footer(text=f"Page {n+1} of {len(cmds_)}")
return cmds_
def command_helper(command: commands.command) -> List[discord.Embed]:
"""Displays a command and it's sub commands"""
try:
cmd = [x for x in command.commands if not x.hidden] # retrieves commands that are not hidden
cmds_ = []
for i in list(chunks(list(cmd), 6)):
embed = discord.Embed(colour=discord.Colour.from_rgb(250,0,0))
embed.set_author(name=command.signature)
embed.description = command.help
for x in i:
embed.add_field(name=f"{x.name} {x.signature}", value=x.help, inline=False)
cmds_.append(embed)
for n, x in enumerate(cmds_):
x.set_footer(text=f"Page {n+1} of {len(cmds_)}")
return cmds_
except AttributeError:
embed = discord.Embed(colour=discord.Colour.from_rgb(250,0,0))
embed.set_author(name=f"{command.name} {command.signature}")
embed.description = command.help
return [embed]
# ?tag lazy paginator is a more refined/easier to read version of this paginator.
async def paginate(ctx, input_: List[discord.Embed]) -> None:
"""Paginator"""
try:
pages = await ctx.send(embed=input_[0])
except (AttributeError, TypeError):
await ctx.send(embed=input_)
return
if len(input_) == 1:
return
current = 0
r = ['\U000023ee', '\U000025c0', '\U000025b6',
'\U000023ed', '\U0001f522', '\U000023f9']
for x in r:
await pages.add_reaction(x)
paging = True
while paging:
reaction = None
def check(r_, u_) -> bool:
return u_ == ctx.author and r_.message.id == pages.id and str(r_.emoji) in r
done, pending = await asyncio.wait([ctx.bot.wait_for('reaction_add', check=check, timeout=120),
ctx.bot.wait_for('reaction_remove', check=check, timeout=120)],
return_when=asyncio.FIRST_COMPLETED)
try:
reaction, user = done.pop().result()
except asyncio.TimeoutError:
try:
await pages.clear_reactions()
except discord.Forbidden:
return
paging = False
for future in pending:
future.cancel()
else:
if str(reaction.emoji) == r[2]:
current += 1
if current == len(input_):
current = 0
try:
await pages.remove_reaction(r[2], ctx.author)
except discord.Forbidden:
pass
await pages.edit(embed=input_[current])
await pages.edit(embed=input_[current])
elif str(reaction.emoji) == r[1]:
current -= 1
if current == 0:
try:
await pages.remove_reaction(r[1], ctx.author)
except discord.Forbidden:
pass
await pages.edit(embed=input_[len(input_) - 1])
await pages.edit(embed=input_[current])
elif str(reaction.emoji) == r[0]:
current = 0
try:
await pages.remove_reaction(r[0], ctx.author)
except discord.Forbidden:
pass
await pages.edit(embed=input_[current])
elif str(reaction.emoji) == r[3]:
current = len(input_) - 1
try:
await pages.remove_reaction(r[3], ctx.author)
except discord.Forbidden:
pass
await pages.edit(embed=input_[current])
elif str(reaction.emoji) == r[4]:
m = await ctx.send(f"What page you do want to go? 1-{len(input_)}")
def pager(m_) -> bool:
return m_.author == ctx.author and m_.channel == ctx.channel and int(m_.content) > 1 <= len(input_)
try:
msg = int((await ctx.bot.wait_for('message', check=pager, timeout=60)).content)
except asyncio.TimeoutError:
#await m.delete()
return
current = msg - 1
try:
await pages.remove_reaction(r[4], ctx.author)
except discord.Forbidden:
pass
await pages.edit(embed=input_[current])
else:
try:
await pages.clear_reactions()
except discord.Forbidden:
return
paging = False
class Help(commands.Cog):
"""Help command"""
__slots__ = ('bot',)
def __init__(self, bot):
self.bot = bot
@commands.command(aliases=["h"], hidden=True)
async def help(self, ctx, *, command: str = None) -> None:
if not command:
await paginate(ctx, helper(ctx))
if command:
thing = ctx.bot.get_cog(command) or ctx.bot.get_command(command)
if not thing:
await ctx.send(f'Looks like "{command}" is not a command or category.')
return
if isinstance(thing, commands.Command):
await paginate(ctx, command_helper(thing))
else:
await paginate(ctx, cog_helper(thing))
def setup(bot) -> None:
bot.add_cog(Help(bot))