2019-01-02 10:21:21 +01:00
const { Listener } = require ( 'discord-akairo' ) ;
2019-12-08 01:47:25 +01:00
const akairoVersion = require ( 'discord-akairo' ) . version ;
const djsVersion = require ( 'discord.js' ) . version ;
const pjson = require ( '../../package.json' ) ;
2019-12-16 17:35:09 +01:00
const { prefix , statsChannel , ownerID , supportServer , exposeStats } = require ( '../../config.json' ) ;
2019-03-23 19:28:57 +01:00
const game = require ( '../../json/status/playing.json' ) ;
const watch = require ( '../../json/status/watching.json' ) ;
2019-01-02 10:21:21 +01:00
2019-12-08 01:47:25 +01:00
2019-01-02 10:21:21 +01:00
class ReadyListener extends Listener {
constructor ( ) {
super ( 'ready' , {
emitter : 'client' ,
event : 'ready'
} ) ;
}
async exec ( ) {
2019-12-08 01:47:25 +01:00
let commandSize = this . client . commandHandler . modules . size ;
let clientTag = this . client . user . tag ;
2020-03-04 02:08:04 +01:00
let guildSize = this . client . guilds . cache . size ;
let userSize = this . client . users . cache . size ;
let channelSize = this . client . channels . cache . size ;
2019-12-08 01:47:25 +01:00
let profilePicture = this . client . user . displayAvatarURL ( ) ;
let clientID = this . client . user . id ;
2020-03-04 02:08:04 +01:00
let author = this . client . users . resolve ( ownerID ) . tag ;
2019-12-08 01:47:25 +01:00
2019-01-02 10:21:21 +01:00
// Send stats to the console
2019-12-08 01:47:25 +01:00
console . log ( '===========[ READY ]===========' ) ;
console . log ( ` \x 1b[32mLogged in as \x 1b[34m ${ clientTag } \x 1b[0m! ( \x 1b[33m ${ clientID } \x 1b[0m) ` ) ;
console . log ( ` Ready to serve in \x 1b[33m ${ channelSize } \x 1b[0m channels on \x 1b[33m ${ guildSize } \x 1b[0m servers, for a total of \x 1b[33m ${ userSize } \x 1b[0m users. ` ) ;
console . log ( ` There is \x 1b[33m ${ commandSize } \x 1b[0m command loaded ` ) ;
console . log ( ` ${ this . client . readyAt } ` ) ;
2019-03-23 19:28:57 +01:00
//Bot status
2020-01-09 23:38:37 +01:00
setStatus ( this . client ) ;
// Change status every 30 minutes
setInterval ( async ( ) => {
setStatus ( this . client ) ;
} , 1800000 ) ;
2019-03-23 19:28:57 +01:00
2020-01-09 23:38:37 +01:00
async function setStatus ( client ) {
2020-03-04 02:08:04 +01:00
let owner = client . users . resolve ( client . ownerID ) ;
2020-01-09 23:38:37 +01:00
let random = Math . floor ( ( Math . random ( ) * 3 ) ) ;
2020-06-15 16:46:29 +02:00
if ( random === 0 ) { // Random "Watching" status taken from json
2020-01-09 23:38:37 +01:00
console . log ( 'Status type: \x1b[32mWatching\x1b[0m' ) ;
let status = watch [ Math . floor ( ( Math . random ( ) * watch . length ) ) ] ;
status = status . replace ( '${prefix}' , prefix [ 0 ] ) ;
client . user . setActivity ( ` ${ status } | My prefix is: ${ prefix [ 0 ] } ` , { type : 'WATCHING' } ) ;
2020-06-15 16:46:29 +02:00
} else if ( random === 1 ) { // Random "Playing" status taken from json
2020-01-09 23:38:37 +01:00
console . log ( 'Status type: \x1b[32mPlaying\x1b[0m' ) ;
let status = game [ Math . floor ( ( Math . random ( ) * game . length ) ) ] ;
status = status . replace ( '${prefix}' , prefix [ 0 ] ) ;
client . user . setActivity ( ` ${ status } | My prefix is: ${ prefix [ 0 ] } ` , { type : 'PLAYING' } ) ;
2020-06-15 16:46:29 +02:00
} else if ( random === 2 && owner . presence . activities != null ) { // Bot owner status
2020-01-09 23:38:37 +01:00
console . log ( 'Status type: \x1b[32mCopying owner status\x1b[0m' ) ;
2020-01-10 00:38:42 +01:00
// Get elapsed time from when the activity started
let diffMins = 0 ;
2020-03-04 02:08:04 +01:00
if ( owner . presence . activities [ 0 ] . timestamps ) {
let diffMs = ( new Date ( ) - owner . presence . activities [ 0 ] . timestamps . start ) ;
2020-01-10 00:38:42 +01:00
diffMins = Math . round ( ( ( diffMs % 86400000 ) % 3600000 ) / 60000 ) ;
}
2020-03-04 02:08:04 +01:00
client . user . setActivity ( ` ${ owner . presence . activities [ 0 ] . name } \n for ${ diffMins } minutes | My prefix is: ${ prefix [ 0 ] } ` , owner . presence . activities [ 0 ] ) ;
2020-01-09 23:38:37 +01:00
}
2019-03-23 19:28:57 +01:00
}
2020-01-09 23:38:37 +01:00
2019-12-08 01:47:25 +01:00
// If stats channel settings exist, send bot stats to it
if ( statsChannel ) {
2020-03-04 02:08:04 +01:00
const channel = this . client . channels . resolve ( statsChannel ) ;
2019-12-08 01:47:25 +01:00
channel . send ( ` Ready to serve in ${ channelSize } channels on ${ guildSize } servers, for a total of ${ userSize } users. \n There is ${ commandSize } command loaded \n ${ this . client . readyAt } ` ) ;
2019-01-02 10:21:21 +01:00
}
2019-12-08 01:47:25 +01:00
// Expose stats
2019-12-16 17:35:09 +01:00
if ( exposeStats ) {
const port = 3000 ;
2019-12-08 01:47:25 +01:00
2019-12-16 17:35:09 +01:00
const http = require ( 'http' ) ;
const requestHandler = ( req , res ) => {
// Refresh some info
commandSize = this . client . commandHandler . modules . size ;
2020-03-04 02:08:04 +01:00
guildSize = this . client . guilds . cache . size ;
userSize = this . client . users . cache . size ;
2019-12-16 17:35:09 +01:00
profilePicture = this . client . user . displayAvatarURL ( ) ;
let response = {
'commandSize' : commandSize ,
'ClientTag' : clientTag ,
'guildSize' : guildSize ,
'userSize' : userSize ,
'prefixSize' : prefix . length ,
'profilePicture' : profilePicture ,
'clientID' : clientID ,
'djsVersion' : djsVersion ,
'akairoVersion' : akairoVersion ,
'homepage' : pjson . homepage ,
'author' : author ,
'supportServer' : supportServer
} ;
res . statusCode = 200 ;
res . setHeader ( 'Content-Type' , 'application/json' ) ;
res . end ( JSON . stringify ( response ) ) ;
2019-12-08 01:47:25 +01:00
} ;
2019-12-16 17:35:09 +01:00
const server = http . createServer ( requestHandler ) ;
server . listen ( port , ( err ) => {
if ( err ) {
return console . log ( 'something bad happened' , err ) ;
}
} ) ;
console . log ( ` Exposing stats on port ${ port } ` ) ;
}
2019-12-08 01:47:25 +01:00
console . log ( '===========[ READY ]===========' ) ;
2019-01-02 10:21:21 +01:00
}
}
module . exports = ReadyListener ;