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.

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