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

46 lines
No EOL
1.8 KiB
TypeScript

import {
AutocompleteInteraction,
ChatInputCommandInteraction,
CommandInteraction,
GuildMember, SlashCommandIntegerOption,
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(): SlashCommandIntegerOption {
return new SlashCommandIntegerOption()
.setName("group")
.setDescription("Defines the group you want to manage the playdates for")
.setRequired(true)
.setAutocomplete(true)
}
public static async handleAutocomplete(interaction: AutocompleteInteraction, onlyLeaders: boolean = false): Promise<void> {
const value = interaction.options.getFocused();
const repo = Container.get<GroupRepository>(GroupRepository.name);
let groups = repo.findGroupsByMember(<GuildMember>interaction.member, onlyLeaders);
await interaction.respond(
groups
.filter((group) => group.name.startsWith(value))
.map((group) => ({name: group.name, value: group.id }))
)
}
public static getGroup(interaction: CommandInteraction|AutocompleteInteraction): GroupModel {
const groupname = interaction.options.get("group", true);
if (!groupname) {
throw new UserError("No group name provided");
}
const group = Container.get<GroupRepository>(GroupRepository.name).getById(<number>(groupname.value ?? 0));
if (!group) {
throw new UserError("No group found");
}
return <GroupModel>group;
}
}