import AuthClient from "./auth-client" ; import { TundraBot } from "../../base/TundraBot" ; import { Guild } from "discord.js" ; import Deps from "../../utils/deps" ; export default class Sessions { client : TundraBot ; AuthClient : AuthClient ; sessions : Map < string , any > ; constructor ( client ) { this . client = client ; this . AuthClient = Deps . get < AuthClient > ( AuthClient ) ; this . sessions = new Map ( ) ; } async get ( key ) { let data = this . sessions . get ( key ) ; if ( data === undefined ) data = await this . create ( key ) ; return data ; } async create ( key ) { setTimeout ( ( ) => this . sessions . delete ( key ) , 5 * 60 * 1000 ) ; await this . update ( key ) ; return this . sessions . get ( key ) ; } async update ( key ) { return this . sessions . set ( key , { authUser : await this . AuthClient . authClient . getUser ( key ) , authGuilds : this . getManageableGuilds ( await this . AuthClient . authClient . getGuilds ( key ) ) , } ) ; } getManageableGuilds ( authGuilds ) { const guilds = [ ] ; for ( const id of authGuilds . keys ( ) ) { if ( ! authGuilds . get ( id ) . permissions . includes ( "MANAGE_GUILD" ) ) continue ; const guild = this . client . guilds . cache . get ( id ) ; if ( ! guild ) continue ; guilds . push ( guild ) ; } return guilds ; } }