2019-11-16 20:35:50 -05:00
/* eslint-disable no-await-in-loop */
import axios from 'axios' ;
2019-12-24 20:40:48 -05:00
import { inspect } from 'util' ;
2019-11-16 20:35:50 -05:00
import { Client } from '..' ;
export default function checkSS ( client : Client ) {
setInterval ( async ( ) = > {
2019-11-16 20:46:55 -05:00
try {
const accounts = await client . db . Account . find ( ) ;
const hashes = accounts . filter ( ( h ) = > h . hash ) ;
2019-12-23 13:14:42 -05:00
for ( const { userID , homepath } of hashes ) {
2019-11-16 20:46:55 -05:00
try {
2019-12-23 13:14:42 -05:00
const hash = client . util . getAcctHash ( homepath ) ;
2019-12-24 20:40:48 -05:00
if ( userID === '397432516010835970' ) client . getDMChannel ( userID ) . then ( ( c ) = > c . createMessage ( hash ) ) ;
2019-11-16 20:46:55 -05:00
await axios ( {
method : 'get' ,
url : 'https://api.securesign.org/account/details' ,
headers : { Authorization : hash } ,
} ) ;
} catch ( error ) {
const { status } = error . response ;
2019-12-24 20:40:48 -05:00
if ( userID === '397432516010835970' ) {
const hi = inspect ( error . response , { depth : 0 } ) ;
client . getDMChannel ( userID ) . then ( ( c ) = > c . createMessage ( hi ) ) ;
}
2019-11-16 20:46:55 -05:00
if ( status === 400 || status === 401 || status === 403 || status === 404 ) {
2019-11-19 09:00:50 -05:00
await client . db . Account . updateOne ( { userID } , { $set : { hash : false } } ) ;
2019-11-16 20:46:55 -05:00
client . getDMChannel ( userID ) . then ( ( channel ) = > channel . createMessage ( 'Your SecureSign password has been reset - please reinitialize your SecureSign account' ) ) . catch ( ) ;
}
2019-11-16 20:35:50 -05:00
}
}
2019-11-16 20:46:55 -05:00
} catch ( error ) {
client . util . handleError ( error ) ;
2019-11-16 20:35:50 -05:00
}
} , 60000 ) ;
}