Newer
Older
import inspect
import re
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
import utils
data = {"category": {"__none__": {"title": "Sonstiges", "description": "Die Kategorie für die Kategorielosen."}},
"command": {}}
def help_category(name=None, title=None, description=None, mod_description=None):
def decorator_help(cmd):
data["category"][name] = {"title": title, "description": description,
"mod_description": mod_description if mod_description else description}
# if not data["category"][name]:
# data["category"][name] = {"description": description}
# else:
# data["category"][name]["description"] = description
return cmd
return decorator_help
@help_category("help", "Hilfe", "Wenn du nicht weiter weißt, gib `!help` ein.",
"Wenn du nicht weiter weißt, gib `!mod-help` ein.")
def text_command_help(name, syntax=None, example=None, brief=None, description=None, mod=False, parameters=None,
category=None):
if parameters is None:
parameters = {}
cmd = re.sub(r"^!", "", name)
if syntax is None:
syntax = name
add_help(cmd, syntax, example, brief, description, mod, parameters, category)
def remove_help_for(name):
data["command"].pop(name)
def help(syntax=None, example=None, brief=None, description=None, mod=False, parameters=None, category=None,
command_group=''):
if parameters is None:
parameters = {}
def decorator_help(cmd):
nonlocal syntax, parameters
cmd_name = f"{command_group} {cmd.name}" if command_group else f"{cmd.name}"
if syntax is None:
arguments = inspect.signature(cmd.callback).parameters
function_arguments = [
f"<{item[1].name}{'?' if item[1].default != inspect._empty else ''}>" for item in
list(arguments.items())[2:]]
syntax = f"!{cmd_name} {' '.join(function_arguments)}"
add_help(cmd_name, syntax, example, brief,
description, mod, parameters, category)
return cmd
return decorator_help
def add_help(cmd, syntax, example, brief, description, mod, parameters, category=None):
if not category:
category = "__none__"
data["command"][cmd] = {
"name": cmd,
"syntax": syntax.strip(),
"brief": brief,
"example": example,
"description": description,
"parameters": parameters,
"mod": mod,
"category": category
}
async def handle_error(ctx, error):
if isinstance(error, commands.errors.MissingRequiredArgument):
# syntax = data[ctx.command.name]['syntax']
# example = data[ctx.command.name]['example']
msg = (
f"Fehler! Du hast ein Argument vergessen. Für weitere Hilfe gib `!help {ctx.command.name}` ein. \n"
f"`Syntax: {data['command'][ctx.command.name]['syntax']}`\n"
)
await ctx.channel.send(msg)
else:
raise error
class Help(commands.Cog):
def __init__(self, bot):
self.bot = bot
@help(
category="help",
brief="Zeigt die verfügbaren Kommandos an. Wenn ein Kommando übergeben wird, wird eine ausführliche Hilfe zu diesem Kommando angezeigt.",
)
@commands.command(name="help")
async def cmd_help(self, ctx, *command):
if len(command) > 0:
command = re.sub(r"^!", "", ' '.join(command))
await self.help_card(ctx, command)
return
await self.help_overview(ctx)
@help(
category="help",
brief="Zeigt die verfügbaren Hilfe-Kategorien an.",
mod=True
)
@commands.command(name="help-categories")
@commands.check(utils.is_mod)
async def cmd_categories(self, ctx):
sorted_groups = {k: v for k, v in sorted(data["category"].items(), key=lambda item: item[1]['title'])}
text = ""
for key, value in sorted_groups.items():
text += f"**{key} => {value['title']}**\n"
text += f"- {value['description']}\n" if value['description'] else ""
await ctx.channel.send(text)
@help(
category="help",
brief="Zeigt die verfügbaren Kommandos *für Mods* an. Wenn ein Kommando übergeben wird, wird eine ausführliche Hilfe zu diesem Kommando angezeigt. ",
mod=True
)
@commands.command(name="mod-help")
@commands.check(utils.is_mod)
async def cmd_mod_help(self, ctx, command=None):
if not command is None:
command = re.sub(r"^!", "", command)
if command == "*" or command == "all":
await self.help_overview(ctx, mod=True, all=True)
return
await self.help_card(ctx, command)
return
await self.help_overview(ctx, mod=True)
async def help_overview(self, ctx, mod=False, all=False):
sorted_groups = {k: v for k, v in sorted(data["category"].items(), key=lambda item: item[1]['title'] if item[
0] != '__none__' else 'zzzzzzzzzzzzzz')}
sorted_commands = {k: v for k, v in sorted(data["command"].items(), key=lambda item: item[1]['syntax'])}
title = "root hilft dir!"
help_command = "!help" if not mod else "!mod-help"
helptext = (
f"Um ausführliche Hilfe zu einem bestimmten Kommando zu erhalten, gib **{help_command} <command>** ein. "
f"Also z.B. **{help_command} stats** um mehr über das Statistik-Kommando zu erfahren.")
helptext += "`!mod-help *` gibt gleichzeitig mod und nicht-mod Kommandos in der Liste aus." if mod else ""
helptext += "\n\n"
msgcount = 1
for key, group in sorted_groups.items():
text = f"\n__**{group['title']}**__\n"
text += f"{group['mod_description']}\n" if group.get('mod_description') and mod else ""
text += f"{group['description']}\n" if group.get('description') and not mod else ""
text += "\n"
for command in sorted_commands.values():
if (not all and command['mod'] != mod) or command['category'] != key:
continue
# {'*' if command['description'] else ''}\n"
text += f"**{command['syntax']}**\n"
text += f"{command['brief']}\n\n" if command['brief'] else "\n"
if (len(helptext) + len(text) > 2048):
description=helptext,
color=19607)
await utils.send_dm(ctx.author, "", embed=embed)
helptext = ""
msgcount = msgcount + 1
title = f"root hilft dir! (Fortsetzung {msgcount})"
helptext += text
text = ""
description=helptext,
color=19607)
await utils.send_dm(ctx.author, "", embed=embed)
async def help_card(self, ctx, name):
try:
command = data['command'][name]
if command['mod'] and not utils.is_mod(ctx):
raise KeyError
except KeyError:
await ctx.channel.send(
"Fehler! Für dieses Kommando habe ich keinen Hilfe-Eintrag. Gib `!help` ein um eine Übersicht zu erhalten. ")
return
title = command['name']
text = f"**{title}**\n"
text += f"{command['brief']}\n\n" if command['brief'] else ""
text += f"**Syntax:**\n `{command['syntax']}`\n"
text += "**Parameter:**\n" if len(command['parameters']) > 0 else ""
for param, desc in command['parameters'].items():
text += f"`{param}` - {desc}\n"
text += f"**Beispiel:**\n `{command['example']}`\n" if command['example'] else ""
text += f"\n{command['description']}\n" if command['description'] else ""
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
description=text,
color=19607)
await utils.send_dm(ctx.author, text) # , embed=embed)
@commands.command(name="debug-help")
@commands.check(utils.is_mod)
async def help_all(self, ctx, mod=False):
sorted_groups = {k: v for k, v in sorted(data["category"].items(), key=lambda item: item[1]['title'] if item[
0] != '__none__' else 'zzzzzzzzzzzzzz')}
sorted_commands = {k: v for k, v in sorted(data["command"].items(), key=lambda item: item[1]['syntax'])}
title = "root hilft dir!"
helptext = ("Um ausführliche Hilfe zu einem bestimmten Kommando zu erhalten, gib **!help <command>** ein. "
"Also z.B. **!help stats** um mehr über das Statistik-Kommando zu erfahren.\n\n\n")
msgcount = 1
for key, group in sorted_groups.items():
text = f"\n__**{group['title']}**__\n"
text += f"{group['description']}\n\n" if group['description'] else "\n"
for command in sorted_commands.values():
if command['category'] != key:
continue
text += f"**{command['name']}**{' (mods only)' if command['mod'] else ''}\n"
text += f"{command['brief']}\n\n" if command['brief'] else ""
text += f"**Syntax:**\n `{command['syntax']}`\n"
text += "**Parameter:**\n" if len(
command['parameters']) > 0 else ""
for param, desc in command['parameters'].items():
text += f"`{param}` - {desc}\n"
text += f"**Beispiel:**\n `{command['example']}`\n" if command['example'] else ""
text += f"\n{command['description']}\n" if command['description'] else ""
text += "=====================================================\n"
if (len(helptext) + len(text) > 2048):
description=helptext,
color=19607)
await utils.send_dm(ctx.author, "", embed=embed)
helptext = ""
msgcount = msgcount + 1
title = f"root hilft dir! (Fortsetzung {msgcount})"
helptext += text
text = ""
description=helptext,
color=19607)
await utils.send_dm(ctx.author, "", embed=embed)