diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 510aba1..b297174 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -1,12 +1,14 @@ -import type { ChatInputCommandInteraction, CommandInteraction, Interaction } from 'discord.js'; -import { map, filter, fromEvent, Observable, of, tap, concatMap} from 'rxjs'; + +import type { Interaction } from 'discord.js'; +import { filter, fromEvent, Observable, of, tap, concatMap} from 'rxjs'; import { None, Some } from 'ts-results'; import type { SlashCommand } from '../..'; import { CommandType } from '../sern'; +import type { ContextMenuMsg, ContextMenuUser } from '../structures/commands/module'; import Context from '../structures/context'; import type Wrapper from '../structures/wrapper'; import * as Files from '../utilities/readFile'; - +import { is } from './interactionHandling'; export const onInteractionCreate = ( wrapper : Wrapper ) => { @@ -16,11 +18,9 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { .pipe( concatMap ( interaction => { if (interaction.isChatInputCommand()) { - return of(interaction.commandName).pipe( - map ( name => Files.Commands.get(name) ), - filter( mod => mod !== undefined - && (mod.type & CommandType.SLASH) != 0 - ), + return of(Files.Commands.get(interaction.commandName)) + .pipe( + filter(mod => is(mod, CommandType.SLASH)), tap ( mod => { const ctx = new Context(None, Some(interaction)); (mod as SlashCommand)!.execute(ctx, ['slash', interaction.options]); @@ -28,10 +28,24 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { ); } if (interaction.isContextMenuCommand()) { - return of(); + return of(Files.ContextMenuUser.get(interaction.commandName)) + .pipe( + filter( mod => is(mod, CommandType.MENU_USER)), + tap ( mod => { + const ctx = new Context(None, Some(interaction)); + (mod as ContextMenuUser)!.execute(ctx); + }) + ) } if (interaction.isMessageContextMenuCommand()) { - return of(); + return of(Files.ContextMenuMsg.get(interaction.commandName)) + .pipe( + filter( mod => is(mod, CommandType.MENU_MSG)), + tap ( mod => { + const ctx = new Context(None, Some(interaction)); + (mod as ContextMenuMsg)!.execute(ctx); + }) + ) } else { return of(); } }) diff --git a/src/handler/events/interactionHandling.ts b/src/handler/events/interactionHandling.ts new file mode 100644 index 0000000..53cd033 --- /dev/null +++ b/src/handler/events/interactionHandling.ts @@ -0,0 +1,7 @@ +import type { CommandType } from "../sern"; +import type { Module } from "../structures/structxports"; + + +export function is(mod: Module | undefined, type : CommandType) : boolean { + return mod !== undefined && (mod.type & type) != 0; +} diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index 46c1e5c..06e11f8 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -7,6 +7,7 @@ import Context from '../structures/context'; import type Wrapper from '../structures/wrapper'; import { isNotFromDM, isNotFromBot, hasPrefix, fmt } from '../utilities/messageHelpers'; import * as Files from '../utilities/readFile'; +import { is } from './interactionHandling'; export const onMessageCreate = (wrapper : Wrapper) => { const { client, defaultPrefix } = wrapper; @@ -24,7 +25,7 @@ export const onMessageCreate = (wrapper : Wrapper) => { args ] as const ), - filter( ([mod]) => mod !== undefined && (mod.type & CommandType.TEXT) != 0 ), + filter( ([mod]) => is( mod, CommandType.TEXT) ), tap ( ([ mod, ctx, args ]) => { (mod as TextCommand)!.execute(ctx, ['text', args]); }), diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 2fc4bda..7277942 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -41,11 +41,10 @@ const handler = ( name : string ) => mod.alias.forEach (a => Files.Alias.set(a, mod)); }, [CommandType.MENU_USER] : mod => { - Files.Commands.set ( name, mod ); + Files.ContextMenuUser.set ( name, mod ); }, [CommandType.MENU_MSG] : mod => { - - Files.Commands.set (name, mod ); + Files.ContextMenuMsg.set (name, mod ); } } as ModuleHandlers); diff --git a/src/handler/sern.ts b/src/handler/sern.ts index bafa7e2..12d1838 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -19,8 +19,6 @@ export function init( wrapper : Wrapper ) { onReady( wrapper ); onMessageCreate( wrapper ); onInteractionCreate ( wrapper ); - - } function eventObserver(client: Client, events: DiscordEvent[] ) { @@ -32,22 +30,6 @@ function eventObserver(client: Client, events: DiscordEvent[] ) { export class Handler { /** - .on('messageCreate', async (message: Message) => { - const module = this.findModuleFrom(message); - if (module === undefined) { - this.defaultLogger.log( - sEvent.MISUSE_CMD, - message.guildId!, - `Unknown legacy command.` - ); - return; - } - const cmdResult = await this.commandResult(module, message); - if (cmdResult === undefined) return; - - message.channel.send(cmdResult); - }) - .on('interactionCreate', async (interaction) => { if (!interaction.isCommand()) return; if (interaction.guild === null) return; // TODO : handle dms diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index 174c7c5..55defd6 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -3,7 +3,8 @@ import { join } from 'path'; import type { Module } from '../structures/commands/module'; import { SernError } from '../structures/errors'; - +export const ContextMenuUser = new Map(); +export const ContextMenuMsg = new Map(); export const Commands = new Map(); export const Alias = new Map();