@@ -77,22 +77,48 @@ export const data = new SlashCommandBuilder()
77
77
. setName ( 'mensa' )
78
78
. setDescription ( 'Was es wohl heute zu Essen gibt?' )
79
79
. addStringOption ( option =>
80
- option
81
- . setName ( 'wochentag' )
82
- . setDescription ( 'Der Wochentag, der angezeigt werden soll.' )
83
- . addChoices ( weekday_choices )
84
- . setRequired ( true ) ,
80
+ option . setName ( 'wochentag' ) . setDescription ( 'Der Wochentag, der angezeigt werden soll.' ) . addChoices ( weekday_choices ) ,
85
81
)
86
82
. addStringOption ( option =>
87
- option
88
- . setName ( 'ort' )
89
- . setDescription ( 'Die Mensa, die angezeigt werden soll.' )
90
- . addChoices ( line_choices )
91
- . setRequired ( true ) ,
83
+ option . setName ( 'ort' ) . setDescription ( 'Die Mensa, die angezeigt werden soll.' ) . addChoices ( line_choices ) ,
92
84
)
93
85
94
86
exports . Command = async ( client : DiscordClient , interaction : DiscordCommandInteraction ) : Promise < void > => {
95
- await mensa ( client , interaction , interaction . options . getString ( 'wochentag' ) , interaction . options . getString ( 'ort' ) )
87
+ const today = new Date ( )
88
+ const weekday = interaction . options . getString ( 'wochentag' )
89
+ ? interaction . options . getString ( 'wochentag' )
90
+ : today . getHours ( ) >= 16
91
+ ? getWeekday ( today . getDay ( ) )
92
+ : getWeekday ( today . getDay ( ) - 1 )
93
+
94
+ const line = interaction . options . getString ( 'ort' ) ? interaction . options . getString ( 'ort' ) : 'adenauerring'
95
+ interaction . reply ( { embeds : [ await mensa ( client , weekday , line , interaction ) ] } )
96
+ }
97
+
98
+ /**
99
+ * Returns the correct shortend weekday based on {@link index}
100
+ * @param {number } index index of Weekday
101
+ * @returns {string } weekday
102
+ */
103
+ export function getWeekday ( index : number ) : string {
104
+ switch ( index ) {
105
+ case 0 :
106
+ return 'mo'
107
+ case 1 :
108
+ return 'di'
109
+ case 2 :
110
+ return 'mi'
111
+ case 3 :
112
+ return 'do'
113
+ case 4 :
114
+ return 'fr'
115
+ case 5 :
116
+ return 'sa'
117
+ case 6 :
118
+ return 'so'
119
+ default :
120
+ return 'mo'
121
+ }
96
122
}
97
123
98
124
class FoodLine {
@@ -169,7 +195,7 @@ const weekdayOptions = {
169
195
so : new Weekday ( 'Sonntag' , 6 ) ,
170
196
}
171
197
172
- function _updateJson ( client : DiscordClient , interaction : DiscordCommandInteraction ) : Promise < string > {
198
+ function _updateJson ( client : DiscordClient ) : Promise < string > {
173
199
return new Promise ( ( resolve , reject ) => {
174
200
/**
175
201
* Fancy API stuff and user credential hashing
@@ -197,7 +223,6 @@ function _updateJson(client: DiscordClient, interaction: DiscordCommandInteracti
197
223
/**
198
224
* TODO: valid error handling
199
225
*/
200
- interaction . reply ( `Error, sadface\n${ error } ` )
201
226
reject ( error )
202
227
} )
203
228
res . on ( 'end' , ( ) => {
@@ -209,7 +234,6 @@ function _updateJson(client: DiscordClient, interaction: DiscordCommandInteracti
209
234
/**
210
235
* TODO: valid error handling
211
236
*/
212
- interaction . followUp ( 'Sadface' )
213
237
reject ( err )
214
238
}
215
239
resolve ( body )
@@ -219,18 +243,25 @@ function _updateJson(client: DiscordClient, interaction: DiscordCommandInteracti
219
243
} )
220
244
}
221
245
222
- async function mensa ( client , interaction , req_weekday , req_mensa ) {
246
+ export async function mensa (
247
+ client : DiscordClient ,
248
+ req_weekday ,
249
+ req_mensa ,
250
+ interaction : DiscordCommandInteraction | null ,
251
+ ) : Promise < MessageEmbed > {
223
252
/**
224
253
* Mensa embed
225
254
*/
226
255
const embed = new MessageEmbed ( ) . setColor ( '#FAD51B' ) . setAuthor ( { name : '🍽️ Mensaplan' } )
227
256
257
+ const embed_language = interaction ?. user ?. language ?? 'de'
258
+
228
259
let raw_mensa , mensa_json
229
260
if ( ( await fs . promises . readFile ( `data/mensa.json` ) ) . toString ( ) . length === 0 ) {
230
261
/**
231
262
* Fetch new mensa plan if none found
232
263
*/
233
- const buffer = await _updateJson ( client , interaction )
264
+ const buffer = await _updateJson ( client )
234
265
if ( buffer ) mensa_json = JSON . parse ( buffer )
235
266
} else {
236
267
/**
@@ -259,26 +290,29 @@ async function mensa(client, interaction, req_weekday, req_mensa) {
259
290
260
291
if ( currentDate + 7 * 86400000 > lastDate ) {
261
292
// 7 * 86400 : number of seconds in one week
262
- embed . setDescription ( ':fork_knife_plate: Aktualisiere JSON...' )
263
-
264
- interaction . channel . send ( {
265
- embeds : [ embed ] ,
266
- } )
267
-
268
- const buffer = await _updateJson ( client , interaction )
293
+ embed . setDescription (
294
+ client . translate ( {
295
+ key : 'interactions.mensa.refreshJSON' ,
296
+ lng : embed_language ,
297
+ } ) ,
298
+ )
299
+
300
+ const buffer = await _updateJson ( client )
269
301
if ( buffer ) mensa_json = JSON . parse ( buffer )
270
302
271
303
mensa_json = await fs . promises . readFile ( `data/mensa.json` )
272
304
}
273
305
274
306
if ( Object . keys ( mensa_json ) . indexOf ( req_mensa ) === - 1 ) {
275
307
embed
276
- . setTitle ( `Mensa ${ mensaOptions [ req_mensa ] . name } ` )
277
- . setDescription ( 'Diese Mensa hat am angeforderten Tag leider geschlossen.' )
278
-
279
- interaction . reply ( {
280
- embeds : [ embed ] ,
281
- } )
308
+ . setTitle (
309
+ `${ client . translate ( { key : 'interactions.mensa.cafeteria' , lng : embed_language } ) } ${
310
+ mensaOptions [ req_mensa ] . name
311
+ } `,
312
+ )
313
+ . setDescription ( client . translate ( { key : 'interactions.mensa.lineClosed' , lng : embed_language } ) )
314
+
315
+ return embed
282
316
}
283
317
284
318
for ( const timestampKey in Object . keys ( mensa_json [ req_mensa ] ) ) {
@@ -297,7 +331,11 @@ async function mensa(client, interaction, req_weekday, req_mensa) {
297
331
const date = new Date ( dayString )
298
332
299
333
embed
300
- . setTitle ( `Mensa ${ mensaOptions [ req_mensa ] . name } ` )
334
+ . setTitle (
335
+ `${ client . translate ( { key : 'interactions.mensa.cafeteria' , lng : embed_language } ) } ${
336
+ mensaOptions [ req_mensa ] . name
337
+ } `,
338
+ )
301
339
. setDescription (
302
340
`${ date . toLocaleDateString ( 'de-DE' , { weekday : 'long' , year : 'numeric' , month : 'numeric' , day : 'numeric' } ) } ` ,
303
341
)
@@ -311,13 +349,16 @@ async function mensa(client, interaction, req_weekday, req_mensa) {
311
349
312
350
// eslint-disable-next-line max-depth
313
351
if ( foodLineData . nodata ) {
314
- mealValues = '__Leider gibt es für diesen Tag hier keine Informationen!__'
352
+ mealValues = client . translate ( { key : 'interactions.mensa.noInformation' , lng : embed_language } )
315
353
break
316
354
}
317
355
318
356
// eslint-disable-next-line max-depth
319
357
if ( foodLineData . closing_start ) {
320
- mealValues = `__Leider ist hier heute geschlossen. Grund: ${ foodLineData . closing_text } __`
358
+ mealValues = client . translate ( {
359
+ key : 'interactions.mensa.closed' ,
360
+ options : { reason : foodLineData . closing_text , lng : embed_language } ,
361
+ } )
321
362
break
322
363
}
323
364
@@ -327,9 +368,15 @@ async function mensa(client, interaction, req_weekday, req_mensa) {
327
368
328
369
mealValues += [ '' , '.' ] . indexOf ( dish ) === - 1 ? `${ meal } ${ dish } \n` : meal
329
370
330
- const allAdditions = foodLineData . add . join ( ', ' )
371
+ const allAdditives = foodLineData . add . join ( ', ' )
331
372
332
- mealValues += allAdditions !== '' ? `_Zusatz: [${ allAdditions } ]_` : '_Keine Zusätze_'
373
+ mealValues +=
374
+ allAdditives !== ''
375
+ ? client . translate ( {
376
+ key : 'interactions.mensa.foodAdditives' ,
377
+ options : { additives : allAdditives , lng : embed_language } ,
378
+ } )
379
+ : client . translate ( { key : 'interactions.mensa.noFoodAdditives' , lng : embed_language } )
333
380
334
381
const foodContainsStringToEmoji = {
335
382
bio : ':earth_africa:' ,
@@ -368,11 +415,15 @@ async function mensa(client, interaction, req_weekday, req_mensa) {
368
415
369
416
embed . addFields ( {
370
417
name : '⠀' ,
371
- value : `Eine Liste aller Zusätze findest du [hier](${
372
- client . config . mensa . base_url + client . config . mensa . additional_info
373
- } ).`,
418
+ value : client . translate ( {
419
+ key : 'interactions.mensa.allAdditivesList' ,
420
+ options : {
421
+ link : client . config . mensa . base_url + client . config . mensa . additional_info ,
422
+ lng : embed_language ,
423
+ } ,
424
+ } ) ,
374
425
inline : false ,
375
426
} )
376
427
377
- interaction . reply ( { embeds : [ embed ] } )
428
+ return embed
378
429
}
0 commit comments