-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcog.py
175 lines (156 loc) · 6.67 KB
/
cog.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
Cog containing help command. Only shows commands that user has access to
and are context commands.
"""
import copy
import disnake
from disnake.ext import commands
import utils
from buttons.embed import PaginationView
from cogs.base import Base
from rubbergod import Rubbergod
from utils import cooldowns
from .messages_cz import MessagesCZ
class Help(Base, commands.Cog):
"""Help command"""
def __init__(self, bot: Rubbergod):
super().__init__()
self.bot = bot
def command_details(self, prefix: str, command: commands.Command):
return {
"command": f"{prefix}{command.name}",
"signature": command.signature,
"description": command.brief,
"aliases": command.aliases,
}
def command_help(self, ctx: commands.Context, command: commands.Command):
"""Generate help for commands and subcommands"""
current_page = list()
prefix = self.config.default_prefix
if isinstance(command, commands.Group):
# group command without invoked subcommand is separate command
# e.g. karma, reviews
if command.usage is not None:
current_page.append(self.command_details(prefix, command))
key_prefix = f"{prefix}{command.name} "
for subcommand in command.commands:
for check in subcommand.checks:
try:
if not check(ctx):
break
except Exception:
break
else:
current_page.append(self.command_details(key_prefix, subcommand))
elif not command.parent:
current_page.append(self.command_details(prefix, command))
return current_page
def generate_pages(self, ctx: commands.Context):
"""Generate pages for help. Page per cog. Including subcommands and applying commands checks"""
pages = list()
for name, cog in self.bot.cogs.items():
current_page = list()
for command in cog.walk_commands():
for check in command.checks:
try:
if not check(ctx):
break
except Exception:
break
else:
current_page += self.command_help(ctx, command)
if current_page:
pages.append({"commands": current_page, "description": cog.description, "groupName": name})
return pages
def generate_embed(self, page):
embed = disnake.Embed(
title=MessagesCZ.title, description=MessagesCZ.description, color=disnake.Colour.yellow()
)
embed.set_thumbnail(url=self.bot.user.display_avatar.url)
self.add_fields(embed, page["commands"])
return embed
def add_fields(self, embed, items):
"""Add fields to embed help.
param: items: List of dict with commands
"""
for item in items:
value = ""
name = f'{item["command"]}'
if item["signature"] and item["signature"] != " ":
name += f' `{item["signature"]}`'
if item["aliases"]:
value += f'**Alias: **{", ".join(item["aliases"])}\n'
value += item["description"] if item["description"] else ""
embed.add_field(name=name, value=value if value else None, inline=False)
async def api(self, message: commands.Context, params: dict[str, str]):
"""Sending commands help to grillbot"""
mock_message = copy.copy(message)
mock_view = commands.view.StringView("")
mock_message.author = self.bot.get_user(params.get("user_id"))
ctx = commands.Context(
bot=self.bot,
view=mock_view,
prefix=self.config.default_prefix,
message=mock_message,
)
if "command" in params and params["command"] is not None:
if params["command"] == "slash_commands":
res = {}
for slash in self.bot.slash_commands:
res[slash.name] = {
"id": utils.general.get_command_id(self.bot, slash.name),
"children": list(slash.children.keys()),
}
return 0, res
command = self.bot.get_command(params["command"])
if not command:
return 1, "Command not found"
help = {}
for check in command.checks:
try:
if not check(ctx):
break
except Exception:
break
else:
help = self.command_help(ctx, command)
else: # return help for all commands
help = self.generate_pages(ctx)
return 0, help
@cooldowns.default_cooldown
@commands.command(aliases=["god"], brief=MessagesCZ.title)
async def help(self, ctx: commands.Context, *command_list: str):
# Subcommand help
command = " ".join(command_list)
if command:
command_obj = self.bot.get_command(command)
if not command_obj:
await ctx.send(MessagesCZ.command_not_found(command=command[:1024]))
else:
# if command group, show all possible subcommands
if isinstance(command_obj, commands.Group):
subcommands = []
if command_obj.usage is not None:
subcommands.append(command_obj.usage.replace("[", "").replace("]", ""))
subcommands += [subcommand.name for subcommand in command_obj.commands]
text = f"`{self.config.default_prefix}{command_obj.name} [{', '.join(subcommands)}]`"
else:
text = f"`{self.config.default_prefix}{command_obj} {command_obj.signature}`"
if command_obj.description:
text += f"\n{command_obj.description}"
elif command_obj.brief:
text += f"\n{command_obj.brief}"
await ctx.send(text)
return
# General help
pages = self.generate_pages(ctx)
pages_total = len(pages)
embeds = []
for idx, page in enumerate(pages):
embed = self.generate_embed(page)
if pages_total > 1:
footer_text = f"Strana {idx+1}/{pages_total}"
embed.set_footer(text=footer_text, icon_url=ctx.author.display_avatar.url)
embeds.append(embed)
view = PaginationView(ctx.author, embeds, perma_lock=True)
view.message = await ctx.reply(embed=embeds[0], view=view)