Advertisement
metalx1000

Basic IRC Bot

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