diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 528515f..39d9b03 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -40,8 +40,24 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { }), ) } - if (interaction.isModalSubmit()) { - return of(); + if (interaction.isButton()) { + return of(Files.Buttons.get(interaction.customId)) + .pipe( + filterTap(CommandType.BUTTON, mod => { + const ctx = Context.wrap(interaction); + mod.execute(ctx); + }) + ) + } + if (interaction.isSelectMenu()) { + return of(Files.SelectMenus.get(interaction.customId)) + .pipe( + filterTap(CommandType.MENU_SELECT, mod => { + const ctx = Context.wrap(interaction); + mod.execute(ctx); + }) + + ) } else { return of() } }) diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index 731b3f0..b417e17 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -1,9 +1,9 @@ import type { Message } from 'discord.js'; -import { fromEvent, Observable, of, concatMap, filter} from 'rxjs'; +import { fromEvent, Observable, of, concatMap } from 'rxjs'; import { CommandType } from '../sern'; import Context from '../structures/context'; import type Wrapper from '../structures/wrapper'; -import { isNotFromDM, isNotFromBot, hasPrefix, fmt } from '../utilities/messageHelpers'; +import { fmt } from '../utilities/messageHelpers'; import * as Files from '../utilities/readFile'; import { filterTap, ignoreNonBot } from './observableHandling'; diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 0f8e3ad..2763250 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -45,6 +45,12 @@ const handler = ( name : string ) => }, [CommandType.MENU_MSG] : mod => { Files.ContextMenuMsg.set (name, mod ); + }, + [CommandType.BUTTON] : mod => { + Files.Buttons.set(name, mod); + }, + [CommandType.MENU_SELECT] : mod => { + Files.SelectMenus.set(name, mod); } } as ModuleHandlers); diff --git a/src/handler/sern.ts b/src/handler/sern.ts index 12d1838..266cd3c 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -94,9 +94,12 @@ export class Handler { * @enum { number }; */ export enum CommandType { - TEXT = 0b0001, - SLASH = 0b0010, - MENU_USER = 0b0100, - MENU_MSG = 0b1000, - BOTH = 0b0011, + TEXT = 0b000001, + SLASH = 0b000010, + MENU_USER = 0b000100, + MENU_MSG = 0b001000, + BUTTON = 0b010000, + MENU_SELECT =0b100000, + BOTH = 0b000011, + ANY = 0b111111 } diff --git a/src/handler/structures/commands/module.ts b/src/handler/structures/commands/module.ts index 28c17f0..06556d3 100644 --- a/src/handler/structures/commands/module.ts +++ b/src/handler/structures/commands/module.ts @@ -1,4 +1,4 @@ -import type { ApplicationCommandOptionData, Awaitable, ChatInputCommandInteraction, ContextMenuCommandInteraction, Interaction, MessageContextMenuCommandInteraction } from 'discord.js'; +import type { ApplicationCommandOptionData, Awaitable, ButtonInteraction, ChatInputCommandInteraction, ContextMenuCommandInteraction, Interaction, MessageContextMenuCommandInteraction, SelectMenuInteraction } from 'discord.js'; import type { Args, Override } from '../../../types/handler'; import type { CommandType } from '../../sern'; import type Context from '../context'; @@ -33,11 +33,20 @@ export type ContextMenuUser = { export type ContextMenuMsg = { type : CommandType.MENU_MSG; } & Override ) => Awaitable }>; +export type ButtonCommand = { + type : CommandType.BUTTON; +} & Override ) => Awaitable }>; + +export type SelectMenuCommand = { + type : CommandType.MENU_SELECT; +} & Override ) => Awaitable }>; export type Module = TextCommand | SlashCommand | BothCommand | ContextMenuUser - | ContextMenuMsg; + | ContextMenuMsg + | ButtonCommand + | SelectMenuCommand; diff --git a/src/handler/structures/commands/moduleHandler.ts b/src/handler/structures/commands/moduleHandler.ts index 405d673..724949e 100644 --- a/src/handler/structures/commands/moduleHandler.ts +++ b/src/handler/structures/commands/moduleHandler.ts @@ -1,5 +1,5 @@ import { CommandType } from '../../sern'; -import type { TextCommand, BothCommand, SlashCommand, BaseModule, ContextMenuMsg, ContextMenuUser } from './module'; +import type { TextCommand, BothCommand, ButtonCommand, SlashCommand, BaseModule, ContextMenuMsg, ContextMenuUser, SelectMenuCommand } from './module'; //https://stackoverflow.com/questions/64092736/alternative-to-switch-statement-for-typescript-discriminated-union @@ -9,7 +9,9 @@ export type ModuleDefs = { [CommandType.SLASH] : SlashCommand, [CommandType.BOTH] : BothCommand, [CommandType.MENU_MSG] : ContextMenuMsg, - [CommandType.MENU_USER] : ContextMenuUser + [CommandType.MENU_USER] : ContextMenuUser, + [CommandType.BUTTON] : ButtonCommand, + [CommandType.MENU_SELECT] : SelectMenuCommand } //Keys of ModuleDefs diff --git a/src/handler/structures/context.ts b/src/handler/structures/context.ts index 6bca59b..d0ac8e5 100644 --- a/src/handler/structures/context.ts +++ b/src/handler/structures/context.ts @@ -1,4 +1,3 @@ -import { ContextMenuCommandBuilder } from '@discordjs/builders'; import type { APIInteractionGuildMember } from 'discord-api-types/v9'; import type { Awaitable, @@ -35,9 +34,6 @@ export default class Context { } return new Context(Some(wrappable), None); } - public static empty() : Context { - return new Context(None, None); - } public isEmpty() { return this.oMsg.none && this.oInterac.none; } @@ -56,7 +52,7 @@ export default class Context { public mapInteraction( cb : ( ctx: I ) => Context ) : Context { - if (this.oInterac.none) return Context.empty(); + if (this.oInterac.none) return new Context(); return cb(this.oInterac.val); } diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index a1ce0c6..e427405 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -3,10 +3,13 @@ import { join } from 'path'; import type { Module } from '../structures/commands/module'; import { SernError } from '../structures/errors'; +//We can look into lazily loading modules once everything is set export const ContextMenuUser = new Map(); export const ContextMenuMsg = new Map(); export const Commands = new Map(); export const Alias = new Map(); +export const Buttons = new Map(); +export const SelectMenus = new Map(); // Courtesy @Townsy45 function readPath(dir: string, arrayOfFiles: string[] = []): string[] {