feat(handler) Filters only executable messages now

This commit is contained in:
Jacob Nguyen
2022-03-11 10:39:30 -06:00
parent 10aee77315
commit 6d2af4728c
6 changed files with 17 additions and 27 deletions

View File

@@ -1,15 +1,17 @@
import type { Message } from "discord.js";
import { filter, fromEvent, Observable } from "rxjs";
import type Wrapper from "../structures/wrapper";
import { isNotFromBot } from "../utilities/messageHelpers";
import { isNotFromDM, isNotFromBot, hasPrefix } from "../utilities/messageHelpers";
export const onMessageCreate = ( wrapper : Wrapper) => {
const { client } = wrapper;
const { client, defaultPrefix } = wrapper;
(fromEvent( client, 'messageCreate') as Observable<Message>)
.pipe (
filter( isNotFromBot ),
).subscribe()
filter( isNotFromDM ),
filter(m => hasPrefix(m, defaultPrefix)),
).subscribe(console.log)
}

View File

@@ -14,7 +14,6 @@ import type {
import { Ok, None, Some } from 'ts-results';
import { isNotFromBot, hasPrefix, fmt } from './utilities/messageHelpers';
import Logger, { sEvent } from './logger';
import { AllTrue } from './utilities/higherOrders';
import type Module from './structures/module';
import Context from './structures/context';
import type Wrapper from './structures/wrapper';
@@ -51,8 +50,6 @@ export class Handler {
.on('messageCreate', async (message: Message) => {
const isExecutable = AllTrue(isNotFromBot, hasPrefix);
if (!isExecutable(message, this.prefix)) return;
if (message.channel.type === 'DM') return; // TODO: Handle dms
const module = this.findModuleFrom(message);
if (module === undefined) {

View File

@@ -1,4 +0,0 @@
export function registerModules () {
}

View File

@@ -1,16 +0,0 @@
import type { Message } from 'discord.js';
type MsgFnArgs = [msgOrInter: Message, prefix?: string];
type MsgFn = (...args: MsgFnArgs) => boolean;
/**
*
* @param {MsgFn} fn any function that has argument `MsgFnArgs` returning boolean
* @returns {(message: Message, prefix: string) => boolean}
*/
export function AllTrue(...fns: MsgFn[]):
(message: Message, prefix: string) => boolean {
return (message: Message, prefix: string) => {
return fns.every(g => g(message, prefix));
};
}

View File

@@ -1,5 +1,16 @@
import type { Message } from 'discord.js';
/**
* @param message The message object
* @returns `true` if message comes from DM, `false` otherwise
* @example isNotFromDM(message) ? 'Not From DM' : 'from DM'
*
*/
export function isNotFromDM ( message: Message ) {
return message.channel.type !== 'DM';
}
/**
* Checks if the author of message is a bot or not
* @param message The message to check