-
Notifications
You must be signed in to change notification settings - Fork 8
/
bot.py
985 lines (889 loc) · 45.8 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
import json
from typing_extensions import TypeAlias
import requests
import pandas as pd
import io
import discord
import datetime
from babel.numbers import format_currency
from discord.ext import commands, tasks
from dotenv import load_dotenv
import os
from discord_slash import SlashCommand
import datetime
import dataframe_image as dfi
import math
import csv
import re
# Create a bot instance and sets a command prefix
client = commands.Bot(command_prefix='.', intents=discord.Intents.all())
client.remove_command('help')
# load the env variable token
load_dotenv()
url1 = os.getenv("url1")
url2 = os.getenv("url2")
url3 = os.getenv("url3")
# Saving file name
filename = 'states.csv'
file_daily = "states_yesterday.csv"
# Mapping for queries to states_yesterday
mapp = {"total": 'TT', 'andaman and nicobar islands': 'AN', "andhra pradesh": 'AP', "arunachal pradesh": 'AR', "assam": 'AS', "bihar": 'BR', "chandigarh": 'CH', "chhattisgarh": 'CT', "dadra and nagar haveli and daman and diu": 'DN', "dadra and nagar haveli and daman and diu": 'DD', "delhi": 'DL', "goa": 'GA', "gujarat": 'GJ', "haryana": 'HR', "himachal pradesh": 'HP', "jammu and kashmir": 'JK', "jharkhand": 'JH',
"karnataka": 'KA', "kerala": 'KL', "ladakh": 'LA', "lakshadweep": 'LD', "madhya pradesh": 'MP', "maharashtra": 'MH', "manipur": 'MN', "meghalaya": 'ML', "mizoram": 'MZ', "nagaland": 'NL', "odisha": 'OR', "puducherry": 'PY', "punjab": 'PB', "rajasthan": 'RJ', "sikkim": 'SK', "tamil nadu": 'TN', "telangana": 'TG', "tripura": 'TR', "uttar pradesh": 'UP', "uttarakhand": 'UT', "west bengal": 'WB', "state unassigned": 'UN'}
# Initialising df to something
df = 0
df_daily = 0
# Update variable
footer = 0
# Initialising states list
s = list()
s_id = []
# Initialising slash command
slash = SlashCommand(client, sync_commands=True)
@client.event
async def on_ready():
global df
global df_daily
global s
global footer
# Start updation loop
update.start()
update_daily.start()
alert.start()
clear.start()
# Create basic data frame and store
test = requests.get(
'https://api.covid19india.org/csv/latest/state_wise.csv')
test = str(test.text)
data = io.StringIO(test)
df = pd.read_csv(data, sep=",")
# Create states list
s = [df.iloc[i]['State'] for i in range(len(df))]
# Removing Total and State Unassigned
s = s[1:len(s) - 1]
# Making column lower case
df["State"] = df["State"].str.lower()
df.to_csv(filename)
print("df Updated at: ", datetime.datetime.now())
time = datetime.datetime.now() + datetime.timedelta(hours=5, minutes=30)
time = time.strftime("%d-%b") + ' at ' + time.strftime("%I:%M %p")
footer = f"Last Updated: {time}"
# Creating daily df
response = requests.get(
'https://api.covid19india.org/csv/latest/state_wise_daily.csv')
response = str(response.text)
data_daily = io.StringIO(response)
df1 = pd.read_csv(data_daily, sep=",")
# Get yestedays date in appropriate formatting
t = datetime.datetime.now() - datetime.timedelta(days=1)
t = t.strftime("%d-%b-%y")
df_daily = df1[df1['Date'] == t]
if len(df_daily) > 0:
df_daily.to_csv(file_daily)
print("df_daily Updated at: ", datetime.datetime.now())
activity = discord.Activity(
name="Plague Inc.", type=discord.ActivityType.playing)
await client.change_presence(activity=activity)
await client.get_channel(841561036305465344).send(f"Bot is online")
@client.event
async def on_guild_join(guild):
# Create embed to send
embed = discord.Embed(color=discord.Color.green(), title="Covid19 India Bot",
description="Gives various statistics regarding Covid19 in India along with vaccination slots near you")
embed.add_field(
name="Hello!", value="Thank you for adding the bot to your server! Use `.help` to find out what commands it currently supports!")
alerts_embed = discord.Embed(
color=discord.Color.green(), title="IMPORTANT")
alerts_embed.add_field(
name="\u200b", value="Members with the `Manage Server` permissions are requested to run `.alerts {channel_name}` command to receive important updates from the developers")
for channels in guild.text_channels:
if channels.permissions_for(guild.me).send_messages:
await channels.send(embed=embed)
await channels.send(embed=alerts_embed)
break
ownerObj = guild.owner
try:
await ownerObj.send("Thank you for adding me to your server! It is important that you run the `.alerts {channel.name}` command so that you get important updates from the developers.\nIf you have any questions or queries, you can mail us at covidindiabot@gmail.com")
except:
pass
@client.event
async def on_guild_remove(guild):
dat = ''
fp = open('alerts.csv', 'r')
for line1 in fp:
if(str(guild.id) not in line1.split(',')):
dat += line1
fp.close()
fp = open('alerts.csv', 'w')
fp.write(dat)
fp.close()
fp = open('mypings.json', 'r')
data = json.load(fp)
fp.close()
new_data = {}
guild_id = str(guild.id)
for pincodes in data:
id_dict = data[pincodes]
new_id_dict = {}
for uid in id_dict:
if guild_id in str(id_dict[uid]):
continue
new_id_dict[uid] = id_dict[uid]
new_data[pincodes] = new_id_dict
fp = open('mypings.json', 'w')
json.dump(new_data, fp)
fp.close()
@client.event
async def on_message(message):
if message.author.bot:
if((message.channel.id == 840644400564142111) and (message.author.id == 836578128305717279)):
await message.publish()
else:
pass
elif (('<@!836578128305717279>' in message.content) or ('<@836578128305717279>' in message.content)):
await message.channel.send(f"{message.author.mention} my prefix in this server is `.`\nUse `.help` to know what all I can do")
else:
await client.process_commands(message)
@client.event
async def on_member_remove(member):
fp = open('mypings.json', 'r')
data = json.load(fp)
fp.close()
new_data = {}
member_id = str(member.id)
for pincodes in data:
id_dict = data[pincodes]
new_id_dict = {}
for uid in id_dict:
if member_id in str(uid):
continue
new_id_dict[uid] = id_dict[uid]
new_data[pincodes] = new_id_dict
fp = open('mypings.json', 'w')
json.dump(new_data, fp)
fp.close()
@client.event
async def on_guild_channel_delete(channel):
ch_id = str(channel.id)
dat = ''
fp = open('alerts.csv', 'r')
for line in fp:
if(ch_id in line.replace('\n', '').split(',')[1]):
continue
dat += line
fp.close()
fp = open('alerts.csv', 'w')
fp.write(dat)
fp.close()
@client.command(aliases=['file', 'f'])
async def file_command(ctx):
if((ctx.author.id == 554876169363652620) or (ctx.author.id == 723377619420184668) or (ctx.author.id == 718845827413442692) or (ctx.author.id == 404597472103759872) or (ctx.author.id == 771985293011058708)):
await ctx.send("You have clearance")
with open('alerts.csv', 'r') as fp:
await client.get_channel(841561036305465344).send(file=discord.File(fp, 'alerts.csv'))
fp.close()
with open('mypings.json', 'r') as fp:
await client.get_channel(841561036305465344).send(file=discord.File(fp, 'mypings.json'))
fp.close()
else:
await ctx.send("You are not authorised to run this command")
@client.command(aliases=['guilds'])
async def guilds_command(ctx):
if((ctx.author.id == 554876169363652620) or (ctx.author.id == 723377619420184668) or (ctx.author.id == 718845827413442692) or (ctx.author.id == 404597472103759872) or (ctx.author.id == 771985293011058708)):
await ctx.channel.trigger_typing()
count = 0
dat = 'SERVER NAME,SERVER ID\n\n'
guilds_details = await client.fetch_guilds(limit=150).flatten()
for guild_deets in guilds_details:
dat += f"{guild_deets.name},{guild_deets.id}\n"
count += 1
dat += f"\nCOUNT: {count}"
with open('guilds.csv', 'w+') as fp:
fp.write(dat)
await ctx.send("You have clearance")
await client.get_channel(841561036305465344).send(file=discord.File('guilds.csv'))
fp.close()
os.remove('guilds.csv')
else:
await ctx.send("You are not authorised for this")
@client.command(aliases=['contribute', 'support'])
async def _support(ctx, *params):
Embeds = discord.Embed(title="Contributions", color=0x00ff00)
Embeds.add_field(
name="Github repo", value="https://github.com/thesuhas/Covid19-India-Discord-Bot/", inline=False)
Embeds.add_field(
name='\u200b', value="If you wish to contribute to the bot, run these steps:", inline=False)
rules = {
1: "Fork this repository",
2: "Create a new branch called `beta-username`",
3: "Do whatever changes you wish to do and create a pull request with the following information furnished in the request message: `The functionality you wish to change/add | What did you change/add`",
4: "Send a review request to any of the following members: `thesuhas`, `RIT3shSapata`, `sach-12`, `ArvindAROO` and/or `rohangrge`",
5: "Wait for approval for reviewers. Your PR may be directly accepted or requested for further changes."
}
for ruleNo in rules:
Embeds.add_field(name='\u200b', value="`" +
str(ruleNo) + '`: ' + rules[ruleNo], inline=False)
guildObj = client.get_guild(742797665301168220)
stark = guildObj.get_member(718845827413442692).mention
sapota = guildObj.get_member(404597472103759872).mention
suhas = guildObj.get_member(554876169363652620).mention
sach = guildObj.get_member(723377619420184668).mention
rohan = guildObj.get_member(771985293011058708).mention
Embeds.add_field(name="Reviewers", value="`thesuhas` - {}\n`ArvindAROO` - {}\n`RIT3shSapata` - {}\n`sach-12` - {} and\n`rohangrge` - {}".format(
suhas, stark, sapota, sach, rohan), inline=False)
Embeds.add_field(
name="Important", value="**Under no circumstances is anyone allowed to merge to the main branch.**", inline=False)
Embeds.add_field(
name="\u200b", value="You can send suggestions and feedback to covidindiabot@gmail.com")
await ctx.send(embed=Embeds)
@client.command(aliases=['invite'])
async def invite_command(ctx):
await ctx.send("Invite me to your server with this link:\nhttps://bit.ly/covid-india-bot")
@client.command(aliases=['h', 'help'])
async def help_command(ctx, text=''):
if text == '':
help_embed = discord.Embed(
title="**Help**", color=discord.Color.green())
help_embed.add_field(
name='**`.india`**', value='Get COVID-19 stats of the entire nation', inline=False)
help_embed.add_field(name='**`.states`**',
value='Get a list of states', inline=False)
help_embed.add_field(
name='**`.state {state}`**', value='Get cases in that particular state', inline=False)
help_embed.add_field(
name='**`.vaccine {pincode} [date]`**', value='Get vaccination slots near you. If `date` is not mentioned, it will take today\'s date', inline=False)
help_embed.add_field(name='**`.beds {type of hospital}`**',
value='Get available beds. Type can be `government/govt` or `private`(Only Bengaluru)', inline=False)
help_embed.add_field(
name='**`.alerts {channel name}`**', value='(only for members with \"Manage Server\" permissions) To register any channel on your server to get important alerts from the developers', inline=False)
help_embed.add_field(name='**`.removealerts`**',
value='(only for members with \"Manage Server\" permissions) To de-register any channel that was registered for alerts', inline=False)
help_embed.add_field(
name='**`.invite`**', value='Get the invite link of the bot', inline=False)
help_embed.add_field(name='**`.contribute`**',
value='If you wish to contribute or learn about the bot', inline=False)
help_embed.add_field(
name='**`.reachout`**', value='(only for Server Administrators) To reach out to the bot developers', inline=False)
await ctx.send(embed=help_embed)
else:
embed = discord.Embed(title='help', color=discord.Color.green(
), description='`.help` does not take any arguments.\n**Syntax:** `.help`')
await ctx.send(embed=embed)
# Slash Command of the same
@slash.slash(name="help", description="Commands available from me")
async def help_slash(ctx):
await ctx.defer()
help_embed = discord.Embed(title="**Help**", color=discord.Color.green())
help_embed.add_field(
name='**`.india`**', value='Get COVID-19 stats of the entire nation', inline=False)
help_embed.add_field(name='**`.states`**',
value='Get a list of states', inline=False)
help_embed.add_field(
name='**`.state {state}`**', value='Get cases in that particular state', inline=False)
help_embed.add_field(
name='**`.vaccine {pincode} [date]`**', value='Get vaccination slots near you. If `date` is not mentioned, it will take today\'s date', inline=False)
help_embed.add_field(name='**`.beds {type of hospital}`**',
value='Get available beds. Type can be `government/govt` or `private`(Only Bengaluru)', inline=False)
help_embed.add_field(name='**`.alerts {channel name}`**',
value='(only for members with \"Manage Server\" permissions) To register any channel on your server to get important alerts from the developers', inline=False)
help_embed.add_field(name='**`.removealerts`**',
value='(only for members with \"Manage Server\" permissions) To de-register any channel that was registered for alerts', inline=False)
help_embed.add_field(name='**`.invite`**',
value='Get the invite link of the bot', inline=False)
help_embed.add_field(name='**`.contribute`**',
value='If you wish to contribute or learn about the bot', inline=False)
help_embed.add_field(name='**`.reachout`**',
value='(only for Server Administrators) To reach out to the bot developers', inline=False)
await ctx.send(embeds=[help_embed])
@client.command(aliases=['state'])
async def state_command(ctx, *, state=''):
state = state.lower()
if state == '':
# If state has not been mentioned
embed = discord.Embed(title="State", color=discord.Color.green(
), description='Need to mention a state.\n**Syntax:** `.state {state}`\n Run `.help` for more info')
await ctx.send(embed=embed)
else:
if (state.lower() == "total"):
await ctx.send("Use `.india` for total cases")
else:
entry = df.loc[df['State'] == state.lower()]
if entry.empty:
await ctx.send("Chosen state not available")
else:
#m = f"**Covid Cases in {state[0].upper() + state[1:]}:**\nConfirmed: {entry['Confirmed'].values[0]}\nRecovered: {entry['Recovered'].values[0]}\nDeaths: {entry['Deaths'].values[0]}\nActive: {entry['Active'].values[0]}"
embed = discord.Embed(
title=f"Cases in {state[0].upper() + state[1:]}", color=discord.Color.green())
embed.add_field(name='Active', value=format_currency(
int(entry['Active'].values[0]), 'INR', locale='en_IN')[1:-3], inline=False)
embed.add_field(name='Confirmed', value=format_currency(int(entry['Confirmed'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Confirmed'][mapp[state]]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.add_field(name='Recovered', value=format_currency(int(entry['Recovered'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Recovered'][mapp[state]]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.add_field(name='Deaths', value=format_currency(int(entry['Deaths'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Deceased'][mapp[state]]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.set_footer(text=footer)
await ctx.send(embed=embed)
# Slash command of state
@slash.slash(name='state', description='State-wise stats of COVID-19')
async def state_slash(ctx, *, state=''):
# .defer lets bot think for upto 15 seconds
await ctx.defer()
if state == '':
# If state has not been mentioned
embed = discord.Embed(title="State", color=discord.Color.green(
), description='Need to mention a state.\n**Syntax:** `.state {state}`\n Run `.help` for more info')
await ctx.send(embed=embed)
else:
if (state.lower() == "total"):
await ctx.send("Use `.india` for total cases")
else:
entry = df.loc[df['State'] == state.lower()]
if entry.empty:
await ctx.send("Chosen state not available")
else:
#m = f"**Covid Cases in {state[0].upper() + state[1:]}:**\nConfirmed: {entry['Confirmed'].values[0]}\nRecovered: {entry['Recovered'].values[0]}\nDeaths: {entry['Deaths'].values[0]}\nActive: {entry['Active'].values[0]}"
embed = discord.Embed(
title=f"Cases in {state[0].upper() + state[1:]}", color=discord.Color.green())
embed.add_field(name='Active', value=format_currency(
int(entry['Active'].values[0]), 'INR', locale='en_IN')[1:-3], inline=False)
embed.add_field(name='Confirmed', value=format_currency(int(entry['Confirmed'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Confirmed'][mapp[state]]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.add_field(name='Recovered', value=format_currency(int(entry['Recovered'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Recovered'][mapp[state]]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.add_field(name='Deaths', value=format_currency(int(entry['Deaths'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Deceased'][mapp[state]]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.set_footer(text=footer)
await ctx.send(embed=embed)
@client.command(aliases=['india'])
async def india_command(ctx):
entry = df.loc[df['State'] == 'total']
#m = f"**Covid Cases in the country:**\nConfirmed: {entry['Confirmed'].values[0]}\nRecovered: {entry['Recovered'].values[0]}\nDeaths: {entry['Deaths'].values[0]}\nActive: {entry['Active'].values[0]}"
embed = discord.Embed(title="Cases in the Country",
color=discord.Color.green())
embed.add_field(name='Active', value=format_currency(
int(entry['Active'].values[0]), 'INR', locale='en_IN')[1:-3], inline=False)
embed.add_field(name='Confirmed', value=format_currency(int(entry['Confirmed'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Confirmed'][mapp['total']]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.add_field(name='Recovered', value=format_currency(int(entry['Recovered'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Recovered'][mapp['total']]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.add_field(name='Deaths', value=format_currency(int(entry['Deaths'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Deceased'][mapp['total']]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.set_footer(text=footer)
await ctx.send(embed=embed)
# Slash command of the above
@slash.slash(name='india', description='Stats of COVID-19 in India')
async def india_slash(ctx):
await ctx.defer()
entry = df.loc[df['State'] == 'total']
embed = discord.Embed(title="Cases in the Country",
color=discord.Color.green())
embed.add_field(name='Active', value=format_currency(
int(entry['Active'].values[0]), 'INR', locale='en_IN')[1:-3], inline=False)
embed.add_field(name='Confirmed', value=format_currency(int(entry['Confirmed'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Confirmed'][mapp['total']]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.add_field(name='Recovered', value=format_currency(int(entry['Recovered'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Recovered'][mapp['total']]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.add_field(name='Deaths', value=format_currency(int(entry['Deaths'].values[0]), 'INR', locale='en_IN')[
1:-3] + '\n(+' + format_currency(int(df_daily[df_daily['Status'] == 'Deceased'][mapp['total']]), 'INR', locale='en_IN')[1:-3] + ')', inline=False)
embed.set_footer(text=footer)
await ctx.send(embeds=[embed])
@client.command(aliases=['states'])
async def states_command(ctx, text=''):
global s
if text != '':
await ctx.send('Wrong usage of command, check `.help`')
else:
# Returns list of states
string = '**States and Union Territories:\n**'
for i in s:
string += i + '\n'
await ctx.send(string)
@client.command(aliases=['vaccine'])
async def vaccine_command(ctx, pincode="", date=datetime.datetime.now().strftime("%d-%m-%Y")):
# If no pincode given
pincheck = await pincodecheckindia(pincode)
if not pincheck:
await ctx.send("Invalid pincode, try again")
else:
headers = {"Accept-Language": "en-IN",
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
data = {"pincode": pincode, "date": date}
res = requests.get(
"https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin", headers=headers, params=data)
# if res.status_code == 400:
# await ctx.send("Invalid pincode")
# return
if res.status_code == 403:
await ctx.send("API is unresponsive at the time. Please try again after sometime")
return
# Extracting json data
data = res.json()
sessions = dict()
if len(data['centers']) > 0:
for i in data['centers']:
# Look at all sessions
for j in i['sessions']:
# If there is an available session
if j['available_capacity'] >= 1:
# Check if hospital exists
if (i['name'], i['pincode'], i['fee_type'], i['address']) in sessions:
sessions[(i['name'], i['pincode'],
i['fee_type'], i['address'])].append(j)
else:
sessions[(i['name'], i['pincode'],
i['fee_type'], i['address'])] = list()
sessions[(i['name'], i['pincode'],
i['fee_type'], i['address'])].append(j)
if len(sessions) == 0:
await ctx.send("No available sessions")
else:
# Create an embed for every session
for sesh in sessions:
embed = discord.Embed(
title=f"Vaccine Available at {sesh[0]}", color=discord.Color.green())
embed.add_field(name='Date', value=date, inline=False)
embed.add_field(
name='Address', value=sesh[3], inline=False)
embed.add_field(
name='Pin Code', value=sesh[1], inline=False)
embed.add_field(
name='Available Capacity for Dose 1', value=sessions[sesh][0]['available_capacity_dose1'], inline=False)
embed.add_field(
name='Available Capacity for Dose 2', value=sessions[sesh][0]['available_capacity_dose2'], inline=False)
embed.add_field(
name='Minimum Age', value=sessions[sesh][0]['min_age_limit'], inline=False)
embed.add_field(
name='Vaccine', value=sessions[sesh][0]['vaccine'])
embed.add_field(
name='Fee type', value=sesh[2], inline=False)
embed.add_field(name="Slots", value='\n'.join(
sessions[sesh][0]['slots']), inline=False)
await ctx.send(embed=embed)
else:
await ctx.send("No available vaccination center")
@slash.slash(name='vaccine', description='List of Vaccination slots near you')
async def vaccine_slash(ctx, pincode="", date=datetime.datetime.now().strftime("%d-%m-%Y")):
await ctx.defer()
# If no pincode given
pincheck = await pincodecheckindia(pincode)
if not pincheck:
await ctx.send("Invalid pincode, try again")
else:
headers = {"Accept-Language": "en-IN",
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
data = {"pincode": pincode, "date": date}
res = requests.get(
"https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin", headers=headers, params=data)
# if res.status_code == 400:
# await ctx.send("Invalid pincode")
# return
if res.status_code == 403:
await ctx.send("API is unresponsive at the time. Please try again after sometime")
return
# Extracting json data
data = res.json()
sessions = dict()
if len(data['centers']) > 0:
# pprint.pprint(data)
for i in data['centers']:
# Look at all sessions
for j in i['sessions']:
# If there is an available session
if j['available_capacity'] >= 1:
# Check if hospital exists
if i['name'] in sessions:
sessions[(i['name'], i['pincode'],
i['fee_type'], i['address'])].append(j)
else:
sessions[(i['name'], i['pincode'],
i['fee_type'], i['address'])] = list()
sessions[(i['name'], i['pincode'],
i['fee_type'], i['address'])].append(j)
if len(sessions) == 0:
await ctx.send("No available sessions")
else:
# Create an embed for every session
for sesh in sessions:
embed = discord.Embed(
title=f"Vaccine Available at {sesh[0]}", color=discord.Color.green())
embed.add_field(name='Date', value=date, inline=False)
embed.add_field(
name='Address', value=sesh[3], inline=False)
embed.add_field(
name='Pin Code', value=sesh[1], inline=False)
embed.add_field(
name='Available Capacity for Dose 1', value=sessions[sesh][0]['available_capacity_dose1'], inline=False)
embed.add_field(
name='Available Capacity for Dose 2', value=sessions[sesh][0]['available_capacity_dose2'], inline=False)
embed.add_field(
name='Minimum Age', value=sessions[sesh][0]['min_age_limit'], inline=False)
embed.add_field(
name='Vaccine', value=sessions[sesh][0]['vaccine'])
embed.add_field(
name='Fee type', value=sesh[2], inline=False)
embed.add_field(name="Slots", value='\n'.join(
sessions[sesh][0]['slots']), inline=False)
await ctx.send(embed=embed)
else:
await ctx.send("No available vaccination center")
@tasks.loop(seconds=1800)
async def update_daily():
global df_daily
# Creating daily df
response = requests.get(
'https://api.covid19india.org/csv/latest/state_wise_daily.csv')
response = str(response.text)
data_daily = io.StringIO(response)
df1 = pd.read_csv(data_daily, sep=",")
# Get yestedays date in appropriate formatting
t = datetime.datetime.now() - datetime.timedelta(days=1)
t = t.strftime("%d-%b-%y")
df_daily = df1[df1['Date'] == t]
if len(df_daily) > 0:
df_daily.to_csv(file_daily)
print("df_daily Updated at: ", datetime.datetime.now())
@client.command(aliases=['beds'])
async def beds_command(ctx, hospital=''):
hospital = hospital.lower()
if hospital == '':
await ctx.send("Mention type of hospital. Example: `.beds government`")
elif hospital != 'govt' and hospital != 'government' and hospital != 'private':
await ctx.send("Hospital types are: `government/govt` and `private`")
else:
if hospital == 'govt':
hospital = "government"
df = pd.read_html("https://bbmpgov.com/chbms/#A")
keys = {"government": 2, "private": 4}
df = df[keys[hospital]].sort_values(by=[('Net Available Beds for C+ Patients', 'Total')],
ascending=False).reset_index(drop=True).drop([('SR. NO.', '#')], axis=1)[:10]
df = df[[('Dedicated Covid Healthcare Centers (DCHCs)', 'Name of facility'),
('Net Available Beds for C+ Patients', 'Gen'),
('Net Available Beds for C+ Patients', 'HDU'),
('Net Available Beds for C+ Patients', 'ICU'),
('Net Available Beds for C+ Patients', 'ICUVentl'),
('Net Available Beds for C+ Patients', 'Total')]]
df.columns = ['Hospital', 'Gen', 'HDU', 'ICU', 'ICUVentl', 'Total']
dfi.export(df, 'test.png', table_conversion='matplotlib')
await ctx.send(file=discord.File('test.png'))
@slash.slash(name='beds', description='Hospitals with beds')
async def beds_slash(ctx, hospital_type):
hospital = hospital_type
hospital = hospital.lower()
if hospital == '':
await ctx.send("Mention type of hospital. Example: `.beds government`")
elif hospital != 'govt' and hospital != 'government' and hospital != 'private':
await ctx.send("Hospital types are: `government/govt` and `private`")
else:
if hospital == 'govt':
hospital = "government"
df = pd.read_html("https://bbmpgov.com/chbms/#A")
keys = {"government": 2, "private": 4}
df = df[keys[hospital]].sort_values(by=[('Net Available Beds for C+ Patients', 'Total')],
ascending=False).reset_index(drop=True).drop([('SR. NO.', '#')], axis=1)[:10]
df = df[[('Dedicated Covid Healthcare Centers (DCHCs)', 'Name of facility'),
('Net Available Beds for C+ Patients', 'Gen'),
('Net Available Beds for C+ Patients', 'HDU'),
('Net Available Beds for C+ Patients', 'ICU'),
('Net Available Beds for C+ Patients', 'ICUVentl'),
('Net Available Beds for C+ Patients', 'Total')]]
df.columns = ['Hospital', 'Gen', 'HDU', 'ICU', 'ICUVentl', 'Total']
dfi.export(df, 'test.png', table_conversion='matplotlib')
await ctx.send(file=discord.File('test.png'))
# Updates dataframe every 30 mins
@tasks.loop(seconds=1800)
async def update():
global footer
global df
# Create basic data frame and store
test = requests.get(
'https://api.covid19india.org/csv/latest/state_wise.csv')
test = str(test.text)
data = io.StringIO(test)
df = pd.read_csv(data, sep=",")
# Making states lowercase
df["State"] = df["State"].str.lower()
df.to_csv(filename)
# Prints when df is last updated
print("df Updated at: ", datetime.datetime.now())
time = datetime.datetime.now() + datetime.timedelta(hours=5, minutes=30)
time = time.strftime("%d-%b") + ' at ' + time.strftime("%I:%M %p")
footer = f"Last Updated: {time}"
@tasks.loop(seconds=7)
async def alert():
global s_id
date = datetime.datetime.now().strftime("%d-%m-%Y")
datetom = (datetime.datetime.now() +
datetime.timedelta(days=1)).strftime("%d-%m-%Y")
dates = [date, datetom]
d_ids = [276, 265, 294]
for j in d_ids:
if(j == 276):
url = url1
if(j == 265):
url = url2
if(j == 294):
url = url3
for i in dates:
headers = {"Accept-Language": "en-IN",
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
data = {"district_id": j, "date": i}
res = requests.get(url, params=data)
#resp = res.json()
# print(res.json())
#print(res.status_code, j)
if(res.status_code == 200):
resp = res.json()
for k in resp['sessions']:
if(len(k) != 0 and k['session_id'] not in s_id):
# if(math.trunc(k['available_capacity_dose1']) >= 8 and k['min_age_limit'] == 18 and ((k['available_capacity_dose1'])-int(k['available_capacity_dose1'])) == 0 and k['session_id'] not in s_id):
embed = discord.Embed(
title=f"Vaccine Available at {k['name']}", color=discord.Color.green())
embed.add_field(
name='Date', value=k['date'], inline=False)
embed.add_field(
name='Vaccine', value=k['vaccine'])
embed.add_field(
name='Address', value=k['address'], inline=False)
embed.add_field(
name='Pincode', value=k['pincode'], inline=False)
embed.add_field(
name='Available Capacity for Dose 1', value=k['available_capacity_dose1'], inline=False)
embed.add_field(
name='Available Capacity for Dose 2', value=k['available_capacity_dose2'], inline=False)
embed.add_field(
name='Minimum Age', value=k['min_age_limit'], inline=False)
if(k['fee_type'] == 'Paid'):
embed.add_field(name='Fee type', value=str(
k['fee_type'])+'-Rs.'+str(k['fee']), inline=False)
else:
embed.add_field(name='Fee type', value=str(
k['fee_type']), inline=False)
embed.add_field(name="Slots", value='\n'.join(
k['slots']), inline=False)
s_id.append(str(k['session_id']))
fp = open('alerts.csv', 'r')
fp2 = open('mypings.json', 'r')
data = json.load(fp2)
fp2.close()
ch_list = [line.split(',')[1] for line in list(
filter(None, fp.read().split('\n')))]
for ch in ch_list:
try:
await client.get_channel(int(ch)).send(embed=embed)
except:
continue
try:
guild_id = str(
client.get_channel(int(ch)).guild.id)
if(str(k['pincode']) in data):
id_dict = data[str(k['pincode'])]
for uid in id_dict:
if(guild_id in str(id_dict[uid])):
member_id = int(uid)
memberMention = client.get_user(
member_id).mention
await client.get_channel(int(ch)).send(memberMention)
except:
continue
# await client.get_channel(841561036305465344).send(embed=embed)
fp.close()
else:
continue
else:
await client.get_channel(841560857602162698).send('There is an issue with alerts,check bot')
continue
@client.command(aliases=['alerts'])
async def alerts_command(ctx, dest: discord.TextChannel = None):
if(dest == None):
await ctx.send("Mention the channel you want to send alerts to")
return
auth_perms = ctx.channel.permissions_for(ctx.author)
if(auth_perms.manage_guild):
file1 = open('alerts.csv', 'r')
for line in file1:
if(str(dest.id) in str(line.split(',')[1].rstrip('\n'))):
await ctx.send(f"Looks like {dest.mention} is already subscribed to our alerts")
file1.close()
return
client_member = ctx.guild.get_member(836578128305717279)
client_perms = client_member.permissions_in(dest)
if(client_perms.send_messages and client_perms.embed_links):
file1 = open('alerts.csv', 'a')
file1.write(f"{ctx.guild.id},{dest.id}\n")
file1.close()
await ctx.send(f"**Success!** You'll now get vaccine slot alerts in Bengaluru and other important notifications from the bot on {dest.mention}")
else:
await ctx.send("I don't have enough permissions in that channel. Enable `Send Messages` and `Embed Links` for me")
else:
await ctx.send("Looks like you don't have the manage server permissions to run this")
@tasks.loop(seconds=300)
async def clear():
global s_id
s_id = []
return
@client.command(aliases=['removealerts'])
async def removealerts_command(ctx, dest: discord.TextChannel = None):
found = False
if(dest == None):
await ctx.send("Mention the channel you want to remove the alerts from")
return
auth_perms = ctx.channel.permissions_for(ctx.author)
if(auth_perms.manage_guild):
fp = open('alerts.csv', 'r')
for line1 in fp:
if(str(dest.id) in str(line1.split(',')[1].rstrip('\n'))):
fp.close()
found = True
break
else:
continue
if(not found):
await ctx.send(f"Looks like {dest.mention} was never set up for alerts")
fp.close()
return
dat = ''
fp = open('alerts.csv', 'r')
for line1 in fp:
if(str(dest.id) not in str(line1.split(',')[1].rstrip('\n'))):
dat += line1
fp.close()
fp = open('alerts.csv', 'w')
fp.write(dat)
fp.close()
await ctx.send(f"**DONE**. {dest.mention} will no longer receive alerts and updates from the developers")
else:
await ctx.send("Looks like you don't have the manage server permissions to run this")
@client.command(aliases=['announce'])
async def announce_command(ctx, *, msg: str = ''):
if((ctx.author.id == 554876169363652620) or (ctx.author.id == 723377619420184668) or (ctx.author.id == 718845827413442692) or (ctx.author.id == 404597472103759872) or (ctx.author.id == 771985293011058708)):
if(msg == ''):
await ctx.send("Put a message man")
return
fp = open('alerts.csv', 'r')
ch_list = [line.split(',')[1] for line in list(
filter(None, fp.read().split('\n')))]
for ch in ch_list:
await client.get_channel(int(ch)).send(f"**NEW ALERT FROM THE DEVS**\n\n{msg}")
await ctx.send("Announcement sent")
fp.close()
else:
await ctx.send("You don't have permission to execute this command")
@client.command(aliases=['reachout'])
async def reachout_command(ctx, *, msg: str = ''):
if(msg == ''):
await ctx.send("Type a message to send to the developers")
return
auth_perms = ctx.channel.permissions_for(ctx.author)
if(auth_perms.administrator):
await ctx.send("Your message has been sent to the devs. We will get back to you with a reply shortly on this channel")
await client.get_channel(841560857602162698).send(f"Reachout from `{ctx.guild.name}`, guild-ID: `{ctx.guild.id}`, channel-ID: `{ctx.channel.id}`\n\n{msg}")
else:
await ctx.send("Only members with administrator perms can run this command. Contact your server admin or anyone with a role who has administrator privileges. You can always contact us on `covidindiabot@gmail.com`")
@client.command(aliases=['reachreply'])
async def reachreply_command(ctx, destid: int = 0, *, msg: str = ''):
if((ctx.author.id == 554876169363652620) or (ctx.author.id == 723377619420184668) or (ctx.author.id == 718845827413442692) or (ctx.author.id == 404597472103759872) or (ctx.author.id == 771985293011058708)):
if(destid == 0):
await ctx.send("Saar, enter channel ID")
return
if(msg == ''):
await ctx.send("Saar, tell something to reply to them")
return
try:
dest = client.get_channel(destid)
except:
await ctx.send("Can't fetch that channel")
return
await dest.send("**MESSAGE FROM THE DEVS**")
await dest.send(msg)
await ctx.send("Message sent")
else:
await ctx.send("You do not have the permission to execute this command")
@client.command(aliases=['myping', 'mp'])
async def personalpingcommand(ctx, pincode: int = 0):
await ctx.channel.trigger_typing()
found = False
pincheck = await pincodecheckbangalore(str(pincode))
if pincheck:
with open('alerts.csv', 'r') as fp:
for line in fp:
if(str(ctx.guild.id) in line.split(',')[0]):
found = True
if not found:
await ctx.send("Looks like your server isn't set up for vaccine alerts at all.\nContact your server moderators and ask them to run the `.alerts` command and then try again")
fp.close()
return
fp = open('mypings.json', 'r')
data = json.load(fp)
fp.close()
user_id = str(ctx.author.id)
guild_id = str(ctx.guild.id)
subdict = {}
if(str(pincode) in data):
subdict = data[str(pincode)]
if(user_id in subdict):
await ctx.send("Looks like you've already subscribed for this pincode")
return
subdict[user_id] = guild_id
data[str(pincode)] = subdict
fp = open('mypings.json', 'w')
json.dump(data, fp)
fp.close()
await ctx.send(f"You'll now get a ping every time there's a slot open in pincode: **{pincode}**")
else:
await ctx.send("Pincode invalid, try again")
@client.command(aliases=['rp', 'removeping'])
async def removepingcommand(ctx, pincode: int = 0):
await ctx.channel.trigger_typing()
pincheck = await pincodecheckbangalore(str(pincode))
if pincheck:
fp = open('mypings.json', 'r')
data = json.load(fp)
fp.close()
user_id = str(ctx.author.id)
try:
subdict = data[str(pincode)]
subdict.pop(user_id)
except KeyError:
await ctx.send(f"No record of pincode: **{pincode}** was found in our database")
return
data[str(pincode)] = subdict
fp = open('mypings.json', 'w')
json.dump(data, fp)
fp.close()
await ctx.send(f"Alright, no more pings for pincode: **{pincode}**")
else:
await ctx.send("Pincode invalid, try again")
@client.command(aliases=['listpings', 'lp'])
async def pinglist(ctx):
fp = open('mypings.json', 'r')
data = json.load(fp)
fp.close()
user_id = str(ctx.author.id)
pinlist = ''
for pincodes in data:
id_dict = data[pincodes]
for uid in id_dict:
if user_id in str(uid):
pinlist += f"{pincodes}\n"
if pinlist == '':
await ctx.send("You haven't set up for any pincode pings. To do so, use `.myping`")
else:
await ctx.send(f"You've set up to get pings for the following pincodes:\n{pinlist}")
async def pincodecheckindia(pincode):
regex = "^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$"
pin = re.compile(regex)
check = re.match(pin, pincode)
if check is not None:
return True
else:
return False
async def pincodecheckbangalore(pincode):
regex = "^[5]{1}[6]{1}[02]{1}\\s{0,1}[0-2]{1}[0-9]{2}$"
pin = re.compile(regex)
check = re.match(pin, pincode)
if check is not None:
return True
else:
return False
# Runs the bot
client.run(os.getenv('TOKEN'))