2021-05-30 14:04:58 -07:00
import {
// Discordeno deps
cache ,
2022-07-10 17:48:38 -07:00
deleteMessage ,
2021-11-11 19:23:36 -08:00
getGuild ,
2022-07-10 17:48:38 -07:00
getMessage ,
log ,
2021-05-30 14:04:58 -07:00
// Log4Deno deps
2022-07-10 17:48:38 -07:00
LT ,
sendDirectMessage ,
sendMessage ,
} from '../deps.ts' ;
2021-05-30 14:04:58 -07:00
2022-07-10 17:48:38 -07:00
import { jsonStringifyBig } from './utils.ts' ;
import { ActiveLFG , BuildingLFG } from './mod.d.ts' ;
2021-05-30 14:04:58 -07:00
2023-01-11 19:23:27 -08:00
import config from '../../config.ts' ;
2021-05-30 14:04:58 -07:00
// 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 = > {
2022-07-10 17:48:38 -07:00
let status = '' ;
2021-05-30 14:04:58 -07:00
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 :
2022-07-10 17:48:38 -07:00
status = 'Mention me to check my prefix!' ;
2021-05-30 14:04:58 -07:00
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 ;
}
2022-07-10 17:48:38 -07:00
2021-05-30 14:04:58 -07:00
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 = > {
2022-07-10 17:48:38 -07:00
config . botLists . forEach ( async ( e ) = > {
2021-05-30 14:04:58 -07:00
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 ) ;
2022-07-10 17:48:38 -07:00
tempHeaders . append ( 'Content-Type' , 'application/json' ) ;
2021-06-02 09:27:05 -07:00
// ?{} is a template used in config, just need to replace it with the real value
2022-07-10 17:48:38 -07:00
const response = await fetch ( e . apiUrl . replace ( '?{bot_id}' , botID . toString ( ) ) , {
'method' : 'POST' ,
'headers' : tempHeaders ,
'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 ) } ` ) ;
2022-07-10 17:48:38 -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 ( ) ;
2022-07-10 17:48:38 -07:00
for ( let i = 0 ; i < activeBuilders . length ; i ++ ) {
2021-05-30 14:04:58 -07:00
if ( activeBuilders [ i ] . lastTouch . getTime ( ) + ( activeBuilders [ i ] . maxIdle * 1000 ) < currentTime ) {
2022-07-10 17:48:38 -07:00
activeBuilders [ i ] . questionMsg . delete ( ) . 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
if ( activeBuilders [ i ] . editing ) {
activeBuilders [ i ] . lfgMsg . edit ( {
2022-07-10 17:48:38 -07:00
content : '' ,
} ) . 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 {
2022-07-10 17:48:38 -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 {
2022-07-10 17:48:38 -07:00
const m = await sendMessage ( activeBuilders [ i ] . channelId , ` <@ ${ activeBuilders [ i ] . userId } >, your LFG ${ activeBuilders [ i ] . editing ? 'editing' : 'creation' } has timed out. Please try again. ` ) ;
m . delete ( 'Channel Cleanup' , 30000 ) . catch ( ( e ) = > {
2021-11-11 19:23:36 -08:00
log ( LT . WARN , ` Failed to delete message | ${ jsonStringifyBig ( e ) } ` ) ;
} ) ;
2022-07-10 17:48:38 -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 ) } ` ) ;
2022-07-10 17:48:38 -07:00
} finally {
2021-06-02 09:27:05 -07:00
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 > = > {
2022-07-10 17:48:38 -07: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 } ` ;
2022-07-10 17:48:38 -07: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 ;
2022-07-10 17:48:38 -07:00
let editMsg = '' ;
members . split ( '\n' ) . forEach ( async ( m ) = > {
if ( m !== 'None' ) {
const [ name , tmpId ] = m . split ( ' - <@' ) ;
2021-06-02 09:27:05 -07:00
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 ] ,
{
2022-07-10 17:48:38 -07:00
name : 'Please start grouping up with the other members of this activity:' ,
value : members ,
} ,
] ,
} ] ,
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 ( {
2022-07-10 17:48:38 -07:00
content : editMsg ,
2021-06-02 09:27:05 -07:00
} ) ;
2021-05-30 14:04:58 -07:00
2021-06-02 09:27:05 -07:00
activeLFGPosts [ i ] . notified = true ;
2022-07-10 17:48:38 -07:00
} 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 ) } ` ) ;
2022-07-10 17:48:38 -07:00
2021-06-02 09:27:05 -07:00
activeLFGPosts . splice ( i , 1 ) ;
i -- ;
}
2022-07-10 17:48:38 -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 ( {
2022-07-10 17:48:38 -07:00
components : [ ] ,
2021-06-02 09:27:05 -07:00
} ) ;
2021-05-30 14:04:58 -07:00
2021-06-02 09:27:05 -07:00
activeLFGPosts [ i ] . locked = true ;
2022-07-10 17:48:38 -07:00
} 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 ) } ` ) ;
2022-07-10 17:48:38 -07:00
2021-06-02 09:27:05 -07:00
activeLFGPosts . splice ( i , 1 ) ;
i -- ;
}
2022-07-10 17:48:38 -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 } ` ) ;
2022-07-10 17:48:38 -07:00
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 -- ;
}
}
2022-07-10 17:48:38 -07:00
localStorage . setItem ( 'activeLFGPosts' , jsonStringifyBig ( activeLFGPosts ) ) ;
2021-11-11 19:23:36 -08:00
} ;
2021-05-30 14:04:58 -07:00
export default { getRandomStatus , updateListStatistics , buildingTimeout , lfgNotifier } ;