@@ -7,7 +7,7 @@ import { statsCommand } from "./commands/stats.ts";
7
7
import { registrationWizard } from "./scenes/registration.ts" ;
8
8
import { getUserLeetcodeInfo } from "../modules/leetcode.ts" ;
9
9
import { User } from "../types/index.ts" ;
10
- import { Update } from "npm:telegraf/types" ;
10
+ import { CallbackQuery , Update } from "npm:telegraf/types" ;
11
11
12
12
export class LeetCodeBot {
13
13
private bot : Telegraf ;
@@ -28,6 +28,9 @@ export class LeetCodeBot {
28
28
29
29
// Set up error handling
30
30
this . setupErrorHandling ( ) ;
31
+
32
+ // Setup checker
33
+ this . setupChecker ( ) ;
31
34
}
32
35
33
36
private setupMiddleware ( ) {
@@ -48,65 +51,27 @@ export class LeetCodeBot {
48
51
}
49
52
50
53
private registerCommands ( ) {
51
- this . bot . command ( 'start' , ( ctx ) => ( ctx as never as Scenes . SceneContext ) . scene . enter ( 'registration-wizard' ) ) ;
52
- this . bot . command ( 'help' , helpCommand ) ;
54
+ this . bot . command ( 'start' , ( ctx ) => ( ctx as unknown as Scenes . SceneContext ) . scene . enter ( 'registration-wizard' ) ) ;
53
55
54
- this . bot . command ( 'stats' , statsCommand ) ;
56
+ // TODO add skip command
57
+ // TODO add reschedule logic
55
58
56
- // Schedule command
57
- this . bot . command ( 'schedule' , async ( ctx ) => {
58
- const userId = ctx . from ?. id ;
59
- if ( ! userId ) return ;
60
-
61
- try {
62
- await ctx . reply (
63
- "How often would you like to solve problems?" ,
64
- {
65
- reply_markup : {
66
- inline_keyboard : [
67
- [
68
- { text : "Daily" , callback_data : "schedule_1" } ,
69
- { text : "Every 2 days" , callback_data : "schedule_2" }
70
- ] ,
71
- [
72
- { text : "Every 3 days" , callback_data : "schedule_3" } ,
73
- { text : "Weekly" , callback_data : "schedule_7" }
74
- ]
75
- ]
76
- }
77
- }
78
- ) ;
79
- } catch ( error ) {
80
- console . error ( 'Error in schedule command:' , error ) ;
81
- await ctx . reply ( 'Sorry, something went wrong. Please try again.' ) ;
82
- }
83
- } ) ;
59
+ this . bot . command ( 'help' , helpCommand ) ;
60
+ this . bot . command ( 'stats' , statsCommand ) ;
84
61
85
62
this . bot . on ( "callback_query" , async ( ctx ) => {
86
- const command = ( ctx . callbackQuery as any ) . data ;
87
- console . log ( command )
63
+ const command = ( ctx . callbackQuery as unknown as CallbackQuery . DataQuery ) . data ;
88
64
89
- // Acknowledge the callback to remove the "loading" spinner.
90
65
await ctx . answerCbQuery ( ) ;
91
66
92
- // Check if command exists, then call the appropriate command function.
93
67
switch ( command ) {
94
68
case "/start" :
95
- // await startCommand (ctx);
69
+ await ( ctx as never as Scenes . SceneContext ) . scene . enter ( 'registration-wizard' ) ;
96
70
break ;
97
71
case "/help" :
98
72
await helpCommand ( ctx ) ;
99
73
break ;
100
- // Add more cases for each command you support.
101
- // For example:
102
- // case "/status":
103
- // await statusCommand(ctx);
104
- // break;
105
- // case "/schedule":
106
- // await scheduleCommand(ctx);
107
- // break;
108
74
default :
109
- // Inform the user if the command is unknown.
110
75
await ctx . reply ( "Unknown command. Please use /help to see available commands." ) ;
111
76
}
112
77
} ) ;
@@ -120,27 +85,17 @@ export class LeetCodeBot {
120
85
}
121
86
122
87
private setupChecker ( ) {
123
- const DAY = 24 * 60 * 60 * 1000 ;
88
+ const HOUR = 60 * 60 * 1000 ;
124
89
125
90
setInterval ( async ( ) => {
126
- const users = await this . userRepo . collection . find ( { frequency : 1 } ) . toArray ( ) ;
127
-
128
- for ( const user of users ) {
129
- await this . checkUser ( user ) ;
130
- }
131
- } , DAY ) ;
132
-
133
- setInterval ( async ( ) => {
134
- const users = await this . userRepo . collection . find ( { frequency : 7 } ) . toArray ( ) ;
135
-
136
- for ( const user of users ) {
137
- await this . checkUser ( user ) ;
138
- }
139
- } , DAY ) ;
91
+ const users = await this . userRepo . collection . find ( ) . toArray ( ) ;
92
+ for ( const user of users ) await this . checkUser ( user ) ;
93
+ } , HOUR ) ;
140
94
}
141
95
142
96
private async checkUser ( user : User ) {
143
97
try {
98
+ if ( user . nextDueDate && Date . now ( ) < user . nextDueDate . getTime ( ) ) return ;
144
99
145
100
const updatedInfo = await getUserLeetcodeInfo ( user . leetcodeUsername ) . catch ( ( ) => null ) ;
146
101
if ( ! updatedInfo ) return ; // Optionally handle later
@@ -178,15 +133,14 @@ export class LeetCodeBot {
178
133
) ;
179
134
}
180
135
181
-
182
136
await this . userRepo . update ( user . telegramId , {
183
137
stats : {
184
138
totalSolved : allSubmissions . count ,
185
139
streakCount : user . stats . streakCount + 1
186
140
} ,
187
- updatedAt : new Date ( )
141
+ updatedAt : new Date ( ) ,
142
+ nextDueDate : new Date ( Date . now ( ) + ( user . frequency * 24 * 60 * 60_000 ) )
188
143
} )
189
-
190
144
} catch ( error ) {
191
145
console . error ( "Error during daily check:" , error ) ;
192
146
}
0 commit comments