Example #1
0
    async def mlb(self, *team: str):
        """Get MLB info

        Help is now too long for a discord message. Check the github link for full help.

        Supported sub commands:
        blank
        <team>
        <division>
        standings <division>
        [l]sp <team>
        line <player>
        ohtani
        <part> <team>
        linescore <team>

            each of the previous commands can end in a number of (+days or -days) to change the date

        dl <team>
        batters <team>
        pitchers <team>

        last [n] <team>
        next [n] <team>

        [h][c][b,p]stats <player> [year] [year2]
        splits <split> <player> [year]
        vs <team> <player>
        [b]last <player> [n]
        [b]log [n] <player>
        [p,f]leaders <stat>

        highlight <query>

        help - https://github.com/efitz11/natsgifbot/blob/master/mlbhelp.txt
        """
        delta = None

        team = ["wsh" if x.lower() == "nats" else x for x in team]

        reddit = False
        for i in team:
            if i.lower() == 'reddit':
                reddit = True
                team.remove(i)

        if len(team) > 0 and (team[-1].startswith('-')
                              or team[-1].startswith('+')):
            delta = team[-1]
            team = team[:-1]
        elif len(team) > 0 and len(team[-1].split('/')) in [2, 3]:
            if team[-1].split('/')[0].isdigit():
                delta = self.convert_date_to_delta(team)
                team = team[:-1]
        elif len(team) > 0 and team[-1].lower() in ['yesterday', 'tomorrow']:
            if team[-1] == 'yesterday':
                delta = "-1"
            elif team[-1] == 'tomorrow':
                delta = "+1"
            team = team[:-1]

        if len(team) == 0 or (len(team) == 1 and team[0] == 'live'):
            liveonly = False
            if len(team) == 1:
                liveonly = True
            output = mymlbstats.get_all_game_info(delta=delta,
                                                  liveonly=liveonly)
            if len(output) > 0:
                await self.bot.say("```python\n" + output + "```")
            else:
                await self.bot.say("```No live games at the moment.```")
            return

        if team[0] == 'help':
            await self.bot.say(
                "https://github.com/efitz11/natsgifbot/blob/master/mlbhelp.txt"
            )
            return

        if team[0] in ["sp", "lsp"]:
            teamname = ' '.join(team[1:])
            if team[0] == "lsp":
                scoring_plays = mymlbstats.list_scoring_plays(teamname,
                                                              delta,
                                                              lastonly=True)
            else:
                scoring_plays = mymlbstats.list_scoring_plays(teamname, delta)
            print(teamname, scoring_plays)
            if len(scoring_plays) > 0:
                output = "```"
                lastinning = ""
                for play in scoring_plays:
                    if play[0] != lastinning:
                        if len(lastinning) != 0:
                            output = output + "```\n```"
                            if len(output) + len(play[0]) > 1600:
                                await self.bot.say(output[:-3])
                                output = "```"
                        output = output + play[0] + "\n"
                        lastinning = play[0]
                    output = output + "\t" + play[1] + "\n"
                output = output + "```"
                await self.bot.say(output)
                return
            else:
                await self.bot.say("No scoring plays")
                return
        elif team[0] == 'homers':
            teamname = ' '.join(team[1:])
            await self.bot.say("```python\n%s```" %
                               mymlbstats.list_home_runs(teamname, delta=delta)
                               )
            return
        elif team[0] == 'line':
            player = '+'.join(team[1:])
            if len(player) == 0:
                out = get_daily_leaders(delta=delta)
                await self.bot.say("ESPN's daily leaders:\n```%s```" % out)
                return
            else:
                out = mymlbstats.get_player_line(player, delta)
            if len(out) == 0:
                await self.bot.say("couldn't find stats")
            else:
                await self.bot.say("```%s```" % out)
            return
        # elif team[0] in ['stats','bstats','pstats','hstats','hbstats','hpstats']:
        elif team[0].endswith('stats'):
            active = 'Y'
            if team[0].startswith('h'):
                team[0] = team[0][1:]
                active = 'N'
            career = False
            if team[0].startswith('c'):
                team[0] = team[0][1:]
                career = True
            year = None
            year2 = None
            if team[-1].isdigit() or (team[-1].split('-')[0].isdigit()
                                      and team[-1].split('-')[1].isdigit()):
                year = team[-1]
                team = team[0:-1]
            # if team[-1].isdigit():
            #     year2 = year
            #     year = team[-1]
            #     team = team[0:-1]
            player = ' '.join(team[1:])
            t = None
            if team[0] in ['bstats']:
                t = "hitting"
            elif team[0] == 'pstats':
                t = "pitching"
            # await self.bot.say("```%s```" % mymlbstats.get_player_season_stats(player,type=t,year=year,year2=year2,active=active, career=career, reddit=reddit))
            await self.bot.say(
                "```%s```" % newmlbstats.get_player_season_stats(
                    player, type=t, year=year, career=career, reddit=reddit))
            return
        elif team[0] == 'compare':
            year = None
            if team[-1].isdigit():
                year = team[-1]
                team = team[0:-1]
            team = team[1:]
            playerlist = []
            for t in team:
                playerlist.append(t)
            if year is None:
                await self.bot.say(
                    "```%s```" %
                    mymlbstats.compare_player_stats(playerlist, reddit=reddit))
            else:
                await self.bot.say("```%s```" %
                                   mymlbstats.compare_player_stats(
                                       playerlist, year=year, reddit=reddit))
        elif team[0] in ['splits', 'psplits']:
            t = 'pitching' if team[0] == 'psplits' else 'hitting'
            split = team[1]
            if team[-1].isdigit():
                year = team[-1]
                player = ' '.join(team[2:-1])
                await self.bot.say(
                    "```%s```" % mymlbstats.get_player_season_splits(
                        player, split, year=year, type=t, reddit=reddit))
            else:
                player = ' '.join(team[2:])
                await self.bot.say("```%s```" %
                                   mymlbstats.get_player_season_splits(
                                       player, split, type=t, reddit=reddit))
            return
        elif team[0] == 'vs':
            opp = team[1]
            year = None
            if team[-1].isdigit():
                year = team[-1]
                team = team[0:-1]
            #     player = ' '.join(team[2:-1])
            #     await self.bot.say("```%s```" % mymlbstats.player_vs_team(player,opp,year=year))
            # else:
            player = ' '.join(team[2:])
            # await self.bot.say("```%s```" % mymlbstats.player_vs_team(player,opp))
            await self.bot.say("```%s```" % mymlbstats.batter_or_pitcher_vs(
                player, opp, year=year, reddit=reddit))
            return
        elif team[0] == 'bvp':
            p1 = team[1]
            p2 = team[2]
            await self.bot.say(
                "```%s```" %
                mymlbstats.player_vs_pitcher(p1, p2, reddit=reddit))
        elif team[0].lower() in ["dl", "il"]:
            team = ' '.join(team[1:])
            await self.bot.say("```%s```" % mymlbstats.get_team_dl(team))
            return
        elif team[0] in ['batters', 'hitters', 'pitchers']:
            h = (team[0] == 'batters' or team[0] == 'hitters')
            team = ' '.join(team[1:])
            await self.bot.say("```%s```" %
                               mymlbstats.print_roster(team, hitters=h))
        elif team[0] in ['past', 'next']:
            num = 2
            backwards = team[0] == 'past'
            if team[1].isdigit():
                num = int(team[1])
                team = ' '.join(team[2:])
            else:
                team = ' '.join(team[1:])
            await self.bot.say(
                "```%s```" %
                mymlbstats.get_team_schedule(team, num, backward=backwards))
            return
        elif team[0].startswith("last") or team[0].startswith(
                "blast") or team[0].startswith("plast"):
            forcebatting = False
            forcepitching = False
            if team[0].startswith('blast'):
                team[0] = team[0][1:]
                forcebatting = True
            elif team[0].startswith('plast'):
                team[0] = team[0][1:]
                forcepitching = True
            if team[1].isdigit():
                days = int(team[1])
                team = team[2:]
            else:
                team = team[1:]
                days = None
            await self.bot.say("```%s```" %
                               mymlbstats.get_player_trailing_splits(
                                   '+'.join(team),
                                   days,
                                   forcebatting=forcebatting,
                                   forcepitching=forcepitching,
                                   reddit=reddit))
            return
        elif team[0].endswith("log"):
            forcebatting = team[0].startswith("b")
            if team[-1].isdigit():
                num = int(team[-1])
                team = team[:-1]
            else:
                num = 5
            player = '+'.join(team[1:])
            await self.bot.say("```%s```" % mymlbstats.get_player_gamelogs(
                player, num, forcebatting=forcebatting))
            return
        elif team[0].endswith("leaders") or team[0].endswith("losers"):
            stat = team[1]

            if stat in self.savant_stats:
                year = None
                if team[-1].isdigit():
                    year = team[-1]
                await self.bot.say(savant.get_oaa_leaders(year=year))
                return

            opts = []
            for i in range(2, len(team)):
                opts.append(team[i])
            stattype = 'bat'
            if team[0].startswith('p'):
                t = team[0][1:]
                stattype = 'pit'
            elif team[0].startswith('f'):
                t = team[0][1:]
                stattype = 'fld'
            else:
                t = team[0]
            if t == "losers":
                opts.append("reverse=yes")
            fg = FG(stat.lower(), options=opts)
            output = fg.get_stat_leaders_str(stattype=stattype)
            await self.bot.say(output)
            return
        elif team[0] == 'ohtani':
            out = mymlbstats.get_ohtani_line(delta)
            if len(out) > 0:
                await self.bot.say("```%s```" % out)
            else:
                await self.bot.say("No stats found")
            return
        elif team[0] in [
                'batting', 'hitting', 'pitching', 'notes', 'info', 'bench',
                'bullpen', 'box'
        ]:
            if team[0] == 'box':
                await self.bot.say(
                    "The box score is divided into parts: use one of: ```batting, pitching, notes, info, bench, bullpen```"
                    "If you want the linescore, use: ```linescore```")
                return
            elif team[0] == 'hitting':
                team[0] = 'batting'
            part = team[0]
            team = ' '.join(team[1:]).lower()
            out = mymlbstats.print_box(team, part=part, delta=delta)
            if out is not None:
                await self.bot.say("```%s```" % out)
            return
        elif team[0] in ['umps', 'umpires']:
            team = ' '.join(team[1:]).lower()
            umps = mymlbstats.print_umpires(team, delta=delta)
            if umps is not None:
                await self.bot.say("```%s```" % umps)
            else:
                await self.bot.say("```Umps not found.```")
        elif team[0] == "linescore":
            team = ' '.join(team[1:]).lower()
            out = mymlbstats.print_linescore(team, delta=delta)
            await self.bot.say("```%s```" % out)
            return
        elif team[0] == "highlight":
            query = ' '.join(team[1:])
            await self.bot.say(mymlbstats.search_highlights(query))
            return
        elif team[0] == "videos":
            team = ' '.join(team[1:]).lower()
            print(team)
            # await self.bot.say(mymlbstats.find_game_highlights(team, delta=delta))
            msg = utils.split_long_message(
                mymlbstats.find_game_highlights(team, delta=delta))
            for m in msg:
                await self.bot.say(m)
        elif team[0] == "plays":
            inning = int(team[-1])
            team = ' '.join(team[1:-1]).lower()
            await self.bot.say(
                mymlbstats.get_inning_plays(team, inning, delta=delta))
        elif team[0] == "standings":
            div = team[1]
            await self.bot.say(mymlbstats.get_div_standings(div))
        elif team[0] in ["broadcast", "broadcasts"]:
            team = ' '.join(team[1:]).lower()
            out = mymlbstats.print_broadcasts(team, delta=delta)
            await self.bot.say("```%s```" % out)
        elif team[0] in [
                "longdongs", "shortdongs", "highdongs", "lowdongs",
                "fastdongs", "slowdongs", "mostdongs"
        ]:
            out = mymlbstats.print_dongs(team[0][:team[0].index('d')],
                                         delta=delta,
                                         reddit=reddit)
            await self.bot.say("```%s```" % out)
        elif team[0] in ["dongs", "newdongs"]:
            out = mymlbstats.print_dongs("recent", delta=delta, reddit=reddit)
            await self.bot.say("```%s```" % out)
        elif team[0] == "captivating":
            out = mymlbstats.print_most_captivating_sp(delta=delta)
            out = utils.split_long_message(out)
            for o in out:
                o = o.rstrip()
                if len(o) > 0:
                    await self.bot.say("```%s```" % o)
        elif team[0] == "abs":
            team = ' '.join(team[1:]).lower()
            await self.bot.say("%s" %
                               mymlbstats.print_at_bats(team, delta=delta))
        elif team[0] == "savant":
            team = ' '.join(team[1:]).lower()
            await self.bot.say(savant.print_player_or_team(team, delta=delta))
        elif team[0] == "pitches" or team[0] == "pbi":
            team = ' '.join(team[1:]).lower()
            await self.bot.say(
                mymlbstats.print_pitches_by_inning(team, delta=delta))
        elif team[0] == "contract":
            name = ' '.join(team[1:])
            await self.bot.say(newmlbstats.print_contract_info(name))
        else:
            teamname = ' '.join(team).lower()
            output = mymlbstats.get_single_game(teamname, delta=delta)
            if len(output) > 0:
                await self.bot.say("```python\n" + output + "```")
            else:
                await self.bot.say("no games found")
Example #2
0
    async def mlb(self, *team: str):
        """Get MLB info

        Help is now too long for a discord message. Check the github link for full help.

        Supported sub commands:
        blank
        <team>
        <division>
        standings <division>
        [l]sp <team>
        line <player>
        ohtani
        <part> <team>
        linescore <team>

            each of the previous commands can end in a number of (+days or -days) to change the date

        dl <team>
        batters <team>
        pitchers <team>

        last [n] <team>
        next [n] <team>

        [h][c][b,p]stats <player> [year] [year2]
        splits <split> <player> [year]
        vs <team> <player>
        [b]last <player> [n]
        [b]log [n] <player>
        [p,f]leaders <stat>

        highlight <query>

        help - https://github.com/efitz11/natsgifbot/blob/master/mlbhelp.txt
        """
        delta = None

        team = ["wsh" if x.lower() == "nats" else x for x in team]

        if len(team) > 0 and (team[-1].startswith('-')
                              or team[-1].startswith('+')):
            delta = team[-1]
            team = team[:-1]

        if len(team) > 0 and len(team[-1].split('/')) == 3:
            delta = self.convert_date_to_delta(team)
            team = team[:-1]

        if len(team) == 0 or (len(team) == 1 and team[0] == 'live'):
            liveonly = False
            if len(team) == 1:
                liveonly = True
            output = mymlbstats.get_all_game_info(delta=delta,
                                                  liveonly=liveonly)
            await self.bot.say("```python\n" + output + "```")
            return

        if team[0] == 'help':
            await self.bot.say(
                "https://github.com/efitz11/natsgifbot/blob/master/mlbhelp.txt"
            )
            return

        if team[0] in ["sp", "lsp"]:
            teamname = ' '.join(team[1:])
            if team[0] == "lsp":
                scoring_plays = mymlbstats.list_scoring_plays(teamname,
                                                              delta,
                                                              lastonly=True)
            else:
                scoring_plays = mymlbstats.list_scoring_plays(teamname, delta)
            print(teamname, scoring_plays)
            if len(scoring_plays) > 0:
                output = "```"
                lastinning = ""
                for play in scoring_plays:
                    if play[0] != lastinning:
                        if len(lastinning) != 0:
                            output = output + "```\n```"
                            if len(output) + len(play[0]) > 1600:
                                await self.bot.say(output[:-3])
                                output = "```"
                        output = output + play[0] + "\n"
                        lastinning = play[0]
                    output = output + "\t" + play[1] + "\n"
                output = output + "```"
                await self.bot.say(output)
                return
            else:
                await self.bot.say("No scoring plays")
                return
        elif team[0] == 'line':
            player = '+'.join(team[1:])
            if len(player) == 0:
                out = get_daily_leaders(delta=delta)
                await self.bot.say("ESPN's daily leaders:\n```%s```" % out)
                return
            else:
                out = mymlbstats.get_player_line(player, delta)
            if len(out) == 0:
                await self.bot.say("couldn't find stats")
            else:
                await self.bot.say("```%s```" % out)
            return
        # elif team[0] in ['stats','bstats','pstats','hstats','hbstats','hpstats']:
        elif team[0].endswith('stats'):
            active = 'Y'
            if team[0].startswith('h'):
                team[0] = team[0][1:]
                active = 'N'
            career = False
            if team[0].startswith('c'):
                team[0] = team[0][1:]
                career = True
            year = None
            year2 = None
            if team[-1].isdigit():
                year = team[-1]
                team = team[0:-1]
            if team[-1].isdigit():
                year2 = year
                year = team[-1]
                team = team[0:-1]
            player = '+'.join(team[1:])
            t = None
            if team[0] in ['bstats']:
                t = "hitting"
            elif team[0] == 'pstats':
                t = "pitching"
            await self.bot.say(
                "```%s```" % mymlbstats.get_player_season_stats(player,
                                                                type=t,
                                                                year=year,
                                                                year2=year2,
                                                                active=active,
                                                                career=career))
            return
        elif team[0] == 'compare':
            year = None
            if team[-1].isdigit():
                year = team[-1]
                team = team[0:-1]
            team = team[1:]
            playerlist = []
            for t in team:
                playerlist.append(t)
            if year is None:
                await self.bot.say("```%s```" %
                                   mymlbstats.compare_player_stats(playerlist))
            else:
                await self.bot.say(
                    "```%s```" %
                    mymlbstats.compare_player_stats(playerlist, year=year))
        elif team[0] == 'splits':
            split = team[1]
            if team[-1].isdigit():
                year = team[-1]
                player = ' '.join(team[2:-1])
                await self.bot.say("```%s```" %
                                   mymlbstats.get_player_season_splits(
                                       player, split, year=year))
            else:
                player = ' '.join(team[2:])
                await self.bot.say(
                    "```%s```" %
                    mymlbstats.get_player_season_splits(player, split))
            return
        elif team[0] == 'vs':
            opp = team[1]
            year = None
            if team[-1].isdigit():
                year = team[-1]
                team = team[0:-1]
            #     player = ' '.join(team[2:-1])
            #     await self.bot.say("```%s```" % mymlbstats.player_vs_team(player,opp,year=year))
            # else:
            player = ' '.join(team[2:])
            # await self.bot.say("```%s```" % mymlbstats.player_vs_team(player,opp))
            await self.bot.say(
                "```%s```" %
                mymlbstats.batter_or_pitcher_vs(player, opp, year=year))
            return
        elif team[0].lower() == "dl":
            team = ' '.join(team[1:])
            await self.bot.say("```%s```" % mymlbstats.get_team_dl(team))
            return
        elif team[0] in ['batters', 'pitchers']:
            h = (team[0] == 'batters')
            team = ' '.join(team[1:])
            await self.bot.say("```%s```" %
                               mymlbstats.print_roster(team, hitters=h))
        elif team[0] in ['past', 'next']:
            num = 2
            backwards = team[0] == 'past'
            if team[1].isdigit():
                num = int(team[1])
                team = ' '.join(team[2:])
            else:
                team = ' '.join(team[1:])
            await self.bot.say(
                "```%s```" %
                mymlbstats.get_team_schedule(team, num, backward=backwards))
            return
        elif team[0].startswith("last") or team[0].startswith("blast"):
            forcebatting = False
            if team[0].startswith('blast'):
                team[0] = team[0][1:]
                forcebatting = True
            if team[1].isdigit():
                days = int(team[1])
                team = team[2:]
            else:
                team = team[1:]
                days = None
            await self.bot.say(
                "```%s```" % mymlbstats.get_player_trailing_splits(
                    '+'.join(team), days, forcebatting=forcebatting))
            return
        elif team[0].endswith("log"):
            forcebatting = team[0].startswith("b")
            if team[-1].isdigit():
                num = int(team[-1])
                team = team[:-1]
            else:
                num = 5
            player = '+'.join(team[1:])
            await self.bot.say("```%s```" % mymlbstats.get_player_gamelogs(
                player, num, forcebatting=forcebatting))
            return
        elif team[0].endswith("leaders") or team[0].endswith("losers"):
            stat = team[1]
            opts = []
            for i in range(2, len(team)):
                opts.append(team[i])
            stattype = 'bat'
            if team[0].startswith('p'):
                t = team[0][1:]
                stattype = 'pit'
            elif team[0].startswith('f'):
                t = team[0][1:]
                stattype = 'fld'
            else:
                t = team[0]
            if t == "losers":
                opts.append("reverse=yes")
            fg = FG(stat.lower(), options=opts)
            output = fg.get_stat_leaders_str(stattype=stattype)
            await self.bot.say(output)
            return
        elif team[0] == 'ohtani':
            out = mymlbstats.get_ohtani_line(delta)
            if len(out) > 0:
                await self.bot.say("```%s```" % out)
            else:
                await self.bot.say("No stats found")
            return
        elif team[0] in [
                'batting', 'pitching', 'notes', 'info', 'bench', 'bullpen'
        ]:
            part = team[0]
            team = ' '.join(team[1:]).lower()
            out = mymlbstats.print_box(team, part=part, delta=delta)
            if out is not None:
                await self.bot.say("```%s```" % out)
            return
        elif team[0] == "linescore":
            team = ' '.join(team[1:]).lower()
            out = mymlbstats.print_linescore(team, delta=delta)
            await self.bot.say("```%s```" % out)
            return
        elif team[0] == "highlight":
            query = ' '.join(team[1:])
            await self.bot.say(mymlbstats.search_highlights(query))
            return
        elif team[0] == "plays":
            inning = int(team[-1])
            team = ' '.join(team[1:-1]).lower()
            await self.bot.say(
                mymlbstats.get_inning_plays(team, inning, delta=delta))
        elif team[0] == "standings":
            div = team[1]
            await self.bot.say(mymlbstats.get_div_standings(div))
        else:
            teamname = ' '.join(team).lower()
            output = mymlbstats.get_single_game(teamname, delta=delta)
            if len(output) > 0:
                await self.bot.say("```python\n" + output + "```")
            else:
                await self.bot.say("no games found")
Example #3
0
    async def mlb(self, ctx, *team: str):
        """Get MLB info
        get help on a specific subcommand using !mlb <subcommand> help
        """
        delta = None

        team = ["wsh" if x.lower() == "nats" else x for x in team]

        reddit = False
        for i in team:
            if i.lower() == 'reddit':
                reddit = True
                team.remove(i)

        if 'help' in team:
            await ctx.send(self.get_help_str(team[0]))
            return

        if len(team) > 0 and (team[-1].startswith('-')
                              or team[-1].startswith('+')):
            delta = team[-1]
            team = team[:-1]
        elif len(team) > 0 and len(team[-1].split('/')) in [2, 3]:
            if team[-1].split('/')[0].isdigit():
                delta = self.convert_date_to_delta(team)
                team = team[:-1]
        elif len(team) > 0 and team[-1].lower() in ['yesterday', 'tomorrow']:
            if team[-1] == 'yesterday':
                delta = "-1"
            elif team[-1] == 'tomorrow':
                delta = "+1"
            team = team[:-1]

        if len(team) == 0 or (len(team) == 1 and
                              (team[0] == 'live' or team[0] == 'close')):
            output, numgames = newmlbstats.print_games(team, delta=delta)
            cmdchannel = ctx.channel
            if numgames > 5:
                cmdchannel = discord.utils.find(lambda m: 'bot' in m.name,
                                                ctx.channel.guild.channels)
                if cmdchannel != ctx.channel:
                    await ctx.channel.send("response in %s" %
                                           cmdchannel.mention)
            if len(output) > 0:
                await cmdchannel.send("```python\n" + output + "```")
        elif team[0] in ["sp", "lsp"]:
            teamname = ' '.join(team[1:])
            if team[0] == "lsp":
                scoring_plays = mymlbstats.list_scoring_plays(teamname,
                                                              delta,
                                                              lastonly=True)
            else:
                scoring_plays = mymlbstats.list_scoring_plays(teamname, delta)
            print(teamname, scoring_plays)
            if len(scoring_plays) > 0:
                output = "```"
                lastinning = ""
                for play in scoring_plays:
                    if play[0] != lastinning:
                        if len(lastinning) != 0:
                            output = output + "```\n```"
                            if len(output) + len(play[0]) > 1600:
                                await ctx.send(output[:-3])
                                output = "```"
                        output = output + play[0] + "\n"
                        lastinning = play[0]
                    output = output + "\t" + play[1] + "\n"
                output = output + "```"
                await ctx.send(output)
                return
            else:
                await ctx.send("No scoring plays")
                return
        elif team[0] == 'homers':
            teamname = ' '.join(team[1:])
            await ctx.send("```python\n%s```" %
                           mymlbstats.list_home_runs(teamname, delta=delta))
            return
        elif team[0] == 'line':
            player = '+'.join(team[1:])
            if len(player) == 0:
                out = get_daily_leaders(delta=delta)
                await ctx.send("ESPN's daily leaders:\n```%s```" % out)
                return
            else:
                out = mymlbstats.get_player_line(player, delta)
            if len(out) == 0:
                await ctx.send("couldn't find stats")
            else:
                await ctx.send(out)
            return
        # elif team[0] in ['stats','bstats','pstats','hstats','hbstats','hpstats']:
        elif team[0].endswith('stats'):
            active = 'Y'
            if team[0].startswith('h'):
                team[0] = team[0][1:]
                active = 'N'
            career = False
            if team[0].startswith('c'):
                team[0] = team[0][1:]
                career = True
            year = None
            year2 = None
            if team[-1].isdigit() or (team[-1].split('-')[0].isdigit()
                                      and team[-1].split('-')[1].isdigit()):
                year = team[-1]
                team = team[0:-1]
            # if team[-1].isdigit():
            #     year2 = year
            #     year = team[-1]
            #     team = team[0:-1]
            player = ' '.join(team[1:])
            t = None
            if team[0] in ['bstats']:
                t = "hitting"
            elif team[0] == 'pstats':
                t = "pitching"
            # await ctx.send("```%s```" % mymlbstats.get_player_season_stats(player,type=t,year=year,year2=year2,active=active, career=career, reddit=reddit))
            await ctx.send("```%s```" % newmlbstats.get_player_season_stats(
                player, type=t, year=year, career=career, reddit=reddit))
            return
        elif team[0] == 'compare':
            year = None
            if team[-1].isdigit():
                year = team[-1]
                team = team[0:-1]
            team = team[1:]

            career = False
            if team[0] == 'career':
                career = True
                team = team[1:]

            if '/' in ' '.join(team):
                playerlist = ' '.join(team).split('/')
            else:
                playerlist = []
                for t in team:
                    playerlist.append(t)
            print(playerlist)
            if year is None:
                await ctx.send("```%s```" % mymlbstats.compare_player_stats(
                    playerlist, career=career, reddit=reddit))
            else:
                await ctx.send("```%s```" % mymlbstats.compare_player_stats(
                    playerlist, year=year, career=career, reddit=reddit))
        elif team[0] == 'range':
            if '-' in team[1]:
                date1, date2 = team[1].split('-')
                date1 = newmlbstats._convert_date_to_mlb_str(date1)
                date2 = newmlbstats._convert_date_to_mlb_str(date2)
                team = team[2:]
                group = None
                for t in team:
                    if t in ['hitting', 'pitching']:
                        group = t
                        team.remove(t)
                await ctx.send(
                    "```%s```" %
                    newmlbstats.print_player_stats(' '.join(team),
                                                   group=group,
                                                   stattype="byDateRange",
                                                   startDate=date1,
                                                   endDate=date2,
                                                   reddit=reddit))
            else:
                await ctx.send(
                    "date range (mm/dd/[yy]yy-mm/dd/[yy]yy) required")
        elif team[0] in ['splits', 'psplits']:
            t = 'pitching' if team[0] == 'psplits' else 'hitting'
            split = team[1]
            if team[-1].isdigit():
                year = team[-1]
                player = ' '.join(team[2:-1])
                await ctx.send(
                    "```%s```" % mymlbstats.get_player_season_splits(
                        player, split, year=year, type=t, reddit=reddit))
            else:
                player = ' '.join(team[2:])
                await ctx.send("```%s```" %
                               mymlbstats.get_player_season_splits(
                                   player, split, type=t, reddit=reddit))
            return
        elif team[0] == 'vs':
            opp = team[1]
            year = None
            if team[-1].isdigit():
                year = team[-1]
                team = team[0:-1]
            #     player = ' '.join(team[2:-1])
            #     await ctx.send("```%s```" % mymlbstats.player_vs_team(player,opp,year=year))
            # else:
            player = ' '.join(team[2:])
            # await ctx.send("```%s```" % mymlbstats.player_vs_team(player,opp))
            await ctx.send("```%s```" % mymlbstats.batter_or_pitcher_vs(
                player, opp, year=year, reddit=reddit))
            return
        elif team[0] == 'bvp':
            p1 = team[1]
            p2 = team[2]
            await ctx.send("```%s```" %
                           mymlbstats.player_vs_pitcher(p1, p2, reddit=reddit))
        elif team[0].lower() in ["dl", "il"]:
            team = ' '.join(team[1:])
            await ctx.send("```%s```" % mymlbstats.get_team_dl(team))
            return
        elif team[0] in ['batters', 'hitters', 'pitchers']:
            h = (team[0] == 'batters' or team[0] == 'hitters')
            team = ' '.join(team[1:])
            await ctx.send("```%s```" %
                           mymlbstats.print_roster(team, hitters=h))
        elif team[0] in ['past', 'next']:
            num = 3
            backwards = team[0] == 'past'
            if team[1].isdigit():
                num = int(team[1])
                team = ' '.join(team[2:])
            else:
                team = ' '.join(team[1:])
            # await ctx.send("```%s```" % mymlbstats.get_team_schedule(team,num,backward=backwards))
            await ctx.send("```%s```" % newmlbstats.print_team_schedule(
                team, num, forward=(not backwards)))
            return
        elif team[0].startswith("last") or team[0].startswith(
                "blast") or team[0].startswith("plast"):
            group, usegames = None, False
            if team[0].startswith('blast'):
                team[0] = team[0][1:]
                group = 'hitting'
            elif team[0].startswith('plast'):
                team[0] = team[0][1:]
                group = 'pitching'
            if team[1].isdigit():
                days = int(team[1])
                team = team[2:]
            else:
                team = team[1:]
                days = None
            if team[0] == 'games':
                usegames = True
                team = team[1:]
            elif team[0] == 'days':
                team = team[1:]
            # await ctx.send("```%s```" % mymlbstats.get_player_trailing_splits('+'.join(team), days, forcebatting=forcebatting, forcepitching=forcepitching, reddit=reddit))
            if usegames:
                await ctx.send("```%s```" % newmlbstats.print_last_x_games(
                    ' '.join(team), days, group=group, reddit=reddit))
            else:
                await ctx.send("```%s```" % newmlbstats.print_last_x_days(
                    ' '.join(team), days, group=group, reddit=reddit))
            return
        elif team[0].endswith("log"):
            forcebatting = team[0].startswith("b")
            if team[-1].isdigit():
                num = int(team[-1])
                team = team[:-1]
            else:
                num = 5
            player = '+'.join(team[1:])
            await ctx.send("```%s```" % mymlbstats.get_player_gamelogs(
                player, num, forcebatting=forcebatting, reddit=reddit))
            return
        elif team[0] in ["leaders", "losers"]:
            stat = team[1:]
            season = None
            if team[-1].isdigit():
                season = team[-1]
                stat = stat[:-1]
            # await ctx.send(newmlbstats.print_stat_leaders(stat, season=season))
            reverse = False
            if team[0] == 'losers':
                reverse = True
            if len(team) == 1:
                await ctx.send(self.get_help_str('leaders'))
            else:
                await ctx.send(
                    newmlbstats.print_sorted_stats(stat,
                                                   season=season,
                                                   reverse=reverse,
                                                   delta=delta))
        elif team[0] == 'ohtani':
            out = mymlbstats.get_ohtani_line(delta)
            if len(out) > 0:
                await ctx.send("%s" % out)
            else:
                await ctx.send("No stats found")
            return
        elif team[0] in [
                'batting', 'hitting', 'pitching', 'notes', 'info', 'bench',
                'bullpen', 'box'
        ]:
            if team[0] == 'box':
                await ctx.send(
                    "The box score is divided into parts: use one of: ```batting, pitching, notes, info, bench, bullpen```"
                    "If you want the linescore, use: ```linescore```")
                return
            elif team[0] == 'hitting':
                team[0] = 'batting'
            part = team[0]
            team = ' '.join(team[1:]).lower()
            out = mymlbstats.print_box(team,
                                       part=part,
                                       delta=delta,
                                       reddit=reddit)
            if out is not None:
                await ctx.send("%s" % out)
            return
        elif team[0] in ['umps', 'umpires']:
            team = ' '.join(team[1:]).lower()
            umps = mymlbstats.print_umpires(team, delta=delta)
            if umps is not None:
                await ctx.send("```%s```" % umps)
            else:
                await ctx.send("```Umps not found.```")
        elif team[0] == "linescore":
            team = ' '.join(team[1:]).lower()
            out = mymlbstats.print_linescore(team, delta=delta)
            await ctx.send("```%s```" % out)
            return
        elif team[0] in ["highlight", "highlights"]:
            query = ' '.join(team[1:])
            await ctx.send(mymlbstats.search_highlights(query, delta=delta))
            return
        elif team[0] == "videos":
            team = ' '.join(team[1:]).lower()
            print(team)
            # await ctx.send(mymlbstats.find_game_highlights(team, delta=delta))
            msg = utils.split_long_message(
                mymlbstats.find_game_highlights(team, delta=delta))
            for m in msg:
                await ctx.send(m)
        elif team[0] == "plays":
            inning = int(team[-1])
            team = ' '.join(team[1:-1]).lower()
            msg = utils.split_long_message(
                mymlbstats.get_inning_plays(team, inning, delta=delta))
            for m in msg:
                await ctx.send(m)
        elif team[0] == "standings":
            if len(team) == 1:
                await ctx.send('a division or league is a required argument')
            else:
                div = team[1]
                await ctx.send(mymlbstats.get_div_standings(div))
        elif team[0] in ["broadcast", "broadcasts"]:
            team = ' '.join(team[1:]).lower()
            out = mymlbstats.print_broadcasts(team, delta=delta)
            await ctx.send("```%s```" % out)
        elif team[0] in [
                "longdongs", "shortdongs", "highdongs", "lowdongs",
                "fastdongs", "slowdongs", "mostdongs"
        ]:
            out = mymlbstats.print_dongs(team[0][:team[0].index('d')],
                                         delta=delta,
                                         reddit=reddit)
            await ctx.send("```%s```" % out)
        elif team[0] in ["dongs", "newdongs"]:
            out = mymlbstats.print_dongs("recent", delta=delta, reddit=reddit)
            await ctx.send("```%s```" % out)
        elif team[0] == "captivating":
            out = mymlbstats.print_most_captivating_sp(delta=delta)
            out = utils.split_long_message(out)
            for o in out:
                o = o.rstrip()
                if len(o) > 0:
                    await ctx.send("```%s```" % o)
        elif team[0] == "abs":
            team = ' '.join(team[1:]).lower()
            await ctx.send("%s" % mymlbstats.print_at_bats(team, delta=delta))
        elif team[0] == "savant":
            team = ' '.join(team[1:]).lower()
            await ctx.send(savant.print_player_or_team(team, delta=delta))
        elif team[0] == "pitches" or team[0] == "pbi":
            team = ' '.join(team[1:]).lower()
            await ctx.send(
                mymlbstats.print_pitches_by_inning(team, delta=delta))
        elif team[0] == "contract":
            name = ' '.join(team[1:])
            await ctx.send(newmlbstats.print_contract_info(name))
        elif team[0] == "birthdays":
            team = ' '.join(team[1:]).lower()
            await ctx.send(
                "```python\n%s```" %
                newmlbstats.print_birthdays(team, delta=delta, reddit=reddit))
        elif team[0] == "pic":
            name = ' '.join(team[1:])
            await ctx.send(newmlbstats.get_player_headshot_url(name))
        elif team[0] == "arsenal":
            season = None
            if team[-1].isdigit():
                season = team[-1]
                team = ' '.join(team[1:-1])
            else:
                team = ' '.join(team[1:])
            await ctx.send(newmlbstats.print_pitch_arsenal(team,
                                                           season=season))
        elif team[0] == "streaks":
            team = team[1:]
            season = None
            if team[-1].isdigit():
                season = team[-1]
                team = team[:-1]
            await ctx.send(
                newmlbstats.print_stat_streaks(team,
                                               season=season,
                                               reddit=reddit))
        elif team[0] == "advanced":
            team = team[1:]
            year = None
            if team[-1].isdigit() or (team[-1].split('-')[0].isdigit()
                                      and team[-1].split('-')[1].isdigit()):
                year = team[-1]
                team = team[:-1]
            player = ' '.join(team)
            await ctx.send(
                savant.print_player_advanced_stats(player,
                                                   year=year,
                                                   reddit=reddit))
        elif team[0] == "percentiles":
            team = team[1:]
            season = None
            if team[-1].isdigit():
                season = team[-1]
                team = team[:-1]
            player = ' '.join(team)
            await ctx.send(savant.print_player_rankings(player, year=season))
        else:
            teamname = ' '.join(team).lower()
            if teamname == 'help':
                await ctx.send(self.get_help_str('mlb'))
            else:
                output, numgames = newmlbstats.print_games(teamname,
                                                           delta=delta)
                if len(output) > 0:
                    await ctx.send("```python\n" + output + "```")
                else:
                    await ctx.send("no games found")
Example #4
0
    async def mlb(self, *team: str):
        """Get MLB info

        Help is now too long for a discord message. Check the github link for full help.

        Supported sub commands:
        blank
        <team>
        <division>
        [l]sp <team>
        line <player>
        ohtani
        <part> <team>
        linescore <team>

            each of the previous commands can end in a number of (+days or -days) to change the date

        last [n] <team>
        next [n] <team>

        [h][c][b,p]stats <player> [year]
        [b]last <player> [n]
        [b]log [n] <player>
        [p,f]leaders <stat>

        help - https://github.com/efitz11/natsgifbot/blob/master/mlbhelp.txt
        """
        delta = None

        team = ["wsh" if x.lower() == "nats" else x for x in team]

        if len(team) > 0 and (team[-1].startswith('-')
                              or team[-1].startswith('+')):
            delta = team[-1]
            team = team[:-1]

        if len(team) > 0 and len(team[-1].split('/')) == 3:
            now = datetime.now().date()
            datelist = team[-1].split('/')
            other = date(int(datelist[2]), int(datelist[0]), int(datelist[1]))
            delta = other - now
            direction = ''
            if delta.days > 0:
                direction = '+'
            delta = direction + str(delta.days)
            team = team[:-1]

        if len(team) == 0 or (len(team) == 1 and team[0] == 'live'):
            liveonly = False
            if len(team) == 1:
                liveonly = True
            output = mymlbstats.get_all_game_info(delta=delta,
                                                  liveonly=liveonly)
            await self.bot.say("```python\n" + output + "```")
            return

        if team[0] == 'help':
            await self.bot.say(
                "https://github.com/efitz11/natsgifbot/blob/master/mlbhelp.txt"
            )
            return

        if team[0] in ["sp", "lsp"]:
            teamname = ' '.join(team[1:]).title()
            if team[0] == "lsp":
                scoring_plays = mymlbstats.list_scoring_plays(teamname,
                                                              delta,
                                                              lastonly=True)
            else:
                scoring_plays = mymlbstats.list_scoring_plays(teamname, delta)
            print(teamname, scoring_plays)
            if len(scoring_plays) > 0:
                output = "```"
                lastinning = ""
                for play in scoring_plays:
                    if play[0] != lastinning:
                        if len(lastinning) != 0:
                            output = output + "```\n```"
                            if len(output) + len(play[0]) > 1800:
                                await self.bot.say(output[:-3])
                                output = "```"
                        output = output + play[0] + "\n"
                        lastinning = play[0]
                    output = output + "\t" + play[1] + "\n"
                output = output + "```"
                await self.bot.say(output)
                return
            else:
                await self.bot.say("No scoring plays")
                return
        elif team[0] == 'line':
            player = '+'.join(team[1:])
            if len(player) == 0:
                out = get_daily_leaders(delta=delta)
                await self.bot.say("ESPN's daily leaders:\n```%s```" % out)
                return
            else:
                out = mymlbstats.get_player_line(player, delta)
            if len(out) == 0:
                await self.bot.say("couldn't find stats")
            else:
                await self.bot.say("```%s```" % out)
            return
        # elif team[0] in ['stats','bstats','pstats','hstats','hbstats','hpstats']:
        elif team[0].endswith('stats'):
            active = 'Y'
            if team[0].startswith('h'):
                team[0] = team[0][1:]
                active = 'N'
            career = False
            if team[0].startswith('c'):
                team[0] = team[0][1:]
                career = True
            year = None
            if team[-1].isdigit():
                year = team[-1]
                team = team[0:-1]
            player = '+'.join(team[1:])
            t = None
            if team[0] in ['bstats']:
                t = "hitting"
            elif team[0] == 'pstats':
                t = "pitching"
            if year is None:
                await self.bot.say(
                    "```%s```" % mymlbstats.get_player_season_stats(
                        player, type=t, active=active, career=career))
            else:
                await self.bot.say(
                    "```%s```" %
                    mymlbstats.get_player_season_stats(player,
                                                       type=t,
                                                       year=year,
                                                       active=active,
                                                       career=career))
            return
        elif team[0] in ['past', 'next']:
            num = 2
            backwards = team[0] == 'past'
            if team[1].isdigit():
                num = int(team[1])
                team = ' '.join(team[2:])
            else:
                team = ' '.join(team[1:])
            await self.bot.say(
                "```%s```" %
                mymlbstats.get_team_schedule(team, num, backward=backwards))
            return
        elif team[0].startswith("last") or team[0].startswith("blast"):
            forcebatting = False
            if team[0].startswith('blast'):
                team[0] = team[0][1:]
                forcebatting = True
            days = 7
            if team[1].isdigit():
                days = int(team[1])
                team = team[2:]
            else:
                team = team[1:]
            await self.bot.say(
                "```%s```" % mymlbstats.get_player_trailing_splits(
                    '+'.join(team), days, forcebatting=forcebatting))
            return
        elif team[0].endswith("log"):
            forcebatting = team[0].startswith("b")
            if team[-1].isdigit():
                num = int(team[-1])
                team = team[:-1]
            else:
                num = 5
            player = '+'.join(team[1:])
            await self.bot.say("```%s```" % mymlbstats.get_player_gamelogs(
                player, num, forcebatting=forcebatting))
            return
        elif team[0].endswith("leaders") or team[0].endswith("losers"):
            stat = team[1]
            opts = []
            for i in range(2, len(team)):
                opts.append(team[i])
            stattype = 'bat'
            if team[0].startswith('p'):
                t = team[0][1:]
                stattype = 'pit'
            elif team[0].startswith('f'):
                t = team[0][1:]
                stattype = 'fld'
            else:
                t = team[0]
            if t == "losers":
                opts.append("reverse=yes")
            fg = FG(stat.lower(), options=opts)
            output = fg.get_stat_leaders_str(stattype=stattype)
            await self.bot.say(output)
            return
        elif team[0] == 'ohtani':
            out = mymlbstats.get_ohtani_line(delta)
            if len(out) > 0:
                await self.bot.say("```%s```" % out)
            else:
                await self.bot.say("No stats found")
            return
        elif team[0] in ['batting', 'pitching', 'notes', 'info']:
            part = team[0]
            team = ' '.join(team[1:]).lower()
            out = mymlbstats.print_box(team, part=part, delta=delta)
            if out is not None:
                await self.bot.say("```%s```" % out)
            return
        elif team[0] == "linescore":
            team = ' '.join(team[1:]).lower()
            out = mymlbstats.print_linescore(team, delta=delta)
            await self.bot.say("```%s```" % out)
            return
        else:
            teamname = ' '.join(team).lower()

        if teamname in [
                'nle', 'nlc', 'nlw', 'ale', 'alc', 'alw', 'nlwc', 'alwc'
        ]:
            output = mymlbstats.get_div_standings(teamname)
            await self.bot.say(output)
            return

        output = mymlbstats.get_single_game(teamname, delta=delta)
        if len(output) > 0:
            await self.bot.say("```python\n" + output + "```")
        else:
            await self.bot.say("no games found")