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.

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