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.

154 lines
5.4 KiB

  1. # Message formatting in console
  2. class String
  3. def red; "\e[31m#{self}\e[0m" end
  4. def yellow; "\e[33m#{self}\e[0m" end
  5. def green; "\e[32m#{self}\e[0m" end
  6. def cyan; "\e[36m#{self}\e[0m" end
  7. def bold; "\e[1m#{self}\e[22m" end
  8. end
  9. # Requied packages / modules
  10. require 'socket'
  11. require 'logger'
  12. require 'open-uri'
  13. # Create logger
  14. File.delete("log.txt") # Clear previous log
  15. log = Logger.new("log.txt", formatter: proc {|severity, datetime, progname, msg|
  16. "#{datetime}: #{msg}\n"})
  17. # Required Info
  18. load "credentials.txt"
  19. log.info("Loading \"credentials.txt\"")
  20. # -------- IGNORE -------- #
  21. OAUTH.downcase!
  22. BOTNAME.downcase!
  23. CHANNEL.downcase!.gsub!("#", "")
  24. # -------- IGNORE -------- #
  25. # Save "Preparing to connect" to "log.txt"
  26. log.info("Preparing to connect")
  27. # Variables
  28. socket = TCPSocket.new('irc.chat.twitch.tv', 6667)
  29. send = "PRIVMSG ##{CHANNEL} :" # shortcut for sending messages
  30. running = true
  31. content = nil
  32. message_count = 0
  33. message_limit = Time.now.to_i
  34. # Commands
  35. commands = ["!about","!uptime","!commands","!cortexio","!followed"] # Add commands here
  36. api_commands = ["!followed","!uptime"]
  37. admin_commands = ["!disconnect"]
  38. # Authorization Login
  39. socket.puts("PASS #{OAUTH}") # Send the password(oauth) to Twitch
  40. socket.puts("NICK #{BOTNAME}") # Send the botname to Twitch
  41. socket.puts("JOIN ##{CHANNEL}") # Send the channel to Twitch
  42. # Save "Connected!" to "log.txt
  43. log.info("Joining #{CHANNEL.capitalize} as #{BOTNAME.capitalize} using OAUTH Token: #{OAUTH[6,OAUTH.length-12]}" + "*"*12)
  44. # This will add "Joining Somechannel as Bot using OAUTH Token: OAUTH:SomeTokenHereWithHid************" to log.txt
  45. Thread.abort_on_exception = true
  46. # Loop (Background Thread) for recieving Twitch chat data
  47. Thread.start do
  48. socket.puts(send + "Connected!") # Send "Connected!" to the Twitch channel
  49. puts "#{BOTNAME} Joined ##{CHANNEL}" # Connection Status
  50. puts "You should be fully connected now" # Connection Status
  51. puts ""
  52. puts "Type \"clear\" to clear terminal"
  53. puts ""
  54. while (running) do
  55. ready = IO.select([socket])
  56. ready[0].each do |s|
  57. line = s.gets
  58. match = line.match(/^:(.+)!(.+)PRIVMSG ##{CHANNEL} :(.+)$/)
  59. message = match && match[3]
  60. if message =~ /^/
  61. message = message.strip
  62. user = match[1] # Get username
  63. # Twitch message limit - (Max 100 messages in 30 secs - Applies to mods and above)
  64. # Avoid global ban
  65. if Time.now.to_i - message_limit > 30 # If more than 30 seconds has passed
  66. message_count = 0 # Reset "message_count"
  67. end
  68. if message_count == 0 # If "message_count" is 0
  69. message_limit = Time.now.to_i # Start counting to 30 again
  70. end
  71. message_count = message_count + 1
  72. end
  73. # // COMMANDS // COMMANDS // COMMANDS
  74. if message != nil
  75. msg = message.downcase
  76. if admin_commands.include?(msg) and user == CHANNEL # ADMIN COMMANDS -> user == CHANNEL (OWNER)
  77. if msg.include?("!disconnect")
  78. socket.puts(send + "Disconnecting") # Disconnect from the channel
  79. socket.puts("PART ##{CHANNEL}") # Disconnect from the channel
  80. Disconnect()
  81. log.info("[Command] #{user}: #{message}")
  82. end
  83. user = user.capitalize # Capitalize first letter (Cuz I'm that kind of person)
  84. elsif commands.include?(msg) and message_count < 80
  85. puts "[Command] ".bold.cyan + "#{user}: ".bold + "#{message}".bold.cyan
  86. if api_commands.include?(msg) # API commands
  87. if msg.include?("uptime")
  88. file = open("https://decapi.me/twitch/uptime?channel=#{CHANNEL}") # API for uptime
  89. content = "#{CHANNEL} has been live for: " + file.read
  90. elsif msg.include?("followed")
  91. file = open("https://decapi.me/twitch/followage/#{CHANNEL}/#{user}") # API for followed
  92. content = file.read
  93. if content == "Follow not found"
  94. content = "#{user} is not following #{CHANNEL}"
  95. else
  96. content = "#{user} has been following #{CHANNEL} for " + content
  97. end
  98. end
  99. puts "[Response] ".bold.red + "Cortexio: ".bold + "API: ".bold.yellow + "\"#{content}\"".bold.red
  100. else
  101. file = open "Responses/" + msg.gsub!("!","") + ".txt" # open matching file
  102. content = file.read
  103. file.close
  104. puts "[Response] ".bold.red + "Cortexio: ".bold + "File: ".bold.yellow + "\"#{msg}.txt\"".bold.red
  105. end
  106. file.close
  107. log.info("[Command] #{user}: #{message}")
  108. else
  109. puts "[Message] ".bold.green + "#{user}: ".bold + "#{message}".bold.green
  110. log.info("[Message] #{user}: #{message}")
  111. if message[0] == "!" # Unrecognized command
  112. content = "Unrecognized command: \"#{message}\" - Type !commands to see a list of available commands."
  113. end
  114. end
  115. # Response handling
  116. if content != nil
  117. content.gsub!("USER", "@#{user}") # Take a look at "ping.txt" to see example usage
  118. content.gsub!("CHANNEL", "#{CHANNEL}")
  119. if content.include? "COMMANDS"
  120. content.gsub!("COMMANDS", "#{commands}")
  121. content.gsub!("\"", "")
  122. content.gsub!("[","")
  123. content.gsub!("]","")
  124. end
  125. socket.puts(send + content) # Send response if any
  126. content = nil # Too avoid multiple messages with the same response
  127. end
  128. end
  129. end
  130. end
  131. end
  132. def Disconnect() # End script
  133. running = false
  134. exit
  135. end
  136. # Loop to keep bot going
  137. while (running) do
  138. gets.chomp
  139. end