1
- import { doWalkthrough , generateMessage } from "@commands/util/walkthrough.js" ;
1
+ import { doWalkthrough , generateQuestion } from "@commands/util/walkthrough.js" ;
2
2
3
3
import issueCategorySelector from "@components/issueCategorySelector.js" ;
4
4
import productSelector from "@components/productSelector.js" ;
@@ -11,6 +11,19 @@ import {
11
11
type InteractionUpdateOptions ,
12
12
} from "discord.js" ;
13
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
14
27
export default function registerEvents ( client : Client ) {
15
28
// Do walkthrough whenever a thread is opened
16
29
client . on ( Events . ThreadCreate , async ( channel ) => doWalkthrough ( channel ) ) ;
@@ -20,12 +33,22 @@ export default function registerEvents(client: Client) {
20
33
if ( interaction . isStringSelectMenu ( ) ) {
21
34
let message : InteractionUpdateOptions ;
22
35
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 ) {
25
44
const dataEmbed = new EmbedBuilder ( )
26
45
. setTitle ( `<#${ interaction . channelId } >` )
27
46
. 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
+ } ,
29
52
{ name : "Product" , value : "N/A" , inline : true } ,
30
53
{ name : "Platform" , value : "N/A" , inline : true } ,
31
54
{
@@ -34,33 +57,35 @@ export default function registerEvents(client: Client) {
34
57
} ,
35
58
] ) ;
36
59
37
- message = generateMessage (
60
+ message = generateQuestion (
38
61
"What product are you using?" ,
39
62
productSelector ,
40
63
[ dataEmbed ] ,
41
64
) ;
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)
44
67
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 ,
52
71
) ;
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 ] ;
59
72
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 ] } ;
62
84
63
- // TODO: pin
85
+ await interaction . message . pin ( ) ;
86
+ } else {
87
+ throw new Error ( "No case matches this walkthrough step" ) ;
88
+ }
64
89
}
65
90
66
91
await interaction . update ( message ) ;
0 commit comments