Advertisement
Guest User

Untitled

a guest
Oct 5th, 2017
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.64 KB | None | 0 0
  1. ####################################################
  2. # IRC bot by Amichay P. K. - am1chay.p.k@gmail.com #
  3. ####################################################
  4.  
  5. ##################################################
  6. # Some basic variables used to configure the bot #
  7. ##################################################
  8.  
  9. server = "irc.nix.co.il" # Server
  10. port = 6667 # Port
  11. channel = "#dc9723" # Channel
  12. botnick = "dc9723_bot" # Your bots nick
  13. password = "************" # Bots password
  14. admin = "Amichay" # Bots admin
  15. cmdchar = "~" # Commands begin with this
  16. printservmsg = 0 # Do you want to print the server messages? 0/1 = false/true
  17. quiet = 0 # Should the bot speak? 0/1 = false/true
  18. win_pass = "123456" # Secret password to win
  19.  
  20.  
  21. # Import some necessary libraries.
  22. import socket
  23. # Set up our commands function
  24. def commands(nick,channel,message):
  25.     if message.find(botnick+": who")!=-1:
  26.         ircsock.send("PRIVMSG %s :%s: I am amichay\"s bot. I hang around on this IRC server in #dc9723 and outher channels.\r\n" % (channel,nick))
  27.     elif message.find(botnick+": list")!=-1:
  28.         ircsock.send("PRIVMSG %s :%s: My current commands are: help, list, who, hi, wtf?!. My command character is ~.\r\n" % (channel,nick))
  29.  
  30. def ping(): # This function will respond to server Pings.
  31.     ircsock.send("PONG :pingis\n")  
  32.  
  33. def sendmsg(chan , msg): # This is the send message function.
  34.    
  35.     if quiet == 0:
  36.         ircsock.send("PRIVMSG "+ channel +" :"+ msg +"\n")
  37.     elif quiet == 1:
  38.         ircsock.send("PRIVMSG "+ channel +" :"+ "I was a bad bot. i shouldn't speak.\n")
  39. #   ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")
  40.  
  41. def msg(msg): # This is anouther send message function.
  42.     if quiet == 0:
  43.         ircsock.send("PRIVMSG "+ channel +" :"+ msg +"\n")
  44.     elif quiet == 1:
  45.         ircsock.send("PRIVMSG "+ channel +" :"+ "I was a bad bot. i shouldn't speak.\n")
  46.  
  47. def joinchan(chan): # This function is used to join channels.
  48.     ircsock.send("JOIN "+ chan +"\n")
  49.  
  50. def win():
  51.     ircsock.send("PRIVMSG "+ channel +" :You win!\n")
  52.  
  53. #Connect to the server
  54.  
  55. ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  56. ircsock.connect((server, port)) # Here we connect to the server using the port, mostly port 6667.
  57.  
  58. print ("\nConnected to server at " + str(server) + ", using port " + str(port) + "\n")
  59.  
  60. ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :dc9723 IRC bot.\n") # user authentication
  61. ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot
  62.  
  63. print ("User: " + str(botnick) + "\nNick: " + str(botnick) + "\n")
  64.  
  65. ircmsg = ircsock.recv(2048) # receive data from the server
  66. ircmsg = ircmsg.strip("\n\r") # removing any unnecessary linebreaks.
  67. if ircmsg.find("PING :") != -1:
  68.    ircsock.send("PONG %s\n" % ircmsg.split(":")[1])
  69.  
  70. joinchan(channel) # Join the channel using the functions we previously defined
  71.  
  72. print ("Channel: " + str(channel)+ "\n")
  73.  
  74. ircsock.send("NICKSERV IDENTIFY "+ password +"\n") # Identify using password
  75.  
  76. print ("\n* * * The bot is working in the channel. * * *\n")
  77.  
  78. ircsock.send("PRIVMSG %s :%s: dc9723 bot ready at your service sir!\r\n" % (channel,admin))
  79.  
  80. #Recive commands
  81.  
  82. while 1: # Be careful with these! it might send you to an infinite loop
  83.     ircmsg = ircsock.recv(2048) # receive data from the server
  84.     ircmsg = ircmsg.strip("\n\r") # removing any unnecessary linebreaks.
  85.     if printservmsg == 1:
  86.         print(ircmsg) # Here we print what"s coming from the server
  87.     if ircmsg.find(" PRIVMSG ")!=-1:
  88.         nick=ircmsg.split("!")[0][1:]
  89.         channel=ircmsg.split(" PRIVMSG ")[-1].split(" :")[0]
  90.         commands(nick,channel,ircmsg)
  91.  
  92. #################
  93. # Commadnd list #
  94. #################
  95.  
  96.     if ircmsg.find(":"+botnick+": "+win_pass) != -1:
  97.         win()
  98.    
  99.     if ircmsg.find(":hi "+botnick) != -1:
  100.         msg("hi!")
  101.    
  102.     if ircmsg.find("who am i?") != -1:
  103.         msg("No one. Go away.")
  104.    
  105.     if ircmsg.find(":"+cmdchar+"bad") != -1:
  106.         if nick == admin:
  107.             msg("sorry.")
  108.             quiet = 1
  109.         else:
  110.             msg("Fuck you! you're not my boss!")
  111.        
  112.     if ircmsg.find(":"+cmdchar+"good") != -1:
  113.         if nick == admin:
  114.             quiet = 0
  115.             msg("ok.")
  116.         else:
  117.             ircsock.send("PRIVMSG %s :Fuck you! you're not my boss!\r\n" % (channel))
  118.    
  119.     if ircmsg.find("42") != -1:
  120.         msg("The answer to life the universe and everything.")
  121.        
  122.     if ircmsg.find("the answer to life the universe and everything?") != -1:
  123.         msg("42")
  124.    
  125.     if ircmsg.find(":"+cmdchar+"WTF?!") != -1:
  126.         msg("Fuck them all!")
  127.    
  128.     if ircmsg.find(":"+cmdchar+"help") != -1:
  129.         ircsock.send("PRIVMSG %s :%s: My current commands are: help, list, who, hi, wtf?!. My command character is ~.\r\n" % (channel,nick))
  130.    
  131.    
  132.     if ircmsg.find("PING :") != -1: # if the server pings us then we"ve got to respond!
  133.         ping()
  134.        
  135. # EOF #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement