diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 39d9b03..3a55e4b 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -10,7 +10,7 @@ import { filterTap } from './observableHandling'; export const onInteractionCreate = ( wrapper : Wrapper ) => { const { client } = wrapper; - (fromEvent(client, 'interactionCreate') as Observable) + (> fromEvent(client, 'interactionCreate')) .pipe( concatMap ( interaction => { if (interaction.isChatInputCommand()) { @@ -29,7 +29,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { const ctx = Context.wrap(interaction); mod.execute(ctx); }), - ) + ); } if (interaction.isMessageContextMenuCommand()) { return of(Files.ContextMenuMsg.get(interaction.commandName)) @@ -38,7 +38,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { const ctx = Context.wrap(interaction); mod.execute(ctx); }), - ) + ); } if (interaction.isButton()) { return of(Files.Buttons.get(interaction.customId)) @@ -47,7 +47,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { const ctx = Context.wrap(interaction); mod.execute(ctx); }) - ) + ); } if (interaction.isSelectMenu()) { return of(Files.SelectMenus.get(interaction.customId)) @@ -56,10 +56,9 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { const ctx = Context.wrap(interaction); mod.execute(ctx); }) - - ) + ); } - else { return of() } + else { return of(); } }) ).subscribe({ error(e) { diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index b417e17..b123ff1 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -9,7 +9,7 @@ import { filterTap, ignoreNonBot } from './observableHandling'; export const onMessageCreate = (wrapper : Wrapper) => { const { client, defaultPrefix } = wrapper; - (fromEvent( client, 'messageCreate') as Observable) + (> fromEvent( client, 'messageCreate')) .pipe ( ignoreNonBot(defaultPrefix), concatMap ( m => { @@ -22,7 +22,7 @@ export const onMessageCreate = (wrapper : Wrapper) => { filterTap(CommandType.TEXT, mod => { mod.execute(ctx, ['text', data]); }) - ) + ); }) ).subscribe ({ error(e) { diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index 0acfb84..cb792d4 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -1,10 +1,10 @@ -import type { Awaitable, Message } from "discord.js"; -import type { CommandType } from "../sern"; -import type { Module } from "../structures/structxports"; +import type { Awaitable, Message } from 'discord.js'; +import type { CommandType } from '../sern'; +import type { Module } from '../structures/structxports'; import { Observable, throwError } from 'rxjs'; -import type { ModuleDefs } from "../structures/commands/moduleHandler"; -import { SernError } from "../structures/errors"; -import { isNotFromBot, isNotFromDM } from "../utilities/messageHelpers"; +import type { ModuleDefs } from '../structures/commands/moduleHandler'; +import { SernError } from '../structures/errors'; +import { isNotFromBot, isNotFromDM } from '../utilities/messageHelpers'; export function match(mod: Module | undefined, type : CommandType) : boolean { return mod !== undefined && (mod.type & type) != 0; @@ -18,8 +18,9 @@ export function filterTap( return src.subscribe({ next(modul ) { if(match(modul, cmdType)) { - tap(modul as ModuleDefs[T]); - subscriber.next(modul as ModuleDefs[T]); + const asModT = modul; + tap(asModT); + subscriber.next(asModT); } else { if (modul === undefined) { return throwError(() => SernError.UNDEFINED_MODULE); @@ -29,9 +30,9 @@ export function filterTap( }, error: (e) => subscriber.error(e), complete: () => subscriber.complete() - }) + }); - }) + }); } export function ignoreNonBot(prefix : string) { return (src : Observable) => @@ -41,7 +42,11 @@ export function ignoreNonBot(prefix : string) { const passAll = [ isNotFromDM, isNotFromBot, - (m : Message) => m.content.startsWith(prefix) + (m : Message) => + m.content + .slice(0,prefix.length) + .toLocaleLowerCase() + .indexOf(prefix.toLocaleLowerCase()) !== -1 ].every( fn => fn(m)); if (passAll) { @@ -50,9 +55,8 @@ export function ignoreNonBot(prefix : string) { }, error: (e) => subscriber.error(e), complete: () => subscriber.complete() - - }) - }) + }); + }); } diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 2763250..11a6451 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -56,7 +56,7 @@ const handler = ( name : string ) => } as ModuleHandlers); const registerModules = (name : string, mod : ModuleStates[T]) => - (handler(name)[mod.type] as HandlerCallback)(mod); + (> handler(name)[mod.type])(mod); function setCommands ( { mod, absPath } : { mod : Module, absPath : string } ) { const name = mod.name ?? Files.fmtFileName(basename(absPath)); diff --git a/src/handler/sern.ts b/src/handler/sern.ts index 266cd3c..c7144bc 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -27,69 +27,6 @@ function eventObserver(client: Client, events: DiscordEvent[] ) { fromEvent(client, event, cb).subscribe(); }); } - -export class Handler { -/** - .on('interactionCreate', async (interaction) => { - if (!interaction.isCommand()) return; - if (interaction.guild === null) return; // TODO : handle dms - const module = this.findModuleFrom(interaction); - if (module === undefined) { - this.defaultLogger.log( - sEvent.MISUSE_CMD, - interaction.guildId!, - `Unknown slash command.` - ); - return; - } - const res = await this.interactionResult(module, interaction); - if (res === undefined) return; - await interaction.reply(res); - }); - } - - - - private async interactionResult( - module: Files.CommandVal, - interaction: CommandInteraction, - ): Promise { - const name = this.findModuleFrom(interaction); - if (name === undefined) return `Could not find ${interaction.commandName} command!`; - - if (module.mod.type < CommandType.SLASH) return 'This is not a slash command'; - const context = new Context(None, Some(interaction)); - const parsedArgs = module.mod.parse?.(context, ['slash', interaction.options]) ?? Ok(''); - - if (parsedArgs.err) return parsedArgs.val; - - return (module.mod.execute?.(context, parsedArgs) as possibleOutput | undefined); - } - - - - private async commandResult( - module: Files.CommandVal, - message: Message, - ): Promise { - if (module.mod.type === CommandType.SLASH) { - this.defaultLogger.log( - sEvent.MISUSE_CMD, - message.guildId!, - `The text command ${module.mod.name} may be a slash command and not a text command` - ); - return; - } - const context = new Context ( Some(message), None ); - const args = message.content.slice(this.prefix.length).trim().split(/s+/g); - const parsedArgs = module.mod.parse?.(context, ['text', args]) ?? Ok(args); - if (parsedArgs.err) return parsedArgs.val; - return (module.mod.execute?.(context, parsedArgs) as possibleOutput | undefined); - }; -*/ - -} - /** * @enum { number }; */ @@ -99,7 +36,7 @@ export enum CommandType { MENU_USER = 0b000100, MENU_MSG = 0b001000, BUTTON = 0b010000, - MENU_SELECT =0b100000, + MENU_SELECT= 0b100000, BOTH = 0b000011, ANY = 0b111111 } diff --git a/src/handler/structures/commands/module.ts b/src/handler/structures/commands/module.ts index 06556d3..f0683e4 100644 --- a/src/handler/structures/commands/module.ts +++ b/src/handler/structures/commands/module.ts @@ -41,6 +41,7 @@ export type SelectMenuCommand = { type : CommandType.MENU_SELECT; } & Override ) => Awaitable }>; + export type Module = TextCommand | SlashCommand diff --git a/src/handler/structures/commands/moduleHandler.ts b/src/handler/structures/commands/moduleHandler.ts index 724949e..13ced06 100644 --- a/src/handler/structures/commands/moduleHandler.ts +++ b/src/handler/structures/commands/moduleHandler.ts @@ -1,6 +1,5 @@ import { CommandType } from '../../sern'; -import type { TextCommand, BothCommand, ButtonCommand, SlashCommand, BaseModule, ContextMenuMsg, ContextMenuUser, SelectMenuCommand } from './module'; - +import type { TextCommand, BothCommand, ButtonCommand, SlashCommand, ContextMenuMsg, ContextMenuUser, SelectMenuCommand } from './module'; //https://stackoverflow.com/questions/64092736/alternative-to-switch-statement-for-typescript-discriminated-union // Explicit Module Definitions for mapping diff --git a/src/handler/structures/context.ts b/src/handler/structures/context.ts index d0ac8e5..e34a7a9 100644 --- a/src/handler/structures/context.ts +++ b/src/handler/structures/context.ts @@ -29,7 +29,7 @@ export default class Context { this.oInterac = oInterac; } static wrap(wrappable: I|Message) : Context { - if ( "token" in wrappable ) { + if ( 'token' in wrappable ) { return new Context( None, Some(wrappable)); } return new Context(Some(wrappable), None); diff --git a/src/handler/structures/wrapper.ts b/src/handler/structures/wrapper.ts index 54168d1..34dc3eb 100644 --- a/src/handler/structures/wrapper.ts +++ b/src/handler/structures/wrapper.ts @@ -14,6 +14,7 @@ interface Wrapper { readonly client: Client; readonly defaultPrefix: string; readonly commands: string; + readonly components : string; init?: (handler: Wrapper) => void; readonly events? : DiscordEvent[]; } diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index e427405..6054f58 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -42,7 +42,7 @@ export async function buildData(commandDir: string ): Promise< > { return Promise.all( getCommands(commandDir).map( async (absPath) => { - const mod = (await import(absPath)).module as Module; + const mod = (await import(absPath)).module; if (mod === undefined) throw Error(`${SernError.UNDEFINED_MODULE} ${absPath}`); return { mod, absPath }; }), diff --git a/src/types/handler.ts b/src/types/handler.ts index 5c334d4..422f525 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -7,11 +7,8 @@ import type { Awaitable, } from 'discord.js'; -import type { Module } from '../handler/structures/structxports'; - // Anything that can be sent in a `#send` or `#reply` export type possibleOutput = T | (MessagePayload & MessageOptions); -export type execute = Module['execute']; export type Nullish = T | undefined | null; // Thanks @cursorsdottsx export type ParseType = {