1
1
import { Bot , pick , Universal } from '@satorijs/core'
2
- import { Channel , Guild , Login } from './types'
2
+ import { Channel , Guild , LoginSync } from './types'
3
3
import { SyncChannel } from './channel'
4
4
import SatoriDatabase from '.'
5
5
6
6
interface CachedBot extends Bot {
7
- sync : Login [ 'sync' ]
7
+ sync : LoginSync
8
8
}
9
9
10
10
class CachedBot {
@@ -33,13 +33,16 @@ class CachedBot {
33
33
}
34
34
const data : Partial < Guild > [ ] = [ ]
35
35
for await ( const guild of self . getGuildIter ( ) ) {
36
- data . push ( { platform : this . platform , ... pick ( guild , [ 'id' , 'name' , 'avatar' ] ) } )
36
+ data . push ( pick ( guild , [ 'id' , 'name' , 'avatar' ] ) )
37
37
}
38
38
await this . sd . ctx . database . set ( 'satori.login' , this . _query , {
39
39
sync : {
40
40
guildListAt : Date . now ( ) ,
41
41
} ,
42
- guildSyncs : data . map ( ( guild ) => ( { guild } ) ) as any ,
42
+ guildSyncs : data . map ( ( guild ) => ( { guild : { $create : {
43
+ platform : this . platform ,
44
+ ...guild ,
45
+ } } } ) ) as any ,
43
46
} )
44
47
return { data }
45
48
}
@@ -58,7 +61,7 @@ class CachedBot {
58
61
id : guildId ,
59
62
} ,
60
63
} )
61
- if ( sync ! . channelListAt >= this . sync . onlineAt ) {
64
+ if ( sync ? .channelListAt >= this . sync . onlineAt ) {
62
65
const data = await this . sd . ctx . database . get ( 'satori.channel' , {
63
66
guild : { id : guildId } ,
64
67
syncs : {
@@ -71,16 +74,20 @@ class CachedBot {
71
74
}
72
75
const data : Partial < Channel > [ ] = [ ]
73
76
for await ( const channel of self . getChannelIter ( guildId ) ) {
74
- data . push ( { platform : this . platform , ... pick ( channel , [ 'id' , 'name' , 'type' ] ) } )
77
+ data . push ( pick ( channel , [ 'id' , 'name' , 'type' , 'parentId' , 'position' ] ) )
75
78
}
76
79
await this . sd . ctx . database . set ( 'satori.login' , this . _query , {
77
80
guildSyncs : {
78
81
$create : {
79
- guild : { id : guildId } ,
82
+ guild : { id : guildId , platform : this . platform } ,
80
83
channelListAt : Date . now ( ) ,
81
84
} ,
82
85
} ,
83
- channelSyncs : data . map ( ( channel ) => ( { channel } ) ) as any ,
86
+ channelSyncs : data . map ( ( channel ) => ( { channel : { $create : {
87
+ platform : this . platform ,
88
+ guild : { id : guildId } ,
89
+ ...channel ,
90
+ } } } ) ) as any ,
84
91
} )
85
92
return { data }
86
93
}
@@ -102,11 +109,15 @@ class CachedBot {
102
109
if ( channel . bot . sid !== this . sid ) continue
103
110
channel . hasLatest = false
104
111
}
112
+ const update : Partial < LoginSync > = {
113
+ onlineAt : Date . now ( ) ,
114
+ }
105
115
const [ login ] = await this . ctx . database . get ( 'satori.login' , this . _query )
106
- this . sync = login ?. sync || { onlineAt : Date . now ( ) }
116
+ this . sync = login ?. sync || { }
117
+ Object . assign ( this . sync , update )
107
118
await this . ctx . database . upsert ( 'satori.login' , [ {
108
119
...this . _query ,
109
- sync : this . sync ,
120
+ sync : update ,
110
121
} ] )
111
122
}
112
123
}
0 commit comments