Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1.  
  2. import socket
  3. import re
  4.  
  5.  
  6. HOST = "irc.twitch.tv"
  7. PORT = 6667
  8. NICK = 'massivly'
  9. PASS = ""
  10. CHANNELS = ['#downgrade']
  11.  
  12. def send_message(chan, msg):
  13. global message_counter
  14. if message_counter % 2 == 0:
  15. message_counter += 1
  16. con.send(bytes('PRIVMSG %s :%s\r\n' % (chan, msg), 'UTF-8'))
  17. else:
  18. message_counter += 1
  19. msg = msg + ' 󠀀 '
  20. con.send(bytes('PRIVMSG %s :%s\r\n' % (chan, msg), 'UTF-8'))
  21.  
  22.  
  23. def send_pong(msg):
  24. con.send(bytes('PONG %s\r\n' % msg, 'UTF-8'))
  25.  
  26.  
  27. def get_sender(msg):
  28. result = ""
  29. for char in msg:
  30. if char == "!":
  31. break
  32. if char != ":":
  33. result += char
  34. return result
  35.  
  36.  
  37. def get_message(msg):
  38. result = ""
  39. i = 3
  40. length = len(msg)
  41. while i < length:
  42. result += msg[i] + " "
  43. i += 1
  44. result = result.replace(':', '', 1)
  45. return result
  46.  
  47.  
  48. con = socket.socket()
  49. con.connect((HOST, PORT))
  50. con.send(bytes('PASS %s\r\n' % PASS, 'UTF-8'))
  51. con.send(bytes('NICK %s\r\n' % NICK, 'UTF-8'))
  52. for channel in CHANNELS:
  53. con.send(bytes('JOIN %s\r\n' % channel, 'UTF-8'))
  54.  
  55.  
  56. data = ''
  57. while True:
  58. try:
  59. data = data+con.recv(1024).decode('UTF-8', 'ignore')
  60. data_split = re.split(r"[~\r\n]+", data)
  61. data = data_split.pop()
  62. for line in data_split:
  63. line = str.rstrip(line)
  64. line = str.split(line)
  65. if len(line) >= 1:
  66. if line[0] == 'PING':
  67. send_pong(line[1])
  68. if line[1] == 'PRIVMSG':
  69. sender = get_sender(line[0])
  70. message = get_message(line)
  71. CHAN = line[2]
  72. print(sender + ": " + message)
  73. except IndexError:
  74. print('loool')
  75. except socket.error:
  76. print("Socket died")
  77. except socket.timeout:
  78. print("Socket timeout")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement