dauzcode

python-bot

Aug 23rd, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. # Import some necessary libraries.
  2. import socket
  3.  
  4. # Some basic variables used to configure the bot        
  5. server = "irc.freenode.net" # Server
  6. channel = "#yourchannel" # Channel
  7. botnick = "botname" # Your bots nick
  8.  
  9.  
  10. def ping(): # This is our first function! It will respond to server Pings.
  11.   ircsock.send("PONG :pingis\n")  
  12.  
  13. #def sendmsg(chan , msg): # This is the send message function, it simply sends messages to the channel.
  14. #  ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")
  15.  
  16. def joinchan(chan): # This function is used to join channels.
  17.   ircsock.send("JOIN "+ chan +"\n")
  18.  
  19. def hello(): # This function responds to a user that inputs "Hello Mybot"
  20.   ircsock.send("PRIVMSG "+ channel +" :Hello!\n")
  21.                  
  22. ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  23. ircsock.connect((server, 6667)) # Here we connect to the server using the port 6667
  24. ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This bot is a result of a tutoral covered on http://shellium.org/wiki.\n") # user authentication
  25. ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot
  26.  
  27. joinchan(channel) # Join the channel using the functions we previously defined
  28.  
  29. while 1: # Be careful with these! it might send you to an infinite loop
  30.   ircmsg = ircsock.recv(2048) # receive data from the server
  31.   ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
  32.   print(ircmsg) # Here we print what's coming from the server
  33.  
  34.   if ircmsg.find(":Hello "+ botnick) != -1: # If we can find "Hello Mybot" it will call the function hello()
  35.     hello()
  36.  
  37.   if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
  38.     ping()
Advertisement
Add Comment
Please, Sign In to add comment