1- import { doWalkthrough , generateMessage } from "@commands/util/walkthrough.js" ;
1+ import { doWalkthrough , generateQuestion } from "@commands/util/walkthrough.js" ;
22
33import issueCategorySelector from "@components/issueCategorySelector.js" ;
44import productSelector from "@components/productSelector.js" ;
@@ -11,6 +11,19 @@ import {
1111 type InteractionUpdateOptions ,
1212} from "discord.js" ;
1313
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
1427export default function registerEvents ( client : Client ) {
1528 // Do walkthrough whenever a thread is opened
1629 client . on ( Events . ThreadCreate , async ( channel ) => doWalkthrough ( channel ) ) ;
@@ -20,12 +33,22 @@ export default function registerEvents(client: Client) {
2033 if ( interaction . isStringSelectMenu ( ) ) {
2134 let message : InteractionUpdateOptions ;
2235
23- // TODO : make this code more generic
24- if ( interaction . customId === issueCategorySelector . data . custom_id ) {
36+ const selector = selectors . filter (
37+ ( element ) => element . data . custom_id === interaction . customId ,
38+ ) [ 0 ] ;
39+ const index = selectors . indexOf ( selector ) ;
40+
41+ const nextSelector = selectors [ index + 1 ] ;
42+
43+ if ( index === 0 ) {
2544 const dataEmbed = new EmbedBuilder ( )
2645 . setTitle ( `<#${ interaction . channelId } >` )
2746 . addFields ( [
28- { name : "Category" , value : interaction . values [ 0 ] , inline : true } ,
47+ {
48+ name : "Category" ,
49+ value : getLabelFromValue ( interaction . values [ 0 ] , selector ) ,
50+ inline : true ,
51+ } ,
2952 { name : "Product" , value : "N/A" , inline : true } ,
3053 { name : "Platform" , value : "N/A" , inline : true } ,
3154 {
@@ -34,33 +57,35 @@ export default function registerEvents(client: Client) {
3457 } ,
3558 ] ) ;
3659
37- message = generateMessage (
60+ message = generateQuestion (
3861 "What product are you using?" ,
3962 productSelector ,
4063 [ dataEmbed ] ,
4164 ) ;
42- } else if ( interaction . customId === productSelector . data . custom_id ) {
43- // Grab the embed from the last message and edit the "Product" field
65+ } else {
66+ // Grab the embed from the last message and edit the corresponding field with the human-readable field (instead of the ID)
4467 const dataEmbed = interaction . message . embeds [ 0 ] ;
45- dataEmbed . fields [ 1 ] . value = interaction . values [ 0 ] ;
46-
47- // TODO: replace "the product" by the name of the product that was chosen in the previous step (productSelector)
48- message = generateMessage (
49- "What operating system are you running the product on?" ,
50- operatingSystemFamilySelector ,
51- [ dataEmbed ] ,
68+ dataEmbed . fields [ index ] . value = getLabelFromValue (
69+ interaction . values [ 0 ] ,
70+ selector ,
5271 ) ;
53- } else if (
54- interaction . customId === operatingSystemFamilySelector . data . custom_id
55- ) {
56- // Grab the embed from the last message and edit the "Product" field
57- const dataEmbed = interaction . message . embeds [ 0 ] ;
58- dataEmbed . fields [ 2 ] . value = interaction . values [ 0 ] ;
5972
60- // Generate an empty message with just the data embed
61- message = { components : [ ] , embeds : [ dataEmbed ] } ;
73+ // TODO : make this part more generic once we have more questions
74+ if ( selector === productSelector ) {
75+ message = generateQuestion (
76+ `What operating system are you running ${ dataEmbed . fields [ index ] . value } on?` ,
77+ nextSelector ,
78+ [ dataEmbed ] ,
79+ ) ;
80+ } else if ( index + 1 === selectors . length ) {
81+ // <- means this is the last step of the walkthrough
82+ // Generate an empty message with just the data embed and pin it
83+ message = { components : [ ] , embeds : [ dataEmbed ] } ;
6284
63- // TODO: pin
85+ await interaction . message . pin ( ) ;
86+ } else {
87+ throw new Error ( "No case matches this walkthrough step" ) ;
88+ }
6489 }
6590
6691 await interaction . update ( message ) ;
0 commit comments