Advertisement
metalx1000

Basic IRC Bot in Python

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