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.

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