|
| 1 | +import discord |
| 2 | +import aiohttp |
| 3 | +import json |
| 4 | +import os |
| 5 | +import datetime |
| 6 | +import typing |
| 7 | +from discord.ext import commands |
| 8 | +from psycopg2 import Timestamp |
| 9 | + |
| 10 | + |
| 11 | +class Codeforces(commands.Cog): |
| 12 | + cf_red = 0xff0000 # 2400+ |
| 13 | + cf_orange = 0xff8c00 # 2200 - 2399 |
| 14 | + cf_violet = 0xb200aa # 1900 - 2199 |
| 15 | + cf_blue = 0x0000ff # 1600 - 1899 |
| 16 | + cf_cyan = 0x03a89e # 1400 - 1599 |
| 17 | + cf_green = 0x008000 # 1200 - 1399 |
| 18 | + cf_gray = 0x808080 # 0 - 1199 |
| 19 | + |
| 20 | + def __init__(self, bot): |
| 21 | + self.bot = bot |
| 22 | + # ---------------------------------------------------------------------------------------------------------------------- |
| 23 | + |
| 24 | + async def get_user(self, username): |
| 25 | + API_URL = "https://codeforces.com/api/user.info?handles=" + username + ";" |
| 26 | + async with aiohttp.ClientSession() as session: |
| 27 | + async with session.get(API_URL) as resp: |
| 28 | + user_json = await resp.json() |
| 29 | + return (user_json) |
| 30 | + |
| 31 | + @commands.command(name="cf-handle", aliases=["cf-user"], help="shows details of the requested codeforces handle") |
| 32 | + async def send_user(self, ctx, username): |
| 33 | + handle = await self.get_user(username) |
| 34 | + if handle["status"] == "FAILED": |
| 35 | + async with ctx.typing(): |
| 36 | + embed = discord.Embed(title="Error", |
| 37 | + description=handle["comment"], |
| 38 | + colour=0xff0000, |
| 39 | + timestamp=datetime.datetime.utcnow()) |
| 40 | + await ctx.send(embed=embed) |
| 41 | + return |
| 42 | + async with ctx.typing(): |
| 43 | + embed = discord.Embed(title=username, |
| 44 | + description=(handle["result"][0]["firstName"] if "firstName" in handle["result"][0] else "") + " " + ( |
| 45 | + handle["result"][0]["lastName"] if "lastName" in handle["result"][0] else ""), |
| 46 | + colour=self.cf_red if handle["result"][0]["maxRating"] >= 2400 else self.cf_orange if handle["result"][0]["maxRating"] >= 2200 else self.cf_violet if handle["result"][0]["maxRating"] >= 1900 else self.cf_blue if handle[ |
| 47 | + "result"][0]["maxRating"] >= 1600 else self.cf_cyan if handle["result"][0]["maxRating"] >= 1400 else self.cf_green if handle["result"][0]["maxRating"] >= 1200 else self.cf_gray, |
| 48 | + timestamp=datetime.datetime.utcnow()) |
| 49 | + embed.add_field(name="City", value=handle["result"][0]["city"] |
| 50 | + if "city" in handle["result"][0] else "Unknown", inline=True) |
| 51 | + embed.add_field(name="Country", value=handle["result"][0]["country"] |
| 52 | + if "country" in handle["result"][0] else "Unknown", inline=True) |
| 53 | + embed.add_field( |
| 54 | + name="Friend of", value=handle["result"][0]["friendOfCount"], inline=True) |
| 55 | + embed.add_field(name="Max Rating", |
| 56 | + value=handle["result"][0]["maxRating"], inline=True) |
| 57 | + embed.add_field(name="Max Rank", |
| 58 | + value=handle["result"][0]["maxRank"], inline=True) |
| 59 | + embed.add_field(name="Organization", value=handle["result"][0]["organization"] |
| 60 | + if handle["result"][0]["organization"] != "" else "Unknown", inline=True) |
| 61 | + embed.add_field( |
| 62 | + name="Rating", value=handle["result"][0]["rating"], inline=True) |
| 63 | + embed.add_field( |
| 64 | + name="Rank", value=handle["result"][0]["rank"], inline=True) |
| 65 | + embed.add_field(name="Last Online", value=datetime.datetime.utcfromtimestamp( |
| 66 | + handle["result"][0]["lastOnlineTimeSeconds"]).strftime('%Y-%m-%d %H:%M:%S'), inline=True) |
| 67 | + embed.set_thumbnail(url=handle["result"][0]["avatar"]) |
| 68 | + await ctx.send(embed=embed) |
| 69 | + # ---------------------------------------------------------------------------------------------------------------------- |
| 70 | + |
| 71 | + |
| 72 | +def setup(bot): |
| 73 | + bot.add_cog(Codeforces(bot)) |
0 commit comments