44 lines
No EOL
1.7 KiB
TypeScript
44 lines
No EOL
1.7 KiB
TypeScript
import {
|
|
AutocompleteInteraction,
|
|
CommandInteraction,
|
|
GuildMember, SlashCommandIntegerOption,
|
|
} 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 this action is 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);
|
|
const 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;
|
|
}
|
|
} |