2019-09-13 20:02:41 +00:00
const client = require ( "../utils/client.js" ) ;
const database = require ( "../utils/database.js" ) ;
const logger = require ( "../utils/logger.js" ) ;
2019-11-13 00:09:06 +00:00
const messages = require ( "../messages.json" ) ;
2019-09-13 20:02:41 +00:00
const misc = require ( "../utils/misc.js" ) ;
2020-06-27 17:18:26 +00:00
const soundPlayer = require ( "../utils/soundplayer.js" ) ;
2020-04-10 02:40:52 +00:00
const helpGenerator =
process . env . OUTPUT !== "" ? require ( "../utils/help.js" ) : null ;
const twitter =
process . env . TWITTER === "true" ? require ( "../utils/twitter.js" ) : null ;
2020-07-22 01:29:32 +00:00
const first = process . env . PMTWO === "true" ? process . env . NODE _APP _INSTANCE === "0" : true ;
2019-09-13 20:02:41 +00:00
// run when ready
module . exports = async ( ) => {
2020-07-02 15:33:27 +00:00
// connect to lavalink
2020-07-16 14:28:09 +00:00
if ( ! soundPlayer . status && ! soundPlayer . connected ) await soundPlayer . connect ( ) ;
2020-07-02 15:33:27 +00:00
2020-12-19 00:50:25 +00:00
await database . setup ( ) ;
2020-04-26 21:55:33 +00:00
2019-12-05 17:40:09 +00:00
// generate docs
2020-07-22 01:29:32 +00:00
if ( helpGenerator && first ) await helpGenerator ( process . env . OUTPUT ) ;
2019-12-05 17:40:09 +00:00
2019-09-13 20:02:41 +00:00
// set activity (a.k.a. the gamer code)
( async function activityChanger ( ) {
2020-02-26 01:57:44 +00:00
client . editStatus ( "dnd" , {
2020-12-19 00:57:41 +00:00
name : ` ${ misc . random ( messages ) } | @ ${ client . user . username } help ` ,
2020-02-26 01:57:44 +00:00
} ) ;
2019-09-13 20:02:41 +00:00
setTimeout ( activityChanger , 900000 ) ;
} ) ( ) ;
2019-11-15 16:59:50 +00:00
// tweet stuff
2020-07-22 01:29:32 +00:00
if ( twitter !== null && twitter . active === false && first ) {
2020-03-14 23:03:45 +00:00
const blocks = await twitter . client . blocks . ids ( ) ;
2019-11-18 01:57:12 +00:00
const tweet = async ( ) => {
2020-07-10 17:07:24 +00:00
const tweetContent = await misc . getTweet ( twitter . tweets ) ;
2019-11-23 23:23:28 +00:00
try {
2020-03-14 23:03:45 +00:00
const info = await twitter . client . statuses . update ( tweetContent ) ;
logger . log ( ` Tweet with id ${ info . id _str } has been posted. ` ) ;
// with status code ${info.resp.statusCode} ${info.resp.statusMessage}
2019-11-23 23:23:28 +00:00
} catch ( e ) {
const error = JSON . stringify ( e ) ;
if ( error . includes ( "Status is a duplicate." ) ) {
logger . log ( "Duplicate tweet, will retry in 30 minutes" ) ;
} else {
logger . error ( e ) ;
}
}
2019-11-18 01:57:12 +00:00
} ;
tweet ( ) ;
setInterval ( tweet , 1800000 ) ;
2019-11-23 23:23:28 +00:00
twitter . active = true ;
2020-07-07 19:28:16 +00:00
try {
const stream = twitter . client . statuses . filter ( ` @ ${ process . env . HANDLE } ` ) ;
stream . on ( "data" , async ( tweet ) => {
2020-12-18 02:32:19 +00:00
if ( tweet . user . screen _name !== "esmBot_" && ! blocks . ids . includes ( tweet . user . id _str ) ) {
const tweetContent = await misc . getTweet ( twitter . tweets , true ) . replace ( /{{user}}/gm , ` @ ${ tweet . user . screen _name } ` ) ;
2020-07-07 19:28:16 +00:00
const payload = {
status : ` @ ${ tweet . user . screen _name } ${ tweetContent } ` ,
in _reply _to _status _id : tweet . id _str ,
} ;
const info = await twitter . client . statuses . update ( payload ) ;
logger . log ( ` Reply with id ${ info . id _str } has been posted. ` ) ;
2020-03-14 23:03:45 +00:00
// with status code ${info.resp.statusCode} ${info.resp.statusMessage}
2020-07-07 19:28:16 +00:00
}
} ) ;
} catch ( e ) {
logger . error ( ` The Twitter streaming API ran into an error: ${ e } ` ) ;
}
2019-11-15 16:59:50 +00:00
}
2020-07-22 01:29:32 +00:00
if ( process . env . PMTWO === "true" ) process . send ( "ready" ) ;
2020-07-16 14:28:09 +00:00
logger . log ( ` Successfully started ${ client . user . username } # ${ client . user . discriminator } with ${ client . users . size } users in ${ client . guilds . size } servers. ` ) ;
2020-04-10 02:40:52 +00:00
} ;