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 commandword
= msg
.split()[ 0 ]
67 #WE CAN SEPARATE OUR PUBLIC AND PRIVATE COMMANDS LIKE SO
68 #PUT PRIVATE COMMANDS YOU WANT THE BOT TO RESPOND TO IN THIS BLOCK
69 print(msg_dest
+ ':' + botnick
)
70 if msg_dest
== botnick
:
72 irc
.send ( bytes( 'PRIVMSG %s :hi there %s!\r\n'% ( src_nick
, src_nick
), "UTF-8" ) )
73 else: print("Something went wrong.")
74 if msg
.split ( ' ' )[ 0 ] == 'slap' and len ( msg
.split( ' ' ) ) == 2:
75 irc
.send ( bytes( "PRIVMSG %s :\x01Action slaps %s!\x01\r\n"% ( botchannel
, msg
.split ( ' ' )[ 1 ] ), "UTF-8") )
76 if msg
.split ( ' ' )[ 0 ] == 'say' and len ( msg
.split( ' ' ) ) >= 2:
77 irc
.send ( bytes( 'PRIVMSG %s :%s\r\n'% ( botchannel
, msg
.strip('say ')), "UTF-8") )
81 #PUT PUBLIC COMMANDS YOU WANT THE BOT TO RESPOND TO IN THIS BLOCK
82 elif msg_dest
== botchannel
:
84 #HERE ARE EXAMPLE TEMPLATES OF HOW YOU WOULD CODE A RESPONSE TO SOMETHING SPECIFIC
86 if "botty who are you" in msg
:
87 say ( 'I am botty, and I\'m you\'re worst nightmare')
89 #In this example, as long as anywhere in the message is said "botty is stupid" it will reply.
90 if "botty is stupid" in msg
:
91 say ( '%s: NO YOU\'RE STUPID!'% ( src_nick
) )
93 #In this example, commandword is specifically looking at the first word in the message
94 if commandword
== "%sping"% ( bottrigger
) :
95 say ( '%s: PONG!\r\n'% ( src_nick
) )
96 if "slaps botty" in msg
:
97 say ( '%s: This is the Trout Protection Agency. Please put the Trout Down and walk away with your hands in the air.'% ( src_nick
) )
98 if commandword
== "%sinfo"% ( bottrigger
) :
101 say ( "Nameless-bot anti-copyright Schnaubelt 2012" )
102 say ( "Python version: " + sys
.version
.replace( "\n", "" ) )
103 if "cheese" in msg
or "cheeze" in msg
:
104 if msg
== "botty, do you like cheese?" :
105 say ( 'I love it more than life itself!' )
107 say ( 'WHERE!?!?!?!' )
108 #Prints nameless in 1337 ascii art.
109 if commandword
== "%snameless"% ( bottrigger
) :
111 if commandword
== "%sroll"% ( bottrigger
) :
112 print( msg
.split ( ' ' ) )
113 if len( msg
.split ( ' ' ) ) == 2:
115 say ( src_nick
+ " rolled a dice with " + msg
.split( ' ' )[ 1 ] + " sides and got a " + str( roll( int( msg
.split( ' ' )[ 1 ] ) ) ) )
117 say ( src_nick
+ " did not enter a valid number, thus no dice was rolled.")
119 say ( src_nick
+ " rolled a dice with 6 sides and got a " + str( roll(6 ) ) )
120 if commandword
== "%ssay"% ( bottrigger
) :
121 say ( "This is a say test." )
122 if commandword
== "%sprint"% ( bottrigger
) :
123 if len ( msg
.split( ' ' ) ) == 2:
125 say ( vars()[ msg
.split ( ' ' )[ 1 ] ] )
127 say ( msg
.split ( ' ' )[ 1 ] + " is not a valid variable.")
128 else: say ( "Please provide a variable." )
130 if commandword
== "%sdoc"% ( bottrigger
) :
131 if len( msg
.split ( ' ' ) ) == 2 :
132 arg
= msg
.split ( ' ' )[ 1 ]
134 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')
135 elif arg
== 'nameless' : say ( "Prints NAMELESS in beautiful ascii art." )
136 elif arg
== 'info' : say ( "Displays nameless ascii art along with anti-copyright and system information.")
137 elif arg
== 'mushroom' : say ( "Prints an ascii-art one-up mushroom." )
138 elif arg
== 'ping' : say ( "Use this, and your ping will be ponged. Often used to test lag." )
139 else : say ("I'm sorry. I have no documentation for " + arg
+ ".")
140 else : say(bottrigger
+ 'doc is a basic documentation system for this bot\'s commands. To use it, simply type "' + bottrigger
+ 'doc <command>"')
142 #prints a colored irc 1-up mushroom.
143 if commandword
== "%smushroom"% (bottrigger
) :
145 say("\x030,1 \x030,3 \x030,1 ")
146 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
147 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
148 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
149 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
150 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
151 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 ")
152 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 ")
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,1 \x030,3 \x030,1 ")
155 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
156 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
157 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,1 \x030,3 \x030,1 ")
158 say("\x030,1 \x030,3 \x030,1 \x030,14 \x030,15 \x030,0 \x030,15 \x030,14 \x030,1 \x030,3 \x030,1 ")
159 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,0 \x030,15 \x030,1 \x030,3 \x030,1 ")
160 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,1 \x030,3 \x030,1 ")
161 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
162 say("\x030,1 \x030,3 \x030,1 ")
163 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")