2021-05-30 14:04:58 -07:00
import {
// Discordeno deps
cache ,
sendMessage , getMessage , deleteMessage , sendDirectMessage ,
2021-11-11 19:23:36 -08:00
getGuild ,
2021-05-30 14:04:58 -07:00
// Log4Deno deps
LT , log
} from "../deps.ts" ;
2021-11-11 19:23:36 -08:00
import { jsonStringifyBig } from "./utils.ts" ;
2021-05-30 14:04:58 -07:00
import { BuildingLFG , ActiveLFG } from "./mod.d.ts" ;
import config from "../config.ts" ;
// getRandomStatus() returns status as string
// Gets a new random status for the bot
2021-11-11 19:23:36 -08:00
const getRandomStatus = ( cachedGuilds : number ) : string = > {
2021-05-30 14:04:58 -07:00
let status = "" ;
switch ( Math . floor ( ( Math . random ( ) * 5 ) + 1 ) ) {
case 1 :
status = ` ${ config . prefix } help for commands ` ;
break ;
case 2 :
status = ` Running V ${ config . version } ` ;
break ;
case 3 :
status = ` ${ config . prefix } info to learn more ` ;
break ;
case 4 :
status = "Mention me to check my prefix!" ;
break ;
default :
2021-11-11 19:23:36 -08:00
status = ` Running LFGs in ${ cachedGuilds + cache . dispatchedGuildIds . size } servers ` ;
2021-05-30 14:04:58 -07:00
break ;
}
return status ;
} ;
// updateListStatistics(bot ID, current guild count) returns nothing
// Sends the current server count to all bot list sites we are listed on
const updateListStatistics = ( botID : BigInt , serverCount : number ) : void = > {
config . botLists . forEach ( async e = > {
if ( e . enabled ) {
2021-11-11 19:23:36 -08:00
log ( LT . LOG , ` Updating statistics for ${ jsonStringifyBig ( e ) } ` ) ;
2021-06-02 09:27:05 -07:00
try {
const tempHeaders = new Headers ( ) ;
tempHeaders . append ( e . headers [ 0 ] . header , e . headers [ 0 ] . value ) ;
tempHeaders . append ( "Content-Type" , "application/json" ) ;
// ?{} is a template used in config, just need to replace it with the real value
const response = await fetch ( e . apiUrl . replace ( "?{bot_id}" , botID . toString ( ) ) , {
"method" : 'POST' ,
"headers" : tempHeaders ,
2021-11-11 19:23:36 -08:00
"body" : jsonStringifyBig ( e . body ) . replace ( '"?{server_count}"' , serverCount . toString ( ) ) // ?{server_count} needs the "" removed from around it aswell to make sure its sent as a number
2021-06-02 09:27:05 -07:00
} ) ;
2021-11-11 19:23:36 -08:00
log ( LT . INFO , ` Posted server count to ${ e . name } . Results: ${ jsonStringifyBig ( response ) } ` ) ;
2021-06-02 09:27:05 -07:00
}
catch ( e ) {
2021-11-11 19:23:36 -08:00
log ( LT . WARN , ` Failed to post statistics to ${ e . name } | ${ jsonStringifyBig ( e ) } ` ) ;
2021-06-02 09:27:05 -07:00
}
2021-05-30 14:04:58 -07:00
}
} ) ;
} ;
const buildingTimeout = async ( activeBuilders : Array < BuildingLFG > ) : Promise < void > = > {
const currentTime = new Date ( ) . getTime ( ) ;
for ( let i = 0 ; i < activeBuilders . length ; i ++ ) {
if ( activeBuilders [ i ] . lastTouch . getTime ( ) + ( activeBuilders [ i ] . maxIdle * 1000 ) < currentTime ) {
2021-11-11 19:23:36 -08:00
activeBuilders [ i ] . questionMsg . delete ( ) . catch ( e = > {
log ( LT . WARN , ` Failed to clean up active builder | edit | ${ activeBuilders [ i ] . userId } - ${ activeBuilders [ i ] . channelId } | ${ jsonStringifyBig ( e ) } ` ) ;
} ) ;
2021-05-30 14:04:58 -07:00
if ( activeBuilders [ i ] . editing ) {
activeBuilders [ i ] . lfgMsg . edit ( {
content : ""
2021-06-02 09:27:05 -07:00
} ) . catch ( e = > {
2021-11-11 19:23:36 -08:00
log ( LT . WARN , ` Failed to clean up active builder | edit | ${ activeBuilders [ i ] . userId } - ${ activeBuilders [ i ] . channelId } | ${ jsonStringifyBig ( e ) } ` ) ;
2021-05-30 14:04:58 -07:00
} ) ;
} else {
2021-06-02 09:27:05 -07:00
activeBuilders [ i ] . lfgMsg . delete ( ) . catch ( e = > {
2021-11-11 19:23:36 -08:00
log ( LT . WARN , ` Failed to clean up active builder | delete | ${ activeBuilders [ i ] . userId } - ${ activeBuilders [ i ] . channelId } | ${ jsonStringifyBig ( e ) } ` ) ;
2021-06-02 09:27:05 -07:00
} ) ;
}
try {
const m = await sendMessage ( activeBuilders [ i ] . channelId , ` <@ ${ activeBuilders [ i ] . userId } >, your LFG ${ activeBuilders [ i ] . editing ? "editing" : "creation" } has timed out. Please try again. ` ) ;
2021-11-11 19:23:36 -08:00
m . delete ( "Channel Cleanup" , 30000 ) . catch ( e = > {
log ( LT . WARN , ` Failed to delete message | ${ jsonStringifyBig ( e ) } ` ) ;
} ) ;
2021-06-02 09:27:05 -07:00
}
catch ( e ) {
2021-11-11 19:23:36 -08:00
log ( LT . WARN , ` Failed to clean up active builder | ${ activeBuilders [ i ] . userId } - ${ activeBuilders [ i ] . channelId } | ${ jsonStringifyBig ( e ) } ` ) ;
2021-06-02 09:27:05 -07:00
}
finally {
activeBuilders . splice ( i , 1 ) ;
i -- ;
2021-05-30 14:04:58 -07:00
}
}
}
2021-11-11 19:23:36 -08:00
} ;
2021-05-30 14:04:58 -07:00
const lfgNotifier = async ( activeLFGPosts : Array < ActiveLFG > ) : Promise < void > = > {
2021-11-11 19:23:36 -08:00
log ( LT . INFO , "Checking for LFG posts to notify/delete/lock" ) ;
2021-05-30 14:04:58 -07:00
const tenMin = 10 * 60 * 1000 ;
const now = new Date ( ) . getTime ( ) ;
for ( let i = 0 ; i < activeLFGPosts . length ; i ++ ) {
// Send notifications
if ( ! activeLFGPosts [ i ] . notified && activeLFGPosts [ i ] . lfgTime < ( now + tenMin ) ) {
log ( LT . INFO , ` Notifying LFG ${ activeLFGPosts [ i ] . ownerId } - ${ activeLFGPosts [ i ] . lfgUid } ` ) ;
2021-06-02 09:27:05 -07:00
try {
const message = await getMessage ( activeLFGPosts [ i ] . channelId , activeLFGPosts [ i ] . messageId ) ;
const lfg = message . embeds [ 0 ] . fields || [ ] ;
const lfgActivity = ` ${ lfg [ 0 ] . name . substr ( 0 , lfg [ 0 ] . name . length - 1 ) } - ${ lfg [ 0 ] . value } ` ;
2021-11-11 19:23:36 -08:00
const guildName = message . guild ? . name || ( await getGuild ( message . guildId , { counts :false , addToCache : false } ) ) . name ;
2021-06-02 09:27:05 -07:00
const members = lfg [ 4 ] . value ;
let editMsg = "" ;
members . split ( "\n" ) . forEach ( async m = > {
if ( m !== "None" ) {
const [ name , tmpId ] = m . split ( " - <@" ) ;
const userId = BigInt ( tmpId . substr ( 0 , tmpId . length - 1 ) ) ;
editMsg += ` <@ ${ userId } >, ` ;
await sendDirectMessage ( userId , {
2021-11-11 19:23:36 -08:00
embeds : [ {
2021-06-02 22:49:13 -07:00
title : ` Hello ${ name } ! Your event in ${ guildName } starts in less than 10 minutes. ` ,
2021-06-02 09:27:05 -07:00
fields : [
lfg [ 0 ] ,
{
name : "Please start grouping up with the other members of this activity:" ,
value : members
}
]
2021-11-11 19:23:36 -08:00
} ]
2021-06-02 09:27:05 -07:00
} ) ;
2021-05-30 14:04:58 -07:00
}
} ) ;
2021-06-02 09:27:05 -07:00
editMsg += ` your ${ lfgActivity } starts in less than 10 minutes. ` ;
2021-05-30 14:04:58 -07:00
2021-06-02 09:27:05 -07:00
await message . edit ( {
content : editMsg
} ) ;
2021-05-30 14:04:58 -07:00
2021-06-02 09:27:05 -07:00
activeLFGPosts [ i ] . notified = true ;
}
catch ( err ) {
2021-11-11 19:23:36 -08:00
log ( LT . WARN , ` Failed to find LFG ${ activeLFGPosts [ i ] . ownerId } - ${ activeLFGPosts [ i ] . lfgUid } | ${ jsonStringifyBig ( err ) } ` ) ;
2021-06-02 09:27:05 -07:00
activeLFGPosts . splice ( i , 1 ) ;
i -- ;
}
2021-05-30 14:04:58 -07:00
}
// Lock LFG from editing/Joining/Leaving
2021-11-11 19:23:36 -08:00
else if ( ! activeLFGPosts [ i ] . locked && activeLFGPosts [ i ] . lfgTime < now ) {
2021-05-30 14:04:58 -07:00
log ( LT . INFO , ` Locking LFG ${ activeLFGPosts [ i ] . ownerId } - ${ activeLFGPosts [ i ] . lfgUid } ` ) ;
2021-06-02 09:27:05 -07:00
try {
const message = await getMessage ( activeLFGPosts [ i ] . channelId , activeLFGPosts [ i ] . messageId ) ;
2021-05-30 14:04:58 -07:00
2021-06-02 09:27:05 -07:00
await message . edit ( {
components : [ ]
} ) ;
2021-05-30 14:04:58 -07:00
2021-06-02 09:27:05 -07:00
activeLFGPosts [ i ] . locked = true ;
}
catch ( err ) {
2021-11-11 19:23:36 -08:00
log ( LT . WARN , ` Failed to find LFG ${ activeLFGPosts [ i ] . ownerId } - ${ activeLFGPosts [ i ] . lfgUid } | ${ jsonStringifyBig ( err ) } ` ) ;
2021-06-02 09:27:05 -07:00
activeLFGPosts . splice ( i , 1 ) ;
i -- ;
}
2021-05-30 14:04:58 -07:00
}
// Delete old LFG post
2021-11-11 19:23:36 -08:00
else if ( activeLFGPosts [ i ] . lfgTime < ( now - tenMin ) ) {
2021-05-30 14:04:58 -07:00
log ( LT . INFO , ` Deleting LFG ${ activeLFGPosts [ i ] . ownerId } - ${ activeLFGPosts [ i ] . lfgUid } ` ) ;
await deleteMessage ( activeLFGPosts [ i ] . channelId , activeLFGPosts [ i ] . messageId , "LFG post expired" ) . catch ( e = > {
2021-11-11 19:23:36 -08:00
log ( LT . WARN , ` Failed to delete LFG ${ activeLFGPosts [ i ] . ownerId } - ${ activeLFGPosts [ i ] . lfgUid } | ${ jsonStringifyBig ( e ) } ` ) ;
2021-05-30 14:04:58 -07:00
} ) ;
activeLFGPosts . splice ( i , 1 ) ;
i -- ;
}
}
2021-11-11 19:23:36 -08:00
localStorage . setItem ( "activeLFGPosts" , jsonStringifyBig ( activeLFGPosts ) ) ;
} ;
2021-05-30 14:04:58 -07:00
export default { getRandomStatus , updateListStatistics , buildingTimeout , lfgNotifier } ;