refactor(menu): Made sure the menu can be used for more than group

This commit is contained in:
Michel Fedde 2025-06-20 17:48:00 +02:00
parent a79898b2e9
commit 1d73ee8a78
16 changed files with 650 additions and 406 deletions

View file

@ -1,12 +1,15 @@
import {
SlashCommandBuilder,
ChatInputCommandInteraction,
MessageFlags,
InteractionReplyOptions,
GuildMember,
EmbedBuilder,
AutocompleteInteraction,
roleMention, time, userMention, GuildMemberRoleManager
ChatInputCommandInteraction,
EmbedBuilder,
GuildMember,
GuildMemberRoleManager,
InteractionReplyOptions,
MessageFlags,
roleMention,
SlashCommandBuilder,
time,
userMention
} from "discord.js";
import {AutocompleteCommand, ChatInteractionCommand, Command} from "./Command";
import {GroupModel} from "../../Models/GroupModel";
@ -21,6 +24,10 @@ import {GroupConfigurationTransformers} from "../../Groups/GroupConfigurationTra
import {GroupConfigurationRepository} from "../../Repositories/GroupConfigurationRepository";
import {PlaydateRepository} from "../../Repositories/PlaydateRepository";
import {Nullable} from "../../types/Nullable";
import {MenuRenderer} from "../../Menu/MenuRenderer";
import {MenuItemType} from "../../Menu/MenuRenderer.types";
import {ConfigurationMenuHandler} from "../../Groups/ConfigurationMenuHandler";
import {MenuTraversal} from "../../Menu/MenuTraversal";
export class GroupCommand implements Command, ChatInteractionCommand, AutocompleteCommand {
private static GOODBYE_MESSAGES: string[] = [
@ -232,16 +239,23 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
private async runConfigurator(interaction: ChatInputCommandInteraction) {
const group = GroupSelection.getGroup(interaction);
const configurationRenderer = new GroupConfigurationRenderer(
const menuHandler = new ConfigurationMenuHandler(
new GroupConfigurationHandler(
Container.get<GroupConfigurationRepository>(GroupConfigurationRepository.name),
group
),
new GroupConfigurationTransformers(),
)
await configurationRenderer.setup(interaction);
const menu = new MenuRenderer(
new MenuTraversal(
menuHandler.getMenuItems(),
'Group Configuration',
"This UI allows you to change settings for your group."
)
)
menu.display(interaction);
}
private async transferLeadership(interaction: ChatInputCommandInteraction) {

View file

@ -1,25 +1,33 @@
import {
AutocompleteInteraction,
AnySelectMenuInteraction,
AutocompleteInteraction, ButtonInteraction,
ChatInputCommandInteraction,
inlineCode,
Interaction,
MessageFlags,
MessageFlags, ModalSubmitInteraction,
} from "discord.js";
import Commands from "./Commands/Commands";
import {Logger} from "log4js";
import {UserError} from "./UserError";
import {Container} from "../Container/Container";
import {EventHandler} from "../Events/EventHandler";
import {ModalInteractionEvent} from "../Events/ModalInteractionEvent";
import {ComponentInteractionEvent} from "../Events/ComponentInteractionEvent";
enum InteractionRoutingType {
Unrouted,
Command,
AutoComplete,
ModalSubmit,
ButtonSubmit,
MenuSubmit,
}
export class InteractionRouter {
constructor(
public readonly commands: Commands,
public readonly logger: Logger
public readonly logger: Logger,
private readonly events: EventHandler
) {
}
@ -36,6 +44,12 @@ export class InteractionRouter {
case InteractionRoutingType.AutoComplete:
await this.handleAutocomplete(<AutocompleteInteraction>interaction)
break;
case InteractionRoutingType.ModalSubmit:
this.events.dispatch(new ModalInteractionEvent(<ModalSubmitInteraction>interaction));
break;
case InteractionRoutingType.ButtonSubmit:
case InteractionRoutingType.MenuSubmit:
this.events.dispatch(new ComponentInteractionEvent(<ButtonInteraction|AnySelectMenuInteraction>interaction))
}
}
@ -47,6 +61,17 @@ export class InteractionRouter {
if (interaction.isAutocomplete()) {
return InteractionRoutingType.AutoComplete;
}
if (interaction.isModalSubmit()) {
return InteractionRoutingType.ModalSubmit;
}
if (interaction.isButton()) {
return InteractionRoutingType.ButtonSubmit;
}
if (interaction.isAnySelectMenu()) {
return InteractionRoutingType.MenuSubmit;
}
return InteractionRoutingType.Unrouted;
}