Last active
September 6, 2019 10:06
-
-
Save benstigsen/982ec7ca1a592b19fdf23f10f84f0835 to your computer and use it in GitHub Desktop.
Twitch chat bot written in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bot.rb | |
require 'socket' | |
require 'logger' | |
# Create logger | |
log = Logger.new("log.txt") | |
# Required Info | |
PASS = "oauth:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # "oauth:YourOauthToken" https://twitchapps.com/tmi/ <--- REQUIRED <--- REQUIRED | |
NICK = "botname" # Name of bot <--- REQUIRED <--- REQUIRED | |
CHAN = "channel" # Channel to join (Don't add a "#" hashtag) <--- REQUIRED <--- REQUIRED | |
# -------- IGNORE -------- # | |
BOTNAME = "#{NICK.downcase}" # CHANNEL and BOTNAME needs to be in lowercase | |
CHANNEL = "##{CHAN.downcase}" # So this was just added to be sure (Might aswell leave it) | |
# -------- IGNORE -------- # | |
# Save "Preparing to connect" to "log.txt" | |
log.info "Preparing to connect" | |
# Variables | |
socket = TCPSocket.new('irc.chat.twitch.tv', 6667) # Creates the "socket" variable // With the specified IRC channel to join | |
running = true # Creates the "running" variable (for loop) | |
send = "PRIVMSG #{CHANNEL} :" # Creates the "send" variable (shortcut for sending messages) | |
# Save "Connected!" to "log.txt | |
log.info "Connected!" | |
# Authorization Login | |
socket.puts("PASS #{PASS}") # Send the password to Twitch | |
socket.puts("NICK #{BOTNAME}") # Send the username to Twitch | |
socket.puts("JOIN #{CHANNEL}") # Send the channel to Twitch | |
socket.puts(send + "Connected!") # Send "Connected!" to the Twitch channel | |
Thread.abort_on_exception = true | |
# Loop (Background Thread) for recieving Twitch chat data | |
Thread.start do | |
puts "" # Empty Line | |
puts "#{BOTNAME} Joined #{CHANNEL}" # Connection Status | |
puts "You should be fully connected now" # Connection Status | |
puts "" # Empty Line | |
puts "Type \"quit\" to stop the bot" | |
puts "Type \"clear\" to clear terminal" # Shows available commands in the terminal | |
puts "Type \"quit\" to send a custom message" # Before it starts the loop to recieve data | |
puts "" # Empty Line | |
while (running) do | |
ready = IO.select([socket]) | |
ready[0].each do |s| | |
line = s.gets | |
#puts line # Show Information Recieved | |
match = line.match(/^:(.+)!(.+)PRIVMSG #{CHANNEL} :(.+)$/) | |
message = match && match[3] | |
if message =~ /^!hello/ # If recieves "!hello" // COMMANDS // COMMANDS // COMMANDS | |
user = match[1] # Get Username | |
user = user.capitalize | |
log.info "[Command] #{user}: !hello" # Save it in "log.txt" | |
puts "[Command] #{user}: !hello" # In terminal it writes "[Command] USER: !hello" | |
socket.puts(send + "Hi #{user} !") # Send back "Hi USER!" | |
elsif message =~ /^!ping/ | |
user = match[1] | |
user = user.capitalize | |
log.info "[Command] #{user}: !ping" | |
puts "[Command] #{user}: !ping" | |
socket.puts(send + "Ping! #{user} ") | |
elsif message !=~ /a1b2c3/ # Add you own commands ABOVE this one [elsif message !=~ /a1b2c3/] (just copy the other commands) ^^ | |
user = match[1] | |
user = user.capitalize # Don't delete this command | |
log.info "[Message] #{user}: #{message}" # If NOT message = "a1b2c3" show the message | |
puts "[Message] #{user}: #{message}" # ^^ No one is going to type that, so it just shows the message and who sent it | |
end # In terminal it writes "[Message] USER: message" | |
end | |
end | |
end | |
# Loop for terminal commands | |
while (running) do | |
command = gets.chomp | |
if command == "quit" # If command = "quit" | |
log.info "Command Executed: quit" # Save "Command Executed: quit" in "log.txt" | |
socket.puts("PART #{CHANNEL}") # Disconnects from the channel | |
running = false # Set "running" to false = loop stops = script stops = bot stops | |
elsif command == "msg" | |
print "Message: " # Here you can send a custom message by typing "msg" | |
msg = gets.chomp | |
socket.puts(send + msg) # Sends message | |
puts "Message Sent: #{msg}" # Shows it in terminal | |
log.info "Message Sent: #{msg}" # Save it to "log.txt" | |
elsif command == "clear" | |
system "clear" or system "cls" | |
log.info "Cleared Terminal" # Save it to "log.txt" | |
puts "Cleared Terminal" | |
puts "Type \"quit\" to stop the bot" | |
puts "Type \"clear\" to clear terminal" | |
puts "Type \"quit\" to send a custom message" | |
else # Add you own commands ABOVE this (else function) (just copy the other commands) ^^^^^^^^^^^^^^ | |
log.info "< #{command}" | |
end | |
end | |
puts "Disconnected" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment