Advertisement
Arcticwolfhowls

WIP

Aug 23rd, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #bot.py
  2. import cfg
  3. import socket
  4. import re
  5. import commands
  6.  
  7. def chat(sock, msg):
  8. """
  9. Send a chat message to the server.
  10. Keyword arguments:
  11. sock -- the socket over which to send the message
  12. msg -- the message to be sent
  13. """
  14. sock.send("PRIVMSG #{} :{}\r\n".format(cfg.CHAN, msg).encode("utf-8"))
  15.  
  16. def ban(sock, user):
  17. """
  18. Ban a user from the current channel.
  19. Keyword arguments:
  20. sock -- the socket over which to send the ban command
  21. user -- the user to be banned
  22. """
  23. chat(sock, ".ban {}".format(user))
  24.  
  25. def timeout(sock, user, secs=600):
  26. """
  27. Time out a user for a set period of time.
  28. Keyword arguments:
  29. sock -- the socket over which to send the timeout command
  30. user -- the user to be timed out
  31. secs -- the length of the timeout in seconds (default 600)
  32. """
  33. chat(sock, ".timeout {}".format(user, secs))
  34.  
  35. HOST = "irc.twitch.tv" # the Twitch IRC server
  36. PORT = 6667 # always use port 6667!
  37. NICK = "wolfmanager" # your Twitch username, lowercase
  38. PASS = "oauth:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # your Twitch OAuth token
  39. CHAN = "#arcticwolfhowls" # the channel you want to join
  40.  
  41. s = socket.socket()
  42. s.connect((HOST, PORT))
  43. s.send("PASS {}\r\n".format(PASS).encode("utf-8"))
  44. s.send("NICK {}\r\n".format(NICK).encode("utf-8"))
  45. s.send("JOIN {}\r\n".format(CHAN).encode("utf-8"))
  46.  
  47. # Make sure you prefix the quotes with an 'r'!
  48. CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
  49.  
  50. while True:
  51. response = s.recv(1024).decode("utf-8")
  52. if response == "PING :tmi.twitch.tv\r\n":
  53. s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
  54. else:
  55. username = re.search(r"\w+", response).group(0) # return the entire match
  56. message = CHAT_MSG.sub("", response)
  57. print(username + ": " + message)
  58.  
  59. #Commands
  60. clist = ["!add", ]
  61. if message.strip() == "!add":
  62. chat(s, "Syntax: !add !<command> <what the command does>")
  63. if message.strip().startswith("!add"):
  64. clist.append(message[5:])
  65. chat(s, "The command has been added!")
  66. if message.strip() == "!wolfmanager":
  67. chat(s, "I am a chat bot coded by Arcticwolfhowls! type: !commands for a list of commands. Check it every now and again because it changes constantly!")
  68. if message.strip() == "!commands":
  69. chat(s, clist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement