I was bored, so I experimented with coding style. IMHO, it's easier to read now.
[nameless-bot.git] / nameless-bot.py
blob9afd0fa292c770eecd05beb09b32949d9bb171a8
1 # Huge respects to Shinobiwan. He is a very helpful dude, and was always helpful when I needed him to be.
3 import socket
4 from die import roll
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
11 botnick = "botty1224"
12 botuser = "botty"
13 bottrigger = "_"
14 botchannel = "#botschool"
15 network = 'localhost'
16 port = 6664
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" ) )
24 def say(msg):
25 irc.send ( bytes ( 'PRIVMSG %s :%s%s'% ( botchannel, msg, '\r\n' ), "UTF-8" ) )
26 def nameless():
27 say (" /$$ ")
28 say (" | $$ ")
29 say (" /$$$$$$$ /$$$$$$ /$$$$$$/$$$$ /$$$$$$ | $$ /$$$$$$ /$$$$$$$ /$$$$$$$")
30 say ("| $$__ $$ |____ $$| $$_ $$_ $$ /$$__ $$| $$ /$$__ $$ /$$_____//$$_____/")
31 say ("| $$ \ $$ /$$$$$$$| $$ \ $$ \ $$| $$$$$$$$| $$| $$$$$$$$| $$$$$$| $$$$$$ ")
32 say ("| $$ | $$ /$$__ $$| $$ | $$ | $$| $$_____/| $$| $$_____/ \____ $$\____ $$")
33 say ("| $$ | $$| $$$$$$$| $$ | $$ | $$| $$$$$$$| $$| $$$$$$$ /$$$$$$$//$$$$$$$/")
34 say ("|__/ |__/ \_______/|__/ |__/ |__/ \_______/|__/ \_______/|_______/|_______/ ")
37 while True :
38 data = irc.recv ( 4096 ).decode ( "UTF-8" ).replace ( "\r\n", "" )
39 print ( strftime ( "%H:%M:%S" ) + ' === ' + data)
41 #THIS IF BLOCK DEALS WITH PINGS
42 if data.split()[ 0 ] == "PING" :
43 irc.send ( bytes ( 'PONG ' + data.split() [ 1 ] + '\r\n', "UTF-8" ) )
44 print ( "PING SENT" )
46 #PUT EVERYTHING THE BOT IS TO RESPOND TO (IN PUBLIC OR PRIVATE) WITHIN THIS BLOCK
47 if data.split()[1] == "PRIVMSG" :
48 #lets define some variables to make coding more easy and fun
49 #making a bot do stuff really depends a lot on your ability
50 #to parse the incoming data, interpret it, and respond.
51 #read about split() and join(), and slicing for more infos.
52 src_nick = data.split ( "!" )[ 0 ].replace ( ":", "" ) #nickname of person responsible for received text
53 src_username = data.split ( " " )[ 0 ].split ( "@" )[ 0 ].split ( "!" )[ 1 ] #username of person responsible for recvd text
54 src_hostname = data.split ( "@" )[ 1 ].split( " " )[ 0 ] #hostname of person responsible for recvd text (@irc2p)
55 msg = ":".join ( data.split ( ":" )[ 2: ]).replace ( "\r\n", "" ) #receieved text from someone
56 msg_dest = data.split ( " " )[ 2 ]#channel or person message was determined for
57 commandword = msg.split()[ 0 ]
60 #WE CAN SEPARATE OUR PUBLIC AND PRIVATE COMMANDS LIKE SO
61 #PUT PRIVATE COMMANDS YOU WANT THE BOT TO RESPOND TO IN THIS BLOCK
62 if msg_dest == botnick :
63 if msg == "hi":
64 irc.send ( bytes( 'PRIVMSG %s :hi there %s!\r\n'% ( src_nick, src_nick ), "UTF-8" ) )
67 #PUT PUBLIC COMMANDS YOU WANT THE BOT TO RESPOND TO IN THIS BLOCK
68 elif msg_dest == botchannel:
70 #HERE ARE EXAMPLE TEMPLATES OF HOW YOU WOULD CODE A RESPONSE TO SOMETHING SPECIFIC
72 if "botty who are you" in msg :
73 say ( 'I am botty, and I\'m you\'re worst nightmare')
75 #In this example, as long as anywhere in the message is said "botty is stupid" it will reply.
76 if "botty is stupid" in msg :
77 say ( '%s: NO YOU\'RE STUPID!'% ( src_nick ) )
79 #In this example, commandword is specifically looking at the first word in the message
80 if commandword == "%sping"% ( bottrigger ) :
81 say ( '%s: PONG!\r\n'% ( src_nick ) )
82 if "slaps botty" in msg :
83 say ( '%s: This is the Trout Protection Agency. Please put the Trout Down and walk away with your hands in the air.'% ( src_nick ) )
84 if commandword == "%sinfo"% ( bottrigger ) :
85 nameless()
86 import sys
87 say ( "Nameless-bot anti-copyright Schnaubelt 2012" )
88 say ( "Python version: " + sys.version.replace( "\n", "" ) )
89 if "cheese" in msg or "cheeze" in msg:
90 if msg == "botty, do you like cheese?" :
91 say ( 'I love it more than life itself!' )
92 else:
93 say ( 'WHERE!?!?!?!' )
94 #Prints nameless in 1337 ascii art.
95 if commandword == "%snameless"% ( bottrigger ) :
96 nameless()
97 if commandword == "%sroll"% ( bottrigger ) :
98 print( msg.split( ' ' ) )
99 if len( msg.split( ' ' ) ) == 2:
100 say ( src_nick + " rolled a dice with " + msg.split( ' ' )[ 1 ] + " sides and got a " + str( roll( int( msg.split( ' ' )[ 1 ] ) ) ) )
101 else:
102 say ( roll( 6 ) )
103 if commandword == "%ssay"% ( bottrigger ) :
104 say ( "This is a say test." )
106 #prints a colored irc 1-up mushroom.
107 if commandword == "%smushroom"% (bottrigger) :
108 say("\x030,1 ")
109 say("\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 \x030,4 \x030,1 \x030,3 \x030,1 ")
112 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
113 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
114 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,0 \x030,4 \x030,1 \x030,3 \x030,1 ")
115 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 ")
116 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 ")
117 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 ")
118 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 ")
119 say("\x030,1 \x030,3 \x030,1 \x030,4 \x030,1 \x030,3 \x030,1 ")
120 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
121 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,1 \x030,3 \x030,1 ")
122 say("\x030,1 \x030,3 \x030,1 \x030,14 \x030,15 \x030,0 \x030,15 \x030,14 \x030,1 \x030,3 \x030,1 ")
123 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,0 \x030,15 \x030,1 \x030,3 \x030,1 ")
124 say("\x030,1 \x030,3 \x030,1 \x030,15 \x030,1 \x030,3 \x030,1 ")
125 say("\x030,1 \x030,3 \x030,1 \x030,3 \x030,1 ")
126 say("\x030,1 \x030,3 \x030,1 ")
127 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")