-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinteraction-handlers.js
727 lines (703 loc) · 36.6 KB
/
interaction-handlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
const { AttachmentBuilder, EmbedBuilder, PermissionsBitField } = require('discord.js');
const globalCache = require('./global-cache');
const mlbAPIUtil = require('./MLB-API-util');
const { joinImages } = require('join-images');
const globals = require('../config/globals');
const commandUtil = require('./command-util');
const queries = require('../database/queries.js');
const { constructPlayEmbed } = require('./gameday');
const examplePlays = require('../spec/data/example-plays');
const exampleLiveFeed = require('../spec/data/example-live-feed');
const liveFeed = require('./livefeed');
const currentPlayProcessor = require('./current-play-processor');
module.exports = {
helpHandler: async (interaction) => {
console.info(`HELP command invoked by guild: ${interaction.guildId}`);
interaction.reply({ content: globals.HELP_MESSAGE, ephemeral: true });
},
startersHandler: async (interaction) => {
console.info(`STARTERS command invoked by guild: ${interaction.guildId}`);
await interaction.deferReply();
// as opposed to other commands, this one will look for the nearest game that is not finished (AKA in "Live" or "Preview" status).
const game = globalCache.values.currentGames.find(game => game.status.abstractGameState !== 'Final');
if (!game) {
await interaction.followUp({
content: 'No game found that isn\'t Final. Is today/tomorrow an off day?',
ephemeral: false
});
return;
}
const matchup = await mlbAPIUtil.matchup(game.gamePk);
const probables = matchup.probables;
const hydratedHomeProbable = await commandUtil.hydrateProbable(probables.homeProbable);
const hydratedAwayProbable = await commandUtil.hydrateProbable(probables.awayProbable);
joinImages([hydratedHomeProbable.spot, hydratedAwayProbable.spot],
{ direction: 'horizontal', offset: 10, margin: 0, color: 'transparent' })
.then(async (img) => {
const attachment = new AttachmentBuilder((await img.png().toBuffer()), { name: 'matchupSpots.png' });
const myEmbed = new EmbedBuilder()
.setTitle('Pitching Matchup - ' + commandUtil.constructGameDisplayString(game))
.setImage('attachment://matchupSpots.png')
.addFields({
name: (hydratedHomeProbable.handedness
? hydratedHomeProbable.handedness + 'HP **'
: '**') + (hydratedHomeProbable.fullName || 'TBD') + '** (' + probables.homeAbbreviation + ')',
value: commandUtil.buildPitchingStatsMarkdown(
hydratedHomeProbable.pitchingStats.season,
hydratedHomeProbable.pitchMix,
hydratedHomeProbable.pitchingStats.lastXGames,
hydratedHomeProbable.pitchingStats.seasonAdvanced,
hydratedHomeProbable.pitchingStats.sabermetrics
),
inline: true
})
.addFields({
name: (hydratedAwayProbable.handedness
? hydratedAwayProbable.handedness + 'HP **'
: '**') + (hydratedAwayProbable.fullName || 'TBD') + '** (' + probables.awayAbbreviation + ')',
value: commandUtil.buildPitchingStatsMarkdown(
hydratedAwayProbable.pitchingStats.season,
hydratedAwayProbable.pitchMix,
hydratedAwayProbable.pitchingStats.lastXGames,
hydratedAwayProbable.pitchingStats.seasonAdvanced,
hydratedAwayProbable.pitchingStats.sabermetrics
),
inline: true
});
await interaction.followUp({
ephemeral: false,
files: [attachment],
embeds: [myEmbed],
components: [],
content: ''
});
});
},
scheduleHandler: async (interaction) => {
console.info(`SCHEDULE command invoked by guild: ${interaction.guildId}`);
const week = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const oneWeek = new Date();
oneWeek.setDate(oneWeek.getDate() + 7);
const nextWeek = await mlbAPIUtil.schedule(
new Date().toISOString().split('T')[0],
(oneWeek).toISOString().split('T')[0]
);
let reply = '';
nextWeek.dates.forEach((date) => {
const game = date.games[0];
const gameDate = new Date(game.gameDate);
const teams = game.teams;
const home = teams.home.team.id === parseInt(process.env.TEAM_ID);
const emoji = globalCache.values.emojis
.find(v => v.name.includes(
(home ? teams.away.team.id : teams.home.team.id)
));
reply += `${week[gameDate.getDay()]} ${date.date.substr(6)}` +
(home ? ' vs. ' : ' @ ') + (home ? teams.away.team.abbreviation : teams.home.team.abbreviation) +
`${emoji ? ` <:${emoji.name}:${emoji.id}>` : ''}` +
' ' +
gameDate.toLocaleString('en-US', {
timeZone: (process.env.TIME_ZONE?.trim() || 'America/New_York'),
hour: 'numeric',
minute: '2-digit',
timeZoneName: 'short'
}) + (game.gameType === 'S' ? ' (Spring Training)' : '') +
'\n';
});
if (reply.length === 0) {
await interaction.reply({
ephemeral: false,
content: 'There are no games in the next week.'
});
} else {
await interaction.reply({
ephemeral: false,
content: reply
});
}
},
standingsHandler: async (interaction) => {
await interaction.deferReply();
console.info(`STANDINGS command invoked by guild: ${interaction.guildId}`);
const team = await mlbAPIUtil.team(process.env.TEAM_ID);
const divisionId = team.teams[0].division.id;
const leagueId = team.teams[0].league.id;
const divisionStandings = (await mlbAPIUtil.standings(leagueId))
.records.find((record) => record.division.id === divisionId);
await interaction.followUp({
ephemeral: false,
files: [new AttachmentBuilder((await commandUtil.buildStandingsTable(divisionStandings, team.teams[0].division.name)), { name: 'standings.png' })]
});
},
wildcardHandler: async (interaction) => {
await interaction.deferReply();
console.info(`WILDCARD command invoked by guild: ${interaction.guildId}`);
const team = await mlbAPIUtil.team(process.env.TEAM_ID);
const leagueId = team.teams[0].league.id;
const leagueName = team.teams[0].league.name;
const wildcard = (await mlbAPIUtil.wildcard()).records
.find(record => record.standingsType === 'wildCard' && record.league === leagueId);
const divisionLeaders = (await mlbAPIUtil.wildcard()).records
.find(record => record.standingsType === 'divisionLeaders' && record.league === leagueId);
if (!divisionLeaders || !wildcard) {
await interaction.followUp({
ephemeral: false,
content: 'Wildcard standings are not available yet for this season.'
});
} else {
await interaction.followUp({
ephemeral: false,
files: [new AttachmentBuilder((await commandUtil.buildWildcardTable(divisionLeaders, wildcard, leagueName)), { name: 'wildcard.png' })]
});
}
},
subscribeGamedayHandler: async (interaction) => {
console.info(`SUBSCRIBE GAMEDAY command invoked by guild: ${interaction.guildId}`);
if (!interaction.member.permissions.has(PermissionsBitField.Flags.ManageMessages)) {
await interaction.reply({
ephemeral: true,
content: 'You do not have permission to subscribe channels to the Gameday feed.'
});
return;
}
const scoringPlaysOnly = interaction.options.getBoolean('scoring_plays_only');
const reportingDelay = interaction.options.getInteger('reporting_delay');
if (interaction.channel) {
await queries.addToSubscribedChannels(
interaction.guild.id,
interaction.channel.id,
scoringPlaysOnly || false,
reportingDelay || 0
).catch(async (e) => {
if (e.message.includes('duplicate key')) {
await interaction.reply({
content: 'This channel is already subscribed to the gameday feed.',
ephemeral: false
});
} else {
await interaction.reply({
content: 'Error subscribing to the gameday feed: ' + e.message,
ephemeral: true
});
}
});
globalCache.values.subscribedChannels = await queries.getAllSubscribedChannels();
} else {
throw new Error('Could not subscribe to the gameday feed.');
}
if (!interaction.replied) {
await interaction.reply({
ephemeral: false,
content: 'Subscribed this channel to the gameday feed.\n' +
'Events: ' + (scoringPlaysOnly ? '**Scoring Plays Only**' : '**All Plays**') + '\n' +
'Reporting Delay: **' + (reportingDelay || 0) + ' seconds**'
});
}
},
testGamedayReportingHandler: async (interaction) => {
console.info(`TEST GAMEDAY REPORTING command invoked by guild: ${interaction.guildId}`);
if (!interaction.member.permissions.has(PermissionsBitField.Flags.ManageMessages)) {
await interaction.reply({
ephemeral: true,
content: 'You do not have permission to use this command.'
});
return;
}
const play = interaction.options.getString('play');
const feed = liveFeed.init(exampleLiveFeed);
if (!interaction.replied) {
await interaction.reply({
ephemeral: true,
embeds: [constructPlayEmbed((() => {
if (play === 'Home Run') {
return currentPlayProcessor.process(
examplePlays.homeRun,
feed,
globalCache.values.emojis.find(e => e.name.includes('angels')),
globalCache.values.emojis.find(e => e.name.includes('brewers'))
);
} else if (play === 'Steal') {
return currentPlayProcessor.process(
examplePlays.steal,
feed,
globalCache.values.emojis.find(e => e.name.includes('angels')),
globalCache.values.emojis.find(e => e.name.includes('brewers'))
);
} else if (play === 'Challenge') {
return currentPlayProcessor.process(
examplePlays.inProgressChallenge,
feed,
globalCache.values.emojis.find(e => e.name.includes('angels')),
globalCache.values.emojis.find(e => e.name.includes('brewers'))
);
}
})(),
feed,
true,
'#BA0021',
'#FFC52F',
globalCache.values.emojis.find(e => e.name.includes('angels')),
globalCache.values.emojis.find(e => e.name.includes('brewers'))
)]
});
}
},
gamedayPreferenceHandler: async (interaction) => {
console.info(`GAMEDAY PREFERENCE command invoked by guild: ${interaction.guildId}`);
if (!interaction.member.permissions.has(PermissionsBitField.Flags.ManageMessages)) {
await interaction.reply({
ephemeral: true,
content: 'You do not have permission to use this command.'
});
return;
}
const scoringPlaysOnly = interaction.options.getBoolean('scoring_plays_only');
const reportingDelay = interaction.options.getInteger('reporting_delay');
if (interaction.channel) {
await queries.updatePlayPreference(
interaction.guild.id,
interaction.channel.id,
scoringPlaysOnly,
reportingDelay
)
.then(async (rows) => {
if (rows.length === 0) {
await interaction.reply({
content: 'This channel isn\'t currently subscribed. Use `/subscribe_gameday` to subscribe and provide a preference.',
ephemeral: false
});
}
})
.catch(async (e) => {
await interaction.reply({
content: 'Error subscribing to the gameday feed: ' + e.message,
ephemeral: true
});
});
globalCache.values.subscribedChannels = await queries.getAllSubscribedChannels();
} else {
throw new Error('Could not update your subscription preference.');
}
if (!interaction.replied) {
await interaction.reply({
ephemeral: false,
content: 'Updated this channel\'s Gameday play reporting preferences:\n' +
'Events: ' + (scoringPlaysOnly ? '**Scoring Plays Only**' : '**All Plays**') + '\n' +
'Reporting Delay: **' + (reportingDelay || 0) + ' seconds**'
});
}
},
unSubscribeGamedayHandler: async (interaction) => {
console.info(`UNSUBSCRIBE GAMEDAY command invoked by guild: ${interaction.guildId}`);
if (!interaction.member.permissions.has(PermissionsBitField.Flags.ManageMessages)) {
await interaction.reply({
ephemeral: true,
content: 'You do not have permission to un-subscribe channels to the Gameday feed.'
});
return;
}
await queries.removeFromSubscribedChannels(interaction.guild.id, interaction.channel.id).catch(async (e) => {
await interaction.reply({ content: 'Error un-subscribing: ' + e.message, ephemeral: true });
});
if (!interaction.replied) {
await interaction.reply({
ephemeral: false,
content: 'This channel is un-subscribed to the Gameday feed. It will no longer receive real-time updates.'
});
}
globalCache.values.subscribedChannels = await queries.getAllSubscribedChannels();
},
linescoreHandler: async (interaction) => {
console.info(`LINESCORE command invoked by guild: ${interaction.guildId}`);
if (!globalCache.values.game.isDoubleHeader) {
await interaction.deferReply();
}
const toHandle = await commandUtil.screenInteraction(interaction);
if (toHandle) {
const game = globalCache.values.game.isDoubleHeader
? globalCache.values.nearestGames.find(game => game.gamePk === parseInt(toHandle.customId)) // the user's choice between the two games of the double-header.
: globalCache.values.nearestGames[0];
const statusCheck = await mlbAPIUtil.statusCheck(game.gamePk);
if (statusCheck.gameData.status.abstractGameState === 'Preview') {
await commandUtil.giveFinalCommandResponse(toHandle, {
ephemeral: false,
content: commandUtil.constructGameDisplayString(game) + ' - the game has not yet started.',
components: []
});
return;
}
const linescore = await mlbAPIUtil.linescore(game.gamePk);
const linescoreAttachment = new AttachmentBuilder(
await commandUtil.buildLineScoreTable(game, linescore)
, { name: 'line_score.png' });
await commandUtil.giveFinalCommandResponse(toHandle, {
ephemeral: false,
content: commandUtil.constructGameDisplayString(game) +
' - **' + (statusCheck.gameData.status.abstractGameState === 'Final'
? 'Final'
: linescore.inningState + ' ' + linescore.currentInningOrdinal) + '**\n\n',
components: [],
files: [linescoreAttachment]
});
}
},
boxScoreHandler: async (interaction) => {
console.info(`BOXSCORE command invoked by guild: ${interaction.guildId}`);
if (!globalCache.values.game.isDoubleHeader) {
await interaction.deferReply();
}
const toHandle = await commandUtil.screenInteraction(interaction);
if (toHandle) {
const game = globalCache.values.game.isDoubleHeader
? globalCache.values.nearestGames.find(game => game.gamePk === parseInt(toHandle.customId)) // the user's choice between the two games of the double-header.
: globalCache.values.nearestGames[0];
const statusCheck = await mlbAPIUtil.statusCheck(game.gamePk);
if (statusCheck.gameData.status.abstractGameState === 'Preview') {
await commandUtil.giveFinalCommandResponse(toHandle, {
ephemeral: false,
content: commandUtil.constructGameDisplayString(game) + ' - the game has not yet started.',
components: []
});
return;
}
const [boxScore, boxScoreNames] = await Promise.all([
mlbAPIUtil.boxScore(game.gamePk),
mlbAPIUtil.liveFeedBoxScoreNamesOnly(game.gamePk)
]);
const boxscoreAttachment = new AttachmentBuilder(
await commandUtil.buildBoxScoreTable(game, boxScore, boxScoreNames, statusCheck.gameData.status.abstractGameState)
, { name: 'boxscore.png' });
const awayAbbreviation = game.teams.away.team?.abbreviation || game.teams.away.abbreviation;
const homeAbbreviation = game.teams.home.team?.abbreviation || game.teams.home.abbreviation;
await commandUtil.giveFinalCommandResponse(toHandle, {
ephemeral: false,
content: homeAbbreviation + ' vs. ' + awayAbbreviation +
', ' + new Date(game.gameDate).toLocaleString('default', {
month: 'short',
day: 'numeric',
timeZone: (process.env.TIME_ZONE?.trim() || 'America/New_York'),
hour: 'numeric',
minute: '2-digit',
timeZoneName: 'short'
}),
components: [],
files: [boxscoreAttachment]
});
}
},
lineupHandler: async (interaction) => {
console.info(`LINEUP command invoked by guild: ${interaction.guildId}`);
if (!globalCache.values.game.isDoubleHeader) {
await interaction.deferReply();
}
const toHandle = await commandUtil.screenInteraction(interaction);
if (toHandle) {
const game = globalCache.values.game.isDoubleHeader
? globalCache.values.nearestGames.find(game => game.gamePk === parseInt(toHandle.customId)) // the user's choice between the two games of the double-header.
: globalCache.values.nearestGames[0];
const gameLineups = (await mlbAPIUtil.lineup(game.gamePk));
let updatedLineup;
/* if a game is postponed and rescheduled, the lineups call returns two games with the same gamePk, one on the original date
and one on the re-scheduled date.
*/
if (gameLineups.dates?.length > 1) {
updatedLineup = gameLineups.dates.find(date => date.games[0].rescheduledFrom)?.games[0];
} else {
updatedLineup = gameLineups.dates[0].games[0];
}
const ourTeamLineup = updatedLineup.teams.home.team.id === parseInt(process.env.TEAM_ID)
? updatedLineup.lineups?.homePlayers
: updatedLineup.lineups?.awayPlayers;
if (!ourTeamLineup) {
await commandUtil.giveFinalCommandResponse(toHandle, {
content: commandUtil.constructGameDisplayString(game) + ' - No lineup card has been submitted for this game yet.',
ephemeral: false,
components: []
});
return;
}
await commandUtil.giveFinalCommandResponse(toHandle, {
ephemeral: false,
content: commandUtil.constructGameDisplayString(game) + '\n',
components: [],
files: [new AttachmentBuilder(await commandUtil.getLineupCardTable(updatedLineup), { name: 'lineup.png' })]
});
}
},
highlightsHandler: async (interaction) => {
console.info(`HIGHLIGHTS command invoked by guild: ${interaction.guildId}`);
if (!globalCache.values.game.isDoubleHeader) {
await interaction.deferReply();
}
const toHandle = await commandUtil.screenInteraction(interaction);
if (toHandle) {
const game = globalCache.values.game.isDoubleHeader
? globalCache.values.nearestGames.find(game => game.gamePk === parseInt(toHandle.customId)) // the user's choice between the two games of the double-header.
: globalCache.values.nearestGames[0];
const statusCheck = await mlbAPIUtil.statusCheck(game.gamePk);
if (statusCheck.gameData.status.abstractGameState === 'Preview') {
await commandUtil.giveFinalCommandResponse(toHandle, {
content: commandUtil.constructGameDisplayString(game) + ' - There are no highlights for this game yet, but here\'s a preview:\n' +
'https://www.mlb.com/stories/game-preview/' + game.gamePk,
ephemeral: false,
components: []
});
return;
}
await commandUtil.giveFinalCommandResponse(toHandle, {
content: '### Highlights: ' + commandUtil.constructGameDisplayString(game) + '\n' + 'https://www.mlb.com/stories/game/' + game.gamePk,
ephemeral: false,
components: []
});
}
},
pitcherHandler: async (interaction) => {
console.info(`PITCHER command invoked by guild: ${interaction.guildId}`);
await interaction.deferReply();
const playerName = interaction.options.getString('player')?.trim();
const statType = interaction.options.getString('stat_type');
const playerResult = await commandUtil.getPlayerFromUserInputOrLiveFeed(playerName, interaction, 'Pitcher');
const pitcher = playerResult.player;
if (!pitcher && !playerName) {
await interaction.followUp('No game is live right now!');
return;
} else if (playerName && !pitcher) {
await interaction.followUp('I didn\'t find a player with a close enough match to your input (use first and last name).');
return;
}
const pitcherInfo = await commandUtil.hydrateProbable(pitcher.id, (statType || 'R'));
const attachment = new AttachmentBuilder(Buffer.from(pitcherInfo.spot), { name: 'spot.png' });
const replyOptions = {
ephemeral: false,
files: [attachment],
embeds: [commandUtil.getPitcherEmbed(
pitcher,
pitcherInfo,
!playerName,
commandUtil.buildPitchingStatsMarkdown(
pitcherInfo.pitchingStats.season,
pitcherInfo.pitchMix,
pitcherInfo.pitchingStats.lastXGames,
pitcherInfo.pitchingStats.seasonAdvanced,
pitcherInfo.pitchingStats.sabermetrics,
true
),
(statType || 'R'))],
components: [],
content: ''
};
await (playerResult.shouldEditReply ? interaction.editReply(replyOptions) : interaction.followUp(replyOptions));
},
batterHandler: async (interaction) => {
console.info(`BATTER command invoked by guild: ${interaction.guildId}`);
await interaction.deferReply();
const playerName = interaction.options.getString('player')?.trim();
const statType = interaction.options.getString('stat_type');
const playerResult = await commandUtil.getPlayerFromUserInputOrLiveFeed(playerName, interaction, 'Batter');
const batter = playerResult.player;
if (!batter && !playerName) {
await interaction.followUp('No game is live right now!');
return;
} else if (playerName && !batter) {
await interaction.followUp('I didn\'t find a player with a close enough match to your input (use first and last name).');
return;
}
const batterInfo = await commandUtil.hydrateHitter(batter.id, (statType || 'R'));
const attachment = new AttachmentBuilder(Buffer.from(batterInfo.spot), { name: 'spot.png' });
const replyOptions = {
ephemeral: false,
files: [attachment],
embeds: [commandUtil.getBatterEmbed(
batter,
batterInfo,
!playerName,
commandUtil.formatSplits(
batterInfo.stats.stats.find(stat => stat.type.displayName === 'season'),
batterInfo.stats.stats.find(stat => stat.type.displayName === 'statSplits'),
batterInfo.stats.stats.find(stat => stat.type.displayName === 'lastXGames'), (statType || 'R')),
(statType || 'R')
)],
components: [],
content: ''
};
await (playerResult.shouldEditReply ? interaction.editReply(replyOptions) : interaction.followUp(replyOptions));
},
batterSavantHandler: async (interaction) => {
console.info(`BATTER SAVANT command invoked by guild: ${interaction.guildId}`);
await interaction.deferReply();
const playerName = interaction.options.getString('player')?.trim();
const playerResult = await commandUtil.getPlayerFromUserInputOrLiveFeed(playerName, interaction, 'Batter');
const batter = playerResult.player;
if (!batter && !playerName) {
await interaction.followUp('No game is live right now!');
return;
} else if (playerName && !batter) {
await interaction.followUp('I didn\'t find a player with a close enough match to your input (use first and last name).');
return;
}
const text = await mlbAPIUtil.savantPage(batter.id, 'hitting');
const statcastData = commandUtil.getStatcastData(text);
if (statcastData.mostRecentStatcast && statcastData.mostRecentMetricYear && statcastData.metricSummaryJSON) {
const batterInfo = await commandUtil.hydrateHitter(batter.id, 'R');
const savantAttachment = new AttachmentBuilder((await commandUtil.buildBatterSavantTable(
statcastData.mostRecentStatcast,
statcastData.metricSummaryJSON[statcastData.mostRecentMetricYear.toString()],
batterInfo.spot)), { name: 'savant.png' });
const replyOptions = {
ephemeral: false,
files: [savantAttachment],
embeds: [commandUtil.getBatterEmbed(batter, batterInfo, !playerName, null, null, true)],
components: [],
content: ''
};
await (playerResult.shouldEditReply ? interaction.editReply(replyOptions) : interaction.followUp(replyOptions));
} else {
await interaction.followUp({
content: 'There was a problem fetching the savant metrics for this player.'
});
}
},
pitcherSavantHandler: async (interaction) => {
console.info(`PITCHER SAVANT command invoked by guild: ${interaction.guildId}`);
await interaction.deferReply();
const playerName = interaction.options.getString('player')?.trim();
const playerResult = await commandUtil.getPlayerFromUserInputOrLiveFeed(playerName, interaction, 'Pitcher');
const pitcher = playerResult.player;
if (!pitcher && !playerName) {
await interaction.followUp('No game is live right now!');
return;
}
const text = await mlbAPIUtil.savantPage(pitcher.id, 'pitching');
const statcastData = commandUtil.getStatcastData(text);
if (statcastData.mostRecentStatcast && statcastData.mostRecentMetricYear && statcastData.metricSummaryJSON) {
const pitcherInfo = await commandUtil.hydrateProbable(pitcher.id, 'R');
const savantAttachment = new AttachmentBuilder((await commandUtil.buildPitcherSavantTable(
statcastData.mostRecentStatcast,
statcastData.metricSummaryJSON[statcastData.mostRecentMetricYear.toString()],
pitcherInfo.spot)), { name: 'savant.png' });
const replyOptions = {
ephemeral: false,
files: [savantAttachment],
embeds: [commandUtil.getPitcherEmbed(pitcher, pitcherInfo, !playerName, null, null, true)],
components: [],
content: ''
};
await (playerResult.shouldEditReply ? interaction.editReply(replyOptions) : interaction.followUp(replyOptions));
} else {
await interaction.followUp({
content: 'There was a problem fetching the savant metrics for this player.'
});
}
},
scoringPlaysHandler: async (interaction) => {
console.info(`SCORING PLAYS command invoked by guild: ${interaction.guildId}`);
if (!globalCache.values.game.isDoubleHeader) {
await interaction.deferReply();
}
const toHandle = await commandUtil.screenInteraction(interaction);
if (toHandle) {
const game = globalCache.values.game.isDoubleHeader
? globalCache.values.nearestGames.find(game => game.gamePk === parseInt(toHandle.customId)) // the user's choice between the two games of the double-header.
: globalCache.values.nearestGames[0];
const liveFeed = await mlbAPIUtil.liveFeed(game.gamePk);
const links = [];
liveFeed.liveData.plays.scoringPlays.forEach((scoringPlayIndex) => {
const play = liveFeed.liveData.plays.allPlays
.find(play => play.about.atBatIndex === scoringPlayIndex);
const link = 'https://www.mlb.com/gameday/' +
liveFeed.gameData.teams.away.teamName.toLowerCase().replaceAll(' ', '-') +
'-vs-' +
liveFeed.gameData.teams.home.teamName.toLowerCase().replaceAll(' ', '-') + '/' +
liveFeed.gameData.datetime.officialDate.replaceAll('-', '/') +
'/' + game.gamePk + '/play/' + scoringPlayIndex;
links.push(commandUtil.getScoreString(liveFeed, play) + ' [' + play.result.description.trim() + '](<' + link + '>)\n');
});
// discord limits messages to 2,000 characters. We very well might need a couple messages to link everything.
const messagesNeeded = Math.ceil(liveFeed.liveData.plays.scoringPlays.length / globals.SCORING_PLAYS_PER_MESSAGE);
if (messagesNeeded > 1) {
for (let i = 0; i < messagesNeeded; i ++) {
const linksForMessage = links.slice(
globals.HIGHLIGHTS_PER_MESSAGE * i,
Math.min((globals.HIGHLIGHTS_PER_MESSAGE * (i + 1)), links.length)
);
if (i === 0) {
await commandUtil.giveFinalCommandResponse(toHandle, {
content: '### Scoring Plays: ' + commandUtil.constructGameDisplayString(game) + '\n' + linksForMessage.join(''),
ephemeral: false,
components: []
});
} else {
await interaction.channel.send('Continued...\n\n' + linksForMessage.join(''));
}
}
} else if (messagesNeeded === 0) {
await commandUtil.giveFinalCommandResponse(toHandle, {
content: commandUtil.constructGameDisplayString(game) + '\nThere are no scoring plays for this game yet.',
ephemeral: false,
components: []
});
} else {
await commandUtil.giveFinalCommandResponse(toHandle, {
content: '### Scoring Plays: ' + commandUtil.constructGameDisplayString(game) + '\n' + links.join(''),
ephemeral: false,
components: []
});
}
}
},
attendanceHandler: async (interaction) => {
console.info(`ATTENDANCE command invoked by guild: ${interaction.guildId}`);
if (!globalCache.values.game.isDoubleHeader) {
await interaction.deferReply();
}
const toHandle = await commandUtil.screenInteraction(interaction);
if (toHandle) {
const game = globalCache.values.game.isDoubleHeader
? globalCache.values.nearestGames.find(game => game.gamePk === parseInt(toHandle.customId)) // the user's choice between the two games of the double-header.
: globalCache.values.nearestGames[0];
const currentLiveFeed = await mlbAPIUtil.liveFeed(game.gamePk, [
'gameData', 'gameInfo', 'attendance', 'venue', 'name', 'fieldInfo', 'capacity'
]);
const attendance = currentLiveFeed.gameData.gameInfo.attendance;
const capacity = currentLiveFeed.gameData.venue.fieldInfo.capacity;
await commandUtil.giveFinalCommandResponse(toHandle, {
ephemeral: false,
files: [],
embeds: [],
components: [],
content: commandUtil.constructGameDisplayString(game) + ': ' + currentLiveFeed.gameData.venue.name + ' attendance: ' +
(attendance && capacity
? attendance.toLocaleString() + ' (' + Math.round((attendance / capacity) * 100) + '% capacity)'
: 'Not Available (yet). This data is usually available around the end of the game.')
});
}
},
weatherHandler: async (interaction) => {
console.info(`WEATHER command invoked by guild: ${interaction.guildId}`);
if (!globalCache.values.game.isDoubleHeader) {
await interaction.deferReply();
}
const toHandle = await commandUtil.screenInteraction(interaction);
if (toHandle) {
const game = globalCache.values.game.isDoubleHeader
? globalCache.values.nearestGames.find(game => game.gamePk === parseInt(toHandle.customId)) // the user's choice between the two games of the double-header.
: globalCache.values.nearestGames[0];
const currentLiveFeed = await mlbAPIUtil.liveFeed(game.gamePk, [
'gameData', 'gameInfo', 'weather', 'condition', 'temp', 'wind', 'venue', 'name'
]);
const weather = currentLiveFeed.gameData.weather;
await commandUtil.giveFinalCommandResponse(toHandle, {
ephemeral: false,
files: [],
embeds: [],
components: [],
content: weather && Object.keys(weather).length > 0
? 'Weather at ' + currentLiveFeed.gameData.venue.name + ':\n' +
commandUtil.getWeatherEmoji(weather.condition) + ' ' + weather.condition + '\n' +
'\uD83C\uDF21 ' + weather.temp + '°\n' +
'\uD83C\uDF43 ' + weather.wind
: 'Not available yet - check back an hour or two before game time.'
});
}
}
};