feat: adding modal and autocomplete support

This commit is contained in:
Jacob Nguyen
2022-05-22 02:08:24 -05:00
parent 26756077ef
commit 77856ce5d0
5 changed files with 49 additions and 3 deletions

View File

@@ -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({

View File

@@ -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);
}

View File

@@ -8,6 +8,7 @@ enum CommandType {
MenuMsg = 0b0001000,
Button = 0b0010000,
MenuSelect = 0b0100000,
Modal = 0b1000000,
Both = 0b0000011,
}

View File

@@ -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;
};

View File

@@ -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 {