mirror of
https://github.com/sern-handler/handler
synced 2026-06-17 13:22:17 +00:00
feat: adding modal and autocomplete support
This commit is contained in:
@@ -2,6 +2,7 @@ import type {
|
||||
CommandInteraction,
|
||||
Interaction,
|
||||
MessageComponentInteraction,
|
||||
ModalSubmitInteraction,
|
||||
SelectMenuInteraction,
|
||||
} from 'discord.js';
|
||||
import { concatMap, fromEvent, map, Observable, of, throwError } from 'rxjs';
|
||||
@@ -125,6 +126,23 @@ function messageComponentInteractionHandler(
|
||||
.otherwise(() => throwError(() => SernError.NotSupportedInteraction));
|
||||
}
|
||||
|
||||
function modalHandler(modul : Module|undefined, ctx: ModalSubmitInteraction) {
|
||||
return of(modul).pipe(
|
||||
filterCorrectModule(CommandType.Modal),
|
||||
concatMap(mod => {
|
||||
return of(mod.onEvent?.map(e => e.execute([ctx], controller)) ?? []).pipe(
|
||||
map(res => ({
|
||||
mod,
|
||||
res,
|
||||
execute() {
|
||||
return mod.execute(ctx);
|
||||
},
|
||||
})),
|
||||
);
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export function onInteractionCreate(wrapper: Wrapper) {
|
||||
const { client } = wrapper;
|
||||
|
||||
@@ -146,7 +164,15 @@ export function onInteractionCreate(wrapper: Wrapper) {
|
||||
interaction.customId,
|
||||
);
|
||||
return messageComponentInteractionHandler(modul, interaction);
|
||||
} else return throwError(() => SernError.NotSupportedInteraction);
|
||||
}
|
||||
if (interaction.isModalSubmit()) {
|
||||
const modul = Files.ModalSubmitCommands.get(interaction.customId);
|
||||
return modalHandler(modul, interaction);
|
||||
}
|
||||
if (interaction.isAutocomplete()) {
|
||||
const modul = Files.ApplicationCommands[1].get(interaction.commandName);
|
||||
}
|
||||
return of();
|
||||
}),
|
||||
)
|
||||
.subscribe({
|
||||
|
||||
@@ -110,5 +110,9 @@ function registerModule(mod: DefinitelyDefined<Module, { name: string }>): Resul
|
||||
Files.MessageCompCommands[ComponentType.SelectMenu].set(name, mod);
|
||||
return Ok.EMPTY;
|
||||
})
|
||||
.with({ type : CommandType.Modal }, mod => {
|
||||
Files.ModalSubmitCommands.set(name, mod);
|
||||
return Ok.EMPTY;
|
||||
})
|
||||
.otherwise(() => Err.EMPTY);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ enum CommandType {
|
||||
MenuMsg = 0b0001000,
|
||||
Button = 0b0010000,
|
||||
MenuSelect = 0b0100000,
|
||||
Modal = 0b1000000,
|
||||
Both = 0b0000011,
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import type {
|
||||
Awaitable,
|
||||
ButtonInteraction,
|
||||
MessageContextMenuCommandInteraction,
|
||||
ModalSubmitInteraction,
|
||||
SelectMenuInteraction,
|
||||
UserContextMenuCommandInteraction,
|
||||
} from 'discord.js';
|
||||
@@ -90,6 +91,17 @@ export type SelectMenuCommand = Override<
|
||||
}
|
||||
>;
|
||||
|
||||
export type ModalSubmitCommand = Override<
|
||||
BaseModule,
|
||||
{
|
||||
type : CommandType.Modal;
|
||||
onEvent?: EventPlugin<CommandType.Modal>[];
|
||||
plugins?: CommandPlugin[];
|
||||
execute : (ctx: ModalSubmitInteraction) => Awaitable<void>;
|
||||
}
|
||||
|
||||
>;
|
||||
|
||||
export type Module =
|
||||
| TextCommand
|
||||
| SlashCommand
|
||||
@@ -97,7 +109,8 @@ export type Module =
|
||||
| ContextMenuUser
|
||||
| ContextMenuMsg
|
||||
| ButtonCommand
|
||||
| SelectMenuCommand;
|
||||
| SelectMenuCommand
|
||||
| ModalSubmitCommand;
|
||||
|
||||
//https://stackoverflow.com/questions/64092736/alternative-to-switch-statement-for-typescript-discriminated-union
|
||||
// Explicit Module Definitions for mapping
|
||||
@@ -109,4 +122,5 @@ export type ModuleDefs = {
|
||||
[CommandType.MenuUser]: ContextMenuUser;
|
||||
[CommandType.Button]: ButtonCommand;
|
||||
[CommandType.MenuSelect]: SelectMenuCommand;
|
||||
[CommandType.Modal] : ModalSubmitCommand;
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ import { join } from 'path';
|
||||
import { from, Observable } from 'rxjs';
|
||||
import type { Module } from '../structures/module';
|
||||
|
||||
//Maybe move this? this probably doesnt belong in utlities/
|
||||
export const BothCommands = new Map<string, Module>();
|
||||
export const ApplicationCommands = {
|
||||
[ApplicationCommandType.User]: new Map<string, Module>(),
|
||||
@@ -20,7 +21,7 @@ export const TextCommands = {
|
||||
text: new Map<string, Module>(),
|
||||
aliases: new Map<string, Module>(),
|
||||
};
|
||||
|
||||
export const ModalSubmitCommands = new Map<string, Module>();
|
||||
// Courtesy @Townsy45
|
||||
function readPath(dir: string, arrayOfFiles: string[] = []): string[] {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user