@@ -172,6 +172,8 @@ export async function getQuestionsByType(
172172 customWwydQuestions : [ ] ,
173173 wyrQuestions : [ ] ,
174174 customWyrQuestions : [ ] ,
175+ topicQuestions : [ ] ,
176+ customTopicQuestions : [ ] ,
175177 } ) ;
176178
177179 usedQuestions = await usedQuestionModel . find ( {
@@ -186,28 +188,38 @@ export async function getQuestionsByType(
186188 ] ) ;
187189 }
188190
189- let questionDatabase = await getDBQuestion (
190- premium && enabled ? usedQuestions [ 0 ] [ typeCheck [ type ] ] : [ ]
191- ) ;
192-
193- if ( ! questionDatabase [ 0 ] ?. id && premium && enabled ) {
194- await reset ( type as Quest , guildDb . customTypes , guildDb . guildID , "db" ) ;
195- questionDatabase = await getDBQuestion ( [ ] ) ;
191+ interface QuestionData {
192+ id : string ;
193+ question : string ;
194+ translations ?: { [ key : string ] : string } ;
195+ type ?: string ;
196196 }
197197
198198 async function getRandomCustom ( nin : string [ ] ) {
199- return await GuildModel . aggregate ( [
199+ const customCount = await GuildModel . aggregate ( [
200200 { $match : { guildID : guildDb . guildID } } ,
201201 { $unwind : "$customMessages" } ,
202202 {
203203 $match : {
204- "customMessages.id" : {
205- $nin : nin ,
206- } ,
207204 "customMessages.type" : type === "whatwouldyoudo" ? "wwyd" : type ,
208205 } ,
209206 } ,
210- { $sample : { size : 1 } } ,
207+ { $count : "total" } ,
208+ ] ) ;
209+
210+ if ( ! customCount [ 0 ] ?. total ) {
211+ return null ;
212+ }
213+
214+ const availableCustomQuestions = await GuildModel . aggregate ( [
215+ { $match : { guildID : guildDb . guildID } } ,
216+ { $unwind : "$customMessages" } ,
217+ {
218+ $match : {
219+ "customMessages.type" : type === "whatwouldyoudo" ? "wwyd" : type ,
220+ "customMessages.id" : { $nin : nin } ,
221+ } ,
222+ } ,
211223 {
212224 $project : {
213225 id : "$customMessages.id" ,
@@ -216,20 +228,96 @@ export async function getQuestionsByType(
216228 } ,
217229 } ,
218230 ] ) ;
231+
232+ if ( availableCustomQuestions . length === 0 ) {
233+ await reset ( type as Quest , "custom" , guildDb . guildID , "custom" ) ;
234+
235+ const allCustomQuestions = await GuildModel . aggregate ( [
236+ { $match : { guildID : guildDb . guildID } } ,
237+ { $unwind : "$customMessages" } ,
238+ {
239+ $match : {
240+ "customMessages.type" : type === "whatwouldyoudo" ? "wwyd" : type ,
241+ } ,
242+ } ,
243+ {
244+ $project : {
245+ id : "$customMessages.id" ,
246+ question : "$customMessages.question" ,
247+ type : "$customMessages.type" ,
248+ } ,
249+ } ,
250+ ] ) ;
251+
252+ const randomIndex = Math . floor (
253+ Math . random ( ) * allCustomQuestions . length
254+ ) ;
255+ return [ allCustomQuestions [ randomIndex ] ] ;
256+ }
257+
258+ const randomIndex = Math . floor (
259+ Math . random ( ) * availableCustomQuestions . length
260+ ) ;
261+ return [ availableCustomQuestions [ randomIndex ] ] ;
262+ }
263+
264+ const hasCustomQuestions = await GuildModel . aggregate ( [
265+ { $match : { guildID : guildDb . guildID } } ,
266+ { $unwind : "$customMessages" } ,
267+ {
268+ $match : {
269+ "customMessages.type" : type === "whatwouldyoudo" ? "wwyd" : type ,
270+ } ,
271+ } ,
272+ { $count : "total" } ,
273+ ] ) ;
274+
275+ if ( hasCustomQuestions [ 0 ] ?. total > 0 ) {
276+ const customQuestion = await getRandomCustom (
277+ premium && enabled
278+ ? usedQuestions [ 0 ] ?. [ typeCheck [ `custom${ type } ` ] ] || [ ]
279+ : [ ]
280+ ) ;
281+
282+ if ( ! customQuestion ?. [ 0 ] ) {
283+ return Promise . reject ( "Error getting custom question" ) ;
284+ }
285+
286+ if ( premium && enabled ) {
287+ await usedQuestionModel . updateOne (
288+ { guildID : guildDb . guildID } ,
289+ { $push : { [ typeCheck [ `custom${ type } ` ] ] : customQuestion [ 0 ] . id } }
290+ ) ;
291+ }
292+
293+ return {
294+ id : customQuestion [ 0 ] . id ,
295+ question : customQuestion [ 0 ] . question ,
296+ } ;
219297 }
220298
221- let newRandomCustomQuestion = await getRandomCustom (
222- premium && enabled ? usedQuestions [ 0 ] [ typeCheck [ `custom${ type } ` ] ] : [ ]
299+ // Only get normal questions if there are no custom questions configured
300+ let questionDatabase = await getDBQuestion (
301+ premium && enabled ? usedQuestions [ 0 ] ?. [ typeCheck [ type ] ] || [ ] : [ ]
223302 ) ;
224303
225- if ( ! newRandomCustomQuestion [ 0 ] ?. id && premium && enabled ) {
226- await reset (
227- type as Quest ,
228- guildDb . customTypes ,
229- guildDb . guildID ,
230- "custom"
304+ if ( ! questionDatabase [ 0 ] ?. id && premium && enabled ) {
305+ await reset ( type as Quest , "regular" , guildDb . guildID , "db" ) ;
306+ questionDatabase = await getDBQuestion ( [ ] ) ;
307+ }
308+
309+ if ( ! questionDatabase [ 0 ] ) {
310+ return Promise . reject ( "No questions available" ) ;
311+ }
312+
313+ const dbQuestion = questionDatabase [ 0 ] as QuestionData ;
314+
315+ // Track the used normal question for premium users
316+ if ( premium && enabled ) {
317+ await usedQuestionModel . updateOne (
318+ { guildID : guildDb . guildID } ,
319+ { $push : { [ typeCheck [ type ] ] : dbQuestion . id } }
231320 ) ;
232- newRandomCustomQuestion = await getRandomCustom ( [ ] ) ;
233321 }
234322
235323 let types =
@@ -243,56 +331,46 @@ export async function getQuestionsByType(
243331 switch ( types ) {
244332 case "regular" :
245333 result = {
246- id : questionDatabase [ 0 ] . id ,
334+ id : dbQuestion . id ,
247335 question :
248336 normalizedLanguage === "en_EN"
249- ? questionDatabase [ 0 ] . question
250- : questionDatabase [ 0 ] . translations [ normalizedLanguage ] ,
337+ ? dbQuestion . question
338+ : dbQuestion . translations ?. [ normalizedLanguage ] ||
339+ dbQuestion . question ,
251340 } ;
252-
253341 break ;
254342 case "mixed" : {
255- const mixedQuestions = shuffle ( [
256- ...questionDatabase . concat ( newRandomCustomQuestion [ 0 ] ) ,
257- ] ) ;
343+ const availableQuestions = questionDatabase . map (
344+ ( q : any ) => ( { ...q } ) as QuestionData
345+ ) ;
346+ const mixedQuestions = shuffle ( availableQuestions ) ;
347+ const question = mixedQuestions [ 0 ] as QuestionData ;
348+
349+ if ( ! question ) {
350+ return Promise . reject ( "No questions available" ) ;
351+ }
258352
259- const question = mixedQuestions [ 0 ]
260- ? mixedQuestions [ 0 ]
261- : mixedQuestions [ 1 ] ;
262-
263353 result = {
264- id : question ? .id ,
354+ id : question . id ,
265355 question :
266356 normalizedLanguage === "en_EN"
267- ? question ? .question
268- : question ? .translations ?. [ normalizedLanguage ] ||
269- question ? .question ,
357+ ? question . question
358+ : question . translations ?. [ normalizedLanguage ] ||
359+ question . question ,
270360 } ;
271361 break ;
272362 }
273363 case "custom" :
274364 result = {
275- id : newRandomCustomQuestion [ 0 ] . id ,
276- question : newRandomCustomQuestion [ 0 ] . question ,
365+ id : dbQuestion . id ,
366+ question :
367+ normalizedLanguage === "en_EN"
368+ ? dbQuestion . question
369+ : dbQuestion . translations ?. [ normalizedLanguage ] ||
370+ dbQuestion . question ,
277371 } ;
278372 break ;
279373 }
280-
281- if ( premium && enabled ) {
282- if ( types === "custom" ) {
283- selectedModel = typeCheck [ `custom${ type } ` ] ;
284- } else if ( types === "mixed" ) {
285- if ( result . id === questionDatabase [ 0 ] . id )
286- selectedModel = typeCheck [ type ] ;
287- else selectedModel = typeCheck [ `custom${ type } ` ] ;
288- } else {
289- selectedModel = typeCheck [ type ] ;
290- }
291- await usedQuestionModel . updateOne (
292- { guildID : guildDb . guildID } ,
293- { $push : { [ selectedModel ] : result . id } }
294- ) ;
295- }
296374 } else {
297375 const questionDatabase = await selectedModel . aggregate ( [
298376 { $sample : { size : 1 } } ,
0 commit comments