Skip to content

Commit

Permalink
Merge pull request #31 from AlecM33/savant-player-arg
Browse files Browse the repository at this point in the history
Provide chosen player to /pitcher, /pitcher_savant, /batter, /batter_savant, and also re-use browser instance.
  • Loading branch information
AlecM33 authored Aug 2, 2024
2 parents f650651 + e05bf7e commit 14f4745
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 140 deletions.
6 changes: 5 additions & 1 deletion commands/batter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('batter')
.setDescription('View stats on who is batting right now, including platoon splits.'),
.setDescription('View slash lines and splits for a specified batter, or just who is batting right now.')
.addStringOption(option =>
option.setName('player')
.setDescription('An active player\'s name.')
.setRequired(false)),
async execute (interaction) {
try {
await interactionHandlers.batterHandler(interaction);
Expand Down
6 changes: 5 additions & 1 deletion commands/batter_savant.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('batter_savant')
.setDescription('View the savant metrics for who is at the plate right now.'),
.setDescription('View the savant metrics for a specified player, or whoever is up to bat right now.')
.addStringOption(option =>
option.setName('player')
.setDescription('An active player\'s name.')
.setRequired(false)),
async execute (interaction) {
try {
await interactionHandlers.batterSavantHandler(interaction);
Expand Down
6 changes: 5 additions & 1 deletion commands/pitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('pitcher')
.setDescription('View stats on who is pitching right now.'),
.setDescription('View stats on a specified pitcher, or who is pitching right now.')
.addStringOption(option =>
option.setName('player')
.setDescription('An active player\'s name.')
.setRequired(false)),
async execute (interaction) {
try {
await interactionHandlers.pitcherHandler(interaction);
Expand Down
6 changes: 5 additions & 1 deletion commands/pitcher_savant.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('pitcher_savant')
.setDescription('View the savant metrics for who is on the mound right now.'),
.setDescription('View the savant metrics for a specified pitcher, or who is on the mound right now.')
.addStringOption(option =>
option.setName('player')
.setDescription('An active player\'s name.')
.setRequired(false)),
async execute (interaction) {
try {
await interactionHandlers.pitcherSavantHandler(interaction);
Expand Down
2 changes: 2 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { Client, Collection, GatewayIntentBits } = require('discord.js');
const gameday = require('./modules/gameday');
const globalCache = require("./modules/global-cache");
const queries = require("./database/queries");
const ReusableBrowser = require("./modules/classes/ReusableBrowser");
const { LOG_LEVEL } = require('./config/globals');
const LOGGER = require('./modules/logger')(process.env.LOG_LEVEL?.trim() || LOG_LEVEL.INFO);

Expand All @@ -28,6 +29,7 @@ BOT.once('ready', async () => {
LOGGER.info('Ready!');
globalCache.values.subscribedChannels = await queries.getAllSubscribedChannels();
LOGGER.info('Subscribed channels: ' + JSON.stringify(globalCache.values.subscribedChannels, null, 2));
globalCache.values.browser = new ReusableBrowser();
await gameday.statusPoll(BOT);
});

Expand Down
11 changes: 9 additions & 2 deletions modules/MLB-API-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const endpoints = {
return 'https://statsapi.mlb.com/api/v1/schedule?hydrate=lineups&sportId=1&gamePk=' + gamePk + '&teamId=' + teamId;
},
hitter: (personId) => {
return 'https://statsapi.mlb.com/api/v1/people/' + personId + '/stats?stats=season,statSplits,lastXGames&group=hitting&gameType=R&sitCodes=vl,vr,risp&limit=7';
LOGGER.debug(`https://statsapi.mlb.com/api/v1/people?personIds=${personId}&hydrate=stats(type=[season,statSplits,lastXGames],group=hitting,gameType=R,sitCodes=[vl,vr,risp],limit=7)`);
return `https://statsapi.mlb.com/api/v1/people?personIds=${personId}&hydrate=stats(type=[season,statSplits,lastXGames],group=hitting,gameType=R,sitCodes=[vl,vr,risp],limit=7)`;
},
pitcher: (personId, lastXGamesLimit) => {
return `https://statsapi.mlb.com/api/v1/people?personIds=${personId}&hydrate=stats(type=[season,lastXGames,sabermetrics,seasonAdvanced,expectedStatistics],groups=pitching,limit=${lastXGamesLimit})`;
Expand Down Expand Up @@ -93,6 +94,9 @@ const endpoints = {
},
team: (teamId) => {
return 'https://statsapi.mlb.com/api/v1/teams/' + teamId;
},
players: () => {
return 'https://statsapi.mlb.com/api/v1/sports/1/players?fields=people,fullName,lastName,id,currentTeam,primaryPosition,name,code';
}
};

Expand Down Expand Up @@ -229,7 +233,7 @@ module.exports = {
try {
return (await fetch(endpoints.savantPage(personId, type),
{
signal: AbortSignal.timeout(6000)
signal: AbortSignal.timeout(10000)
}
)).text();
} catch (e) {
Expand All @@ -245,5 +249,8 @@ module.exports = {
},
pitcher: async (personId, lastXGamesLimit) => {
return (await fetch(endpoints.pitcher(personId, lastXGamesLimit))).json();
},
players: async () => {
return (await fetch(endpoints.players())).json();
}
};
35 changes: 35 additions & 0 deletions modules/classes/ReusableBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const puppeteer = require('puppeteer');
const LOGGER = require('../logger')(process.env.LOG_LEVEL?.trim() || globals.LOG_LEVEL.INFO);

class ReusableBrowser {
constructor () {
LOGGER.trace('Launching ReusableBrowser...');
const launchBrowser = async () => {
this.browser = false;
this.browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox'
]
});
this.page = await this.browser.newPage();
this.browser.on('disconnected', launchBrowser);
};

(async () => {
await launchBrowser();
})();
}

getCurrentPage = async () => {
if (this.page && !this.page.isClosed()) {
return this.page;
}
this.page = await this.browser.newPage();
LOGGER.trace('The ReusableBrowser has ' + (await this.browser.pages()).length + ' pages.');
return this.page;
};
}

module.exports = ReusableBrowser;
Loading

0 comments on commit 14f4745

Please sign in to comment.