|
| 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 | + |
| 9 | + |
| 10 | +class Other(commands.Cog): |
| 11 | + def __init__(self, bot): |
| 12 | + self.bot = bot |
| 13 | + # ---------------------------------------------------------------------------------------------------------------------- |
| 14 | + |
| 15 | + async def get_covid19_details(self): |
| 16 | + API_URL = "https://api.covid19api.com/summary" |
| 17 | + async with aiohttp.ClientSession() as session: |
| 18 | + async with session.get(API_URL) as resp: |
| 19 | + data_json = await resp.json() |
| 20 | + return (data_json) |
| 21 | + |
| 22 | + @commands.command(name="covid19", help="sends COVID-19 stats of the given country (global stats if country == null)") |
| 23 | + async def covid19_data(self, ctx, *args): |
| 24 | + countr = "" |
| 25 | + for arg in args: |
| 26 | + countr += arg + " " |
| 27 | + size = len(countr) |
| 28 | + country = countr[:size - 1] |
| 29 | + original = country |
| 30 | + found = False |
| 31 | + stats = await self.get_covid19_details() |
| 32 | + if country: |
| 33 | + for k in stats["Countries"]: |
| 34 | + if ((k["CountryCode"]).lower() == country.lower()) or ((k["Country"]).lower() == country.lower()): |
| 35 | + embed = discord.Embed( |
| 36 | + title=(k["Country"]).title(), |
| 37 | + description="COVID-19 Statistics", |
| 38 | + colour=0xff0000, |
| 39 | + timestamp=datetime.datetime.utcnow() |
| 40 | + ) |
| 41 | + flag_url = "https://flagcdn.com/w640/" + \ |
| 42 | + str(k["CountryCode"]).lower() + ".jpg" |
| 43 | + embed.set_thumbnail(url=flag_url) |
| 44 | + fields = [ |
| 45 | + ("New Confirmed Cases", k["NewConfirmed"], True), |
| 46 | + ("Total Confirmed Cases", k["TotalConfirmed"], True), |
| 47 | + ("Country Code", k["CountryCode"], True), |
| 48 | + ("New Deaths", k["NewDeaths"], True), |
| 49 | + ("Total Deaths", k["TotalDeaths"], True), |
| 50 | + ("Report Time (UTC)", "Date: " + |
| 51 | + k["Date"][0:10] + " & Time: " + k["Date"][11:19], True), |
| 52 | + ] |
| 53 | + for n, v, i in fields: |
| 54 | + embed.add_field(name=n, value=v, inline=i) |
| 55 | + await ctx.send(embed=embed) |
| 56 | + found = True |
| 57 | + else: |
| 58 | + k = stats["Global"] |
| 59 | + embed = discord.Embed( |
| 60 | + title="Global", |
| 61 | + description="COVID-19 Statistics", |
| 62 | + colour=0xff0000, |
| 63 | + timestamp=datetime.datetime.utcnow() |
| 64 | + ) |
| 65 | + embed.set_thumbnail( |
| 66 | + url="https://user-images.githubusercontent.com/63065397/156144079-6f90504d-ad48-4f2e-bec5-bae31cebd858.png" |
| 67 | + ) |
| 68 | + fields = [ |
| 69 | + ("New Confirmed Cases", k["NewConfirmed"], True), |
| 70 | + ("Total Confirmed Cases", k["TotalConfirmed"], True), |
| 71 | + ("New Deaths", k["NewDeaths"], True), |
| 72 | + ("Total Deaths", k["TotalDeaths"], True), |
| 73 | + ] |
| 74 | + for n, v, i in fields: |
| 75 | + embed.add_field(name=n, value=v, inline=i) |
| 76 | + await ctx.send(embed=embed) |
| 77 | + found = True |
| 78 | + if not found: |
| 79 | + embed = discord.Embed( |
| 80 | + title="Error", |
| 81 | + description="Country Not Found", |
| 82 | + colour=0xff0000 |
| 83 | + ) |
| 84 | + embed.add_field(name="Given Country Name", |
| 85 | + value=original, inline=True) |
| 86 | + await ctx.send(embed=embed) |
| 87 | + # ---------------------------------------------------------------------------------------------------------------------- |
| 88 | + |
| 89 | + @commands.command(name="ping", aliases=["latency"], help="shows the latency of the bot") |
| 90 | + async def ping_command(self, ctx): |
| 91 | + async with ctx.typing(): |
| 92 | + ping = round(self.bot.latency * 1000, 1) |
| 93 | + high = 400 |
| 94 | + low = 30 |
| 95 | + red = min((ping)/high, 1) |
| 96 | + green = 1-red |
| 97 | + if ping >= high: |
| 98 | + red = 1 |
| 99 | + green = 0 |
| 100 | + if ping <= low: |
| 101 | + red = 0 |
| 102 | + green = 1 |
| 103 | + embed = discord.Embed( |
| 104 | + title="Ping", |
| 105 | + description="**"+str(ping)+"ms**", |
| 106 | + colour=discord.Color.from_rgb(int(red*255), int(green*255), 0), |
| 107 | + timestamp=datetime.datetime.utcnow() |
| 108 | + ) |
| 109 | + await ctx.send(embed=embed) |
| 110 | + # ---------------------------------------------------------------------------------------------------------------------- |
| 111 | + |
| 112 | +def setup(bot): |
| 113 | + bot.add_cog(Other(bot)) |
0 commit comments