11import { setTimeout } from 'node:timers' ;
2- import { PermissionsBitField } from '@chatsift/backend-core' ;
2+ import type { BotId } from '@chatsift/backend-core' ;
3+ import { BOTS , GlobalCaches , PermissionsBitField , promiseAllObject } from '@chatsift/backend-core' ;
34import type { APIUser , RESTAPIPartialCurrentUserGuild } from '@discordjs/core' ;
45import { PermissionFlagsBits } from '@discordjs/core' ;
56import { context } from '../context.js' ;
67import { discordAPIOAuth } from './discordAPI.js' ;
78
8- // TODO: Proper bots type
99export type MeGuild = Pick < RESTAPIPartialCurrentUserGuild , 'icon' | 'id' | 'name' > & {
10- bots : string [ ] ;
10+ bots : BotId [ ] ;
1111 meCanManage : boolean ;
1212} ;
1313export type Me = APIUser & { guilds : MeGuild [ ] ; isGlobalAdmin : boolean } ;
@@ -28,6 +28,17 @@ export async function fetchMe(discordAccessToken: string, force = false): Promis
2828 const discordUser = await discordAPIOAuth . users . getCurrent ( { auth } ) ;
2929 const guildsRaw = await discordAPIOAuth . users . getGuilds ( { } , { auth } ) ;
3030
31+ const guildsByBot = await promiseAllObject (
32+ Object . fromEntries (
33+ BOTS . map ( ( bot ) => [
34+ bot ,
35+ context . redis
36+ . get ( GlobalCaches . GuildList . key ( bot ) )
37+ . then ( ( data ) => ( data ? GlobalCaches . GuildList . recipe . decode ( data ) . guilds : [ ] ) ) ,
38+ ] ) ,
39+ ) as Record < BotId , Promise < string [ ] > > ,
40+ ) ;
41+
3142 const guilds = guildsRaw . map < MeGuild > ( ( { id, name, icon, owner, permissions } ) => ( {
3243 id,
3344 name,
@@ -39,7 +50,7 @@ export async function fetchMe(discordAccessToken: string, force = false): Promis
3950 BigInt ( permissions ) ,
4051 PermissionFlagsBits . ManageGuild | PermissionFlagsBits . Administrator ,
4152 ) || owner ,
42- bots : [ ] ,
53+ bots : BOTS . filter ( ( bot ) => guildsByBot [ bot ] ?. includes ( id ) ) ,
4354 } ) ) ;
4455
4556 const me : Me = {
0 commit comments