@@ -4,6 +4,11 @@ import knex, { Knex } from "knex";
44import toml from 'toml' ;
55import * as Sentry from '@sentry/node' ;
66import { nodeProfilingIntegration } from "@sentry/profiling-node" ;
7+ import { join } from "path" ;
8+ import url from "url" ;
9+ import { readdirSync } from "fs" ;
10+
11+ const __dirname = url . fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
712
813class DBot extends Client {
914 kogBot : KOGBot ;
@@ -22,17 +27,50 @@ class DBot extends Client {
2227 this . REST . setToken ( this . kogBot . environment . discord . client_token ) ;
2328
2429 if ( ! kogBot . ci_workflow ) {
30+ this . gatewayEvents . listen ( ) . then ( ( ) => console . log ( "Listening to events." ) ) ;
2531 this . login ( this . kogBot . environment . discord . client_token ) ;
32+ }
33+ }
34+
35+ gatewayEvents : EventsClass = {
36+ functions : [ ] ,
37+
38+ parse : async ( ) => {
39+ const discordEvents : GatewayEvent [ ] = [ ] ;
40+ const eventsPath = join ( __dirname , 'events' ) ;
41+ const eventFiles = readdirSync ( eventsPath ) . filter ( file => file . endsWith ( '.js' ) ) ;
42+
43+ for ( const file of eventFiles ) {
44+ const eventPath = join ( eventsPath , file ) ;
45+ const importedEvent = ( await import ( `file://${ eventPath } ` ) ) . default ;
2646
27- this . on ( 'ready' , ( ) => {
28- console . log ( `Logged in as ${ this . user ?. tag } ` ) ;
29- } ) ;
47+ const theEvent = new importedEvent ( ) ;
3048
31- this . on ( 'messageCreate' , ( message : Message ) => {
32- if ( message . content === 'hi' ) {
33- message . reply ( 'hi' ) ;
49+ const eventName = file . replace ( / \. [ ^ / . ] + $ / , "" ) ;
50+ const eventCode = theEvent . code ;
51+ const eventOnce : boolean = theEvent . once ;
52+
53+ discordEvents . push ( {
54+ name : eventName ,
55+ code : eventCode . bind ( null , this . kogBot ) ,
56+ once : eventOnce
57+ } ) ;
58+ }
59+
60+ return discordEvents ;
61+ } ,
62+
63+ listen : async ( ) => {
64+ const events : GatewayEvent [ ] = await this . gatewayEvents . parse ( ) ;
65+ this . gatewayEvents . functions = events ;
66+
67+ for ( const event of this . gatewayEvents . functions ) {
68+ if ( event . once ) {
69+ this . once ( event . name , event . code ) ;
70+ } else {
71+ this . on ( event . name , event . code ) ;
3472 }
35- } ) ;
73+ }
3674 }
3775 }
3876}
0 commit comments