async def stats(self, ctx): DATA = utils.from_json(utils.DATA_PATH) my_csv = utils.from_json(utils.CSV_DATA_PATH) t, r, c = utils.difference_on_update(my_csv, DATA) confirmed = DATA['total']['confirmed'] recovered = DATA['total']['recovered'] deaths = DATA['total']['deaths'] embed = discord.Embed( description="[+**CURRENT_UPDATE-MORNING_UPDATE**]", timestamp=utils.discord_timestamp(), color=utils.COLOR, ) embed.set_author(name="Coronavirus COVID-19 Stats", icon_url=self.author_thumb) embed.set_thumbnail(url=self.thumb) embed.add_field(name="<:confirmed:688686089548202004> Confirmed", value=f"**{confirmed}** [+**{t}**]", inline=False) embed.add_field( name="<:recov:688686059567185940> Recovered", value= f"**{recovered}** ({utils.percentage(confirmed, recovered)}) [+**{r}**]", inline=False) embed.add_field( name="<:_death:688686194917244928> Deaths", value= f"**{deaths}** ({utils.percentage(confirmed, deaths)}) [+**{c}**]", inline=False) embed.set_footer(text=utils.last_update("stats.png"), icon_url=ctx.guild.me.avatar_url) with open("stats.png", "rb") as p: img = discord.File(p, filename="stats.png") embed.set_image(url=f'attachment://stats.png') await ctx.send(file=img, embed=embed)
async def info(self, ctx): DATA = utils.from_json(utils.DATA_PATH) if ctx.author.is_on_mobile(): header, text = utils.string_formatting(DATA) embed = discord.Embed(description=header + "\n\n" + text, color=utils.COLOR, timestamp=utils.discord_timestamp()) else: my_csv = utils.from_json(utils.CSV_DATA_PATH) embed = utils.make_tab_embed_all() c, r, d = utils.difference_on_update(my_csv, DATA) tot = DATA['total'] header = utils.mkheader( tot["confirmed"], c, tot["recovered"], utils.percentage(tot["confirmed"], tot["recovered"]), r, tot["deaths"], utils.percentage(tot["confirmed"], tot["deaths"]), d, False) embed.description = header embed.set_author( name=f"All countries affected by Coronavirus COVID-19", url="https://www.who.int/home", icon_url=self.author_thumb) embed.set_thumbnail(url=self.thumb) embed.set_footer(text=utils.last_update(utils.DATA_PATH), icon_url=ctx.guild.me.avatar_url) with open("stats.png", "rb") as p: img = discord.File(p, filename="stats.png") embed.set_image(url=f'attachment://stats.png') await ctx.send(file=img, embed=embed)
async def send_tracker(self): tracked = db.send_tracker() DATA = utils.from_json(utils.DATA_PATH) tot = DATA['total'] my_csv = utils.from_json(utils.CSV_DATA_PATH) c, r, d = utils.difference_on_update(my_csv, DATA) header = utils.mkheader( tot["confirmed"], c, tot["recovered"], utils.percentage(tot["confirmed"], tot["recovered"]), r, tot["deaths"], utils.percentage(tot["confirmed"], tot["deaths"]), d, False) for t in tracked: try: dm = self.bot.get_user(int(t["user_id"])) # embed = utils.make_tab_embed_all(is_country=True, params=t["country"].split(" ")) header, text = utils.string_formatting(DATA, t["country"].split(" ")) embed = discord.Embed(description=header + "\n\n" + text, color=utils.COLOR, timestamp=utils.discord_timestamp()) embed.set_author( name="Personal tracker for Coronavirus COVID-19", url="https://www.who.int/home", icon_url=self.author_thumb) embed.set_thumbnail(url=self.thumb) try: guild = self.bot.get_guild(int(t["guild_id"])) embed.set_footer(text=utils.last_update(utils.DATA_PATH), icon_url=guild.me.avatar_url) except: pass await dm.send(embed=embed) except: pass
async def country(self, ctx, *country): if not len(country): embed = discord.Embed( description="No args provided, I can't tell you which country/region is affected if you won't tell me everything!", color=utils.COLOR, timestamp=utils.discord_timestamp() ) else: DATA = utils.from_json(utils.DATA_PATH) header, text = utils.string_formatting( DATA, country ) if len(text) > 0: embed = discord.Embed( description=header + "\n" + text, color=utils.COLOR, timestamp=utils.discord_timestamp() ) else: embed = discord.Embed( description="Wrong country selected or this country/region is not affected by Coronavirus COVID-19", color=utils.COLOR, timestamp=utils.discord_timestamp() ) embed.set_author(name="Current Region/Country affected by Coronavirus COVID-19", url="https://www.who.int/home", icon_url=self.author_thumb) embed.set_thumbnail(url=self.thumb) embed.set_footer( text=utils.last_update(utils.DATA_PATH), icon_url=ctx.guild.me.avatar_url ) await ctx.send(embed=embed)
def __init__(self, *args, loop=None, **kwargs): super().__init__(command_prefix=when_mentioned_or("c!"), activity=discord.Game(name="Starting..."), status=discord.Status.dnd) self.remove_command("help") self._load_extensions() data = utils.from_json(utils.DATA_PATH)
async def on_ready(self): # waiting internal cache to be ready await self.wait_until_ready() i = 0 while True: try: data = utils.from_json(utils.DATA_PATH) if i == 0: confirmed = data["total"]["confirmed"] await self.change_presence(activity=discord.Game( name="[c!help] | {} Confirmed".format(confirmed))) elif i == 1: deaths = data["total"]["deaths"] await self.change_presence(activity=discord.Game( name="[c!help] | {} Deaths".format(deaths))) else: recovered = data["total"]["recovered"] await self.change_presence(activity=discord.Game( name="[c!help] | {} Recovered".format(recovered))) i = (i + 1) % 3 except Exception as e: logger.exception(e, exc_info=True) await self.change_presence(activity=discord.Game( name="[c!help] | Updating data...")) await asyncio.sleep(random.randint(15, 20))
async def news(self, ctx): news = utils.from_json(utils.NEWS_PATH) embed = discord.Embed( title= ":newspaper: Recent news about Coronavirus COVID-19 :newspaper:", timestamp=utils.discord_timestamp(), color=utils.COLOR) sources = [] length = 0 max_size = 5800 for n in news["articles"]: source = n["source"]["name"] if source not in sources: sources.append(source) else: continue try: length += len( f"🞄 **{source}** : {n['title']} {n['description']} [Link]({n['url']})" ) if length >= max_size: break embed.add_field( name=f"🞄 **{source}** : {n['title']}", value=f"{n['description']} [Link]({n['url']})", inline=False) except discord.errors.HTTPException: break embed.set_thumbnail( url="https://avatars2.githubusercontent.com/u/32527401?s=400&v=4") embed.set_footer(text=utils.last_update(utils.NEWS_PATH) + " | newsapi.org", icon_url=ctx.guild.me.avatar_url) await ctx.send(embed=embed)
async def main(self): await self.bot.wait_until_ready() await self.parse_and_update() starting = True while True: before = time.time() if not starting: old_data = utils.from_json(utils.DATA_PATH) try: await self.parse_and_update() except: pass new_data = utils.from_json(utils.DATA_PATH) await self.send_notifications(old_data, new_data) await self.send_tracker() else: starting = False after = time.time() await asyncio.sleep(3600 - int(after - before))
async def country(self, ctx, *country): if not len(country): embed = discord.Embed( description= "No args provided, I can't tell you which country/region is affected if you won't tell me everything!", color=utils.COLOR, timestamp=utils.discord_timestamp()) else: DATA = utils.from_json(utils.DATA_PATH) is_mobile = ctx.author.is_on_mobile() if is_mobile: header, text = utils.string_formatting(DATA, country) embed = discord.Embed(description=header + "\n\n" + text, color=utils.COLOR, timestamp=utils.discord_timestamp()) else: try: embed = utils.make_tab_embed_all(is_country=True, params=country) c, r, d = utils.difference_on_update( utils.from_json(utils.CSV_DATA_PATH), DATA) tot = DATA['total'] header = utils.mkheader( tot["confirmed"], c, tot["recovered"], utils.percentage(tot["confirmed"], tot["recovered"]), r, tot["deaths"], utils.percentage(tot["confirmed"], tot["deaths"]), d, is_mobile) embed.description = header except: embed = discord.Embed( description= "Wrong country selected.\nViews information about multiple chosen country/region. You can either use autocompletion or country code. Valid country/region are listed in `c!info`.\nExample : `c!country fr germ it poland`", color=utils.COLOR, timestamp=utils.discord_timestamp()) embed.set_author(name="Country affected by COVID-19", url="https://www.who.int/home", icon_url=self.author_thumb) embed.set_thumbnail(url=self.thumb) embed.set_footer(text=utils.last_update(utils.DATA_PATH), icon_url=ctx.guild.me.avatar_url) await ctx.send(embed=embed)
async def main(self): await self.bot.wait_until_ready() starting = True while True: channels_id = db.to_send() if not os.path.exists(utils._CONFIRMED_PATH): self._csv_update() if self.diff_checker(utils.data_reader(utils._CONFIRMED_PATH)): logger.info("Datas are up to date") else: self._csv_update() old_data = utils.from_json(utils.DATA_PATH) self.cache() new_data = utils.from_json(utils.DATA_PATH) logger.info("New data downloaded") plot_csv() logger.info("New plot generated") if not starting: await self.send_notifications(channels_id, old_data, new_data) else: starting = False await asyncio.sleep(3600)
async def on_guild_join(self, guild: discord.Guild): general = find(lambda x: x.name == "general", guild.text_channels) if general and general.permissions_for(guild.me).send_messages: data = utils.from_json(utils.DATA_PATH) embed = discord.Embed( title="Coronavirus COVID-19 Tracker", description= "[Wold Health Organization advices](https://www.who.int/emergencies/diseases/novel-coronavirus-2019/advice-for-public)", timestamp=datetime.datetime.utcfromtimestamp(time.time()), color=utils.COLOR) embed.set_thumbnail( url= "https://upload.wikimedia.org/wikipedia/commons/thumb/2/26/COVID-19_Outbreak_World_Map.svg/langfr-280px-COVID-19_Outbreak_World_Map.svg.png" ) embed.add_field( name="Vote", value="[Click here](https://top.gg/bot/682946560417333283/vote)" ) embed.add_field( name="Invite Coronavirus COVID-19", value= "[Click here](https://discordapp.com/oauth2/authorize?client_id=682946560417333283&scope=bot&permissions=313408)" ) embed.add_field( name="Discord Support", value="[Click here](https://discordapp.com/invite/wTxbQYb)") embed.add_field( name="Source code and commands", value= "[Click here](https://github.com/takitsu21/covid-19-tracker)") embed.add_field(name="Help command", value="c!help") embed.add_field(name="Prefix", value="c!") nb_users = 0 for s in self.guilds: nb_users += len(s.members) embed.add_field(name="Total confirmed", value=data["total"]["confirmed"], inline=False) embed.add_field(name="Total recovered", value=data["total"]["recovered"], inline=False) embed.add_field(name="Total deaths", value=data["total"]["deaths"], inline=False) embed.add_field(name="Servers", value=len(self.guilds)) embed.add_field(name="Members", value=nb_users) embed.set_footer(text="Made by Taki#0853 (WIP)", icon_url=guild.me.avatar_url) await general.send(embed=embed)
async def about(self, ctx): DATA = utils.from_json(utils.DATA_PATH) embed = discord.Embed( description= "[Wold Health Organization advices](https://www.who.int/emergencies/diseases/novel-coronavirus-2019/advice-for-public)", timestamp=utils.discord_timestamp(), color=utils.COLOR) embed.set_author(name="Coronavirus COVID-19 Tracker", icon_url=ctx.guild.me.avatar_url) embed.set_thumbnail(url=self.thumb) embed.add_field( name="Vote", value="[Click here](https://top.gg/bot/682946560417333283/vote)") embed.add_field( name="Invite Coronavirus COVID-19", value= "[Click here](https://discordapp.com/oauth2/authorize?client_id=682946560417333283&scope=bot&permissions=313408)" ) embed.add_field( name="Discord Support", value="[Click here](https://discordapp.com/invite/wTxbQYb)") embed.add_field( name="Source code", value="[Click here](https://github.com/takitsu21/covid-19-tracker)" ) embed.add_field(name="Help command", value="c!help") embed.add_field(name="Prefix", value="c!") embed.add_field( name="Donate", value= "[Patreon](https://www.patreon.com/takitsu)\nBuy me a [Ko-fi](https://ko-fi.com/takitsu)" ) nb_users = 0 for s in self.bot.guilds: nb_users += len(s.members) embed.add_field(name="Confirmed", value=DATA["total"]["confirmed"]) embed.add_field(name="Recovered", value=DATA["total"]["recovered"]) embed.add_field(name="Deaths", value=DATA["total"]["deaths"]) embed.add_field(name="Servers", value=len(self.bot.guilds)) embed.add_field(name="Members", value=nb_users) embed.set_footer(text="Made by Taki#0853 (WIP) " + utils.last_update(utils.DATA_PATH), icon_url=ctx.guild.me.avatar_url) await ctx.send(embed=embed)
async def info_error(self, ctx, error): DATA = utils.from_json(utils.DATA_PATH) header, text = utils.string_formatting(DATA) embed = discord.Embed( description=header + "\n" + text, color=utils.COLOR, timestamp=utils.discord_timestamp() ) embed.set_author(name="Country/Region affected by Coronavirus COVID-19 (Cases confirmed)", url="https://www.who.int/home", icon_url=self.author_thumb) embed.set_thumbnail(url=self.thumb) embed.set_footer( text=utils.last_update(utils.DATA_PATH), icon_url=ctx.guild.me.avatar_url ) with open("stats.png", "rb") as p: img = discord.File(p, filename="stats.png") embed.set_image(url=f'attachment://stats.png') await ctx.send(file=img, embed=embed)
async def track(self, ctx, *country): if not len(country): embed = discord.Embed( description= "No country provided. **`c!track <COUNTRY>`** work like **`c!country <COUNTRY>`** see `c!help`", color=utils.COLOR, timestamp=utils.discord_timestamp()) embed.set_author(name="c!track", icon_url=self.author_thumb) elif ''.join(country) == "disable": embed = discord.Embed( description= "If you want to reactivate the tracker : **`c!track <COUNTRY>`**", color=utils.COLOR, timestamp=utils.discord_timestamp()) embed.set_author(name="Tracker has been disabled!", icon_url=self.author_thumb) try: db.delete_tracker(str(ctx.author.id)) except: pass else: DATA = utils.from_json(utils.DATA_PATH) if ctx.author.is_on_mobile(): header, text = utils.string_formatting(DATA, country) embed = discord.Embed(description=header + "\n\n" + text, color=utils.COLOR, timestamp=utils.discord_timestamp()) if len(embed.description) > 0: try: db.insert_tracker(str(ctx.author.id), str(ctx.guild.id), ' '.join(country)) except IntegrityError: db.update_tracker(str(ctx.author.id), ' '.join(country)) embed.set_author( name= "Tracker has been set up! You can see your tracked list. Updates will be send in DM", icon_url=self.author_thumb) else: embed = discord.Embed( description="Wrong country selected.", color=utils.COLOR, timestamp=utils.discord_timestamp()) embed.set_author(name="c!track", icon_url=self.author_thumb) else: embed = utils.make_tab_embed_all(is_country=True, params=country) my_csv = utils.from_json(utils.CSV_DATA_PATH) c, r, d = utils.difference_on_update(my_csv, DATA) tot = DATA['total'] header = utils.mkheader( tot["confirmed"], c, tot["recovered"], utils.percentage(tot["confirmed"], tot["recovered"]), r, tot["deaths"], utils.percentage(tot["confirmed"], tot["deaths"]), d, False) embed.description = header if len(embed.fields[0].value) > 0: try: db.insert_tracker(str(ctx.author.id), str(ctx.guild.id), ' '.join(country)) except IntegrityError: db.update_tracker(str(ctx.author.id), ' '.join(country)) embed.set_author( name= "Tracker has been set up! You can see your tracked list. Updates will be send in DM", icon_url=self.author_thumb) else: embed = discord.Embed( description="Wrong country selected.", color=utils.COLOR, timestamp=utils.discord_timestamp()) embed.set_author(name="c!track", icon_url=self.author_thumb) embed.set_thumbnail(url=self.thumb) embed.set_footer(text=utils.last_update(utils.DATA_PATH), icon_url=ctx.guild.me.avatar_url) await ctx.send(embed=embed)
async def info(self, ctx): DATA = utils.from_json(utils.DATA_PATH) header, text = utils.string_formatting(DATA) # pages = self.generate_pages(DATA, len(header)) # index = 0 # page_length = len(pages) # print(page_length) # if page_length > 1: # page_left = "⬅️" # page_right = "➡️" # base_reactions = [page_left, page_right] # embed.set_author( # name=f"Page {index + 1}", # url="https://www.who.int/home", # icon_url=self.author_thumb # ) # pagination = await ctx.send(embed=embed) # for reaction in base_reactions: # await pagination.add_reaction(reaction) # while True: # def check(reaction, user): # return user == ctx.message.author and str(reaction.emoji) in base_reactions # try: # reaction, user = await self.bot.wait_for('reaction_add', check=check, timeout=300.0) # emoji = str(reaction.emoji) # except asyncio.TimeoutError: # try: # await ctx.message.delete() # except: # pass # return await pagination.delete() # if page_left in emoji and index > 0: # index -= 1 # elif page_right in emoji and index < page_length: # index += 1 # if index >= 0 and index <= page_length: # embed = discord.Embed( # description=pages[0][index] # ) # embed.set_author( # name=f"Page {index + 1}", # url="https://www.who.int/home", # icon_url=self.author_thumb # ) # embed.color = utils.COLOR # embed.timestamp = utils.discord_timestamp() # embed.set_thumbnail(url=self.thumb) # embed.set_footer(text=utils.last_update(utils.DATA_PATH), # icon_url=ctx.guild.me.avatar_url) # await pagination.edit(embed=embed) # for i, _p in enumerate(pages): # embed = discord.Embed( # description=header + "\n" + _p[i], # color=utils.COLOR, # timestamp=utils.discord_timestamp() # ) # embed.set_author( # name=f"Coronavrius COVID-19 Pages {i + 1}/{page_length} (Cases confirmed)", # url="https://www.who.int/home", # icon_url=self.author_thumb # ) # embed.color = utils.COLOR # embed.timestamp = utils.discord_timestamp() # embed.set_thumbnail(url=self.thumb) # embed.set_footer(text=utils.last_update(utils.DATA_PATH), # icon_url=ctx.guild.me.avatar_url) # with open("stats.png", "rb") as p: # img = discord.File(p, filename="stats.png") # embed.set_image(url=f'attachment://stats.png') # await ctx.send(file=img, embed=embed) # else: embed = discord.Embed( description=header + "\n" + text, color=utils.COLOR, timestamp=utils.discord_timestamp() ) embed.set_author( name=f"Country/Region affected by Coronavirus COVID-19 (Cases confirmed)", url="https://www.who.int/home", icon_url=self.author_thumb ) embed.color = utils.COLOR embed.timestamp = utils.discord_timestamp() embed.set_thumbnail(url=self.thumb) embed.set_footer(text=utils.last_update(utils.DATA_PATH), icon_url=ctx.guild.me.avatar_url) with open("stats.png", "rb") as p: img = discord.File(p, filename="stats.png") embed.set_image(url=f'attachment://stats.png') await ctx.send(file=img, embed=embed)