Skip to content

Commit 2aeae64

Browse files
authored
feat(walkthrough): mention /close and /reopen as command mentions (#44)
* feat(walkthrough): mention /close and /reopen as command mentions * refactor(walkthrough): resolve command mentions inside message builder
1 parent a4897f6 commit 2aeae64

2 files changed

Lines changed: 75 additions & 41 deletions

File tree

‎src/commands/util/walkthrough.ts‎

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { config } from "@lib/config.js";
22

33
import { isHelpPost as isHelpThread } from "@lib/discord/channels.js";
4+
import { getCommandMention } from "@lib/discord/commands.js";
45
import issueCategorySelector from "@components/issueCategorySelector.js";
56

67
import {
78
type ChatInputCommandInteraction,
9+
type Client,
810
SlashCommandBuilder,
911
ActionRowBuilder,
1012
type StringSelectMenuBuilder,
@@ -24,52 +26,63 @@ import {
2426
type InteractionReplyOptions,
2527
} from "discord.js";
2628

27-
const resourcesMessage = {
28-
flags: MessageFlags.IsComponentsV2,
29-
30-
components: [
31-
new ContainerBuilder().addSectionComponents([
32-
new SectionBuilder()
33-
.addTextDisplayComponents(
34-
new TextDisplayBuilder({ content: "Where to find logs" }),
35-
)
36-
.setButtonAccessory(
37-
new ButtonBuilder()
38-
.setStyle(ButtonStyle.Link)
39-
.setLabel("Docs")
40-
.setURL("https://coder.com/docs/admin/monitoring/logs"),
41-
),
42-
43-
new SectionBuilder()
44-
.addTextDisplayComponents(
45-
new TextDisplayBuilder({
46-
content: "Troubleshooting templates",
47-
}),
48-
)
49-
.setButtonAccessory(
50-
new ButtonBuilder()
51-
.setStyle(ButtonStyle.Link)
52-
.setLabel("Docs")
53-
.setURL("https://coder.com/docs/admin/templates/troubleshooting"),
54-
),
29+
async function buildResourcesMessage(client: Client) {
30+
return {
31+
flags: MessageFlags.IsComponentsV2,
5532

56-
new SectionBuilder()
33+
components: [
34+
new ContainerBuilder()
35+
.addSectionComponents([
36+
new SectionBuilder()
37+
.addTextDisplayComponents(
38+
new TextDisplayBuilder({ content: "Where to find logs" }),
39+
)
40+
.setButtonAccessory(
41+
new ButtonBuilder()
42+
.setStyle(ButtonStyle.Link)
43+
.setLabel("Docs")
44+
.setURL("https://coder.com/docs/admin/monitoring/logs"),
45+
),
46+
47+
new SectionBuilder()
48+
.addTextDisplayComponents(
49+
new TextDisplayBuilder({
50+
content: "Troubleshooting templates",
51+
}),
52+
)
53+
.setButtonAccessory(
54+
new ButtonBuilder()
55+
.setStyle(ButtonStyle.Link)
56+
.setLabel("Docs")
57+
.setURL(
58+
"https://coder.com/docs/admin/templates/troubleshooting",
59+
),
60+
),
61+
62+
new SectionBuilder()
63+
.addTextDisplayComponents(
64+
new TextDisplayBuilder({
65+
content: "Troubleshooting networking",
66+
}),
67+
)
68+
.setButtonAccessory(
69+
new ButtonBuilder()
70+
.setStyle(ButtonStyle.Link)
71+
.setLabel("Docs")
72+
.setURL(
73+
"https://coder.com/docs/admin/networking/troubleshooting",
74+
),
75+
),
76+
])
77+
.addSeparatorComponents(new SeparatorBuilder())
5778
.addTextDisplayComponents(
5879
new TextDisplayBuilder({
59-
content: "Troubleshooting networking",
80+
content: `When your issue is resolved, use ${await getCommandMention(client, "close")} to close this post. Use ${await getCommandMention(client, "reopen")} to reopen it if needed.`,
6081
}),
61-
)
62-
.setButtonAccessory(
63-
new ButtonBuilder()
64-
.setStyle(ButtonStyle.Link)
65-
.setLabel("Docs")
66-
.setURL("https://coder.com/docs/admin/networking/troubleshooting"),
6782
),
68-
]),
69-
70-
new SeparatorBuilder(),
71-
],
72-
};
83+
],
84+
};
85+
}
7386

7487
export function generateQuestion(
7588
question: string,
@@ -94,6 +107,8 @@ export async function doWalkthrough(
94107
if (await isHelpThread(channel)) {
95108
const threadChannel = channel as PublicThreadChannel; // necessary type cast, isHelpThread does the check already
96109

110+
const resourcesMessage = await buildResourcesMessage(channel.client);
111+
97112
// Check for tags in the forum post
98113
const appliedTags = threadChannel.appliedTags ?? [];
99114
if (!appliedTags.includes(config.helpChannel.openedTag)) {

‎src/lib/discord/commands.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { config } from "@lib/config.js";
2+
3+
import type { Client } from "discord.js";
4+
5+
// Resolves a slash command mention (</name:id>) by looking the command ID up
6+
// from the ones the bot registered in the guild. Falls back to plain text if
7+
// the command cannot be found.
8+
export async function getCommandMention(
9+
client: Client,
10+
name: string,
11+
): Promise<string> {
12+
const commands = await client.application.commands.fetch({
13+
guildId: config.serverId,
14+
});
15+
16+
const command = commands.find((cmd) => cmd.name === name);
17+
18+
return command ? `</${command.name}:${command.id}>` : `/${name}`;
19+
}

0 commit comments

Comments
 (0)