1
1
import { ClusterClient } from 'detritus-client' ;
2
2
import { ClientEvents } from 'detritus-client/lib/constants' ;
3
- import { EventSubscription } from 'detritus-utils' ;
3
+ import { EventSubscription , Timers } from 'detritus-utils' ;
4
4
5
5
import { Store } from './store' ;
6
6
@@ -25,11 +25,11 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
25
25
async getOrFetch ( context : RequestContext , guildId : string ) : Promise < GuildSettings | null > {
26
26
let settings : GuildSettings | null = null ;
27
27
if ( GuildSettingsPromisesStore . has ( guildId ) ) {
28
- const promise = GuildSettingsPromisesStore . get ( guildId ) as GuildSettingsPromise ;
28
+ const { promise } = GuildSettingsPromisesStore . get ( guildId ) ! ;
29
29
settings = await promise ;
30
30
} else {
31
31
if ( this . has ( guildId ) ) {
32
- settings = this . get ( guildId ) as GuildSettings ;
32
+ settings = this . get ( guildId ) ! ;
33
33
} else {
34
34
settings = await this . fetch ( context , guildId ) ;
35
35
}
@@ -38,11 +38,17 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
38
38
}
39
39
40
40
async fetch ( context : RequestContext , guildId : string ) : Promise < GuildSettings | null > {
41
- let promise : GuildSettingsPromise ;
41
+ let promise : Promise < GuildSettings | null > ;
42
42
if ( GuildSettingsPromisesStore . has ( guildId ) ) {
43
- promise = GuildSettingsPromisesStore . get ( guildId ) as GuildSettingsPromise ;
43
+ promise = GuildSettingsPromisesStore . get ( guildId ) ! . promise ;
44
44
} else {
45
+ const timeout = new Timers . Timeout ( ) ;
45
46
promise = new Promise ( async ( resolve ) => {
47
+ timeout . start ( 5000 , ( ) => {
48
+ GuildSettingsPromisesStore . delete ( guildId ) ;
49
+ resolve ( null ) ;
50
+ } ) ;
51
+
46
52
const { client } = context ;
47
53
const guild = client . guilds . get ( guildId ) ;
48
54
try {
@@ -60,9 +66,10 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
60
66
} catch ( error ) {
61
67
resolve ( null ) ;
62
68
}
69
+ timeout . stop ( ) ;
63
70
GuildSettingsPromisesStore . delete ( guildId ) ;
64
71
} ) ;
65
- GuildSettingsPromisesStore . set ( guildId , promise ) ;
72
+ GuildSettingsPromisesStore . set ( guildId , { promise, timeout } ) ;
66
73
}
67
74
return promise ;
68
75
}
@@ -74,7 +81,7 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
74
81
const { channel, shard } = event ;
75
82
const { guildId } = channel ;
76
83
if ( guildId && this . has ( guildId ) ) {
77
- const settings = this . get ( guildId ) as GuildSettings ;
84
+ const settings = this . get ( guildId ) ! ;
78
85
{
79
86
const loggers = settings . loggers . filter ( ( logger ) => logger . channelId === channel . id ) ;
80
87
for ( let logger of loggers ) {
@@ -125,7 +132,7 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
125
132
const { channelId, guildId, shard } = event ;
126
133
if ( this . has ( guildId ) ) {
127
134
const channel = shard . channels . get ( channelId ) ;
128
- const settings = this . get ( guildId ) as GuildSettings ;
135
+ const settings = this . get ( guildId ) ! ;
129
136
const loggers = settings . loggers . filter ( ( logger ) => logger . channelId === channelId ) ;
130
137
if ( loggers . length && channel && channel . canManageWebhooks ) {
131
138
try {
@@ -147,7 +154,7 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
147
154
{
148
155
const subscription = redis . subscribe ( RedisChannels . GUILD_ALLOWLIST_UPDATE , ( payload : RedisPayloads . GuildAllowlistUpdate ) => {
149
156
if ( this . has ( payload . id ) ) {
150
- const settings = this . get ( payload . id ) as GuildSettings ;
157
+ const settings = this . get ( payload . id ) ! ;
151
158
settings . merge ( payload ) ;
152
159
}
153
160
} ) ;
@@ -156,7 +163,7 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
156
163
{
157
164
const subscription = redis . subscribe ( RedisChannels . GUILD_BLOCKLIST_UPDATE , ( payload : RedisPayloads . GuildBlocklistUpdate ) => {
158
165
if ( this . has ( payload . id ) ) {
159
- const settings = this . get ( payload . id ) as GuildSettings ;
166
+ const settings = this . get ( payload . id ) ! ;
160
167
settings . merge ( payload ) ;
161
168
}
162
169
} ) ;
@@ -165,7 +172,7 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
165
172
{
166
173
const subscription = redis . subscribe ( RedisChannels . GUILD_DISABLED_COMMAND_UPDATE , ( payload : RedisPayloads . GuildDisabledCommandUpdate ) => {
167
174
if ( this . has ( payload . id ) ) {
168
- const settings = this . get ( payload . id ) as GuildSettings ;
175
+ const settings = this . get ( payload . id ) ! ;
169
176
settings . merge ( payload ) ;
170
177
}
171
178
} ) ;
@@ -174,7 +181,7 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
174
181
{
175
182
const subscription = redis . subscribe ( RedisChannels . GUILD_LOGGER_UPDATE , ( payload : RedisPayloads . GuildLoggerUpdate ) => {
176
183
if ( this . has ( payload . id ) ) {
177
- const settings = this . get ( payload . id ) as GuildSettings ;
184
+ const settings = this . get ( payload . id ) ! ;
178
185
settings . merge ( payload ) ;
179
186
}
180
187
} ) ;
@@ -183,7 +190,7 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
183
190
{
184
191
const subscription = redis . subscribe ( RedisChannels . GUILD_PREFIX_UPDATE , ( payload : RedisPayloads . GuildPrefixUpdate ) => {
185
192
if ( this . has ( payload . id ) ) {
186
- const settings = this . get ( payload . id ) as GuildSettings ;
193
+ const settings = this . get ( payload . id ) ! ;
187
194
settings . merge ( payload ) ;
188
195
}
189
196
} ) ;
@@ -192,7 +199,7 @@ class GuildSettingsStore extends Store<string, GuildSettings> {
192
199
{
193
200
const subscription = redis . subscribe ( RedisChannels . GUILD_SETTINGS_UPDATE , ( payload : RedisPayloads . GuildSettingsUpdate ) => {
194
201
if ( this . has ( payload . id ) ) {
195
- const settings = this . get ( payload . id ) as GuildSettings ;
202
+ const settings = this . get ( payload . id ) ! ;
196
203
settings . merge ( payload ) ;
197
204
}
198
205
} ) ;
@@ -207,11 +214,11 @@ export default new GuildSettingsStore();
207
214
208
215
209
216
210
- export type GuildSettingsPromise = Promise < GuildSettings | null > ;
217
+ export type GuildSettingsPromiseItem = { promise : Promise < GuildSettings | null > , timeout : Timers . Timeout } ;
211
218
212
- class GuildSettingsPromises extends Store < string , GuildSettingsPromise > {
213
- insert ( guildId : string , promise : GuildSettingsPromise ) : void {
214
- this . set ( guildId , promise ) ;
219
+ class GuildSettingsPromises extends Store < string , GuildSettingsPromiseItem > {
220
+ insert ( guildId : string , item : GuildSettingsPromiseItem ) : void {
221
+ this . set ( guildId , item ) ;
215
222
}
216
223
}
217
224
0 commit comments