|
1
|
|
|
import requests |
|
2
|
|
|
import asyncio |
|
3
|
|
|
import discord |
|
4
|
|
|
from datetime import datetime |
|
5
|
|
|
import random |
|
6
|
|
|
from discord.ext import commands |
|
7
|
|
|
from bs4 import BeautifulSoup |
|
8
|
|
|
from utils import colors |
|
9
|
|
|
from utils.global_utils import send_embedded |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class COD(commands.Cog): |
|
13
|
|
|
def __init__(self, bot): |
|
14
|
|
|
self.bot = bot |
|
15
|
|
|
self._last_result = None |
|
16
|
|
|
|
|
17
|
|
|
@commands.group(name="warzone", case_insensitive=True, invoke_without_command=True) |
|
18
|
|
|
@commands.cooldown(3, 10, commands.BucketType.member) |
|
19
|
|
|
async def warzone(self, ctx): |
|
20
|
|
|
await ctx.send_help(ctx.command) |
|
21
|
|
|
|
|
22
|
|
|
async def get_stats(self, ctx, URL, game_tag): |
|
23
|
|
|
page = requests.get(URL) |
|
24
|
|
|
soup = BeautifulSoup(page.content, "html.parser") |
|
25
|
|
|
|
|
26
|
|
|
warzone_stats = soup.find("div", class_="segment-stats") |
|
27
|
|
|
|
|
28
|
|
|
if warzone_stats is None: |
|
29
|
|
|
await send_embedded(ctx, "No data available for this username.\nPlease enter a valid username!") |
|
30
|
|
|
return |
|
31
|
|
|
|
|
32
|
|
|
titles = warzone_stats.find_all("span", class_="name") |
|
33
|
|
|
values = warzone_stats.find_all("span", class_="value") |
|
34
|
|
|
|
|
35
|
|
|
title_list = [_.text for _ in titles] |
|
36
|
|
|
value_list = [_.text for _ in values] |
|
37
|
|
|
|
|
38
|
|
|
stats = {k: v for (k, v) in zip(title_list, value_list)} |
|
39
|
|
|
|
|
40
|
|
|
clean_stats = { |
|
41
|
|
|
_: stats[_] |
|
42
|
|
|
for _ in ("Wins", "Top 5", "Top 10", "K/D Ratio", "Damage/game", "Win %", "Kills", "Deaths", "Downs",) |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
await self.make_embed(ctx, clean_stats, URL, game_tag) |
|
46
|
|
|
|
|
47
|
|
|
async def make_embed(self, ctx, clean_stats, URL, game_tag): |
|
48
|
|
|
description = f"Call of Duty: Warzone statistics as requested by {ctx.author.mention}" |
|
49
|
|
|
# img = "https://profile.callofduty.com/resources/cod/images/shared-logo.jpg" |
|
50
|
|
|
img = "https://i.pinimg.com/originals/af/3f/1c/af3f1c8d937fe7d65d03cb897ace169e.jpg" |
|
51
|
|
|
|
|
52
|
|
|
embed = discord.Embed(description=description, color=discord.Colour(0xA8000D), timestamp=datetime.utcnow(),) |
|
53
|
|
|
embed.set_author(name=game_tag, icon_url=img, url=URL) |
|
54
|
|
|
# embed.add_field(name=list(stats.keys())[0], value=stats["Wins"]) |
|
55
|
|
|
for (name, val) in clean_stats.items(): |
|
56
|
|
|
embed.add_field(name=name, value=val) |
|
57
|
|
|
embed.set_footer(text="Data from cod.tracker.gg") |
|
58
|
|
|
|
|
59
|
|
|
await ctx.send(embed=embed) |
|
60
|
|
|
|
|
61
|
|
|
@warzone.command(name="battle") |
|
62
|
|
|
async def _battle(self, ctx, game_tag: str): |
|
63
|
|
|
tag, identifier = game_tag.split("#") |
|
64
|
|
|
URL = f"https://cod.tracker.gg/warzone/profile/battlenet/{tag}%23{identifier}/overview" |
|
65
|
|
|
await self.get_stats(ctx, URL, game_tag) |
|
66
|
|
|
|
|
67
|
|
|
@warzone.command(name="psn") |
|
68
|
|
|
async def _psn(self, ctx, game_tag: str): |
|
69
|
|
|
URL = f"https://cod.tracker.gg/warzone/profile/psn/{game_tag}/overview" |
|
70
|
|
|
await self.get_stats(ctx, URL, game_tag) |
|
71
|
|
|
|
|
72
|
|
|
@warzone.command(name="activision") |
|
73
|
|
|
async def _activision(self, ctx, *, game_tag: str): |
|
74
|
|
|
game_tag = game_tag.translate(str.maketrans({" ": "%20", "#": "%23"})) |
|
75
|
|
|
URL = f"https://cod.tracker.gg/warzone/profile/atvi/{game_tag}/overview" |
|
76
|
|
|
await self.get_stats(ctx, URL, game_tag) |
|
77
|
|
|
|
|
78
|
|
|
@warzone.command(name="xbox") |
|
79
|
|
|
async def _xbox(self, ctx, *, game_tag: str): |
|
80
|
|
|
game_tag = game_tag.translate(str.maketrans({" ": "%20"})) |
|
81
|
|
|
URL = f"https://cod.tracker.gg/warzone/profile/xbl/{game_tag}/overview" |
|
82
|
|
|
await self.get_stats(ctx, URL, game_tag) |
|
83
|
|
|
|
|
84
|
|
|
@_battle.error |
|
85
|
|
|
@_psn.error |
|
86
|
|
|
@_activision.error |
|
87
|
|
|
@_xbox.error |
|
88
|
|
|
async def _error(self, ctx, error): |
|
89
|
|
|
if isinstance(error, commands.MissingRequiredArgument): |
|
90
|
|
|
await send_embedded(ctx, "The game tag is missing!") |
|
91
|
|
|
elif isinstance(error, commands.BadArgument): |
|
92
|
|
|
await send_embedded(ctx, "The game tag is missing!") |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
def setup(bot): |
|
96
|
|
|
bot.add_cog(COD(bot)) |
|
97
|
|
|
|