|
18 | 18 |
|
19 | 19 | delete_btn = Button(style=ButtonStyle.RED, custom_id="delete", emoji="🗑️")
|
20 | 20 |
|
21 |
| -load_dotenv() |
22 |
| -api_key = os.getenv("LICHESS_API") |
| 21 | +try: |
| 22 | + load_dotenv() |
| 23 | + api_key = os.getenv("LICHESS_API") |
23 | 24 |
|
24 |
| -session = lichess.TokenSession(api_key) |
25 |
| -client = lichess.Client(session=session) |
| 25 | + session = lichess.TokenSession(api_key) |
| 26 | + client = lichess.Client(session=session) |
| 27 | +except: |
| 28 | + print("Lichess API key not found") |
| 29 | + client = None |
26 | 30 |
|
27 | 31 |
|
28 | 32 | class Lichess(Extension):
|
29 |
| - @slash_command(name="lichess", description="Get lichess stats") |
30 |
| - @slash_option( |
31 |
| - name="username", |
32 |
| - description="Input a username to get stats", |
33 |
| - required=True, |
34 |
| - opt_type=OptionType.STRING, |
35 |
| - ) |
36 |
| - async def lichess(self, ctx, username): |
37 |
| - |
38 |
| - # TODO: Get the user statuses to work |
39 |
| - |
40 |
| - try: |
41 |
| - user = client.users.get_public_data(username) |
42 |
| - # userstatus = client.users.get_realtime_statuses(user)[0].get("online") |
43 |
| - except Exception as e: |
44 |
| - await ctx.send(f"```{e}```") |
45 |
| - return |
46 |
| - if user["perfs"]["bullet"]["games"] < 10: |
47 |
| - user["perfs"]["bullet"]["rating"] = str(user["perfs"]["bullet"]["rating"]) + "?" |
48 |
| - if user["perfs"]["blitz"]["games"] < 10: |
49 |
| - user["perfs"]["blitz"]["rating"] = str(user["perfs"]["blitz"]["rating"]) + "?" |
50 |
| - if user["perfs"]["rapid"]["games"] < 10: |
51 |
| - user["perfs"]["rapid"]["rating"] = str(user["perfs"]["rapid"]["rating"]) + "?" |
52 |
| - if user["perfs"]["classical"]["games"] < 10: |
53 |
| - user["perfs"]["classical"]["rating"] = str(user["perfs"]["classical"]["rating"]) + "?" |
54 |
| - if user["perfs"].get("puzzle") == None: |
55 |
| - user["perfs"]["puzzle"] = {"games": 0, "rating": 1500} |
56 |
| - if user["perfs"]["puzzle"]["games"] < 10: |
57 |
| - user["perfs"]["puzzle"]["rating"] = str(user["perfs"]["puzzle"]["rating"]) + "?" |
58 |
| - # if userstatus: |
59 |
| - # if user["patron"]: |
60 |
| - # status = f"<:Don_online:1059387820097142794> [{user['username']}]({user['url']})" |
61 |
| - # else: |
62 |
| - # status = f"<:Nor_online:1059387814854271006> [{user['username']}]({user['url']})" |
63 |
| - # elif userstatus == None: |
64 |
| - # if user["patron"]: |
65 |
| - # status = f"<:Don_offline:1059387818566238269> [{user['username']}]({user['url']})" |
66 |
| - # else: |
67 |
| - # status = f"<:Nor_offline:1059387816833982514> [{user['username']}]({user['url']})" |
68 |
| - if user.get("patron"): |
69 |
| - user["patron"] = "<:Donator:1059387813235273728> Activated" |
70 |
| - else: |
71 |
| - user["patron"] = "<:Normal:1059387811687567380> Not activated" |
72 |
| - embed = Embed( |
73 |
| - title=f"{user['username']}'s Lichess Stats", |
74 |
| - thumbnail="https://cdn.discordapp.com/attachments/1054486043665125436/1059235301056331816/Lichess_Logo_2019.png", |
75 |
| - color=Color.from_hex("#FFFFFF"), |
76 |
| - timestamp=datetime.now(), |
77 |
| - ) |
78 |
| - embed.set_footer( |
79 |
| - text=f"Requested by {ctx.author.display_name}", icon_url=ctx.author.avatar.url |
80 |
| - ) |
81 |
| - embed.add_field( |
82 |
| - name="Ratings", |
83 |
| - value=f""" |
84 |
| -> **Bullet:** {user['perfs']['bullet']['rating']} |
85 |
| -> **Blitz:** {user['perfs']['blitz']['rating']} |
86 |
| -> **Rapid:** {user['perfs']['rapid']['rating']} |
87 |
| -> **Classical:** {user['perfs']['classical']['rating']} |
88 |
| -> **Puzzle:** {user['perfs']['puzzle']['rating']} |
89 |
| -""", |
90 |
| - ) |
91 |
| - embed.add_field( |
92 |
| - name="Stats", |
93 |
| - value=f""" |
94 |
| -> **Games:** {user['count']['all']} |
95 |
| -> **Wins:** {user['count']['win']} |
96 |
| -> **Losses:** {user['count']['loss']} |
97 |
| -> **Draws:** {user['count']['draw']} |
98 |
| -""", |
| 33 | + if client != None: |
| 34 | + @slash_command(name="lichess", description="Get lichess stats") |
| 35 | + @slash_option( |
| 36 | + name="username", |
| 37 | + description="Input a username to get stats", |
| 38 | + required=True, |
| 39 | + opt_type=OptionType.STRING, |
99 | 40 | )
|
100 |
| - embed.add_field(name="Patron status", value=f"> {user['patron']}") |
101 |
| - # embed.add_field(name="Full profile", value=f"> {status}") |
102 |
| - await ctx.send(embed=embed, components=[delete_btn]) |
| 41 | + async def lichess(self, ctx, username): |
| 42 | + try: |
| 43 | + user = client.users.get_public_data(username) |
| 44 | + # userstatus = client.users.get_realtime_statuses(user)[0].get("online") |
| 45 | + except Exception as e: |
| 46 | + await ctx.send(f"```{e}```") |
| 47 | + return |
| 48 | + if user["perfs"]["bullet"]["games"] < 10: |
| 49 | + user["perfs"]["bullet"]["rating"] = str(user["perfs"]["bullet"]["rating"]) + "?" |
| 50 | + if user["perfs"]["blitz"]["games"] < 10: |
| 51 | + user["perfs"]["blitz"]["rating"] = str(user["perfs"]["blitz"]["rating"]) + "?" |
| 52 | + if user["perfs"]["rapid"]["games"] < 10: |
| 53 | + user["perfs"]["rapid"]["rating"] = str(user["perfs"]["rapid"]["rating"]) + "?" |
| 54 | + if user["perfs"]["classical"]["games"] < 10: |
| 55 | + user["perfs"]["classical"]["rating"] = str(user["perfs"]["classical"]["rating"]) + "?" |
| 56 | + if user["perfs"].get("puzzle") == None: |
| 57 | + user["perfs"]["puzzle"] = {"games": 0, "rating": 1500} |
| 58 | + if user["perfs"]["puzzle"]["games"] < 10: |
| 59 | + user["perfs"]["puzzle"]["rating"] = str(user["perfs"]["puzzle"]["rating"]) + "?" |
| 60 | + # if userstatus: |
| 61 | + # if user["patron"]: |
| 62 | + # status = f"<:Don_online:1059387820097142794> [{user['username']}]({user['url']})" |
| 63 | + # else: |
| 64 | + # status = f"<:Nor_online:1059387814854271006> [{user['username']}]({user['url']})" |
| 65 | + # elif userstatus == None: |
| 66 | + # if user["patron"]: |
| 67 | + # status = f"<:Don_offline:1059387818566238269> [{user['username']}]({user['url']})" |
| 68 | + # else: |
| 69 | + # status = f"<:Nor_offline:1059387816833982514> [{user['username']}]({user['url']})" |
| 70 | + if user.get("patron"): |
| 71 | + user["patron"] = "<:Donator:1059387813235273728> Activated" |
| 72 | + else: |
| 73 | + user["patron"] = "<:Normal:1059387811687567380> Not activated" |
| 74 | + embed = Embed( |
| 75 | + title=f"{user['username']}'s Lichess Stats", |
| 76 | + thumbnail="https://cdn.discordapp.com/attachments/1054486043665125436/1059235301056331816/Lichess_Logo_2019.png", |
| 77 | + color=Color.from_hex("#FFFFFF"), |
| 78 | + timestamp=datetime.now(), |
| 79 | + ) |
| 80 | + embed.set_footer( |
| 81 | + text=f"Requested by {ctx.author.display_name}", icon_url=ctx.author.avatar.url |
| 82 | + ) |
| 83 | + embed.add_field( |
| 84 | + name="Ratings", |
| 85 | + value=f""" |
| 86 | + > **Bullet:** {user['perfs']['bullet']['rating']} |
| 87 | + > **Blitz:** {user['perfs']['blitz']['rating']} |
| 88 | + > **Rapid:** {user['perfs']['rapid']['rating']} |
| 89 | + > **Classical:** {user['perfs']['classical']['rating']} |
| 90 | + > **Puzzle:** {user['perfs']['puzzle']['rating']} |
| 91 | + """, |
| 92 | + ) |
| 93 | + embed.add_field( |
| 94 | + name="Stats", |
| 95 | + value=f""" |
| 96 | + > **Games:** {user['count']['all']} |
| 97 | + > **Wins:** {user['count']['win']} |
| 98 | + > **Losses:** {user['count']['loss']} |
| 99 | + > **Draws:** {user['count']['draw']} |
| 100 | + """, |
| 101 | + ) |
| 102 | + embed.add_field(name="Patron status", value=f"> {user['patron']}") |
| 103 | + # embed.add_field(name="Full profile", value=f"> {status}") |
| 104 | + await ctx.send(embed=embed, components=[delete_btn]) |
103 | 105 |
|
104 | 106 |
|
105 | 107 | def setup(bot):
|
|
0 commit comments