import { AutocompleteInteraction, ChatInputCommandInteraction, CommandInteraction, GuildMember, SlashCommandStringOption } from "discord.js"; import {Container} from "../../Container/Container"; import {GroupRepository} from "../../Repositories/GroupRepository"; import {GroupModel} from "../../Models/GroupModel"; import {UserError} from "../UserError"; export class GroupSelection { public static createOptionSetup(): SlashCommandStringOption { return new SlashCommandStringOption() .setName("group") .setDescription("Defines the group you want to manage the playdates for") .setRequired(true) .setAutocomplete(true) } public static async handleAutocomplete(interaction: AutocompleteInteraction): Promise { const value = interaction.options.getFocused(); const repo = Container.get(GroupRepository.name); const groups = repo.findGroupsByMember(interaction.member); await interaction.respond( groups .filter((group) => group.name.startsWith(value)) .map((group) => ({name: group.name, value: group.name })) ) } public static getGroup(interaction: CommandInteraction): GroupModel { const groupname = interaction.options.get("group"); if (!groupname) { throw new UserError("No group name provided"); } const group = Container.get(GroupRepository.name).findGroupByName((groupname.value ?? '').toString()); if (!group) { throw new UserError("No group found"); } return group; } }