Guest User

IRC Bot Problems

a guest
Jul 17th, 2014
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. import socket
  2.  
  3. irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  4. network = "irc.twitch.tv"
  5. port = 6667
  6. default_join = ["#channel_name"]
  7.  
  8. # twitch command functions
  9. def what_is_game():  # under construction
  10.     pass
  11.  
  12. def sellout(raw_message):
  13.     message = ('long joke here')
  14.     send_privmsg(raw_message.split('#')[1].split(" ")[0], message)
  15.  
  16. # general commands
  17. def send_privmsg(location, message):
  18.     irc.sendall("PRIVMSG #{0} :{1}\r\n".format(location, message))
  19.  
  20. def echo(raw_message):  # format = PRIVMSG target :message
  21.     words_to_echo = raw_message.split(":")[2].split(" ")[1:]
  22.     target = raw_message.split("#")[1].split(" ")[0]
  23.     message = ''
  24.     for word in words_to_echo:
  25.         if word[-4:] != '\r\n':
  26.             message += word + ' '
  27.         else:
  28.             message += word
  29.     irc.send("PRIVMSG #{0} :{1}\r\n".format(target, message))
  30.  
  31. if __name__ == "__main__":
  32.     irc.connect((network, port))
  33.     print irc.recv(4096)
  34.     irc.send("PASS {0}\r\n".format("oauth:codehere")) # format() was just to see if I was doing it wrong
  35.     irc.send("USER TwitchName 0 * :Owner - Failfixer\r\n")
  36.     irc.send("NICK TwitchName\r\n")
  37.     for channel in default_join:
  38.         irc.send("JOIN "+channel+"\r\n")
  39.     while True:
  40.         action = ''
  41.         data = irc.recv(4096)
  42.         print data #for debug
  43.  
  44.         if data.find('PING') != -1:
  45.             irc.send("PONG "+data.split()[1]+"\r\n")
  46.  
  47.         if data.find("#") != -1:
  48.             action = data.split("#")[0].split(" ")[1]
  49.  
  50.         if action != '':
  51.  
  52.             if action == 'PRIVMSG':
  53.                 command = ''
  54.                 if data.find(":!") != -1:
  55.                     command = data.split(":")[2].split(" ")[0].strip("\r\n")
  56.                     if command == "!say":
  57.                         echo(data)
  58.                     if command == "!sellout":
  59.                         sellout(data)
Advertisement
Add Comment
Please, Sign In to add comment