mirror of
https://github.com/sern-handler/handler
synced 2026-06-28 02:32:15 +00:00
-file,-updateModule,publish?
This commit is contained in:
@@ -44,8 +44,8 @@ const TypeMap = new Map<number, number>([[CommandType.Text, 0],
|
||||
[CommandType.CtxUser, ApplicationCommandType.User],
|
||||
[CommandType.CtxMsg, ApplicationCommandType.Message],
|
||||
[CommandType.Button, ComponentType.Button],
|
||||
[CommandType.Modal, InteractionType.ModalSubmit],
|
||||
[CommandType.StringSelect, ComponentType.StringSelect],
|
||||
[CommandType.Modal, InteractionType.ModalSubmit],
|
||||
[CommandType.UserSelect, ComponentType.UserSelect],
|
||||
[CommandType.MentionableSelect, ComponentType.MentionableSelect],
|
||||
[CommandType.RoleSelect, ComponentType.RoleSelect],
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
/**
|
||||
* This file holds sern's rxjs operators used for processing data.
|
||||
* Each function should be modular and testable, not bound to discord / sern
|
||||
* and independent of each other.
|
||||
*/
|
||||
import {
|
||||
concatMap,
|
||||
EMPTY,
|
||||
fromEvent,
|
||||
Observable,
|
||||
of,
|
||||
OperatorFunction,
|
||||
share,
|
||||
} from 'rxjs';
|
||||
import type { Emitter, ErrorHandling, Logging } from './interfaces';
|
||||
import util from 'node:util';
|
||||
import type { Result } from 'ts-results-es';
|
||||
|
||||
/**
|
||||
* if {src} is true, mapTo V, else ignore
|
||||
* @param item
|
||||
*/
|
||||
export function filterMapTo<V>(item: () => V): OperatorFunction<boolean, V> {
|
||||
return concatMap(keep => keep ? of(item()) : EMPTY);
|
||||
}
|
||||
|
||||
export const arrayifySource = <T>(src: T) =>
|
||||
Array.isArray(src) ? src : [src];
|
||||
|
||||
export const sharedEventStream = <T>(e: Emitter, eventName: string) =>
|
||||
(fromEvent(e, eventName) as Observable<T>).pipe(share());
|
||||
|
||||
export function handleError<C>(crashHandler: ErrorHandling, emitter: Emitter, logging?: Logging) {
|
||||
return (pload: unknown, caught: Observable<C>) => {
|
||||
// This is done to fit the ErrorHandling contract
|
||||
if(!emitter.emit('error', pload)) {
|
||||
const err = pload instanceof Error ? pload : Error(util.inspect(pload, { colors: true }));
|
||||
logging?.error({ message: util.inspect(pload) });
|
||||
crashHandler.updateAlive(err);
|
||||
}
|
||||
return caught;
|
||||
};
|
||||
}
|
||||
//// Temporary until i get rxjs operators working on ts-results-es
|
||||
export const filterTap = <K, R>(onErr: (e: R) => void): OperatorFunction<Result<K, R>, K> =>
|
||||
concatMap(result => {
|
||||
if(result.isOk()) {
|
||||
return of(result.value)
|
||||
}
|
||||
onErr(result.error);
|
||||
return EMPTY;
|
||||
})
|
||||
@@ -61,4 +61,5 @@ export const Presence = {
|
||||
export type PresenceConfig <T extends (keyof Dependencies)[]> = {
|
||||
inject?: [...T]
|
||||
execute: (...v: IntoDependencies<T>) => PresenceResult;
|
||||
|
||||
};
|
||||
|
||||
@@ -13,6 +13,7 @@ import { Result, Ok, Err } from 'ts-results-es';
|
||||
import * as assert from 'assert';
|
||||
import type { ReplyOptions } from '../../types/utility';
|
||||
import { fmt } from '../functions'
|
||||
import { SernError } from './enums';
|
||||
|
||||
|
||||
/**
|
||||
@@ -21,7 +22,7 @@ import { fmt } from '../functions'
|
||||
* Message and ChatInputCommandInteraction
|
||||
*/
|
||||
export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
||||
|
||||
|
||||
get options() {
|
||||
if(this.isMessage()) {
|
||||
const [, ...rest] = fmt(this.message.content, this.prefix);
|
||||
@@ -30,6 +31,7 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
||||
return this.interaction.options;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected constructor(protected ctx: Result<Message, ChatInputCommandInteraction>,
|
||||
private __prefix?: string) {
|
||||
@@ -94,6 +96,15 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
||||
.mapErr(i => i.member));
|
||||
}
|
||||
|
||||
get message(): Message {
|
||||
return this.ctx.expect(SernError.MismatchEvent);
|
||||
}
|
||||
|
||||
get interaction(): ChatInputCommandInteraction {
|
||||
return this.ctx.expectErr(SernError.MismatchEvent);
|
||||
}
|
||||
|
||||
|
||||
public get client(): Client {
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.client)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Result as Either } from 'ts-results-es';
|
||||
import { SernError } from './enums';
|
||||
import * as assert from 'node:assert';
|
||||
|
||||
/**
|
||||
@@ -9,13 +8,6 @@ export abstract class CoreContext<M, I> {
|
||||
protected constructor(protected ctx: Either<M, I>) {
|
||||
assert.ok(typeof ctx === 'object' && ctx != null, "Context was nonobject or 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.isOk();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user