-
Notifications
You must be signed in to change notification settings - Fork 12
/
bot.py
1172 lines (956 loc) · 63.3 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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import threading
from itertools import count
import karma_bot
from datetime import date
from selenium import webdriver
from openwa import WhatsAPIDriver
import time
import os
import wikipedia
import psycopg2
while True:
flag = 0
# time to noted after which whole bot restart
s_time = time.time()
# Change these variable before running the bot
YOUR_MOBILE_NUMBER = os.environ.get("MOB_NUMBER") # Ex-: 918273627374
Jdoodle_clientId = os.environ.get("JDOODLE_CLID")
Jdoodle_clientSecret = os.environ.get("JDOODLE_SID")
CRYPTOPANIC_API = os.environ.get('CRYPTOPANIC_API')
# pre-defining varibale
all_cmds = ["#delete","#calc","#cnews", "#cdetail", "#cprice", "#msg_count", "#help_ludo", "#ludo", "#rdice", "#pmove", "#quitludo",
"#currludo", "#last_tag", "#all_cmd", "#help", "#run python3#", "#run cpp#", "#resetrun", "#ticgame",
"#currtic", "#quit_tic", "#help_tic", "#wordgame", "#currword", "#ans ", "#join ", "#score", "#skip",
"#help_wgame", "#gfg#", "#matchgame", "#help_match", "#currmatch", "#quitmatch", "#m", "#minegame",
"#mine ", "#currmine", "#minemark", "#mineunmark", "#help_mine", "#wiki ", "#add", "#kick", "#link",
"#source"]
conn = psycopg2.connect(os.environ.get("PGSQL_SERVER"), sslmode='require')
cur = conn.cursor()
# dict of group where bot can run
cur.callproc('get_chats')
out = cur.fetchone()
group = karma_bot.db_data_to_dictionary().get(out, 3, 1)
print(group,(len(group)))
# getting all_chats bot added date
bot_added = karma_bot.db_data_to_dictionary().get(out, 3, 2)
print(bot_added)
# getting score_board and players list from database
cur.callproc('get_score')
out = cur.fetchone()
score = karma_bot.db_data_to_dictionary().get(out, 2, 1)
#getting players
cur.callproc('get_player')
out = cur.fetchone()
player = karma_bot.db_data_to_dictionary().get(out, 2, 1)
#getting ignore list
cur.callproc('get_ignore_list')
out=cur.fetchone()
if out[0]==None:
ignore_list=set()
else:
ignore_list=set()
for i in out[0]:
m=i.replace("\"","")
ignore_list.add(m)
# creating all classes object
sticker = karma_bot.karma_sticker()
Word = karma_bot.karma_word_game(score, player)
Crypto = karma_bot.crypto()
GFG = karma_bot.GFG()
COMP = karma_bot.compiler(Jdoodle_clientId, Jdoodle_clientSecret)
suggest = karma_bot.cmd_suggesstion(all_cmds)
quit = karma_bot.quit_bot()
# object dict for matchgame
match_player_dict = dict()
# object dict for tic game
tic_player_dict = dict()
# object list for minegame
mine_player_dict = dict()
# object list for ludo game
ludo_game_dict = dict()
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
wd = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), options=chrome_options)
li = [("user-data-dir=" + os.environ.get("USER_DATA")), "--disable-dev-shm-usage", "--no-sandbox"]
driver = WhatsAPIDriver(client='chrome', headless=True, chrome_options=li,
executable_path=os.environ.get("CHROMEDRIVER_PATH"))
wd.get("https://www.google.com/") # opening google in one tab
win1 = wd.window_handles[0]
wd.execute_script("window.open('');") # opening second empty tab
win2 = wd.window_handles[1]
# database variables for maintaining transactions control
db_members = 0
db_chats = 0
p_adding = 0
s_adding = 0
def main(message):
global bot
global db_members
global db_chats
global s_adding
global p_adding
global s_time
global flag
if message.chat_id in group:
all_msg.append(message)
if (message.type == 'chat' or message.type == 'image' or message.type == 'video') and (
(hasattr(message, 'caption') and message.caption == '#sticker') or message.content[0:1] == '#'):
if message.chat_id not in group and db_chats == 0:
db_chats = 1
try:
cur.execute('CALL add_chat(\'{}\',\'{}\',\'{}\')'.format("\"" + message.chat_id + "\"", 1,
str(date.today().strftime("%Y-%m-%d")
)))
conn.commit()
group[message.chat_id] = 1
bot_added[message.chat_id] = str(date.today().strftime("%Y-%m-%d"))
driver.chat_send_message(message.chat_id, "Send #help to check out all the features of bot ✨")
except Exception as e:
print("Starting bot for group failed" + str(e))
driver.chat_send_message(YOUR_MOBILE_NUMBER + "@c.us", "Starting bot for group failed" + str(e))
driver.chat_send_message(message.chat_id, "I-Bot failed to start for this chat!")
db_chats = 0
if message.type == 'chat' and message.content == '#on' and db_chats == 0:
db_chats = 1
if str(message.sender.id) == YOUR_MOBILE_NUMBER + "@c.us" or message.sender.id in driver.wapi_functions.getGroupAdmins(
message.chat_id):
if message.chat_id not in group or group[message.chat_id] == 0:
try:
cur.execute('CALL add_chat(\'{}\',\'{}\',\'{}\')'.format("\"" + message.chat_id + "\"", 1,
str(date.today().strftime(
"%Y-%m-%d")
)))
conn.commit()
group[message.chat_id] = 1
bot_added[message.chat_id] = str(date.today().strftime("%Y-%m-%d"))
driver.chat_send_message(message.chat_id, "I-Bot is now active ✨")
except Exception as e:
driver.chat_send_message(YOUR_MOBILE_NUMBER + "@c.us",
"Starting bot for group failed" + str(e))
print("Starting bot for group failed" + str(e))
driver.chat_send_message(message.chat_id, "I-Bot failed to start for this chat!")
else:
driver.chat_send_message(message.chat_id, "I-Bot is already ON for this group")
else:
driver.chat_send_message(message.chat_id, "Command only for admins!")
db_chats = 0
if message.type == 'chat':
if message.content != "#on" and message.chat_id in group and group[
message.chat_id] == 1 and message.sender.id not in ignore_list:
if message.content == '#off' and db_chats == 0 and ((
str(message.sender.id) == YOUR_MOBILE_NUMBER + "@c.us") or message.sender.id in driver.wapi_functions.getGroupAdmins(
message.chat_id)):
db_chats = 1
try:
cur.execute('CALL add_chat(\'{}\',\'{}\',\'{}\')'.format("\"" + message.chat_id + "\"", 0,
str(date.today().strftime(
"%Y-%m-%d"))))
conn.commit()
group[message.chat_id] = 0
driver.chat_send_message(message.chat_id, "I-Bot is now inactive for this chat 🥺")
except Exception as e:
print(e)
driver.chat_send_message(message.chat_id, "I-Bot failed to start for this chat!")
db_chats = 0
elif message.content == "#ping":
driver.chat_send_message(message.chat_id, "Pong!!")
# commands for help and controls
elif (message.content == '#help' or message.content == '#command'):
s = []
s.append(
"*Welcome to the I-Bot*\n\n*Features*\n\n*Crypto*✅ \n-Check the price of crypto coin by sending \n*#cprice* _coin_ \n\n-Check latest crypto news by sending *#cnews* \nFor specific topic send *#cnews* _topic or coin name_\n\n-Check detail of a coin by sending *#cdetail* _coin_ .\n\n--------------------------------------------------\n")
s.append(
"*Compiler*✅ \n-Run any language code by sending \n*#run* _language_name_# \nWrite your code here from next line\n\n-Put language name as cpp,python3, c, java, etc\nNote-: Don't give runtime input statements or try to run infinite loop,it will give error.\n\n--------------------------------------------------\n")
s.append(
"*Ludo*✅ \n-Play Ludo with your friends on whatsapp. \nSend *#help_ludo* to know how to play.\n\n--------------------------------------------------\n")
s.append(
"*Tic Tac Toe Game*✅\n-To play send *#ticgame* _tag the number you want to play with_\n-To end the game early send *#quit_tic*\nType #help_tic for controls.\n\n--------------------------------------------------\n")
s.append(
"*Word game*✅\n-To start send #wordgame\n-Type #help_wgame for controls.\n\n--------------------------------------------------\n")
s.append(
"*Geeks for Geeks code extractor*✅\n-Get the code from geeks for geeks site according to the asked question.\n-To get the code for particular problem, type \n\n*#gfg#*_Your question_*#*_the language in which you want the code_\n\nEx-: ->*#gfg#merge sort#python*\n ->*#gfg #kadane algorithm#c++*.\n\n--------------------------------------------------\n")
s.append(
"*Match Emoji Game*✅\n\n-To start the game send *#matchgame*\n-For setting level add 2 or 4 or 6 after *#matchgame* with a space\n-For more detail send *#help_match*.\n\n--------------------------------------------------\n")
s.append(
"*Minesweeper Game*✅.*\n\n-To start the game send *#minegame* and to chosse a pair send *#mine* _xy_ where x is row and y is column.\n-For more commands of this game use #help_mine.\n-To know how to play visit-https://www.instructables.com/How-to-play-minesweeper/ \n\n--------------------------------------------------\n")
s.append(
"*Wikipedia Search*✅.*\n\n-Search anything on wikipedia by sending *#wiki* _title_\n\nEx. *#wiki monkey*.\n\n--------------------------------------------------\n")
s.append(
"*Tagger and Counter*✅\n\n-Now you will not miss the tags\nCheck where you were tagged by using *#last_tag* command.\n-Use it again to check second last tag and so on.\n-You can check upto last 50 tags.\n\n-You can also check the total number of messages you have sent by using *#msg_count* .\n\n--------------------------------------------------\n")
s.append(
"*Some admin/extra commands*\n\n- *#delete* to delete the bot message.\n-*#add* _919876543210_\n- *#kick* _tag the person you want to remove_\n- *#link* for getting the link of the group\n- *#tagall* \n- *#tagadmins* \n-Note-: You can also add some text after #tagall and #tagadmins.\n\nBot created by *_Karma_*\nGithub link-:https://github.com/Shyguy99/Whatsapp-bot")
out = ''.join(s)
driver.chat_send_message(message.chat_id, out)
elif message.content == "#help_ludo":
s = """*Welcome to the I-Bot Ludo*\n\nTo start the game send \n*#ludo* _and tag the members you want to play with_\n\nTo roll the dice send\n*#rdice*\n\nTo move your _heart_ piece send\n*#pmove h*\n\nTo move your circle piece send \n*#pmove c*\n\nTo see current ludo board send\n*#currludo*\n\nTo quit your game send\n*#quitludo*"""
driver.chat_send_message(message.chat_id, s)
elif message.content == '#help_wgame':
s = """*Welcome to the Word Game*\n\n*First join by entering your name send*\n#join _your name_\n\n*To guess the word send*\n#ans _your answer_\n\n*To check the score send*\n#score\n\n*To see the current word enter*\n#currword\n\n*If unable to guess and want to skip to the next word send*\n#skip\n\n*NOTE- IT'LL REQUIRE 3 PEOPLE TO SKIP CURRENT WORD*"""
driver.chat_send_message(message.chat_id, s)
elif message.content == '#help_tic':
s = """*Welcome to Tic Tac Toe Game*\n\n*Instructions*\n\nSend the corresponding number to the block where you want to place your symbol*\n\n#1 | #2 | #3\n#4 | #5 | #6\n#7 | #8 | #9\n*To end the game early send *#quit_tic*\n*To see your current game board uses #currtic"""
driver.chat_send_message(message.chat_id, s)
elif message.content == "#help_match":
s = """*Welcome to Match Emoji Game*\n\n*To end your current game send #quitmatch\n\n*To guess the pairs send #m with two pairs which you want to try matching\n*Ex. #m xy qr means xth row and yth column match with qth row and rth column\n*To check your curren game send #currmatch"""
driver.chat_send_message(message.chat_id, s)
elif message.content == "#help_mine":
s = """*Welcome to Minesweeper Game*\n\n*To choose a position send #mine xy where x is the row and y is column\n*To check your curren game send #currmine \n*To mark a position send #minemark xy\n*To unmark a position send #mineunmark xy."""
driver.chat_send_message(message.chat_id, s)
# add people to ignore list
elif message.content == "#ignore" and (message.sender.id == YOUR_MOBILE_NUMBER + "@c.us" or message.sender.id in driver.wapi_functions.getGroupAdmins(
message.chat_id)):
if message._js_obj["quotedMsgObj"]["sender"]["id"] not in ignore_list:
id = message._js_obj["quotedMsgObj"]["sender"]["id"]
cur.execute(
'CALL update_ignore_list(\'{}\',\'{}\')'.format("\"" + id + "\"", 1))
conn.commit()
ignore_list.add(message._js_obj["quotedMsgObj"]["sender"]["id"])
driver.chat_send_message(message.chat_id, "Done!!")
else:
driver.chat_send_message(message.chat_id, "He is already in ignore list!")
elif message.content == "#ignore" and (message.sender.id == YOUR_MOBILE_NUMBER + "@c.us" or message.sender.id in driver.wapi_functions.getGroupAdmins(message.chat_id)):
if message._js_obj["quotedMsgObj"]["sender"]["id"] in ignore_list:
id = message._js_obj["quotedMsgObj"]["sender"]["id"]
cur.execute(
'CALL update_ignore_list(\'{}\',\'{}\')'.format("\"" + id + "\"", 0))
conn.commit()
ignore_list.remove(message._js_obj["quotedMsgObj"]["sender"]["id"])
driver.chat_send_message(message.chat_id, "Done!!")
else:
driver.chat_send_message(message.chat_id, "He is not in ignore list")
# execute the python code from the message (owner command)
elif "#exec" in message.content:
if str(message.sender.id) == YOUR_MOBILE_NUMBER + "@c.us":
try:
exec(str(message.content))
except Exception as e:
driver.chat_send_message(message.chat_id, str(e))
else:
driver.chat_send_message(message.chat_id, "Owner command only!")
# reply something directly from code
elif "#reply" in message.content[:6]:
if str(message.sender.id) == YOUR_MOBILE_NUMBER + "@c.us":
k = message.content.replace("#reply", "").strip()
try:
exec("driver.chat_send_message(message.chat_id,str({}))".format(k))
except Exception as e:
driver.chat_send_message(message.chat_id, str(e))
else:
driver.chat_send_message(message.chat_id, "Owner command only!")
# to get all people message count
elif message.content == "#mcount":
if message.sender.id in driver.wapi_functions.getGroupAdmins(message.chat_id) or str(
message.sender.id) == YOUR_MOBILE_NUMBER + "@c.us":
while db_members == 1:
continue
db_members = 1
all_count = dict()
try:
cur.callproc('get_all_msg_count', ("\"" + message.chat_id + "\"",))
getcount = cur.fetchone()[0]
print(getcount)
l = len(getcount) // 2
j = l
for i in range(l):
id = getcount[i].replace("\"", "").replace("@c.us", "")
all_count[id] = int(getcount[j])
j += 1
out = ""
for key, value in all_count.items():
out += str(value) + "--> " + str(key) + "\n"
driver.wapi_functions.sendMessage(message.chat_id,
"Count of messages of all members\n(From {})\n\n".format(
bot_added[message.chat_id]) + out)
except Exception as e:
driver.chat_send_message(YOUR_MOBILE_NUMBER + "@c.us",
"Getting all counts got error:\n" + str(e))
print("Getting all counts got error:\n" + str(e))
db_members = 0
else:
driver.chat_send_message(message.chat_id, "Admin Command!!")
# ludo game
elif "#ludo" in message.content:
s = message.content.replace("#ludo", "")
s = s.strip()
s = s.split(" ")
fin_s = []
for i in s:
fin_s.append(i.replace("@", "") + "@c.us")
fin_s.append(message.sender.id)
fin_s = set(fin_s)
print(fin_s)
if len(fin_s) < 2 or len(fin_s) > 4:
driver.chat_send_message(message.chat_id,
"Number of players must be between 2 to 4.\nCurrent number of players- {}").format(
str(len(fin_s)))
else:
if len(set(fin_s).intersection(set(ludo_game_dict.keys()))) == 0:
all_parti = set(driver.wapi_functions.getGroupParticipantIDs(message.chat_id))
print(fin_s, all_parti)
if fin_s.issubset(all_parti):
fin_s = list(fin_s)
p1 = fin_s[0]
p2 = fin_s[1]
p3 = None
p4 = None
if len(fin_s) > 2:
p3 = fin_s[2]
if len(fin_s) > 3:
p4 = fin_s[3]
s_time = s_time + 1800
ludo_game_dict[message.sender.id] = karma_bot.ludo(driver, message, p1, p2, p3,
p4)
for i in fin_s:
ludo_game_dict[i] = ludo_game_dict[message.sender.id]
else:
driver.chat_send_message(message.chat_id, "Invalid Players")
else:
driver.chat_send_message(message.chat_id, "One or more player is already in a game.")
elif "#rdice" in message.content:
if message.sender.id in ludo_game_dict:
if ludo_game_dict[message.sender.id].players[message.sender.id].chance == 1:
if ludo_game_dict[message.sender.id].dthrow == 0:
s = message.content.replace("#rdice", "")
if len(s) == 1:
s = int(s)
else:
s = 0
ludo_game_dict[message.sender.id].dice(driver, message, s)
else:
driver.chat_send_message(message.chat_id,
"You have already threw the dice.Move your dice by sending #pmove h or #pmove c")
else:
driver.chat_send_message(message.chat_id, "Not your chance")
else:
driver.chat_send_message(message.chat_id, "You are not in the game")
elif "#pmove" in message.content:
if message.sender.id in ludo_game_dict:
if ludo_game_dict[message.sender.id].players[message.sender.id].chance == 1:
if ludo_game_dict[message.sender.id].dthrow == 1:
s = message.content.replace("#pmove", "").lower()
s = s.strip()
if len(s) != 0:
if s == "h" or s == "c":
ludo_game_dict[message.sender.id].move_piece(driver, message, s)
if len(ludo_game_dict[message.sender.id].cur_player_list) == 0:
t = ludo_game_dict[message.sender.id].li_players[:]
for pl in t:
del ludo_game_dict[pl]
else:
driver.chat_send_message(message.chat_id, "Wrong piece!! Choose c or h")
else:
driver.chat_send_message(message.chat_id,
"Empty parameter! Choose a piece h or c\nExample:- #pmove c")
else:
driver.chat_send_message(message.chat_id,
"First you have to throw the dice by sending #rdice")
else:
driver.chat_send_message(message.chat_id, "Not your chance")
else:
driver.chat_send_message(message.chat_id, "You are not in the game")
elif message.content == "#currludo":
if message.sender.id in ludo_game_dict:
ludo_game_dict[message.sender.id].current_board(driver, message)
else:
driver.chat_send_message(message.chat_id, "You are not in the game.")
elif message.content == "#quitludo":
if message.sender.id in ludo_game_dict:
ludo_game_dict[message.sender.id].quit(driver, message)
print(ludo_game_dict[message.sender.id].cur_player_list)
if len(ludo_game_dict[message.sender.id].cur_player_list) == 1:
driver.chat_send_message(message.chat_id, "Game ended!")
del ludo_game_dict[ludo_game_dict[message.sender.id].cur_player_list[0]]
del ludo_game_dict[message.sender.id]
driver.chat_send_message(message.chat_id, "Quitted!!")
else:
driver.chat_send_message(message.chat_id, "You are not in the game.")
# commands for playing word game
elif message.content == '#wordgame':
if Word.start == 1:
driver.chat_send_message(message.chat_id,
"Word Game already in progress 🏁\n Send #currword to check the word to be guessed.")
else:
Word.wgame_start(driver, message)
elif '#ans' in message.content[:5]:
if Word.start == 1:
if message.sender.id in Word.players.keys():
s = message.content.replace("#ans", "")
s = s.strip()
if len(s) > 2:
if Word.ans(driver, message, s):
threading.Thread(target=add_score,
args=(Word.players[message.sender.id],)).start()
Word.new_word(driver, message)
else:
driver.chat_send_message(message.chat_id,
"Empty Answer.Write your answer after #ans\nCheck current word by sending #currword")
else:
driver.chat_send_message(message.chat_id,
"You first have to join the game\nSend #join your name")
else:
Word.wgame_start(driver, message)
elif '#join' in str(message.content)[:6]:
if Word.start == 1:
s = message.content.replace("#join", "")
s = s.strip()
if len(s) != 0:
if message.sender.id in Word.players.keys():
driver.chat_send_message(message.chat_id,
"You are already in the game! 🤓\nSend #ans your answer to guess.")
elif s in Word.players.values():
driver.chat_send_message(message.chat_id, "Name already taken!")
else:
if p_adding == 0 and s_adding == 0:
p_adding = 1
s_adding = 1
cur.execute('CALL add_player(\'{}\',\'{}\')'.format(message.sender.id, s))
conn.commit()
Word.enter_game(driver, message, s)
p_adding = 0
s_adding = 0
else:
driver.chat_send_message(message.chat_id, "Wait! Try again")
else:
driver.chat_send_message(message.chat_id, "Empty Name!!Write your name after #join.")
else:
driver.chat_send_message(message.chat_id,
"Game haven't started yet!\nSend #wordgame to start.")
elif message.content == '#skip':
if Word.start == 1:
Word.skip(driver, message)
else:
driver.chat_send_message(message.chat_id, "Game not started yet!\nSend #wordgame to start")
elif message.content == '#currword':
if Word.start == 1:
Word.current_word(driver, message)
else:
Word.wgame_start(driver, message)
elif message.content == '#score':
Word.show_score(driver, message)
# to get tagged msg
elif message.content == "#last_tag":
all_get_tag_msg.append(message)
if db_members == 0:
db_members = 1
try:
cur.callproc('get_last_tag',
("\"" + message.chat_id + "\"", "\"" + message.sender.id + "\""))
out = cur.fetchone()
if out[0] == None:
driver.chat_send_message(message.chat_id,
"You don't have any tag left or your tags are updating\nTry later.")
else:
out = out[0]
msg = out
driver.chat_send_message(message.chat_id, msg)
except Exception as e:
driver.chat_send_message(YOUR_MOBILE_NUMBER + "@c.us",
"Getting last tag got error:\n" + str(e))
print("Getting last tag got error:\n" + str(e))
driver.chat_send_message(message.chat_id,
"Got some error!\nCause can be:Tagged Message Deleted")
db_members = 0
else:
driver.chat_send_message(message.chat_id, "Try again in 2 sec. Let me process last query")
# to get msg_count
elif message.content == "#msg_count":
while db_members == 1:
continue
db_members = 1
try:
s = message.sender.id
name = message.sender.push_name
if message._js_obj["quotedMsgObj"]:
s = message._js_obj["quotedMsgObj"]['sender']['id']
name = message._js_obj["quotedMsgObj"]['sender']['pushname']
cur.callproc('get_msg_count',
("\"" + message.chat_id + "\"", "\"" + s + "\""))
out = cur.fetchone()
if out[0] == None:
driver.chat_send_message(message.chat_id, "{} message count:\n\n*1*.".format(name))
else:
out = out[0]
driver.chat_send_message(message.chat_id,
"{} message count from {}:\n\n*{}*".format(name, bot_added[
message.chat_id], out))
except Exception as e:
driver.chat_send_message(YOUR_MOBILE_NUMBER + "@c.us",
"Getting msg count got error:\n" + str(e))
print("Getting msg count got error:\n" + str(e))
db_members = 0
# commands for playing Tic Tac Toe game
elif '#ticgame' in message.content:
if message.sender.id not in tic_player_dict:
s = message.content.split()
if len(s) == 2 and "@" in s[1]:
p2 = s[1].replace("@", "") + "@c.us"
if p2 not in tic_player_dict:
tic_player_dict[message.sender.id] = karma_bot.tic_tac_game(driver, message,
message.sender.id, p2)
tic_player_dict[p2] = tic_player_dict[message.sender.id]
else:
driver.chat_send_message(message.chat_id,
"The person you are trying to play with is already in a game 😐")
else:
driver.chat_send_message(message.chat_id,
"Wrong Command 🤐!\nType #ticgame <tag the person>.")
else:
driver.chat_send_message(message.chat_id,
"You are already in a game!\nQuit it by sending #quit_tic")
elif "#" in message.content and len(message.content) == 2 and str(message.content)[1].isdigit():
if message.sender.id in tic_player_dict:
tic_player_dict[message.sender.id].mark(driver, message, str(message.content)[1])
# check if match ended then remove the players
if tic_player_dict[message.sender.id].status != "":
pl = tic_player_dict[message.sender.id].players
del tic_player_dict[pl[0]]
del tic_player_dict[pl[1]]
else:
driver.chat_send_message(message.chat_id,
"You don't have any match!\nType #ticgame tag the person to play with. to start the game.")
elif message.content == "#quit_tic":
if message.sender.id in tic_player_dict:
pl = tic_player_dict[message.sender.id].players
pl.remove(message.sender.id)
del tic_player_dict[message.sender.id]
del tic_player_dict[pl[0]]
out1 = "Match quit by {}\n{} you won!!".format(
"@" + str(message.sender.id).replace("@c.us", ""),
"@" + str(pl[0]).replace("@c.us", ""))
driver.wapi_functions.sendMessageWithMentions(message.chat_id, out1, "")
else:
driver.chat_send_message(message.chat_id,
"You don't have any ongoing match!\nType #ticgame tag the person to play with to start the game.")
elif message.content == "#currtic":
if message.sender.id in tic_player_dict:
tic_player_dict[message.sender.id].current_match()
else:
driver.chat_send_message(message.chat_id,
"You don't have any ongoing match!\nType #ticgame tag the person to play with. to start the game.")
# calculator command
elif "#calc" in message.content[:5]:
s = message.content.replace("#calc", "").strip()
if s != "":
karma_bot.Calculator().calc(driver, message, s)
else:
driver.chat_send_message(message.chat_id,
"Empty expression. Write some exp after *#calc* \nExample:- #calc 2+12")
# command for getting code from Geeks For Geeks
elif "#gfg" in message.content[:5]:
GFG.gfg(driver, message, wd, win1, win2)
# command for starting match game
elif "#matchgame" in message.content:
if message.sender.id not in match_player_dict:
s = message.content.split()
print(s)
if len(s) == 2 and s[1].isdigit() and int(s[1]) <= 6:
diff = int(s[1])
match_player_dict[message.sender.id] = (karma_bot.matcher(driver, message, diff))
else:
match_player_dict[message.sender.id] = (karma_bot.matcher(driver, message))
else:
driver.chat_send_message(message.chat_id,
"You already started the game!\n Send #quitmatch to end ur game")
# command for quiting match game
elif message.content == "#quitmatch":
if message.sender.id in match_player_dict:
del match_player_dict[message.sender.id]
driver.chat_send_message(message.chat_id, "Your game deleted!")
else:
driver.chat_send_message(message.chat_id,
"You are not in the game!\n Send #matchgame to start.")
# command for guessing pairs in match game
elif "#m " in message.content[:4]:
if message.sender.id in match_player_dict:
s = message.content.replace("#m", "")
s = s.strip()
s = s.split()
if len(s) == 2:
s1 = s[0]
s2 = s[1]
if s1 == s2:
driver.chat_send_message(message.chat_id, "Don't choose same pairs")
elif s1.isdigit() and s2.isdigit():
match_player_dict[message.sender.id].guess(driver, message, s1, s2)
if match_player_dict[message.sender.id].corr == pow(
match_player_dict[message.sender.id].diff,
2):
del match_player_dict[message.sender.id]
else:
driver.chat_send_message(message.chat_id,
"Wrong input! Please check\n You have to choose two pairs Example:- #m 12 34")
else:
driver.chat_send_message(message.chat_id,
"Wrong input! Please check\n You have to choose two pairs \nExample:- #m 12 34")
else:
driver.chat_send_message(message.chat_id,
"Your game haven't started yet!!\nStart it by sending #matchgame")
# command for checking current match game
elif message.content == "#currmatch":
if message.sender.id in match_player_dict:
match_player_dict[message.sender.id].current_game(driver, message)
else:
driver.chat_send_message(message.chat_id, "Your game haven't started yet!!")
# command to start mine game
elif "#minegame" in message.content:
s = message.content.split()
if message.sender.id not in mine_player_dict:
if len(s) == 2 and s[1].isdigit() and int(s[1]) <= 6 and int(s[1]) > 0:
if message.sender.id not in mine_player_dict:
mine_player_dict[message.sender.id] = karma_bot.mine(driver, message, int(s[1]))
elif len(s) == 1:
mine_player_dict[message.sender.id] = karma_bot.mine(driver, message)
else:
driver.chat_send_message(message.chat_id, "Wrong input 😐")
else:
driver.chat_send_message(message.chat_id,
"You have already started the game!🤐\nQuit previous game to start again")
# command to choose position in mine map to mine
elif "#mine" in message.content:
if message.sender.id in mine_player_dict:
s = message.content.replace("#mine", "")
s = s.strip()
if len(s) != 0:
if s.isdigit():
mine_player_dict[message.sender.id].choose(driver, message, s)
if mine_player_dict[message.sender.id].status == "Lose" or mine_player_dict[
message.sender.id].status == "Won":
del mine_player_dict[message.sender.id]
else:
driver.chat_send_message(message.chat_id,
"Invalid command!\n Check valid command using #help_mine")
else:
driver.chat_send_message(message.chat_id,
"Empty parameter!You have to choose the box using row and column\nExample:-#mine 23")
else:
driver.chat_send_message(message.chat_id,
"You haven't started your game yet 😅\nStart it by sending #minegame")
elif "#minemark" in message.content or "#mineunmark" in message.content:
if message.sender.id in mine_player_dict:
s = message.content.replace("#minemark", "")
s = s.replace("#mineunmark", "")
s = s.strip()
if len(s) > 0:
if s.isdigit():
if "unmark" in s[0]:
mine_player_dict[message.sender.id].mark_pos(driver, message, s[1], 0)
else:
mine_player_dict[message.sender.id].mark_pos(driver, message, s[1], 1)
else:
driver.chat_send_message(message.chat_id,
"Invalid command!\n Check valid command using #help_mine")
else:
driver.chat_send_message(message.chat_id,
"Empty parameter!You have to choose the box\nExample:-#minemark 34")
else:
driver.chat_send_message(message.chat_id,
"You haven't started your game yet 😅\nStart it by sending #minegame")
elif message.content == "#currmine":
if message.sender.id in mine_player_dict:
s = mine_player_dict[message.sender.id].mine_cov_map
s = mine_player_dict[message.sender.id].listtostring(s)
driver.chat_send_message(message.chat_id, "Your ongoing game!\n\n" + s)
else:
driver.chat_send_message(message.chat_id,
"You haven't started your game yet 😅\nStart it by sending #minegame")
elif message.content == "#quitmine":
if message.sender.id in mine_player_dict:
del mine_player_dict[message.sender.id]
driver.chat_send_message(message.chat_id,
"You have quit your game in middle!🤭\n*LOSER!!*")
else:
driver.chat_send_message(message.chat_id,
"You haven't started your game yet 😅\nStart it by sending #minegame")
elif "current transaction is aborted, commands ignored until end of transaction block" in message.content:
flag = 1
# wikipedia command
elif "#wiki" in message.content[:6]:
s = message.content.replace("#wiki", "")
s = s.strip()
if len(s) > 2:
try:
out = wikipedia.page(s)
driver.chat_send_message(message.chat_id,
'*Title* :{}\n*Source* : {}\n{}'.format(out.title, out.url,
out.content))
except:
driver.chat_send_message(message.chat_id, "Can't find anything!!")
else:
driver.chat_send_message(message.chat_id,
"Empty parameter!!You have give some word to be searched\nExample:-#wiki money")
elif len(message.content) > 5 and "#run " in message.content[0:5]:
try:
if COMP.inuse != 1:
s = message.content[1:]
idx = s.index("#")
s1 = s[:idx].strip()
lang = s1.replace("run ", "")
code = s[idx + 1:]
if len(code) > 3:
COMP.run(driver, message, lang, code)
else:
driver.chat_send_message(message.chat_id,
"Empty Code!!Write some code after the command\Example:-\n#run python3#\nprint(\"Hello World\")")
else:
driver.chat_send_message(message.chat_id,
"Someone using the compiler.\nLet him/her finish or use #resetrun to terminate")
except Exception as e:
print(e)
driver.chat_send_message(message.chat_id, "Some Error Occured")
COMP.inuse = 0
elif message.content == "#runlimit":
driver.chat_send_message(message.chat_id, "Limit Left: " + str(200 - int(COMP.r.usage())))
elif message.content == "#listlang":
s = ','.join(COMP.languages)
driver.chat_send_message(message.chat_id, "Languages supported by compiler:-\n" + s)
elif message.content == "#resetrun":
COMP.inuse = 0
driver.chat_send_message(message.chat_id, "Program Terminated!!")
# crypto commands
elif "#cprice" in message.content[:8]:
s = message.content.replace("#cprice", "")
s = s.strip()
if s != '':
Crypto.price(driver, message, s)
else:
driver.chat_send_message(message.chat_id,
"Empty Parameter! Type the coin symbol\nExample:- #cprice btc")
elif "#cnews" in message.content[:7]:
s = message.content.replace("#cnews", "")
s = s.strip()
Crypto.news(driver, message.chat_id, CRYPTOPANIC_API, s)
elif "#cdetail" in message.content[:10]:
s = message.content.replace("#cdetail", "")
s = s.strip()
if s != '':
Crypto.detail(driver, message, s)
else:
driver.chat_send_message(message.chat_id,
"Empty Parameter! Type the coin symbol\nExample:- #cdetail btc")
elif message.content == "#mmi":
Crypto.mmi(driver, message)
# mass kick members
elif "#masskick " in message.content and str(message.sender.id) == YOUR_MOBILE_NUMBER + "@c.us":
all_members = driver.wapi_functions.getGroupParticipantIDs(message.chat_id)
l = (message.content.replace("#masskick ", "")).strip()
l = l.split()
if len(l) == 1:
limit = len(all_members)
else:
limit = int(l[1])
s = int(l[0])
cur.execute(
'select member_id from members_table where msg_count>{} and chat_id=\'{}\''.format(s,
"\"" + message.chat_id + "\""))
out = cur.fetchall()
p = [i[0].replace("\"", "") for i in out]
driver.chat_send_message(message.chat_id, "Removing inactive members....")
n = 0
for i in range(len(all_members)):
if limit == 0:
break
print(all_members[i], p)
if all_members[i] not in p and all_members[i] != YOUR_MOBILE_NUMBER + "@c.us":
driver.remove_participant_group(message.chat_id,
all_members[i])
time.sleep(1)
limit -= 1
n += 1
driver.chat_send_message(message.chat_id, "{} Inactive members removed".format(n))
# kick and add member command
elif "#kick" in message.content[0:6] or "#add" in message.content[:6]:
s = message.content.replace("#kick", "")
s = s.replace("#add", "")
s = s.strip()
if len(s) > 1 or message._js_obj["quotedMsgObj"]:
s = s.replace("@", "") + "@c.us"
if message._js_obj["quotedMsgObj"]:
s = message._js_obj["quotedMsgObj"]["sender"]["id"]
try:
if message.sender.id in driver.wapi_functions.getGroupAdmins(message.chat_id):
if isAdmin(message.chat_id):
if "#kick" in message.content:
if driver.remove_participant_group(message.chat_id,
s):
print("Participant Removed")
else:
driver.chat_send_message(message.chat_id, "Can't remove")
else:
if driver.add_participant_group(message.chat_id, s):
print("Participant added")
else:
driver.chat_send_message(message.chat_id,
"Fail!!\n Number is invalid or Format for adding number is:\n#add 918888888888")
else:
driver.wapi_functions.sendMessage(message.chat_id, "Bot not admin yet")
else:
driver.chat_send_message(message.chat_id, 'Sorry!! Admin command')
except Exception as e:
print(f"Error -> {str(e)}")
driver.chat_send_message(message.chat_id, "Fail!!")
else:
driver.chat_send_message(message.chat_id, "No person/number tagged!!")
# for getting group link
elif message.content == "#link":
try:
driver.send_message_with_auto_preview(message.chat_id,
driver.wapi_functions.getGroupInviteLink(
message.chat_id), "")
except Exception as e:
print(str(e))
driver.chat_send_message(message.chat_id, "Fail!!")
# to get source code
elif message.content == "#source":
driver.send_message_with_auto_preview(message.chat_id,
"https://github.com/Shyguy99/Whatsapp-bot", "")
elif "#tagalll" in message.content or "#tagadminss" in message.content:
if message.sender.id in driver.wapi_functions.getGroupAdmins(message.chat_id):
if "#tagadmins" in message.content:
s = message.content.split("#tagadmins")
all_parti = driver.wapi_functions.getGroupAdmins(message.chat_id)
else:
s = message.content.split("#tagall")
all_parti = driver.wapi_functions.getGroupParticipantIDs(message.chat_id)
msg = s[0] + "\n"
msg = msg.replace("#", "")
for i in all_parti:
msg += " @{} \n".format(i).replace("@c.us", "")
msg += s[1]
driver.wapi_functions.sendMessageWithMentions(message.chat_id, msg, '')