mirror of
https://github.com/sern-handler/handler
synced 2026-06-17 13:22:17 +00:00
feat(handler) context class updates and more minor additions
This commit is contained in:
@@ -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()) {
|
||||
|
||||
@@ -14,10 +14,12 @@ export function filterTap<T extends keyof ModuleDefs>(
|
||||
tap: (mod : ModuleDefs[T]) => Awaitable<void>
|
||||
) {
|
||||
return (src : Observable<Module|undefined>) =>
|
||||
new Observable( subscriber => {
|
||||
return src.subscribe({ next(modul) {
|
||||
new Observable<Module|undefined>( 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);
|
||||
|
||||
@@ -46,6 +46,7 @@ const handler = ( name : string ) =>
|
||||
[CommandType.MENU_MSG] : mod => {
|
||||
Files.ContextMenuMsg.set (name, mod );
|
||||
}
|
||||
|
||||
} as ModuleHandlers);
|
||||
|
||||
const registerModules = <T extends ModuleType >(name : string, mod : ModuleStates[T]) =>
|
||||
|
||||
@@ -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<I extends Interaction = Interaction> {
|
||||
this.msg = message;
|
||||
this.interac = interaction;
|
||||
}
|
||||
static wrap<I extends Interaction = Interaction>(wrappable: I | Message) : Context<I> {
|
||||
if ( "token" in wrappable) {
|
||||
static wrap<I extends Interaction = Interaction>(wrappable: I|Message) : Context<I> {
|
||||
if ( "token" in wrappable ) {
|
||||
return new Context<I>( None, Some(wrappable));
|
||||
}
|
||||
return new Context<I>(Some(wrappable), None)
|
||||
return new Context<I>(Some(wrappable), None);
|
||||
}
|
||||
public static empty<T extends Interaction>() : Context<T> {
|
||||
return new Context<T>(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<I extends Interaction = Interaction> {
|
||||
private get interaction() {
|
||||
return this.interac;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* maps a general Context<I> to Context<B>
|
||||
* if interaction is None return Context.empty()
|
||||
*/
|
||||
|
||||
public map_interaction<B extends Interaction = Interaction>(
|
||||
cb : ( ctx: I ) => Context<B>
|
||||
) : Context<B> {
|
||||
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<I extends Interaction = Interaction> {
|
||||
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))
|
||||
)!;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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[];
|
||||
}
|
||||
|
||||
|
||||
@@ -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 };
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user