1 # Huge respects to Shinobiwan. He is a very helpful dude, and was always helpful when I needed him to be.
5 #now it's easier to modify the bots nickname and username,
6 #just change these strings and it will update in the rest
7 #of the code where %s substitution is used
12 botchannel
= "#botschool"
15 irc
= socket
.socket ( socket
.AF_INET
, socket
.SOCK_STREAM
)
16 irc
.connect ( ( network
, port
) )
17 irc
.send ( bytes('USER %s %s %s :Python IRC\r\n'%(botuser
, botuser
, botuser
), "UTF-8") )
18 irc
.send ( bytes('NICK %s\r\n'%(botnick), "UTF-8") )
19 irc
.send ( bytes('JOIN %s\r\n'%(botchannel), "UTF-8") )
20 irc
.send ( bytes('PRIVMSG %s :Hello World.\r\n'%(botchannel), "UTF-8") )
23 irc
.send(bytes('PRIVMSG %s :%s%s'% (botchannel
, msg
, '\r\n'), "UTF-8"))
26 data
= irc
.recv ( 4096 ).decode("UTF-8").replace("\r\n", "")
29 #THIS IF BLOCK DEALS WITH PINGS
30 if data
.split()[0] == "PING":
31 irc
.send ( bytes('PONG ' + data
.split() [ 1 ] + '\r\n', "UTF-8") )
34 #PUT EVERYTHING THE BOT IS TO RESPOND TO (IN PUBLIC OR PRIVATE) WITHIN THIS BLOCK
35 if data
.split()[1] == "PRIVMSG":
36 #lets define some variables to make coding more easy and fun
37 #making a bot do stuff really depends a lot on your ability
38 #to parse the incoming data, interpret it, and respond.
39 #read about split() and join(), and slicing for more infos.
40 src_nick
= data
.split("!")[0].replace (":", "") #nickname of person responsible for received text
41 src_username
= data
.split(" ")[0].split("@")[0].split("!")[1] #username of person responsible for recvd text
42 src_hostname
= data
.split("@")[1].split(" ")[0] #hostname of person responsible for recvd text (@irc2p)
43 msg
= ":".join(data
.split(":")[2:]).replace("\r\n", "") #receieved text from someone
44 msg_dest
= data
.split(" ")[2]#channel or person message was determined for
45 commandword
= msg
.split()[0]
48 #WE CAN SEPARATE OUR PUBLIC AND PRIVATE COMMANDS LIKE SO
49 #PUT PRIVATE COMMANDS YOU WANT THE BOT TO RESPOND TO IN THIS BLOCK
50 if msg_dest
== botnick
:
52 irc
.send ( bytes('PRIVMSG %s :hi there %s!\r\n'%(src_nick
, src_nick
), "UTF-8") )
55 #PUT PUBLIC COMMANDS YOU WANT THE BOT TO RESPOND TO IN THIS BLOCK
56 elif msg_dest
== botchannel
:
58 #HERE ARE EXAMPLE TEMPLATES OF HOW YOU WOULD CODE A RESPONSE TO SOMETHING SPECIFIC
60 if msg
== "botty who are you":
61 say ( 'I am botty, and I\'m you\'re worst nightmare')
63 #In this example, as long as anywhere in the message is said "botty is stupid" it will reply.
64 if "botty is stupid" in msg
:
65 say ( '%s: NO YOU\'RE STUPID!'%(src_nick))
67 #In this example, commandword is specifically looking at the first word in the message
68 if commandword
== "%sping"%(bottrigger):
69 say( '%s: PONG!\r\n'%(src_nick))
70 if "slaps botty" in msg
:
71 say( '%s: This is the Trout Protection Agency. Please put the Trout Down and walk away with your hands in the air.'% (src_nick
))
72 if "cheese" in msg
or "cheeze" in msg
:
73 if msg
== "botty, do you like cheese?":
74 say ('I love it more than life itself!')
77 #Prints nameless in 1337 ascii art.
78 if commandword
== "%snameless"%(bottrigger):
81 say (" /$$$$$$$ /$$$$$$ /$$$$$$/$$$$ /$$$$$$ | $$ /$$$$$$ /$$$$$$$ /$$$$$$$")
82 say ("| $$__ $$ |____ $$| $$_ $$_ $$ /$$__ $$| $$ /$$__ $$ /$$_____//$$_____/")
83 say ("| $$ \ $$ /$$$$$$$| $$ \ $$ \ $$| $$$$$$$$| $$| $$$$$$$$| $$$$$$| $$$$$$ ")
84 say ("| $$ | $$ /$$__ $$| $$ | $$ | $$| $$_____/| $$| $$_____/ \____ $$\____ $$")
85 say ("| $$ | $$| $$$$$$$| $$ | $$ | $$| $$$$$$$| $$| $$$$$$$ /$$$$$$$//$$$$$$$/")
86 say ("|__/ |__/ \_______/|__/ |__/ |__/ \_______/|__/ \_______/|_______/|_______/ ")
88 if commandword
== "%ssay"%(bottrigger):
89 say("This is a say test.")
91 #prints a colored irc 1-up mushroom.
92 if commandword
== "%smushroom"%(bottrigger):
94 say("\x030,1 \x030,3 \x030,1 ")
95 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
96 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
97 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
98 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
99 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
100 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
101 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
102 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
103 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
104 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
105 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
106 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,1 \x030,3 \x030,1 ")
107 say("\x030,1 \x030,3 \x030,1 \x030,14 \x030,15 \x030,0 \x030,15 \x030,14 \x030,1 \x030,3 \x030,1 ")
108 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,0 \x030,15 \x030,1 \x030,3 \x030,1 ")
109 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,1 \x030,3 \x030,1 ")
110 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
111 say("\x030,1 \x030,3 \x030,1 ")