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.

100 lines
2.6 KiB

  1. # This is a simple test to see if the commit worked.
  2. # Message formatting in console
  3. class String
  4. def red; "\e[31m#{self}\e[0m" end
  5. def yellow; "\e[33m#{self}\e[0m" end
  6. def green; "\e[32m#{self}\e[0m" end
  7. def cyan; "\e[36m#{self}\e[0m" end
  8. def bold; "\e[1m#{self}\e[22m" end
  9. end
  10. # Requied packages / modules
  11. require('socket')
  12. require('logger')
  13. # Create logger
  14. if File.file?("log.txt")
  15. File.delete("log.txt") # Clear previous log
  16. end
  17. log = Logger.new("log.txt", formatter: proc {|severity, datetime, progname, msg|
  18. "#{datetime}: #{msg}\n"})
  19. # Required Info
  20. load "credentials.txt"
  21. log.info("Loading \"credentials.txt\"")
  22. require_relative('CommandsHandler') # File for handling commands
  23. log.info("Loading \"CommandsHandler.rb\"")
  24. # -------- IGNORE -------- #
  25. OAUTH.downcase!
  26. BOTNAME.downcase!
  27. CHANNEL.downcase!.gsub!("#", "")
  28. # Save "Preparing to connect" to "log.txt"
  29. log.info("Preparing to connect")
  30. # Variables
  31. socket = TCPSocket.new('irc.chat.twitch.tv', 6667)
  32. running = true
  33. # Authorization Login
  34. socket.puts("PASS #{OAUTH}") # Send the password(oauth) to Twitch
  35. socket.puts("NICK #{BOTNAME}") # Send the botname to Twitch
  36. socket.puts("JOIN ##{CHANNEL}") # Send the channel to Twitch
  37. # Save "Connected!" to "log.txt
  38. log.info("Joining #{CHANNEL.capitalize} as #{BOTNAME.capitalize} using OAUTH Token: #{OAUTH[6,OAUTH.length-16]}" + "*"*16)
  39. Thread.abort_on_exception = true
  40. # Loop (Background Thread) for recieving Twitch chat data
  41. Thread.start do
  42. socket.puts("PRIVMSG ##{CHANNEL} :Connected! PogChamp") # Send "Connected!" to the Twitch channel
  43. log.info("Connected to chat!")
  44. puts "#{BOTNAME} Joined ##{CHANNEL}" # Connection Status
  45. puts "You should be fully connected now" # Connection Status
  46. puts ""
  47. puts "Type \"clear\" to clear terminal"
  48. puts "Type \"project <text>\" to write to project file"
  49. puts "Type \"disconnect\" to disconnect"
  50. puts ""
  51. while (running) do
  52. ready = IO.select([socket])
  53. ready[0].each do |s|
  54. line = s.gets
  55. if line =~ /^/
  56. response, log_msg = HandleCommands(line)
  57. end
  58. if response
  59. socket.puts(response)
  60. response = nil
  61. end
  62. if log_msg
  63. log.info(log_msg)
  64. log_msg = nil
  65. end
  66. end
  67. end
  68. end
  69. # Loop to keep bot going
  70. while (running) do
  71. input = gets.chomp
  72. if input == "clear"
  73. Clear() # Clear console
  74. elsif input == "disconnect"
  75. socket.puts("PRIVMSG ##{CHANNEL} : Disconnected BibleThump")
  76. socket.puts("PART ##{CHANNEL}\r\n")
  77. running = false
  78. exit
  79. end
  80. end