Advertisement
metalx1000

Python IRC Bot

May 4th, 2015
1,946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 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 = "#filmsbykris" # Channel
  11. botnick = "BOTpy" # 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. def info():
  27.   ircsock.send("PRIVMSG "+ channel +" :I am a bot made by Metalx1000! I am Version "+ str(version) +"\n")
  28.  
  29. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  30. s.connect((server, port)) # Here we connect to the server using the port 6667
  31. ircsock = ssl.wrap_socket(s)
  32. ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This bot is a result of a tutoral covered on http://shellium.org/wiki.\n") # user authentication
  33. ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot
  34.  
  35. joinchan(channel) # Join the channel using the functions we previously defined
  36.  
  37. while 1: # Be careful with these! it might send you to an infinite loop
  38.   ircmsg = ircsock.recv(2048) # receive data from the server
  39.   ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
  40.   print(ircmsg) # Here we print what's coming from the server
  41.  
  42.  
  43.   if ircmsg.find(":Hello "+ botnick) != -1: # If we can find "Hello Mybot" it will call the function hello()
  44.     hello()
  45.  
  46.   if ircmsg.find("#botinfo") != -1: # If we can find "Hello Mybot" it will call the function hello()
  47.     info()
  48.  
  49.   if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
  50.     ping()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement