Skip to content

Commit b9f1e32

Browse files
committed
feat: choices
1 parent 82d84a4 commit b9f1e32

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/handlers/MessageCommandHandler.ts

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client, Collection, CommandInteractionOptionResolver, Guild, Message, TextChannel, User } from 'discord.js';
1+
import { Client, Collection, CommandInteractionOptionResolver, Guild, Message, MessageActionRow, MessageSelectMenu, SelectMenuInteraction, TextChannel, User } from 'discord.js';
22
import type { GClient } from '../lib/GClient';
33
import { CommandContext } from '../lib/structures/contexts/CommandContext';
44
import { CommandType } from '../lib/structures/Command';
@@ -13,10 +13,38 @@ const cooldowns = new Collection<string, Collection<string, number>>();
1313

1414
const checkValidation = async(arg: MessageArgumentTypes, content: string, client: Client, guild: Guild, argument: Argument, channel: TextChannel, user: User) => {
1515
if (!content) {
16-
channel.send(`${user.toString()}, please define argument \`${argument.name}\`, type: ${Util.toPascalCase(ArgumentType[argument.type.toString()])}`);
17-
const message = await channel.awaitMessages({ filter: (m) => m.author.id === user.id && m.channelId === channel.id, time: 60000, max: 1 });
16+
const text = `${user.toString()}, please define argument \`${argument.name}\`, type: ${Util.toPascalCase(ArgumentType[argument.type.toString()])}`
17+
if (argument.type === ArgumentType.STRING && argument.choices?.length !== 0) {
18+
const menu = new MessageSelectMenu()
19+
.setCustomId('argument_choices')
20+
.setMaxValues(1)
21+
.setMinValues(0)
22+
.setPlaceholder('Select a choice');
1823

19-
content = [...message.values()]?.[0]?.content;
24+
menu.setOptions(
25+
argument.choices.map(
26+
ch => ({
27+
label: ch.name,
28+
value: ch.value
29+
})
30+
)
31+
);
32+
33+
const message = await channel.send({
34+
content: text,
35+
components: [ new MessageActionRow().addComponents(menu) ]
36+
});
37+
38+
const component: SelectMenuInteraction = await channel.awaitMessageComponent({ filter: (m) => m.componentType === 'SELECT_MENU' && m.user.id === user.id && m.channelId === channel.id && m.message.id === message.id && m.customId === 'argument_choices', time: 60000 }) as SelectMenuInteraction;
39+
40+
component.deferUpdate();
41+
content = component.values?.[0];
42+
} else {
43+
channel.send(text);
44+
const message = await channel.awaitMessages({ filter: (m) => m.author.id === user.id && m.channelId === channel.id, time: 60000, max: 1 });
45+
46+
content = [...message.values()]?.[0]?.content;
47+
}
2048
}
2149

2250
const validate = arg.validate(content);

0 commit comments

Comments
 (0)