mirror of
https://github.com/sern-handler/handler
synced 2026-06-17 05:12:16 +00:00
fix(messageEvent) made messageEvent more linear and make more
complex observables
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
|
||||
import type { Interaction } from 'discord.js';
|
||||
import { filter, fromEvent, Observable, of, tap, concatMap} from 'rxjs';
|
||||
import { None, Some } from 'ts-results';
|
||||
import type { SlashCommand } from '../..';
|
||||
import { fromEvent, Observable, of, concatMap} from 'rxjs';
|
||||
import { CommandType } from '../sern';
|
||||
import type { ContextMenuMsg, ContextMenuUser } from '../structures/commands/module';
|
||||
import Context from '../structures/context';
|
||||
import type Wrapper from '../structures/wrapper';
|
||||
import * as Files from '../utilities/readFile';
|
||||
import { filterTap, is } from './interactionHandling';
|
||||
import { filterTap } from './observableHandling';
|
||||
|
||||
|
||||
export const onInteractionCreate = ( wrapper : Wrapper ) => {
|
||||
@@ -45,7 +42,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
|
||||
}),
|
||||
)
|
||||
}
|
||||
else { return of(); }
|
||||
else { return of() }
|
||||
})
|
||||
).subscribe({
|
||||
error(e) {
|
||||
@@ -55,8 +52,6 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
|
||||
//log on each command emitted
|
||||
console.log(command);
|
||||
},
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1,41 +1,32 @@
|
||||
import type { Message } from 'discord.js';
|
||||
import { map, filter, fromEvent, Observable, of, concatMap, tap } from 'rxjs';
|
||||
import { None, Some } from 'ts-results';
|
||||
import { fromEvent, Observable, of, concatMap, filter} from 'rxjs';
|
||||
import { CommandType } from '../sern';
|
||||
import type { TextCommand } from '../structures/commands/module';
|
||||
import Context from '../structures/context';
|
||||
import type Wrapper from '../structures/wrapper';
|
||||
import { isNotFromDM, isNotFromBot, hasPrefix, fmt } from '../utilities/messageHelpers';
|
||||
import * as Files from '../utilities/readFile';
|
||||
import { is } from './interactionHandling';
|
||||
import { filterTap, ignoreNonBot } from './observableHandling';
|
||||
|
||||
export const onMessageCreate = (wrapper : Wrapper) => {
|
||||
const { client, defaultPrefix } = wrapper;
|
||||
(fromEvent( client, 'messageCreate') as Observable<Message>)
|
||||
.pipe (
|
||||
filter( isNotFromBot ),
|
||||
filter( isNotFromDM ),
|
||||
filter( m => hasPrefix(m, defaultPrefix)),
|
||||
concatMap ( m => of( fmt(m, defaultPrefix) )
|
||||
.pipe (
|
||||
map(([prefix, ...args ]) =>
|
||||
[
|
||||
Files.Commands.get(prefix) ?? Files.Alias.get(prefix),
|
||||
new Context(Some(m), None),
|
||||
args
|
||||
] as const
|
||||
),
|
||||
filter( ([mod]) => is( mod, CommandType.TEXT) ),
|
||||
tap ( ([ mod, ctx, args ]) => {
|
||||
(mod as TextCommand)!.execute(ctx, ['text', args]);
|
||||
}),
|
||||
)
|
||||
)
|
||||
|
||||
ignoreNonBot(defaultPrefix),
|
||||
concatMap ( m => {
|
||||
const [ prefix, ...data ] = fmt(m, defaultPrefix);
|
||||
const posMod = Files.Commands.get(prefix) ?? Files.Alias.get(prefix);
|
||||
const ctx = Context.wrap(m);
|
||||
|
||||
return of( posMod )
|
||||
.pipe (
|
||||
filterTap(CommandType.TEXT, mod => {
|
||||
mod.execute(ctx, ['text', data]);
|
||||
})
|
||||
)
|
||||
})
|
||||
).subscribe ({
|
||||
error(e) {
|
||||
//log things
|
||||
console.log('Failed to finished message subscription!');
|
||||
throw e;
|
||||
},
|
||||
next(command) {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import type { Awaitable } from "discord.js";
|
||||
import type { Awaitable, Message } from "discord.js";
|
||||
import type { CommandType } from "../sern";
|
||||
import type { Module } from "../structures/structxports";
|
||||
import { ignoreElements, Observable, throwError } from 'rxjs';
|
||||
import { Observable, throwError } from 'rxjs';
|
||||
import type { ModuleDefs } from "../structures/commands/moduleHandler";
|
||||
import { SernError } from "../structures/errors";
|
||||
import { isNotFromBot, isNotFromDM } from "../utilities/messageHelpers";
|
||||
|
||||
export function match(mod: Module | undefined, type : CommandType) : boolean {
|
||||
return mod !== undefined && (mod.type & type) != 0;
|
||||
@@ -13,9 +14,8 @@ 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( subscriber => {
|
||||
return src.subscribe({ next(modul) {
|
||||
if(match(modul, cmdType)) {
|
||||
tap(modul as ModuleDefs[T]);
|
||||
} else {
|
||||
@@ -30,5 +30,27 @@ export function filterTap<T extends keyof ModuleDefs>(
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
export function ignoreNonBot(prefix : string) {
|
||||
return (src : Observable<Message>) =>
|
||||
new Observable<Message>(subscriber => {
|
||||
return src.subscribe({
|
||||
next(m) {
|
||||
const passAll = [
|
||||
isNotFromDM,
|
||||
isNotFromBot,
|
||||
(m : Message) => m.content.startsWith(prefix)
|
||||
].every( fn => fn(m));
|
||||
|
||||
if (passAll) {
|
||||
subscriber.next(m);
|
||||
}
|
||||
},
|
||||
error: (e) => subscriber.error(e),
|
||||
complete: () => subscriber.complete()
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user