diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 5d6e218..6c088ea 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -1,13 +1,12 @@ import type { Interaction } from 'discord.js'; -import { fromEvent, Observable, of, concatMap} 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 * as Files from '../utilities/readFile'; import { filterTap } from './observableHandling'; - export const onInteractionCreate = ( wrapper : Wrapper ) => { const { client } = wrapper; @@ -20,7 +19,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { filterTap(CommandType.SLASH, mod => { const ctx = Context.wrap(interaction); mod.execute(ctx, ['slash', interaction.options]); - }) + }), ); } if (interaction.isContextMenuCommand()) { @@ -30,7 +29,6 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { const ctx = Context.wrap(interaction); mod.execute(ctx); }), - ) } if (interaction.isMessageContextMenuCommand()) { diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index 2253318..0acfb84 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -14,10 +14,12 @@ export function filterTap( tap: (mod : ModuleDefs[T]) => Awaitable ) { return (src : Observable) => - new Observable( subscriber => { - return src.subscribe({ next(modul) { + new Observable( subscriber => { + return src.subscribe({ + next(modul ) { if(match(modul, cmdType)) { tap(modul as ModuleDefs[T]); + subscriber.next(modul as ModuleDefs[T]); } else { if (modul === undefined) { return throwError(() => SernError.UNDEFINED_MODULE); diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 7277942..0f8e3ad 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -46,6 +46,7 @@ const handler = ( name : string ) => [CommandType.MENU_MSG] : mod => { Files.ContextMenuMsg.set (name, mod ); } + } as ModuleHandlers); const registerModules = (name : string, mod : ModuleStates[T]) => diff --git a/src/handler/structures/context.ts b/src/handler/structures/context.ts index e965371..e7870ba 100644 --- a/src/handler/structures/context.ts +++ b/src/handler/structures/context.ts @@ -1,6 +1,7 @@ import type { Interaction, - Message + Message, + Snowflake } from 'discord.js'; import { None, Option, Some } from 'ts-results'; @@ -19,17 +20,23 @@ export default class Context { this.msg = message; this.interac = interaction; } - static wrap(wrappable: I | Message) : Context { - if ( "token" in wrappable) { + static wrap(wrappable: I|Message) : Context { + if ( "token" in wrappable ) { return new Context( None, Some(wrappable)); } - return new Context(Some(wrappable), None) + return new Context(Some(wrappable), None); + } + public static empty() : Context { + return new Context(None, None); + } + public isEmpty() { + return this.msg.none && this.interaction.none; } - private get messageUnchecked() { + public get messageUnchecked() { return this.msg.unwrap(); } - private get interactionUnchecked() { + public get interactionUnchecked() { return this.interac.unwrap(); } private get message() { @@ -38,7 +45,26 @@ export default class Context { private get interaction() { return this.interac; } - + + /** + * maps a general Context to Context + * if interaction is None return Context.empty() + */ + + public map_interaction( + cb : ( ctx: I ) => Context + ) : Context { + if (this.interac.none) return Context.empty(); + return cb(this.interactionUnchecked); + } + + public get id() : Snowflake { + return firstSome( + this.interac.andThen( i => Some(i.id)), + this.msg.andThen(m => Some(m.id)) + )!; + } + public get channel() { return firstSome( this.message.andThen(m => Some(m.channel)), @@ -51,5 +77,14 @@ export default class Context { this.interaction.andThen(i => Some(i.user)) ); } + public get createdTimestamp() : number { + return firstSome( + this.message.andThen(m => Some(m.createdTimestamp)), + this.interaction.andThen(i => Some(i.createdTimestamp)) + )!; + } } + + + diff --git a/src/handler/structures/wrapper.ts b/src/handler/structures/wrapper.ts index 1b9d6da..96989f7 100644 --- a/src/handler/structures/wrapper.ts +++ b/src/handler/structures/wrapper.ts @@ -16,7 +16,6 @@ interface Wrapper { readonly defaultPrefix: string; readonly commands: string; init?: (handler: Wrapper) => void; - readonly privateServers: { test: boolean; id: string }[]; readonly events? : DiscordEvent[]; } diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index 55defd6..a1ce0c6 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -41,7 +41,7 @@ export async function buildData(commandDir: string ): Promise< getCommands(commandDir).map( async (absPath) => { const mod = (await import(absPath)).module as Module; if (mod === undefined) throw Error(`${SernError.UNDEFINED_MODULE} ${absPath}`); - return { mod, absPath }; + return { mod, absPath }; }), ); }