-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhelp.ts
113 lines (95 loc) · 3.29 KB
/
help.ts
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
import { Bot, CommandRegistration } from '../bot';
import { Snippet } from '../entities/Snippet';
import { sendWithMessageOwnership } from '../util/send';
import { MessageBuilder } from '../util/messageBuilder';
function getCategoryHelp(cat: string, commands: Iterable<CommandRegistration>) {
const out: string[] = [];
for (const cmd of new Set(commands)) {
if (!cmd.description) continue;
const [cat2, description] = splitCategoryDescription(cmd.description);
if (cat !== cat2) continue;
out.push(`\`${cmd.aliases[0]}\` ► ${description}`);
}
return out.join('\n');
}
function splitCategoryDescription(description: string): [string, string] {
const split = description.split(': ', 2);
if (split.length !== 2) {
return ['Misc', description];
}
return split as [string, string];
}
function getCommandCategories(commands: Iterable<CommandRegistration>) {
const categories = new Set<string>();
for (const cmd of commands) {
categories.add(splitCategoryDescription(cmd.description ?? '')[0]);
}
return [...categories].sort((a, b) => a.localeCompare(b));
}
export function helpModule(bot: Bot) {
bot.registerCommand({
aliases: ['help', 'commands', 'h'],
description: "Sends what you're looking at right now",
async listener(msg) {
const cmdTrigger = msg.content.split(/\s/)[1];
if (!msg.guild) return;
if (!cmdTrigger) {
const response = new MessageBuilder()
.setTitle('Bot Usage')
.setDescription(
`Hello ${msg.author.username}! Here is a list of all commands in me! To get detailed description on any specific command, do \`help <command>\``,
);
for (const cat of getCommandCategories(bot.commands.values())) {
response.addFields({
name: `${cat} Commands:`,
value: getCategoryHelp(cat, bot.commands.values()),
});
}
response.addFields({
name: 'Playground Links:',
value: 'I will shorten any [TypeScript Playground](<https://www.typescriptlang.org/play>) links in a message or attachment and display a preview of the code. You can choose specific lines to embed by selecting them before copying the link.',
});
return await sendWithMessageOwnership(msg, response.build());
}
let cmd: { description?: string; aliases?: string[] } =
bot.getByTrigger(cmdTrigger) || {};
if (!cmd.description && cmdTrigger.includes(':')) {
const snippet = await Snippet.findOne({
where: { title: cmdTrigger },
});
if (snippet)
cmd = {
description: `A custom snippet created by <@${snippet.owner}>`,
};
else
cmd = {
description:
'Run the first snippet that matches that pattern',
};
}
if (!cmd.description)
return await sendWithMessageOwnership(
msg,
`:x: Command not found`,
);
const builder = new MessageBuilder().setTitle(
`\`${cmdTrigger}\` Usage`,
);
// Get rid of duplicates, this can happen if someone adds the method name as an alias
const triggers = new Set(cmd.aliases ?? [cmdTrigger]);
if (triggers.size > 1) {
builder.addFields({
name: 'Aliases',
value: Array.from(triggers, t => `\`${t}\``).join(', '),
});
}
builder.addFields({
name: 'Description',
value: `*${
splitCategoryDescription(cmd.description ?? '')[1]
}*`,
});
await sendWithMessageOwnership(msg, builder.build());
},
});
}