2020-02-21 00:26:49 +00:00
const gm = require ( "gm" ) ;
const { promisify } = require ( "util" ) ;
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" ) ;
2019-12-05 17:40:09 +00:00
const helpGenerator = process . env . OUTPUT !== "" ? require ( "../utils/help.js" ) : null ;
2019-11-15 16:59:50 +00:00
const twitter = process . env . TWITTER === "true" ? require ( "../utils/twitter.js" ) : null ;
2019-09-13 20:02:41 +00:00
// run when ready
module . exports = async ( ) => {
// make sure settings/tags exist
2019-12-16 18:13:38 +00:00
for ( const [ id ] of client . guilds ) {
2019-11-05 15:52:46 +00:00
const guildDB = ( await database . guilds . find ( { id : id } ) . exec ( ) ) [ 0 ] ;
2019-10-28 20:21:06 +00:00
if ( ! guildDB ) {
2019-12-29 16:56:32 +00:00
logger . log ( ` Registering guild database entry for guild ${ id } ... ` ) ;
2019-11-05 15:52:46 +00:00
const newGuild = new database . guilds ( {
id : id ,
2019-10-28 20:21:06 +00:00
tags : misc . tagDefaults ,
prefix : "&"
} ) ;
await newGuild . save ( ) ;
}
}
2019-09-13 20:02:41 +00:00
2019-12-05 17:40:09 +00:00
// generate docs
if ( helpGenerator ) {
await helpGenerator ( process . env . OUTPUT ) ;
}
2019-09-13 20:02:41 +00:00
// set activity (a.k.a. the gamer code)
( async function activityChanger ( ) {
2019-11-18 01:57:12 +00:00
client . editStatus ( "dnd" , { name : ` ${ misc . random ( messages ) } | @esmBot help ` } ) ;
2019-09-13 20:02:41 +00:00
setTimeout ( activityChanger , 900000 ) ;
} ) ( ) ;
2020-02-21 00:26:49 +00:00
// add gm extensions
gm . prototype . writePromise = promisify ( gm . prototype . write ) ;
gm . prototype . streamPromise = promisify ( gm . prototype . stream ) ;
gm . prototype . sizePromise = promisify ( gm . prototype . size ) ;
gm . prototype . identifyPromise = promisify ( gm . prototype . identify ) ;
2019-11-15 16:59:50 +00:00
// tweet stuff
2019-11-23 23:23:28 +00:00
if ( twitter !== null && twitter . active === false ) {
2019-12-29 21:11:58 +00:00
const blocks = await twitter . client . get ( "blocks/ids" , { stringify _ids : true } ) ;
2019-11-18 01:57:12 +00:00
const tweet = async ( ) => {
const tweets = ( await database . tweets . find ( { enabled : true } ) . exec ( ) ) [ 0 ] ;
const tweetContent = await misc . getTweet ( tweets ) ;
2019-11-23 23:23:28 +00:00
try {
const info = await twitter . client . post ( "statuses/update" , { status : tweetContent } ) ;
logger . log ( ` Tweet with id ${ info . data . id _str } has been tweeted with status code ${ info . resp . statusCode } ${ info . resp . statusMessage } ` ) ;
} 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 ;
2019-11-15 16:59:50 +00:00
const stream = twitter . client . stream ( "statuses/filter" , {
track : ` @ ${ process . env . HANDLE } `
} ) ;
stream . on ( "tweet" , async ( tweet ) => {
2019-12-29 19:55:23 +00:00
if ( tweet . user . screen _name !== "esmBot_" && ! blocks . data . ids . includes ( tweet . user . id _str ) ) {
2019-11-18 01:57:12 +00:00
const tweets = ( await database . tweets . find ( { enabled : true } ) . exec ( ) ) [ 0 ] ;
2019-11-23 23:23:28 +00:00
let tweetContent ;
if ( tweet . text . includes ( "@this_vid" ) || tweet . text . includes ( "@DownloaderBot" ) || tweet . text . includes ( "@GetVideoBot" ) || tweet . text . includes ( "@DownloaderB0t" ) || tweet . text . includes ( "@thisvid_" ) ) {
tweetContent = await misc . getTweet ( tweet , true , true ) ;
} else {
tweetContent = await misc . getTweet ( tweets , true ) ;
}
2019-11-15 16:59:50 +00:00
const payload = {
status : ` @ ${ tweet . user . screen _name } ${ tweetContent } ` ,
in _reply _to _status _id : tweet . id _str
} ;
const info = await twitter . client . post ( "statuses/update" , payload ) ;
logger . log ( ` Reply with id ${ info . data . id _str } has been tweeted with status code ${ info . resp . statusCode } ${ info . resp . statusMessage } ` ) ;
}
} ) ;
}
2019-09-13 20:02:41 +00:00
logger . log ( "info" , ` Successfully started ${ client . user . username } # ${ client . user . discriminator } with ${ client . users . size } users in ${ client . guilds . size } servers. ` ) ;
} ;