1
1
const ServiceUtils = require ( '@switchblade/service-utils' )
2
2
const winston = require ( 'winston' )
3
3
const Influx = require ( 'influx' )
4
- const { Client } = require ( 'eris ' )
4
+ const { Gateway } = require ( 'detritus-client-socket ' )
5
5
const logger = winston . createLogger ( )
6
6
7
7
const DATABASE = process . env . INFLUXDB_DATABASE
@@ -46,7 +46,7 @@ const influx = new Influx.InfluxDB({
46
46
} )
47
47
48
48
influx . getDatabaseNames ( ) . then ( names => {
49
- if ( ! names . includes ( DATABASE ) ) this . influx . createDatabase ( DATABASE )
49
+ if ( ! names . includes ( DATABASE ) ) influx . createDatabase ( DATABASE )
50
50
} )
51
51
52
52
function writeBotMeasurement ( measurement , fields , tags ) {
@@ -69,42 +69,42 @@ function writeBotEvent (eventName, tags) {
69
69
} ) . catch ( error => { logger . error ( error , { label : 'InfluxDB' } ) } )
70
70
}
71
71
72
- const client = new Client ( process . env . DISCORD_TOKEN )
73
-
74
- client . on ( 'debug' , message => {
75
- logger . debug ( message )
72
+ const client = new Gateway . Socket ( process . env . DISCORD_TOKEN , {
73
+ intents : [
74
+ 1 << 9 ,
75
+ 1 << 1
76
+ ]
76
77
} )
77
78
78
- client . on ( 'messageCreate' , message => {
79
- writeBotEvent ( 'messageCreate' , {
80
- guild_id : message . channel . guild . id ,
81
- channel_id : message . channel . id ,
82
- user_id : message . author . id
83
- } )
79
+ client . on ( 'ready' , ( ) => {
80
+ logger . info ( 'Connected' , { label : 'Discord' } )
84
81
} )
85
82
86
- client . on ( 'guildMemberAdd' , ( guild , member ) => {
87
- writeBotEvent ( 'guildMemberAdd' , {
88
- guild_id : guild . id ,
89
- user_id : member . id
90
- } )
91
- writeBotMeasurement ( 'members' , {
92
- member_count : guild . memberCount
93
- } , {
94
- guild_id : guild . id
95
- } )
83
+ client . on ( 'warn' , error => {
84
+ logger . error ( error , { label : 'Discord' } )
96
85
} )
97
86
98
- client . on ( 'guildMemberRemove' , ( guild , member ) => {
99
- writeBotEvent ( 'guildMemberRemove' , {
100
- guild_id : guild . id ,
101
- user_id : member . id
102
- } )
103
- writeBotMeasurement ( 'members' , {
104
- member_count : guild . memberCount
105
- } , {
106
- guild_id : guild . id
107
- } )
87
+ client . on ( 'packet' , packet => {
88
+ logger . debug ( `${ packet . op } ${ packet . t ? ` ${ packet . t } ` : '' } ` )
89
+ if ( packet . op === 0 ) {
90
+ switch ( packet . t ) {
91
+ case 'MESSAGE_CREATE' :
92
+ writeBotEvent ( packet . t , {
93
+ guild_id : packet . d . guild_id ,
94
+ channel_id : packet . d . channel_id ,
95
+ user_id : packet . d . author . id
96
+ } )
97
+ break
98
+ // TODO: Find a way to get the guild's member count
99
+ case 'GUILD_MEMBER_ADD' :
100
+ case 'GUILD_MEMBER_REMOVE' :
101
+ writeBotEvent ( packet . t , {
102
+ guild_id : packet . d . guild_id ,
103
+ user_id : packet . d . user . id
104
+ } )
105
+ break
106
+ }
107
+ }
108
108
} )
109
109
110
- client . connect ( )
110
+ client . connect ( 'wss://gateway.discord.gg/' )
0 commit comments