Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move image out of thumbnail for savant embeds #33

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions modules/command-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ module.exports = {
return {};
},

buildBatterSavantTable: async (statcast, metricSummaries) => {
buildBatterSavantTable: async (statcast, metricSummaries, spot) => {
const value = [
{ label: 'Batting Run Value', value: statcast.swing_take_run_value, metric: 'swing_take_run_value', percentile: statcast.percent_rank_swing_take_run_value },
{ label: 'Baserunning Run Value', value: statcast.runner_run_value, metric: 'runner_run_value', percentile: statcast.percent_rank_runner_run_value },
Expand Down Expand Up @@ -314,6 +314,9 @@ module.exports = {
];
const html = `
<div id='savant-table'>` +
`<img src="data:image/jpeg;base64, ${
Buffer.from(spot).toString('base64')
}" alt="alt text" />` +
'<h3>Value</h3>' +
buildSavantSection(value, metricSummaries) +
'<h3>Hitting</h3>' +
Expand All @@ -327,7 +330,7 @@ module.exports = {
return (await getScreenshotOfSavantTable(html));
},

buildPitcherSavantTable: async (statcast, metricSummaries) => {
buildPitcherSavantTable: async (statcast, metricSummaries, spot) => {
const value = [
{ label: 'Pitching Run Value', value: statcast.swing_take_run_value, metric: 'swing_take_run_value', percentile: statcast.percent_rank_swing_take_run_value },
{ label: 'Fastball Run Value', value: Math.round(statcast.pitch_run_value_fastball), metric: 'pitch_run_value_fastball', percentile: statcast.percent_rank_pitch_run_value_fastball },
Expand All @@ -350,6 +353,9 @@ module.exports = {
];
const html = `
<div id='savant-table'>` +
`<img src="data:image/jpeg;base64, ${
Buffer.from(spot).toString('base64')
}" alt="alt text" />` +
'<h3>Value</h3>' +
buildSavantSection(value, metricSummaries, true) +
'<h3>Pitching</h3>' +
Expand Down Expand Up @@ -505,7 +511,7 @@ module.exports = {
return matchingPlayer;
},

getPitcherEmbed: (pitcher, pitcherInfo, isLiveGame, description) => {
getPitcherEmbed: (pitcher, pitcherInfo, isLiveGame, description, savantMode = false) => {
const feed = liveFeed.init(globalCache.values.game.currentLiveFeed);
if (isLiveGame) {
const abbreviations = {
Expand All @@ -517,36 +523,44 @@ module.exports = {
? abbreviations.home
: abbreviations.away;
const inning = feed.inning();
return new EmbedBuilder()
const embed = new EmbedBuilder()
.setTitle(halfInning.toUpperCase() + ' ' + inning + ', ' +
abbreviations.away + ' vs. ' + abbreviations.home + ': Current Pitcher')
.setDescription('## ' + (pitcherInfo.handedness
? pitcherInfo.handedness + 'HP **'
: '**') + (pitcher.fullName || 'TBD') + '** (' + abbreviation + ')' + (description || ''))
.setThumbnail('attachment://spot.png')
.setImage('attachment://savant.png')
.setColor((halfInning === 'top'
? globalCache.values.game.homeTeamColor
: globalCache.values.game.awayTeamColor)
);

if (!savantMode) {
embed.setThumbnail('attachment://spot.png');
}

return embed;
} else {
const embed = new EmbedBuilder()
.setTitle((pitcherInfo.handedness
? pitcherInfo.handedness + 'HP '
: '') + pitcher.fullName)
.setThumbnail('attachment://spot.png')
.setImage('attachment://savant.png')
.setColor(globals.TEAMS.find(team => team.id === pitcher.currentTeam.id).primaryColor);

if (description) {
embed.setDescription(description);
}

if (!savantMode) {
embed.setThumbnail('attachment://spot.png');
}

return embed;
}
},

getBatterEmbed: (batter, batterInfo, isLiveGame, description) => {
getBatterEmbed: (batter, batterInfo, isLiveGame, description, savantMode = false) => {
const feed = liveFeed.init(globalCache.values.game.currentLiveFeed);
if (isLiveGame) {
const abbreviations = {
Expand All @@ -558,29 +572,37 @@ module.exports = {
? abbreviations.home
: abbreviations.away;
const inning = feed.inning();
return new EmbedBuilder()
const embed = new EmbedBuilder()
.setTitle(halfInning.toUpperCase() + ' ' + inning + ', ' +
abbreviations.away + ' vs. ' + abbreviations.home + ': Current Batter')
.setDescription('## ' + feed.currentBatterBatSide() +
'HB ' + batter.fullName + ' (' + abbreviation + ')' + (description || ''))
.setThumbnail('attachment://spot.png')
.setImage('attachment://savant.png')
.setColor((halfInning === 'top'
? globalCache.values.game.awayTeamColor
: globalCache.values.game.homeTeamColor)
);

if (!savantMode) {
embed.setThumbnail('attachment://spot.png');
}

return embed;
} else {
const embed = new EmbedBuilder()
.setTitle(batterInfo.stats.batSide.code +
'HB ' + batter.fullName)
.setThumbnail('attachment://spot.png')
.setImage('attachment://savant.png')
.setColor(globals.TEAMS.find(team => team.id === batter.currentTeam.id).primaryColor);

if (description) {
embed.setDescription(description);
}

if (!savantMode) {
embed.setThumbnail('attachment://spot.png');
}

return embed;
}
},
Expand Down
16 changes: 8 additions & 8 deletions modules/interaction-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,14 @@ module.exports = {
const statcastData = commandUtil.getStatcastData(text);
if (statcastData.mostRecentStatcast && statcastData.mostRecentMetricYear && statcastData.metricSummaryJSON) {
const batterInfo = await commandUtil.hydrateHitter(batter.id);
const attachment = new AttachmentBuilder(Buffer.from(batterInfo.spot), { name: 'spot.png' });
const savantAttachment = new AttachmentBuilder((await commandUtil.buildBatterSavantTable(
statcastData.mostRecentStatcast,
statcastData.metricSummaryJSON[statcastData.mostRecentMetricYear.toString()])), { name: 'savant.png' });
statcastData.metricSummaryJSON[statcastData.mostRecentMetricYear.toString()],
batterInfo.spot)), { name: 'savant.png' });
await interaction.followUp({
ephemeral: false,
files: [attachment, savantAttachment],
embeds: [commandUtil.getBatterEmbed(batter, batterInfo, !playerName)],
files: [savantAttachment],
embeds: [commandUtil.getBatterEmbed(batter, batterInfo, !playerName, null, true)],
components: [],
content: ''
});
Expand All @@ -477,14 +477,14 @@ module.exports = {
const statcastData = commandUtil.getStatcastData(text);
if (statcastData.mostRecentStatcast && statcastData.mostRecentMetricYear && statcastData.metricSummaryJSON) {
const pitcherInfo = await commandUtil.hydrateProbable(pitcher.id);
const attachment = new AttachmentBuilder(Buffer.from(pitcherInfo.spot), { name: 'spot.png' });
const savantAttachment = new AttachmentBuilder((await commandUtil.buildPitcherSavantTable(
statcastData.mostRecentStatcast,
statcastData.metricSummaryJSON[statcastData.mostRecentMetricYear.toString()])), { name: 'savant.png' });
statcastData.metricSummaryJSON[statcastData.mostRecentMetricYear.toString()],
pitcherInfo.spot)), { name: 'savant.png' });
await interaction.followUp({
ephemeral: false,
files: [attachment, savantAttachment],
embeds: [commandUtil.getPitcherEmbed(pitcher, pitcherInfo, !playerName)],
files: [savantAttachment],
embeds: [commandUtil.getPitcherEmbed(pitcher, pitcherInfo, !playerName, null, true)],
components: [],
content: ''
});
Expand Down
Loading