chore: refactor and add multiplatform typings

This commit is contained in:
Jacob Nguyen
2023-05-04 18:46:52 -05:00
parent 7f53521ef4
commit 1288a6b5cc
3 changed files with 44 additions and 74 deletions

View File

@@ -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<M, I> {
get message(): M;
get interaction(): I;
export abstract class CoreContext<M, I> {
protected constructor(protected ctx: Either<M, I>) {
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<M, never> {
return this.ctx.map(() => true).unwrapOr(false);
}
public isSlash(): this is CoreContext<never, I> {
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<T>(res: Either<T, T>) {
return res.val;
}
export function wrap<A extends unknown, B extends unknown>(
val: B|A,
): Either<A,B> {
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);
}

View File

@@ -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
*/

View File

@@ -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[];
}