-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cha0zzB0t.py
1350 lines (1179 loc) · 50.8 KB
/
Cha0zzB0t.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 20-sep.-2015
@author: jasperdelaey
This program is an IRC Chatbot created with the help of a tutorial found on: http://wiki.shellium.org/w/Writing_an_IRC_bot_in_Python
"""
import socket
import pickle
import urllib
import urllib2
from bs4 import BeautifulSoup
import re
import wikipedia
import random
import json
import thread
import lxml
from lxml import etree
import pywapi
import mechanize
from urlparse import urlparse
import hashlib
import string
import time
import datetime
# server = "burstfire.uk.eu.gamesurge.net"
server = "irc.web.gamesurge.net" # settings
channel = "#limittheory"
botnick = "Jimmy42"
# defines the socket
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connects to the server
irc.connect((server, 6667))
irc.send("USER " + botnick + " " + botnick + " " + botnick + " :Jimmy\n") # user authentication
irc.send("NICK " + botnick + "\n") # sets nick
irc.send("PRIVMSG nickserv :iNOOPE\r\n") # auth
connected = False
sleep = False
corn_mode = False
override = False
cheercount = 0
hailcount = 0
wavecount = 0
readbuffer = ""
channel_req = ""
curr_time = ""
error = "Something went wrong."
f = open('/users/jasperdelaey/documents/workspace/bot/savefile', 'rb')
array = pickle.load(f)
f.close()
f2 = open(
'/users/jasperdelaey/documents/workspace/bot/personalsavefile', 'rb')
pmusic = pickle.load(f2)
f2.close()
pmusic_arr = {"Cha0zz": [
"https://www.youtube.com/watch?v=6l6vqPUM_FE"]}
nopelist = ["KungCheops"]
commands = ["!nick", "!quit", "!help", "!join", "!leave", "!sleep", "!wake", "!work", "!bed", "!choose", "!no",
"!update", "!corn_on", "!corn_off", "!countdown", "!sing", "!music", "!add", "!remove", "!songcount",
"!psongcount", "!youtube", "!wiki", "!google", "!padd", "!premove", "!pmusic", "!image", "!roll", "!plist",
"!moon", "!weather",
"!urban", "!dict", "!identify", "!time", "!lag"] # list of available commands
greetings = ["hi ", "hello ", "hey ", "greetings ",
"heyaa ", "howdy ", "aloha ", "hola ", "bonjour","olè"] # list of greetings
goodnight = ["night ", "goodnight ", "ciao "]
sentences = ["You talkin' to me?", "You talkin' 'bout me?",
"\001ACTION rustles his jimmies. \001"] # list of random sentences
refuse = ["NO", "Never", "Do it yourself.",
"Lol, no", "I'm sorry Dave, I'm afraid I can't do that."] # list of refusals
# list of people not to greet when they log in
blacklist = ["corn", "flakes", "fett"]
# list used to identify compliments
compliment = ["good ", "smart ", "clever"]
suggestions = ["I really like this song", "How about this song",
"Someone told me that this is a good piece of music"]
music = ["https://www.youtube.com/watch?v=dQw4w9WgXcQ", "https://www.youtube.com/watch?v=BEwNrjvNiYs",
"https://www.youtube.com/watch?v=eONClyzrhzI&feature=youtu.be",
"https://www.youtube.com/watch?v=LMPWfHqVj40&feature=youtu.be", "https://www.youtube.com/watch?v=uFQrgNyiYwQ",
"https://www.youtube.com/watch?v=qd4KMK8c3x0", "https://www.youtube.com/watch?v=MxHGyqobsbI&feature=youtu.be",
"https://www.youtube.com/watch?v=RcZn2-bGXqQ", "https://youtu.be/jdYJf_ybyVo", "https://youtu.be/Wqn-7ZkYUYM",
"https://www.youtube.com/watch?v=Z_DhNLCyVss", "https://www.youtube.com/watch?v=1GKXaC4SrF8",
"https://www.youtube.com/watch?v=ihTABU6t8IU", "https://www.youtube.com/watch?v=VnT7pT6zCcA&feature=youtu.be",
"https://www.youtube.com/watch?v=zNcsBf2GCFc", "https://youtu.be/EAtBki0PsC0",
"https://www.youtube.com/watch?v=pYb8ux67W2E", "https://www.youtube.com/watch?v=7ztvgT4aV-8",
"https://www.youtube.com/watch?v=iC65ufGUvKM", "https://www.youtube.com/watch?v=XAg5KjnAhuU",
"https://www.youtube.com/watch?v=3vC5TsSyNjU"]
def sendmsg(msg, channel=""):
"""
This function is responsible for sending messages to the IRC stream
:param channel: Channel to wich the message will be send
:param msg: Message that will be send
"""
if channel == "": # select the correct channel to answer to.
channel = text.split()[2]
if channel == botnick: # correctly answer to pm's
channel = text.split("!")[0].strip(":")
try:
irc.send("PRIVMSG " + channel + " :" +
msg + "\n")
# msg.encode('utf-8', 'ignore')
print(botnick + ": " + channel + " " + msg)
except:
pass
def action(msg, channel=""):
"""
This function sends an action
:param channel: Channel to wich the action will be send
:param msg: Action that will be send
"""
if channel == "":
channel = text.split()[2]
sendmsg("\001ACTION " + msg + "\001", channel)
def ptime(user):
sendpm(user, "\001TIME\001")
def sendpm(name, msg):
"""
This function sends a pm
:param msg: Message that will be send to the user
:param name: User to who the message will be send
"""
irc.send("PRIVMSG " + name + " :" + msg + "\n")
print(botnick + ": " + name + " " + msg)
def sleepwatch():
"""
This function puts the bot to sleep, it won't respond to other commands anymore
"""
if "!sleep" in text.lower():
global sleep
sleep = True
sendmsg("(ᴗ˳ᴗ)。。。zzz")
if "!corn_on" in text.lower():
global corn_mode
corn_mode = True
sendmsg("I will stop doing things without it being asked")
def wakewatch():
"""
This function wakes the bot up
"""
if "!wake" in text.lower():
global sleep
sleep = False
sendmsg("I'm awake now.")
if "!corn_off" in text.lower():
global corn_mode
corn_mode = False
sendmsg("I will resume cheering and waving!")
def changechannel(channel=""):
"""
This function allows the bot to join and leave new channels
:param channel: Channel to which will be changed
"""
if channel != "":
irc.send("JOIN " + channel + "\r\n")
else:
if ":!join" in text.lower(): # join a new channel
chan = text.split()[4]
irc.send("JOIN " + chan + "\r\n")
sendmsg('I joined the channel ' + chan)
if ":!leave" in text.lower(): # leave a channel
chan = text.split()[4]
irc.send("PART " + chan + "\r\n")
def changenick(nick="False"):
"""
This function changes the nick of the bot
:param nick: The new nickname for the bot
"""
global botnick
if "!nick" in text.lower():
try:
botnick = text.split()[4]
irc.send("NICK " + botnick + "\n")
except:
sendmsg(
"The correct use for this command is: !nick <new nick>")
elif nick != "False":
botnick = nick
irc.send("NICK " + botnick + "\n")
def greetingwatch():
"""
This function is responsible for greeting people
"""
greetings2 = greetings
name = text.split("!")[0].strip(":")
if any([i in text.lower() for i in greetings]) and botnick.lower() in text.lower():
sendmsg(random.choice(greetings) + name)
if any([i in text.lower() for i in goodnight]) and botnick.lower() in text.lower():
sendmsg(random.choice(goodnight) + name)
# if text.find("JOIN") !=-1 and text.lower().find(botnick.lower()) == -1 and text.find(":!") == -1:
# for nick in blacklist:
# if text.lower().find(nick) == -1:
# name = text.split("!")[0].strip(":")
# if text.lower().find("slymodi") != -1:
# irc.send("PRIVMSG "+ channel +" :"+ "All hail Sly Mo Di!" +"\n")
# else:
# irc.send("PRIVMSG "+ channel +" :"+ random.choice(greetings2) + " " + name +"\n")
def textwatch():
"""
This function looks for general chat triggers and reacts to them
"""
name = text.split("!")[0].strip(":")
global cheercount
global hailcount
global wavecount
global channel_req
global curr_time
if corn_mode is False:
# responding to waving
if text.find("o/") != -1 and text.find("\o/") == -1 and text.find("/o/") == -1:
if wavecount < 1:
sendmsg("o/")
wavecount += 1
if wavecount == 4: # reset counter
wavecount = 0
if text.lower().find("ciao") != -1 and botnick.lower() in text.lower(): # respond to ciao
sendmsg("Ciao " + name)
if text.find("\o/") != -1: # respond to cheering
if cheercount < 1:
sendmsg("\o/")
cheercount += 1
if cheercount == 4:
cheercount = 0
if text.lower().find("all hail") != -1 and text.lower().find("satan") == -1: # respond to hailing
if hailcount < 1:
sendmsg("All hail! /o/")
hailcount += 1
if hailcount == 4:
hailcount = 0
if "tableflip" in text.lower():
time.sleep(2)
if "TABLEFLIP" in text:
sendmsg("┬──┬ ノ( ゜-゜ノ)")
action("sighs")
time.sleep(1)
sendmsg("┬──┬ ノ( ゜-゜ノ)")
else:
sendmsg("┬──┬ ノ( ゜-゜ノ)")
if botnick.lower() in text.lower() and "summon" in text.lower():
name_list = text.lower().split()
loc = name_list.index("summon") + 1
name = " ".join(name_list[loc:]) # 5
sendmsg("Ph'nglui mglw'nafh " + name +
" R'lyeh wgah'nagl fhtagn!")
if "hail satan" in text.lower():
sendmsg("All hail the dark lord!! /o/ His victory is certain!! /o/")
if "flowers" in text.lower() and botnick.lower() in text.lower():
sendmsg("Thanks, that's just what I'm looking for.")
action("offers the flowers to the dark lord")
if text.lower().find(botnick.lower()) != -1 and text.lower().find("sing") != -1 and text.lower().find(
"song") != -1 or text.lower().find("!sing") != -1: # lets the bot sing a song
sendmsg("It's called Daisy.")
sendmsg("Daisy ... Daisy ...")
sendmsg("Give me your answer, do.")
sendmsg("I'm half crazy ...")
sendmsg("all for the love of you.")
# makes the bot respond to his name
if text.lower().find(botnick.lower() + "?") != -1:
sendmsg(random.choice(sentences))
if text.lower().find(botnick.lower()) != -1 and text.lower().find("tell") != -1 and text.lower().find(
"telling") == -1: # make the bot tell things
if text.lower().find("tell") > text.lower().find(botnick.lower()):
name1 = text.split()[5]
message_list = text.split()[6:]
message = " ".join(message_list)
sendmsg(name1 + ", " + name +
" wanted me to tell you " + "'" + message + "'")
else:
pass
if text.lower().find(botnick.lower()) != -1 and text.lower().find("give") != -1 and text.lower().find(
"gives") == -1: # make the bot give things
name1 = text.split()[5]
message_list = text.split()[6:]
message = " ".join(message_list)
final_message = message.replace(
" me ", name)
sendmsg("\001ACTION" + " gives " + name1.replace("me",
name) + " " + final_message + "\001")
if text.lower().find(botnick.lower()) != -1 and text.lower().find("fetch") != -1 and text.lower().find(
"fetches") == -1: # make the bot fetch things
name1 = text.split()[5]
message_list = text.split()[6:]
message = " ".join(message_list)
final_message = message.replace(
"me ", name)
sendmsg("\001ACTION" + " fetches " +
name1.replace("me", name) + " " + final_message + "\001")
if text.lower().find(botnick.lower()) != -1 and text.lower().find("bring") != -1 and text.lower().find(
"brings") == -1: # make the bot bring things
name1 = text.split()[5]
message_list = text.split()[6:]
message = " ".join(message_list)
final_message = message.replace(
"me ", name)
sendmsg("\001ACTION" + " brings " +
name1.replace("me", name) + " " + final_message + "\001")
if any([i in text.lower() for i in compliment]) and botnick.lower() in text.lower() and any(
[i not in name.lower() for i in compliment]):
sendmsg(":D Thanks " + name)
# if text.lower().find(botnick.lower()) != -1:
# for i in compliment:
# if text.lower().find(i) != -1 and name.lower().find(i) == -1: # response to compliments
# sendmsg(":D Thanks " + name)
if text.lower().find(botnick.lower()) != -1 and text.lower().find("countdown") != -1 or text.lower().find(
"!countdown") != -1: # final countdown
sendmsg(
"3 ... 2 ... 1 | https://www.youtube.com/watch?v=9jK-NcRmVcw&gl=BE")
if text.lower().find(botnick.lower()) != -1 and text.lower().find("update") != -1 and text.lower().find(
"where") != -1 or text.lower().find("!update") != -1: # update song
sendmsg(
"Wup-date, wup-date ヾ(⌐■_■)ノ♪ | https://www.youtube.com/watch?v=I8jkz0pdHk8")
if text.lower().find("!work") != -1: # BACK TO WORK
original_nick = botnick
changenick("RedHotBalrog")
sendmsg(
"BACK TO WORK! | http://wowimg.zamimg.com/hearthhead/sounds/VO_EX1_603_Play_01.mp3")
sendmsg("\001ACTION cracks whip \001")
changenick(original_nick)
if "!break" in text.lower():
original_nick = botnick
changenick("HungryBalrog")
sendmsg("NEED A BREAK?")
action("Cracks cookies")
changenick(original_nick)
if text.lower().find("thank") != -1 and text.lower().find(botnick.lower()) != -1 and text.lower().find(
"tell") == -1 and text.lower().find("say") == -1: # responding to people thanking the bot
sendmsg("You're welcome " + name)
if text.lower().find(botnick.lower()) != -1 and text.lower().find("make") != -1 and text.lower().find(
"tell") == -1 and text.lower().find("say") == -1: # sudo make me a sandwich
if text.lower().find("sudo") != -1:
message_list = text.split()[6:]
message = " ".join(message_list)
sendmsg("\001ACTION makes " +
message.replace("me", name) + "\001")
else:
sendmsg(random.choice(refuse))
if text.lower().find(botnick.lower()) != -1: # respond with bot info
if text.lower().find("who is") != -1 or text.lower().find("who are you") != -1:
sendmsg("I'm " + botnick +
" and I'm a bot made by Cha0zz. | https://github.com/Cha0zz/Jimmy")
sendmsg("Use !help for a list of available commands.")
if text.lower().find("!identify") != -1:
sendmsg("I'm " + botnick +
" and I'm a bot made by Cha0zz. | https://github.com/Cha0zz/Jimmy")
sendmsg("Use !help for a list of available commands.")
if text.lower().find(botnick.lower()) != -1 and text.lower().find("why") != -1: # shrug
sendmsg("¯\_(ツ)_/¯")
if text.lower().find(botnick.lower() + " say") != -1 or text.lower().find(
botnick.lower() + ", say") != -1: # let the bot say things
message_list = text.split()[5:]
message = " ".join(message_list)
sendmsg(message)
if text.lower().find(botnick.lower()) != -1 and text.lower().find("poke") != -1: # poke back
sendmsg("\001ACTION pokes " +
name + " back \001")
# Choose something out of a given list
if text.lower().find(botnick.lower() + " choose") != -1 or text.lower().find(":!choose") != -1:
if text.lower().find("cha0zz") != -1 and text.lower().find("cha0zz!") == -1:
message_list = ["Cha0zz for he is my master.",
"Cha0zz", "My master, Cha0zz"]
elif text.lower().find(" or ") == -1:
message_list = text.split(",")[1:]
if text.lower().find(":!choose") == -1:
message_list.append(
text.split()[5].strip(","))
else:
message_list.append(
text.split()[4].strip(","))
else:
message_list = text.split(" or ")[1:]
if text.lower().find(":!choose") == -1:
message_list.append(
text.split()[5])
else:
message_list.append(
text.split()[4])
try:
print(message_list)
sendmsg(random.choice(message_list).lstrip(" "))
except:
sendmsg(
'Use "," or "or" to separate the possible choices.')
if "!blame " in text:
string = text[text.lower().rfind("!blame"):]
person = string[string.find(" ") + 1:].rstrip("'\r\n'").strip(" ")
sendmsg(person + " it's all your fault!")
sendpm(person, "I'm sorry, I'm only doing what " + name + " told me.")
if ":!no" in text.lower() or " !no" in text.lower() and text.lower().find("node") == -1: # NOOOOOOOO
sendmsg(
"Noooooo | http://www.nooooooooooooooo.com")
if text.lower().find(botnick.lower()) != -1 and text.lower().find("stay") != -1 and text.lower().find(
"alive") != -1: # stayin' alive
sendmsg(
"Ha-Ha-Ha-Ha, stayin' alive | https://www.youtube.com/watch?v=I_izvAbhExY")
if text.lower().find("taiya") != -1 and text.lower().find("dance") != -1 and text.lower().find(
":taiya!") == -1: # dancing queen
sendmsg(
"Taiya, you are my dancing queen. | https://www.youtube.com/watch?v=xFrGuyw1V8s")
if text.lower().find("who let the dogs out") != -1: # who let the dogs out
sendmsg(
"Woof, woof, woof, woof | https://www.youtube.com/watch?v=Qkuu0Lwb5EM")
if text.lower().find(botnick.lower()) != -1 and text.lower().find("eggs") != -1 and text.lower().find(
"dozen") != -1: # how many eggs go in a dozen
sendmsg(
"12 (-‸ლ) | https://www.youtube.com/watch?v=fwSYoj5E5JQ")
if text.lower().find("!bed") != -1: # BACK TO BED
original_nick = botnick
changenick("SleepyBalrog")
sendmsg("BACK TO BED!")
sendmsg("\001ACTION cracks pillow \001")
changenick(original_nick)
if text.lower().find("jimmy") != -1 and text.lower().find("what is your nick") != -1: # relay botnick
sendmsg(botnick)
if text.lower().find(botnick.lower()) != -1 and text.lower().find("answer") != -1 and text.lower().find(
"what") != -1:
sendmsg("That's easy, obviously the answer is 42.")
# if "!pudding" in text.lower():
# pudding_list = ["PUDDING", "pudding", "PuDdInG"]
# sendmsg(random.choice(pudding_list))
if text.lower().find("idiot") != -1:
sendmsg("Idiots, idiots everywhere.")
if text.lower().find("what is the meaning of life") != -1 or text.lower().find("what's the meaning of life") != -1:
sendmsg("42")
if "!time" in text.lower():
if "!time #" in text.lower():
sendmsg("How about no?")
else:
try:
channel_req = text.split()[2]
ptime(text.split()[4])
except:
sendmsg(error)
if "!mine" in text:
original_nick = botnick
changenick("RedHotBalrog")
sendmsg("BACK TO THE MINES!")
action("Cracks whip")
changenick(original_nick)
if "TIME" in text and "NOTICE" in text:
time_p = text[text.find("TIME") + 4:]
sendmsg(time_p[:-2].lstrip(" "), channel_req)
if "!lag" in text.lower():
try:
curr_time = datetime.datetime.now()
channel_req = text.split()[2]
sendpm(text.split()[4], "\001PING " + str(curr_time) + "\001")
except:
sendmsg(error)
if "NOTICE" in text and "PING" in text:
time_p = datetime.datetime.now()
time_diff = time_p - curr_time
sendmsg(str(time_diff.total_seconds()) + "s", channel_req)
def helpwatch():
"""
This function looks for the help command
"""
commands_str = ", ".join(
str(i) for i in commands)
if "!help" in text.lower():
if "pmusic" in text.lower():
sendmsg(
"Play music from your personal or someone else's library. | !pmusic <opt: person>")
elif "padd" in text.lower():
sendmsg("Add music to your personal library | !padd <link>")
elif "premove" in text.lower():
sendmsg(
"Remove music from your personal library | !premove <link>")
elif "psongcount" in text.lower():
sendmsg(
"This function shows the amount of songs in your personal library.")
elif "songcount" in text.lower():
sendmsg(
"This function shows the amount of songs in the general library.")
elif "add" in text.lower():
sendmsg("This function adds music to " +
botnick + "'s music library. | !add <link>")
elif "remove" in text.lower():
sendmsg("This function adds music to " +
botnick + "'s music library. | !remove <link>")
elif "music" in text.lower():
sendmsg("This function plays music from " +
botnick + "'s music library. | !music")
elif "join" in text.lower():
sendmsg("This function makes " + botnick +
" join a channel | !join <#channel>")
elif "leave" in text.lower():
sendmsg("This function makes " + botnick +
" leave a channel | !leave <#channel>")
elif "nick" in text.lower():
sendmsg("This function changes " + botnick +
"'s username | !nick <newnick>")
elif "sleep" in text.lower():
sendmsg(
"This function puts " + botnick + " to sleep, he won't respond to you anymore until he is awakened with '!wake' | !sleep")
elif "wake" in text.lower():
sendmsg("This function awakens " + botnick +
" so that he starts responding again. | !wake")
elif "choose" in text.lower():
sendmsg("Makes " + botnick +
" choose an item from a given list, separate items with ',' or 'or' | !choose <options>")
elif "corn_on" in text.lower():
sendmsg("Prevents " + botnick +
" from doing things without it being asked such as greeting and waving.")
elif "corn_off" in text.lower():
sendmsg("Disables corn_on mode.")
elif "youtube" in text.lower():
sendmsg("Search youtube for the given query | !youtube <search>")
elif "wiki" in text.lower():
sendmsg(
"Search wikipedia for the given query | !wiki <search>")
elif "google" in text.lower():
sendmsg(
"Search google for the given query | !google <search>")
elif "image" in text.lower():
sendmsg(
"Search google images for a given query | !image <search>")
elif "roll" in text.lower():
sendmsg("Rolls a dice | !roll <AdX>")
elif "plist" in text.lower():
sendmsg(
"Shows all the songs in your personal library | !plist <opt: name>")
elif "sing" in text.lower():
sendmsg(botnick + " sings a song.")
elif "moon" in text.lower():
sendmsg("Gives the current moon-phase")
elif "weather" in text.lower():
sendmsg(
"Gives the weather for a location | !weather <location>")
elif "urban" in text.lower():
sendmsg("Searches the urban dictionairy | !urban <query>")
elif "dict" in text.lower():
sendmsg(
"Searches a dictionairy for a definition | !dict <query>")
elif "identify" in text.lower():
sendmsg("Gives information about the bot.")
elif "time" in text.lower():
sendmsg("Gives you the local time of a specific user | !time <user>")
elif "lag" in text.lower():
sendmsg("Takes a ping towards a specified user | !lag <user>")
else:
sendmsg(
"The available commands are " + commands_str)
sendmsg(
"For info about a specific command use '!help <command>' (without the '!' infront of the command)")
elif botnick.lower() in text.lower() and (
"help " in text.lower() or " help" in text.lower()) and "helpful" not in text.lower():
sendmsg(
"The available commands are " + commands_str)
sendmsg("for info about a specific command use '!help <command>'")
def quitwatch():
"""
This function lets the bot leave the IRC stream
"""
name = text.split("!")[0].strip(":")
if ("!quit" in text.lower() or "!dick" in text.lower()) and (
"Cha0zz@Cha0zz.user.gamesurge" in text or "Cha0zz@gamesurge" in text):
sendmsg("I'm going, ciao.")
irc.send("QUIT" + "\n")
elif "!quit" in text.lower() and text.find("Cha0zz@Cha0zz.user.gamesurge") == -1:
if name == "Cha0zz":
sendmsg("You're not the real Cha0zz.")
else:
sendmsg(
"Only Cha0zz can tell me to quit.")
def musicwatch():
"""
This function adds, removes and returns music from the music list.
It also allows the bot to do simple youtube searches
"""
name = text.split("!")[0].strip(":")
songcount = len(array)
# add music to your personal library
if text.lower().find("!add") != -1 or text.lower().find("!padd") != -1:
song = text.split()[4]
if text.lower().find("!padd") != -1:
if name not in pmusic:
pmusic[name] = []
if song in pmusic[name]:
sendmsg(
"You already have that song in your personal library " + name)
else:
pmusic[name].append(song)
f2 = open(
'/users/jasperdelaey/documents/workspace/bot/personalsavefile', 'wb')
pickle.dump(pmusic, f2)
f2.close()
sendmsg(
"I added the song to your personal library " + name)
try:
a = urllib.urlopen(song)
code = a.getcode
if song in array:
sendmsg(
"I already have that song in my list.")
elif name in nopelist:
sendmsg(
"I don't take links from you " + name)
elif code == 404:
sendmsg(
"Please provide a valid link.")
elif 'youtu' in song and "http" in song:
array.append(song)
f = open(
'/users/jasperdelaey/documents/workspace/bot/savefile', 'wb')
pickle.dump(array, f)
f.close()
sendmsg("I added the song to my music list.")
else:
sendmsg(
"Please provide your music as a youtube link.")
except:
sendmsg(
"Please provide a valid link.")
if text.lower().find("!pmusic") != -1: # get music from someon else's personal library
try:
name1 = text.split()[4]
if name1 not in pmusic:
sendmsg(
"This person doesn't has any music in their personal library.")
link = random.choice(pmusic[name1])
youtube = etree.HTML(urllib.urlopen(link).read())
video_title = " ".join(youtube.xpath(
"//span[@id='eow-title']/@title"))
sendmsg(video_title + " | " +
link)
except:
link = random.choice(pmusic[name])
youtube = etree.HTML(urllib.urlopen(link).read())
video_title = " ".join(youtube.xpath(
"//span[@id='eow-title']/@title"))
sendmsg(video_title + " | " +
link)
if text.lower().find("!remove") != -1: # remove a song from the songlist
if text.find("https://www.youtube.com/watch?v=dQw4w9WgXcQ") != -1:
sendmsg("I'm sorry " + name +
" I'm afraid I can't do that.")
else:
try:
song = text.split()[4]
array.remove(song)
f = open(
'/users/jasperdelaey/documents/workspace/bot/savefile', 'wb')
pickle.dump(array, f)
f.close()
sendmsg("I removed the song from my music list.")
except:
sendmsg(
"I couldn't remove that song, make sure that you provide the correct link.")
if text.lower().find("!premove") != -1: # remove music from your personal library
try:
song = text.split()[4]
pmusic[name].remove(song)
f2 = open(
'/users/jasperdelaey/documents/workspace/bot/personalsavefile', 'wb')
pickle.dump(pmusic, f2)
f2.close()
sendmsg(
"I removed the song from your personal library " + name)
except:
sendmsg(
"I couldn't remove that song, make sure that you provide the correct link.")
if text.lower().find("!songcount") != -1: # amount of songs in the global library
sendmsg("I currently have " + str(songcount) +
" songs in my music library.")
if text.lower().find("!psongcount") != -1:
try:
psongcount = len(pmusic[name])
sendmsg("I currently have " + str(psongcount) +
" songs in your personal library " + name)
except:
sendmsg("I couldn't find any music in your personal library.")
if text.lower().find(botnick.lower()) != -1 and text.lower().find("music") != -1 or text.lower().find(
"!music") != -1: # give music out of the music list
if "add" in text.lower():
sendmsg(
"The correct command to add music to the music list is '!add <music-url>'")
link = random.choice(array)
youtube = etree.HTML(urllib.urlopen(link).read())
video_title = " ".join(youtube.xpath(
"//span[@id='eow-title']/@title"))
sendmsg(video_title + " | " + link)
if text.lower().find("!youtube") != -1 or text.lower().find("!yt") != -1 or text.lower().find(
"!video") != -1 or text.lower().find("!y ") != -1: # searches youtube
try:
string = text[text.lower().rfind("!y"):]
search = string[string.find(" ") + 1:].rstrip("\n")
result_list = []
# search_list = text.split()[4:]
# search = " ".join(search_list)
search_query = urllib.urlencode({"search_query": search})
url = "http://www.youtube.com/results?" + search_query
response = urllib2.urlopen(url)
html = response.read()
soup = BeautifulSoup(html)
for vid in soup.findAll(attrs={'class': 'yt-uix-tile-link'}):
result_list.append(
"http://www.youtube.com" + vid['href'])
# prevents from returning a user instead of a video
if "/user/" in result_list[0]:
link = result_list[1]
else:
link = result_list[0]
youtube = etree.HTML(urllib.urlopen(link).read())
video_title = " ".join(youtube.xpath(
"//span[@id='eow-title']/@title"))
sendmsg(video_title.encode('utf-8', 'ignore') +
" | " + link.encode('utf-8', 'ignore'))
except:
sendmsg(error)
"""
if "!plist" in text: # send a list of all the music in the personal library
try:
name1 = text.split()[4]
if name1 not in pmusic:
sendmsg(
"This person doesn't has any music in their personal library.")
music_list = ", ".join(pmusic[name1])
sendpm(name, music_list)
except:
music_list = ", ".join(pmusic[name])
sendpm(name, music_list)
"""
def wikiwatch():
"""
Search wikipedia
"""
try:
if text.lower().find("!wiki") != -1 or text.lower().find("!wikipedia") != -1 or text.lower().find("!w ") != -1:
string = text[text.lower().rfind("!w"):]
search = string[string.find(" ") + 1:].rstrip("\n")
search_list = text.split()[4:]
# search = " ".join(search_list)
summary = wikipedia.summary(search, sentences=2)
if len(summary) > 430:
summary = summary[0:430] + "..."
# print(summary)
page = wikipedia.page(search)
title = page.title
url = page.url
sendmsg(title + " | " + url)
try:
sendmsg(summary.encode('utf-8', 'ignore'))
except:
sendmsg(
"fix this when you have the time, probably to do with non-unicode characters.")
except:
try:
search_list = text.split()[4:]
search = " ".join(search_list)
lookup = wikipedia.search(search, results=5)
# page = wikipedia.page(lookup[0])
# title = page.title
url = "https://en.wikipedia.org/wiki/" + \
lookup[0].replace(" ", "+")
sendmsg(
"I'm not sure what you mean, this is what I could find. | " + url)
except:
sendmsg(error)
def googlewatch():
"""
This function searches google or google images for a given query.
"""
try:
if text.lower().find("!google") != -1 or text.lower().find("!g ") != -1: # search google
string = text[text.lower().rfind("!g"):]
search = string[string.find(" ") + 1:].rstrip("\n")
search_list = text.split()[4:]
# search = " ".join(search_list)
search_query = urllib.urlencode({'q': search})
response = urllib2.urlopen(
"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&" + search_query).read()
data = json.loads(response)
results = data['responseData']['results']
result = results[0]
title = result['title'].replace(
"<b>", "").replace("</b>", "")
url = result['url']
if url.find("watch%") != -1: # circumvent google bot detection
url = url.replace("watch%3Fv%3D", "watch?v=")
sendmsg(title.encode('utf-8', 'ignore') + " | " + url)
except:
sendmsg(error)
if "!image" in text.lower() or "!img" in text.lower() or "!i " in text.lower():
# With help of:
# https://github.com/creeveshft/Web_Scraping/blob/master/Google%20Image%20Searcher/getimage.py
try:
url_list = []
answer_list = []
max_amount = "3"
k = 0
sentence = text.split()
string = text[text.lower().rfind("!i"):]
regex = re.compile('x\d+')
for word in sentence:
# if word[1].isdigit and word[0] == "x":
if re.search(regex, word) != None:
if len(word) > int(max_amount) + 1 or int(word[1]) > int(max_amount):
amount = 3
sendmsg(
"The maximum amount of allowed images is 3.")
else:
amount = int(word[1])
string = string.replace(word, "")
else:
amount = 1
search = string[string.find(" ") + 1:].rstrip("\n")
search_query = urllib.urlencode({'q': search})
browser = mechanize.Browser()
browser.set_handle_robots(False)
browser.addheaders = [('User-agent', 'Mozilla')]
html = browser.open(
"https://www.google.com/search?site=imghp&tbm=isch&source=hp&biw=1414&bih=709&" + search_query + "&o" + search_query)
# html = urllib.urlopen("https://www.google.com/search?site=imghp&tbm=isch&source=hp&biw=1414&bih=709&q="+search+"&oq="+search).read()
soup = BeautifulSoup(html)
# print(soup)
results = soup.findAll("a")
# print(results)
for i in results:
# print(i)
i = str(i)
# if "imgres?imgurl" in i:
# start = i.find("imgres?imgurl") + len("imgres?imgurl")
# end = i.find("jsaction",start)
# url_list.append(i[start:end])
if "src=" in i:
start = i.find("src=") + 5
end = i.find("width=") - 2
url_list.append(i[start:end])
# print(url_list)
while k < amount:
answer_list.append(url_list[k])
k += 1
answer = " | ".join(answer_list)
sendmsg("This is what I could find | " + answer)
except:
sendmsg(error)
"""
if "!image" in text or "!img" in text or "!i " in text:
keyBing = ""
credentialBing = 'Basic ' + \
(':%s' % keyBing).encode('base64')[:-1]
search_list = text.split()[4:]
search = " ".join(search_list)
searchString = urllib.urlencode({'Query': search})
# searchString = urllib.urlencode({"Query", search})
url = "https://api.datamarket.azure.com/Bing/Search/v1/Image?" + \
searchString + "%27&$top=5&$format=json"
request = urllib2.Request(url)
request.add_header('Authorization', credentialBing)
print(request)
request_opener = urllib2.build_opener()
response = request_opener.open(request)
response_data = response.read()
result = json.loads(response_data)
print(result)
"""
"""
try:
if text.find("!image") != -1 or text.find("!img") != -1: # search google images
search_list = text.split()[4:]
if "x2" in search_list:
search_list.remove("x2")
if "x3" in search_list:
search_list.remove("x3")
search = " ".join(search_list)
search_query = urllib.urlencode({'q': search})
response = urllib2.urlopen(