pnp-scheduler/source/Discord/CommandPartials/GroupSelection.ts

46 lines
No EOL
1.7 KiB
TypeScript

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<void> {
const value = interaction.options.getFocused();
const repo = Container.get<GroupRepository>(GroupRepository.name);
const groups = repo.findGroupsByMember(<GuildMember>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>(GroupRepository.name).findGroupByName((groupname.value ?? '').toString());
if (!group) {
throw new UserError("No group found");
}
return <GroupModel>group;
}
}