diff --git a/src/core/structures/context.ts b/src/core/structures/context.ts index 3701636..49cf76d 100644 --- a/src/core/structures/context.ts +++ b/src/core/structures/context.ts @@ -1,31 +1,36 @@ -import { Result as Either, Ok, Err } from 'ts-results-es'; +import { Result as Either } from 'ts-results-es'; +import { SernError } from './errors'; +import * as assert from 'node:assert' + + /** - * * @since 3.0.0 - * */ -export interface CoreContext { - get message(): M; - get interaction(): I; +export abstract class CoreContext { + protected constructor(protected ctx: Either) { + assert.ok(typeof ctx.val === 'object' && ctx.val != null) + } + get message(): M { + return this.ctx.expect(SernError.MismatchEvent); + } + get interaction(): I { + return this.ctx.expectErr(SernError.MismatchEvent); + } + + public isMessage(): this is CoreContext { + return this.ctx.map(() => true).unwrapOr(false); + } + + public isSlash(): this is CoreContext { + return !this.isMessage(); + } + //todo: add agnostic options resolver for Context + abstract get options() : unknown + + abstract get id() : string + + static wrap(_: unknown): unknown { throw Error("You need to override this method; cannot wrap an abstract class") } + } -export function safeUnwrap(res: Either) { - return res.val; -} - -export function wrap( - val: B|A, -): Either { - if(typeof val !== 'object' || Array.isArray(val) || val == null) { - throw Error("Could not form correct Context." + "Recieved " + val) - } - const msgInteractionObject = (val as unknown as { interaction: unknown }).interaction; - //falsy comparison: ==. Checks if its null OR undefined - if(msgInteractionObject == null) { - return Ok(val as A) - } - return Err(val as B); -} - - diff --git a/src/core/structures/enums.ts b/src/core/structures/enums.ts index ea4a209..9cc3fb1 100644 --- a/src/core/structures/enums.ts +++ b/src/core/structures/enums.ts @@ -15,45 +15,18 @@ * ``` */ export enum CommandType { - /** - * The CommandType for text commands - */ - Text = 1, - /** - * The CommandType for slash commands - */ - Slash = 2, - /** - * The CommandType for hybrid commands, text and slash - */ - Both = 3, - /** - * The CommandType for UserContextMenuInteraction commands - */ - CtxUser = 4, - /** - * The CommandType for MessageContextMenuInteraction commands - */ - CtxMsg = 8, - /** - * The CommandType for ButtonInteraction commands - */ - Button = 16, - /** - * The CommandType for StringSelectMenuInteraction commands - */ - StringSelect = 32, - /** - * The CommandType for ModalSubmitInteraction commands - */ - Modal = 64, - /** - * The CommandType for the other SelectMenuInteractions - */ - ChannelSelect = 256, - MentionableSelect = 512, - RoleSelect = 1024, - UserSelect = 2048, + Text = 1 << 0, + Slash = 1 << 1, + Both = 3, + CtxUser = 1 << 2, + CtxMsg = 1 << 3, + Button = 1 << 4, + StringSelect = 1 << 5, + Modal = 1 << 6, + ChannelSelect = 1 << 7, + MentionableSelect = 1 << 8, + RoleSelect = 1 << 9, + UserSelect = 1 << 10, } /** @@ -106,16 +79,6 @@ export enum PluginType { * The PluginType for InitPlugins */ Init = 1, - /** - * @deprecated - * Use PluginType.Init instead - */ - Command = 1, - /** - * @deprecated - * Use PluginType.Control instead - */ - Event = 2, /** * The PluginType for EventPlugins */ diff --git a/src/core/structures/wrapper.ts b/src/core/structures/wrapper.ts index ca5a36a..ade68e6 100644 --- a/src/core/structures/wrapper.ts +++ b/src/core/structures/wrapper.ts @@ -32,6 +32,8 @@ export type Wrapper = WebsocketWrapper | ServerlessWrapper export interface ServerlessWrapper { readonly platform: ServerlessStrategy + commands: string; + events?: string; containerConfig: { get: (...keys: (keyof ServerlessDependencies)[]) => unknown[]; }