Skip to content

Commit d3cd9d1

Browse files
committed
Autocomplete
1 parent 972b148 commit d3cd9d1

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/aliases.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export const _COMMAND = {
1010
{
1111
name: 'topic',
1212
description: 'What seekest thou to understand?',
13-
type: 3
13+
type: 3,
14+
autocomplete: true
1415
},
1516
],
1617
};
@@ -22,7 +23,8 @@ export const _COMMAND_HOSHI = {
2223
{
2324
name: 'topic',
2425
description: 'What seekest thou to understand?',
25-
type: 3
26+
type: 3,
27+
autocomplete: true
2628
},
2729
],
2830
};
@@ -34,7 +36,8 @@ export const _COMMAND_NEMI = {
3436
{
3537
name: 'topic',
3638
description: 'What seekest thou to understand?',
37-
type: 3
39+
type: 3,
40+
autocomplete: true
3841
},
3942
],
4043
};

src/server.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,36 @@ router.post('/', async (request, env) => {
7777
}
7878
}
7979

80+
if (interaction.type === InteractionType.APPLICATION_COMMAND_AUTOCOMPLETE) {
81+
switch (interaction.data.name.toLowerCase()) {
82+
case _COMMAND.name.toLowerCase():
83+
case _COMMAND_HOSHI.name.toLowerCase():
84+
case _COMMAND_NEMI.name.toLowerCase(): {
85+
// I know this code doesn't look or feel too great...
86+
// Please leave me alone, I'm a C# dev... I hate JS......
87+
const optionValue = interaction.data.options?.[0]?.value.toLowerCase();
88+
const focused = optionValue || '';
89+
90+
const matches = Object.keys(commandMap)
91+
.filter(key => key.toLowerCase().includes(focused))
92+
.slice(0, 25) // Discord max
93+
.map(key => ({
94+
name: key, // what shows up in dropdown
95+
value: key, // what gets sent back if selected
96+
}));
97+
98+
return new JsonResponse({
99+
type: InteractionResponseType.APPLICATION_COMMAND_AUTOCOMPLETE_RESULT,
100+
data: {
101+
choices: matches,
102+
},
103+
});
104+
}
105+
default:
106+
return new JsonResponse({ error: 'Unknown Type' }, { status: 400 });
107+
}
108+
}
109+
80110
console.error('Unknown Type');
81111
return new JsonResponse({ error: 'Unknown Type' }, { status: 400 });
82112
});

0 commit comments

Comments
 (0)