1 # Huge respects to Shinobiwan. He is a very helpful dude, and was always helpful when I needed him to be.
5 from time
import strftime
7 #now it's easier to modify the bots nickname and username,
8 #just change these strings and it will update in the rest
9 #of the code where %s substitution is used
14 botchannel
= "#botschool"
17 irc
= socket
.socket ( socket
.AF_INET
, socket
.SOCK_STREAM
)
18 irc
.connect ( ( network
, port
) )
19 irc
.send ( bytes ( 'USER %s %s %s :Python IRC\r\n'% ( botuser
, botuser
, botuser
), "UTF-8" ) )
20 irc
.send ( bytes ( 'NICK %s\r\n'% (botnick
), "UTF-8" ) )
21 irc
.send ( bytes ( 'JOIN %s\r\n'% ( botchannel
), "UTF-8" ) )
22 irc
.send ( bytes ( 'PRIVMSG %s :Hello World.\r\n'% ( botchannel
), "UTF-8" ) )
27 irc
.send ( bytes ( 'PRIVMSG %s :%s%s'% ( botchannel
, msg
, '\r\n' ), "UTF-8" ) )
31 say (" /$$$$$$$ /$$$$$$ /$$$$$$/$$$$ /$$$$$$ | $$ /$$$$$$ /$$$$$$$ /$$$$$$$")
32 say ("| $$__ $$ |____ $$| $$_ $$_ $$ /$$__ $$| $$ /$$__ $$ /$$_____//$$_____/")
33 say ("| $$ \ $$ /$$$$$$$| $$ \ $$ \ $$| $$$$$$$$| $$| $$$$$$$$| $$$$$$| $$$$$$ ")
34 say ("| $$ | $$ /$$__ $$| $$ | $$ | $$| $$_____/| $$| $$_____/ \____ $$\____ $$")
35 say ("| $$ | $$| $$$$$$$| $$ | $$ | $$| $$$$$$$| $$| $$$$$$$ /$$$$$$$//$$$$$$$/")
36 say ("|__/ |__/ \_______/|__/ |__/ |__/ \_______/|__/ \_______/|_______/|_______/ ")
41 data
= irc
.recv ( 4096 ).decode ( "UTF-8" ).replace ( "\r\n", "" )
42 except UnicodeDecodeError:
43 say("This bot encountered a unicode decoding error.")
44 print ( strftime ( "%H:%M:%S" ) + ' === ' + data
)
45 if data
.split ( ' ' )[1] == 'NICK':
46 botnick
= data
.split ( ' ' )[ 2 ].split ( ':' )[ 0 ]
47 #THIS IF BLOCK DEALS WITH PINGS
48 if data
.split()[ 0 ] == "PING" :
49 irc
.send ( bytes ( 'PONG ' + data
.split() [ 1 ] + '\r\n', "UTF-8" ) )
52 #PUT EVERYTHING THE BOT IS TO RESPOND TO (IN PUBLIC OR PRIVATE) WITHIN THIS BLOCK
53 if data
.split()[1] == "PRIVMSG" :
54 #lets define some variables to make coding more easy and fun
55 #making a bot do stuff really depends a lot on your ability
56 #to parse the incoming data, interpret it, and respond.
57 #read about split() and join(), and slicing for more infos.
59 src_nick
= data
.split ( "!" )[ 0 ].replace ( ":", "" ) #nickname of person responsible for received text
60 src_username
= data
.split ( " " )[ 0 ].split ( "@" )[ 0 ].split ( "!" )[ 1 ] #username of person responsible for recvd text
61 src_hostname
= data
.split ( "@" )[ 1 ].split( " " )[ 0 ] #hostname of person responsible for recvd text (@irc2p)
62 msg
= ":".join ( data
.split ( ":" )[ 2: ]).replace ( "\r\n", "" ) #receieved text from someone
63 msg_dest
= data
.split ( " " )[ 2 ].split('!')[0]#channel or person message was determined for
64 if len(msg
.split()) != 0:
65 commandword
= msg
.split()[ 0 ]
66 else: commandword
= "nothing"
69 #WE CAN SEPARATE OUR PUBLIC AND PRIVATE COMMANDS LIKE SO
70 #PUT PRIVATE COMMANDS YOU WANT THE BOT TO RESPOND TO IN THIS BLOCK
71 print(msg_dest
+ ':' + botnick
)
72 if msg_dest
== botnick
:
74 irc
.send ( bytes( 'PRIVMSG %s :hi there %s!\r\n'% ( src_nick
, src_nick
), "UTF-8" ) )
75 else: print("Something went wrong.")
76 if msg
.split ( ' ' )[ 0 ] == 'slap' and len ( msg
.split( ' ' ) ) == 2:
77 irc
.send ( bytes( "PRIVMSG %s :\x01Action slaps %s!\x01\r\n"% ( botchannel
, msg
.split ( ' ' )[ 1 ] ), "UTF-8") )
78 if msg
.split ( ' ' )[ 0 ] == 'say' and len ( msg
.split( ' ' ) ) >= 2:
79 irc
.send ( bytes( 'PRIVMSG %s :%s\r\n'% ( botchannel
, msg
.strip('say ')), "UTF-8") )
83 #PUT PUBLIC COMMANDS YOU WANT THE BOT TO RESPOND TO IN THIS BLOCK
84 elif msg_dest
== botchannel
:
86 #HERE ARE EXAMPLE TEMPLATES OF HOW YOU WOULD CODE A RESPONSE TO SOMETHING SPECIFIC
88 if "botty who are you" in msg
:
89 say ( 'I am botty, and I\'m you\'re worst nightmare')
91 #In this example, as long as anywhere in the message is said "botty is stupid" it will reply.
92 if "botty is stupid" in msg
:
93 say ( '%s: NO YOU\'RE STUPID!'% ( src_nick
) )
95 #In this example, commandword is specifically looking at the first word in the message
96 if commandword
== "%sping"% ( bottrigger
) :
97 say ( '%s: PONG!\r\n'% ( src_nick
) )
98 if "slaps botty" in msg
:
99 say ( '%s: This is the Trout Protection Agency. Please put the Trout Down and walk away with your hands in the air.'% ( src_nick
) )
100 if commandword
== "%sinfo"% ( bottrigger
) :
103 say ( "Nameless-bot anti-copyright Schnaubelt 2012" )
104 say ( "Python version: " + sys
.version
.replace( "\n", "" ) )
105 if "cheese" in msg
or "cheeze" in msg
:
106 if msg
== "botty, do you like cheese?" :
107 say ( 'I love it more than life itself!' )
109 say ( 'WHERE!?!?!?!' )
110 #Prints nameless in 1337 ascii art.
111 if commandword
== "%snameless"% ( bottrigger
) :
113 if commandword
== "%sroll"% ( bottrigger
) :
114 print( msg
.split ( ' ' ) )
115 if len( msg
.split ( ' ' ) ) == 2:
117 say ( src_nick
+ " rolled a dice with " + msg
.split( ' ' )[ 1 ] + " sides and got a " + str( roll( int( msg
.split( ' ' )[ 1 ] ) ) ) )
119 say ( src_nick
+ " did not enter a valid number, thus no dice was rolled.")
121 say ( src_nick
+ " rolled a dice with 6 sides and got a " + str( roll(6 ) ) )
122 if commandword
== "%ssay"% ( bottrigger
) :
123 say ( "This is a say test." )
124 if commandword
== "%sprint"% ( bottrigger
) :
125 if len ( msg
.split( ' ' ) ) == 2:
127 say ( vars()[ msg
.split ( ' ' )[ 1 ] ] )
129 say ( msg
.split ( ' ' )[ 1 ] + " is not a valid variable.")
130 else: say ( "Please provide a variable." )
132 if commandword
== "%sdoc"% ( bottrigger
) :
133 if len( msg
.split ( ' ' ) ) == 2 :
134 arg
= msg
.split ( ' ' )[ 1 ]
136 say ( bottrigger
+ 'roll simulates the roll of a die. Without parameters, it rolls a six sided die. Give it a number, and it will roll with a die with that many sides')
137 elif arg
== 'nameless' : say ( "Prints NAMELESS in beautiful ascii art." )
138 elif arg
== 'info' : say ( "Displays nameless ascii art along with anti-copyright and system information.")
139 elif arg
== 'mushroom' : say ( "Prints an ascii-art one-up mushroom." )
140 elif arg
== 'ping' : say ( "Use this, and your ping will be ponged. Often used to test lag." )
141 else : say ("I'm sorry. I have no documentation for " + arg
+ ".")
142 else : say(bottrigger
+ 'doc is a basic documentation system for this bot\'s commands. To use it, simply type "' + bottrigger
+ 'doc <command>"')
144 #prints a colored irc 1-up mushroom.
145 if commandword
== "%smushroom"% (bottrigger
) :
147 say("\x030,1 \x030,3 \x030,1 ")
148 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
149 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
150 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
151 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
152 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
153 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 ")
154 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 ")
155 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 ")
156 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 ")
157 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
158 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
159 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,1 \x030,3 \x030,1 ")
160 say("\x030,1 \x030,3 \x030,1 \x030,14 \x030,15 \x030,0 \x030,15 \x030,14 \x030,1 \x030,3 \x030,1 ")
161 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,0 \x030,15 \x030,1 \x030,3 \x030,1 ")
162 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,1 \x030,3 \x030,1 ")
163 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
164 say("\x030,1 \x030,3 \x030,1 ")
165 say("\x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1 \x030,1")