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.

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