import { Collection , Message , Guild } from "discord.js" ; import * as fs from "fs" ; import { Bot } from "." ; import { filterAction , executeActions } from "./utils/filters" ; import { sendError } from "./utils/messages" ; import { filtersObject } from "./database/schemas" ; export interface export interface filterOutput { reason : string ; actions : filterAction [ ] ; } export interface export interface filterInterface { type { string } memberof name : string ; type { string } memberof path : string ; type { string } memberof shortHelp : string ; memberof embedHelp : ( guild : Guild ) => any ; memberof run : ( message : Message ) => Promise < filterOutput > ; } ; export class export class Filters { type { Collection < string , filterInterface > } memberof filters : Collection < string , filterInterface > ; type { Object } memberof structure : Object ; param { string } dir memberof constructor ( dir ) { this . filters = new Collection ( ) ; this . structure = { } ; this . loadFilters ( dir , this . structure ) ; } param { string } dir param { Object } structureObject memberof loadFilters ( dir , structureObject ) { fs . readdir ( dir , ( err , files ) => { if ( err ) console . error ( err ) ; var folders = files . filter ( f => fs . lstatSync ( dir + f ) . isDirectory ( ) ) ; folders . forEach ( ( f , i ) => { structureObject [ f . toLowerCase ( ) ] = { _categoryName : f } ; this . loadFilters ( dir + f + "/" , structureObject [ f . toLowerCase ( ) ] ) ; } ) ; var filters = files . filter ( f => f . split ( "." ) . pop ( ) == "js" ) ; if ( filters . length <= 0 ) { console . error ( "no filters to load in " + dir ) ; return ; } console . info ( `loading ${ filters . length } filters in ${ dir }` ) ; filters . forEach ( ( f , i ) => { var props = require ( dir + f ) . default ; console . info ( `${ i + 1 }: ${ f } loaded!` ) ; this . filters . set ( props . name , props ) ; var strucObject = structureObject ; if ( props . path != "" ) { var keys = props . path . toLowerCase ( ) . split ( "/" ) ; strucObject = this . structure ; for ( var i = 0 ; i < keys . length ; i ++ ) { if ( ! strucObject [ keys [ i ] ] ) { strucObject [ keys [ i ] ] = { _categoryName : keys [ i ] } ; } strucObject = strucObject [ keys [ i ] ] ; } } strucObject [ props . name ] = props ; } ) ; } ) ; } param { Message } message returns memberof async filterMessage ( message ) { try { var filtersDoc = await Bot . database . findFiltersDoc ( message . guild . id ) ; if ( ! filtersDoc ) return ; var filterObject = filtersDoc . toObject ( ) ; for ( const filter of this . filters . array ( ) ) { if ( ! filterObject . filters [ filter . name ] || ! filterObject . filters [ filter . name ] . _enabled ) continue ; var output = await filter . run ( message ) ; if ( output ) { Bot . mStats . logFilterCatch ( filter . name ) ; Bot . logger . logFilterCatch ( message , filter , output . reason , output . actions ) ; executeActions ( message , output . actions ) ; return ; } } } catch ( e ) { sendError ( message . channel , { error : "Error occurred at filterMessage" , e } ) ; Bot . mStats . logError ( e ) ; } } param { string } filter returns memberof get ( filter ) { return this . filters . get ( filter ) ; } }