First pass at the battlefield 2042 module

This commit is contained in:
Luke Robles 2023-03-13 14:11:57 -07:00
parent cc407464cc
commit d82402cc06
3 changed files with 75 additions and 60 deletions

59
app/battlefield.py Executable file
View File

@ -0,0 +1,59 @@
import requests, json, pprint, discord
def get_player(player):
player = player.lower()
url = (
"https://api.gametools.network/bf2042/stats/?raw=false&format_values=true&name="
+ player
+ "&platform=pc&skip_battlelog=false"
)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)",
"accept": "application/json",
}
response = requests.get(url, headers=headers).json()
# Build the embed
embed = discord.Embed(description="-------", color=15105570, type="rich")
embed.set_thumbnail(url=response["avatar"])
embed.set_author(name="Battlefield 2042 stats for %s" % response["userName"])
# embed.add_field(
# name="**Rank**", value="%s, %s" % (rank_number, rank_title), inline=False
# )
gameplay_stats = {
"bestClass": "Best Class",
"kills": "Total Kills",
"mvp": "Times you were an MVP",
"deaths": "Total Deaths",
"killDeath": "KDR",
"wins": "All Time Wins",
"loses": "All Time Loses",
"winPercent": "Win/loss percentage",
"killsPerMinute": "Kills per Minute",
"damagePerMinute": "Damage per Minute",
"killsPerMatch": "Avg kills per game",
"damagePerMatch": "Avg damage per game",
"headShots": "Total Head shots",
"headshots": "Kills via Head Shot as a Percentage",
"timePlayed": "Total time played",
"accuracy": "Accuracy percentage",
"revives": "Total # of revives",
"heals": "Total amount healed",
"resupplies": "Total # of ammo resupplies",
"repairs": "Total vehicle damage repaired",
"squadmateRevive": "Squad mate revives",
"matchesPlayed": "Total games played",
"vehiclesDestroyed": "Total vehicles destroyed",
}
for stat, readable in gameplay_stats.items():
print(stat)
print(readable)
embed.add_field(
name="**%s**" % readable,
value=response[stat],
inline=True,
)
return embed

View File

@ -1,60 +0,0 @@
import requests, json, pprint, discord
from bs4 import BeautifulSoup
def get_player(player):
player = player.lower()
url = "https://battlefieldtracker.com/bfv/profile/origin/" + player + "/overview"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)"}
response = requests.get(url, headers=headers).text
soup = BeautifulSoup(response, "html.parser")
script = soup.find_all("script")
jsonstring = str(script[1])
jsonstring = jsonstring[33:-131]
data = json.loads(jsonstring)
# Get Player Data
origin_name = "bfv|origin|" + player
base_stat_blob = data["stats-v2"]["standardProfiles"][origin_name]
account_name = base_stat_blob["platformInfo"]["platformUserHandle"]
avatar_url = base_stat_blob["platformInfo"]["avatarUrl"]
stats_overview = base_stat_blob["segments"][0]["stats"]
rank = stats_overview["rank"]
rank_title = rank["metadata"]["label"]
rank_number = rank["displayValue"]
# Build the embed
embed = discord.Embed(description="-------", color=15105570, type="rich")
embed.set_thumbnail(url=avatar_url)
embed.set_author(name="Battlefield V stats for %s" % account_name)
embed.add_field(
name="**Rank**", value="%s, %s" % (rank_number, rank_title), inline=False
)
gameplay_stats = [
"scorePerMinute",
"kdRatio",
"assists",
"shotsAccuracy",
"killStreak",
"dogtagsTaken",
"headshots",
"longestHeadshot",
"heals",
"revives",
"revivesRecieved",
"resupplies",
"aceSquad",
"wlPercentage",
]
for stat in gameplay_stats:
embed.add_field(
name="**%s**" % stats_overview[stat]["displayName"],
value=stats_overview[stat]["displayValue"],
inline=True,
)
return embed

View File

@ -51,6 +51,22 @@ class Games(commands.Cog):
"I encountered an error while searching for that player.\nPlease check that your player name and server are spelled correctly"
)
@commands.slash_command(
guild_ids=None,
name="2042",
description="Query Battlefield 2042's API for data about a player",
)
@option("player_name", description="Your Origin/EA account name", required=True)
async def battlefield(self, ctx: commands.Context, player_name: str):
import battlefield
# try:
embed = battlefield.get_player(player=player_name.lower())
await ctx.defer()
await ctx.send_followup(embed=embed)
# except Exception:
# await ctx.send("I encountered an error while searching for that player.")
def setup(bot):
bot.add_cog(Games(bot))