|
1 | | -import { doWalkthrough, generateQuestion } from "@commands/util/walkthrough.js"; |
| 1 | +import { doWalkthrough } from "@commands/util/walkthrough.js"; |
| 2 | +import { handleSelection } from "@ui/walkthrough.js"; |
2 | 3 |
|
3 | | -import issueCategorySelector from "@components/issueCategorySelector.js"; |
4 | | -import productSelector from "@components/productSelector.js"; |
5 | | -import operatingSystemFamilySelector from "@components/operatingSystemFamilySelector.js"; |
| 4 | +import { type Client, Events } from "discord.js"; |
6 | 5 |
|
7 | | -import { |
8 | | - type Client, |
9 | | - EmbedBuilder, |
10 | | - Events, |
11 | | - type InteractionUpdateOptions, |
12 | | -} from "discord.js"; |
13 | | - |
14 | | -// This has to follow the order of the walkthrough steps |
15 | | -const selectors = [ |
16 | | - issueCategorySelector, |
17 | | - productSelector, |
18 | | - operatingSystemFamilySelector, |
19 | | -]; |
20 | | - |
21 | | -function getLabelFromValue(value, selector: (typeof selectors)[number]) { |
22 | | - return selector.options.filter((option) => option.data.value === value)[0] |
23 | | - .data.label; |
24 | | -} |
25 | | - |
26 | | -// TODO: make this readable |
27 | 6 | export default function registerEvents(client: Client) { |
28 | | - // Do walkthrough whenever a thread is opened |
| 7 | + // Run the walkthrough whenever a thread is opened. |
29 | 8 | client.on(Events.ThreadCreate, async (channel) => doWalkthrough(channel)); |
30 | 9 |
|
31 | | - // Register events for the actual walkthrough steps |
| 10 | + // Advance the walkthrough as the user answers each select menu. |
32 | 11 | client.on(Events.InteractionCreate, async (interaction) => { |
33 | 12 | if (interaction.isStringSelectMenu()) { |
34 | | - let messageData: InteractionUpdateOptions; |
35 | | - |
36 | | - const selector = selectors.filter( |
37 | | - (element) => element.data.custom_id === interaction.customId, |
38 | | - )[0]; |
39 | | - const index = selectors.indexOf(selector); |
40 | | - |
41 | | - const lastStep = index + 1 === selectors.length; |
42 | | - |
43 | | - if (index === 0) { |
44 | | - const dataEmbed = new EmbedBuilder() |
45 | | - .setTitle(`<#${interaction.channelId}>`) |
46 | | - .addFields([ |
47 | | - { |
48 | | - name: "Category", |
49 | | - value: getLabelFromValue(interaction.values[0], selector), |
50 | | - inline: true, |
51 | | - }, |
52 | | - { name: "Product", value: "N/A", inline: true }, |
53 | | - { name: "Platform", value: "N/A", inline: true }, |
54 | | - { |
55 | | - name: "Logs", |
56 | | - value: "Please post any relevant logs/error messages.", |
57 | | - }, |
58 | | - ]); |
59 | | - |
60 | | - messageData = generateQuestion( |
61 | | - "What product are you using?", |
62 | | - productSelector, |
63 | | - [dataEmbed], |
64 | | - ); |
65 | | - } else { |
66 | | - // Grab the embed from the last message and edit the corresponding field with the human-readable field (instead of the ID) |
67 | | - const dataEmbed = interaction.message.embeds[0]; |
68 | | - dataEmbed.fields[index].value = getLabelFromValue( |
69 | | - interaction.values[0], |
70 | | - selector, |
71 | | - ); |
72 | | - |
73 | | - // TODO : make this part more generic once we have more questions |
74 | | - if (selector === productSelector) { |
75 | | - messageData = generateQuestion( |
76 | | - `What operating system are you running ${dataEmbed.fields[index].value} on?`, |
77 | | - selectors[index + 1], // next selector |
78 | | - [dataEmbed], |
79 | | - ); |
80 | | - } else if (lastStep) { |
81 | | - // This is the last step of the walkthrough, so we generate an empty message with just the data embed |
82 | | - messageData = { components: [], embeds: [dataEmbed] }; |
83 | | - } else { |
84 | | - throw new Error("No case matches this walkthrough step"); |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - await interaction.update(messageData); |
89 | | - |
90 | | - // If this is the last step of the walkthrough, we pin the message |
91 | | - if (lastStep) { |
92 | | - await interaction.message.pin(); |
93 | | - } |
| 13 | + await handleSelection(interaction); |
94 | 14 | } |
95 | 15 | }); |
96 | 16 | } |
0 commit comments