import { SlashCommandBuilder, Interaction, CommandInteraction, ChatInputCommandInteraction, MessageFlags, GuildMemberRoleManager, InteractionReplyOptions, GuildMember } from "discord.js"; import {ChatInteractionCommand, Command} from "./Command"; import {GroupModel} from "../../Models/GroupModel"; import {GroupRepository} from "../../Repositories/GroupRepository"; import {DatabaseConnection} from "../../Database/DatabaseConnection"; import {Container} from "../../Container/Container"; export class GroupCommand implements Command, ChatInteractionCommand { definition(): SlashCommandBuilder { // @ts-ignore return new SlashCommandBuilder() .setName('groups') .setDescription(`Manages groups`) .addSubcommand(create => create.setName("create") .setDescription("Creates a new group, with executing user being the leader") .addStringOption((option) => option.setName("name") .setDescription("Defines the name for the group.") .setRequired(true) ) .addRoleOption((builder) => builder.setName("role") .setDescription("Defines the role, where all the members are located in.") .setRequired(true) ) ) .addSubcommand(listCommand => listCommand .setName("list") .setDescription("Displays the groups you are apart of.") ); } execute(interaction: ChatInputCommandInteraction): Promise { switch (interaction.options.getSubcommand()) { case "create": this.create(interaction); break; case "list": this.list(interaction); break; default: throw new Error("Unsupported command"); } return Promise.resolve(); } private create(interaction: ChatInputCommandInteraction): void { const name = interaction.options.getString("name") ?? ''; const role = interaction.options.getRole("role"); const group: GroupModel = { id: -1, name: name, leader: { server: interaction.guildId ?? '', memberid: interaction.member?.user.id ?? '' }, role: { server: interaction.guildId ?? '', roleid: role?.id ?? '' } } Container.get(GroupRepository.name).create(group); interaction.reply({content: `:white_check_mark: Created group \`${name}\``, flags: MessageFlags.Ephemeral }) } private list(interaction: ChatInputCommandInteraction) { const repo = Container.get(GroupRepository.name); const groups = repo.findGroupsByMember(interaction.member); const reply: InteractionReplyOptions = { embeds: [ { title: "Your groups on this server:", fields: groups.map((group) => { return { name: group.name, value: "" } }) } ], flags: MessageFlags.Ephemeral } interaction.reply(reply); } }