1
1
import 'dotenv/config' ;
2
2
import fetch from 'node-fetch' ;
3
+ import { CronJob } from 'cron' ;
3
4
import { Client , GatewayIntentBits } from 'discord.js' ;
4
5
import { getProgressStats , getProgressList , getRanking , getDefaultLink , generateNewCodeShare } from './utils.js' ;
5
6
@@ -160,6 +161,9 @@ client.on('interactionCreate', async interaction => {
160
161
const rankingDesc = "Take a look where you stand in comparison to other users using this bot.\n\n" +
161
162
"When using this you will be presented with a choice of which difficulty leaderboard to view." ;
162
163
164
+ const codeshareDesc = "Creates a codeshare link for multiple users to join.\n" +
165
+ "Codeshare links expire 24 hours after creation, so Leetcode Tracker will also delete the message after 24 hours." ;
166
+
163
167
return interaction . reply ( {
164
168
embeds : [
165
169
{
@@ -187,6 +191,10 @@ client.on('interactionCreate', async interaction => {
187
191
{
188
192
"name" : `➡️ __**/ranking**__` ,
189
193
"value" : rankingDesc
194
+ } ,
195
+ {
196
+ "name" : `➡️ __**/codeshare**__` ,
197
+ "value" : codeshareDesc
190
198
}
191
199
]
192
200
}
@@ -200,8 +208,10 @@ client.on('interactionCreate', async interaction => {
200
208
201
209
const codeShareLink = await generateNewCodeShare ( ) ;
202
210
211
+ const expireTime = new Date ( Date . now ( ) + 24 * 60 * 60 * 1000 ) ;
212
+
203
213
interaction . editReply ( {
204
- content : " Here is a new codeshare link" ,
214
+ content : ` Here is a new codeshare link. It will expire in 24 hours ( ${ expireTime . toLocaleString ( ) } PST)` ,
205
215
components : [
206
216
{
207
217
"type" : 1 ,
@@ -217,6 +227,15 @@ client.on('interactionCreate', async interaction => {
217
227
}
218
228
] ,
219
229
fetchReply : true
230
+ } ) . then ( async msg => {
231
+ // Schedule a cron job to delete the reply after 24 hours
232
+ const job = new CronJob ( expireTime , function ( ) {
233
+ msg . delete ( )
234
+ . then ( ( ) => console . log ( `Codeshare link deleted successfully: ${ codeShareLink } ` ) )
235
+ . catch ( console . error ) ;
236
+ } ) ;
237
+
238
+ job . start ( ) ;
220
239
} ) ;
221
240
}
222
241
} ) ;
0 commit comments