A Twitch Bot, in Ruby
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.0 KiB

  1. require 'open-uri'
  2. def HandleCommands(line)
  3. timestamp = Time.now.to_i
  4. message_count = 0
  5. max_messages = 80 # Make sure the bot is a moderator in your channel
  6. # moderators = 100 messages (within a 30 second window)
  7. # default = 20 messages (within a 30 second window)
  8. # max_messages is set to 80 just to keep it safe
  9. user = ""
  10. prefix = "!"
  11. admin_commands = {
  12. #"example" => "raw socket message"
  13. }
  14. commands = [
  15. "commands",
  16. "chatbot",]
  17. replacements = [
  18. ["USER", "@#{user}"],
  19. ["CHANNEL", "#{CHANNEL}"],
  20. ["COMMANDS", "#{commands}"],
  21. ["\"", ""], ["[", ""], ["]", ""]]
  22. if line.index("PING") == 0
  23. response = Ping(line)
  24. else
  25. match = line.match(/^:(.+)!(.+)PRIVMSG ##{CHANNEL} :(.+)$/)
  26. message = match && match[3]
  27. if message =~ /^/
  28. message.strip!
  29. message.downcase!
  30. user = match[1] # Get username
  31. if message
  32. if message.index("!") == 0
  33. message = "#{message.partition(" ").first}" # Get first word
  34. # ----- API COMMANDS ----- #
  35. if message.include?(prefix + "uptime")
  36. response, log_msg = Uptime(user)
  37. elsif message.include?(prefix + "followed")
  38. response, log_msg = Followed(user)
  39. end
  40. # ----- ADMIN COMMANDS ----- #
  41. if user == CHANNEL
  42. admin_commands.each do |command, value|
  43. if message.include?(prefix + command)
  44. return value + "\r\n"
  45. end
  46. end
  47. end
  48. # ----- COMMANDS ----- #
  49. commands.each do |command|
  50. if message.include?(prefix + command)
  51. file = open("Responses/" + command + ".txt")
  52. response = file.read
  53. file.close
  54. response = "PRIVMSG ##{CHANNEL} :" + response
  55. break
  56. end
  57. end
  58. # ----- GLOBAL BAN PREVENTION ----- #
  59. if Time.now.to_i - timestamp > 30 # If more than 30 seconds has passed
  60. message_count = 0 # Reset "message_count"
  61. end
  62. if message_count == 0 # If "message_count" is 0
  63. timestamp = Time.now.to_i # Start counting to 30 again
  64. end
  65. message_count = message_count + 1
  66. # ----- GLOBAL BAN PREVENTION ----- #
  67. if response and message_count < max_messages
  68. replacements.each {|replacement| response.gsub!(replacement[0], replacement[1])}
  69. puts("[Command] ".bold.cyan + "#{user}: ".bold + "#{message}".bold.cyan)
  70. log_msg = "[Command] #{user}: " + message
  71. return response, log_msg
  72. else
  73. puts("[Command] ".bold.red + "#{user}: ".bold + "#{message}".bold.red)
  74. end
  75. else
  76. puts("[Message] ".bold.green + "#{user}: ".bold + "#{message}".bold.green)
  77. log_msg = "[Message] #{user}: " + message
  78. return nil, log_msg
  79. end
  80. end
  81. end
  82. end
  83. end
  84. # ----- TWITCH IRC CONFIRMATION MESSAGE ----- #
  85. def Ping(line)
  86. line.strip!
  87. puts("-".bold.red*line.length)
  88. puts "[Twitch] ".bold.cyan + "IRC: ".bold.yellow + line.bold.green
  89. puts("-".bold.red*line.length)
  90. response = ("PONG :tmi.twitch.tv\r\n")
  91. log_msg = "[IRC Message]: PING :tmi.twitch.tv"
  92. return response, log_msg
  93. end
  94. # ----- API COMMANDS ----- #
  95. def Uptime(user)
  96. file = open("https://decapi.me/twitch/uptime?channel=#{CHANNEL}")
  97. response = "#{CHANNEL} has been streaming for: " + file.read
  98. response = "PRIVMSG ##{CHANNEL} :" + response
  99. log_msg = "[Command] #{user}: !uptime"
  100. return response, log_msg
  101. end
  102. def Followed(user)
  103. if user != CHANNEL
  104. file = open("https://decapi.me/twitch/followage/#{CHANNEL}/#{user}")
  105. response = file.read
  106. if response == "Follow not found"
  107. response = "#{user} is not following #{CHANNEL}"
  108. else
  109. response = "#{user} has been following #{CHANNEL} for " + response
  110. end
  111. response = "PRIVMSG ##{CHANNEL} :" + response
  112. end
  113. log_msg = "[Command] #{user}: !followed"
  114. return response, log_msg
  115. end
  116. # ----- CONSOLE COMMANDS ----- #
  117. def Clear()
  118. system "clear" or system "cls"
  119. puts "Type \"clear\" to clear terminal"
  120. puts "Type \"project <text>\" to write to project file"
  121. puts "Type \"disconnect\" to disconnect"
  122. puts ""
  123. end