Skip to content

Instantly share code, notes, and snippets.

@benstigsen
Last active September 6, 2019 10:06
Show Gist options
  • Save benstigsen/982ec7ca1a592b19fdf23f10f84f0835 to your computer and use it in GitHub Desktop.
Save benstigsen/982ec7ca1a592b19fdf23f10f84f0835 to your computer and use it in GitHub Desktop.

Revisions

  1. PhantomScripts revised this gist Jul 22, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions TwitchBot.rb
    Original file line number Diff line number Diff line change
    @@ -50,8 +50,8 @@
    ready[0].each do |s|
    line = s.gets
    #puts line # Show Information Recieved
    match = line.match(/^:(.+)!(.+)PRIVMSG #(.+) :(.+)$/)
    message = match && match[4]
    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
  2. PhantomScripts revised this gist Jul 22, 2017. No changes.
  3. PhantomScripts revised this gist Jul 22, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions TwitchBot.rb
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@
    elsif message =~ /^!ping/
    user = match[1]
    user = user.capitalize
    log.info "[Command] #{user}: !ping" # IF YOU ARE CONFUSED ABOUT HOW TO ADD COMMANDS READ "help.txt" <--<--<--<--<--<--<--<--<--<--|
    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) ^^
    @@ -91,7 +91,7 @@
    system "clear" or system "cls"
    log.info "Cleared Terminal" # Save it to "log.txt"
    puts "Cleared Terminal"
    puts "Type \"quit\" to stop the bot" # IF YOU ARE CONFUSED ABOUT HOW TO ADD COMMANDS READ "help.txt" <--<--<--<--<--<--<--<--<--<--|
    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) ^^^^^^^^^^^^^^
  4. PhantomScripts revised this gist Jul 22, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TwitchBot.rb
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    log = Logger.new("log.txt")

    # Required Info
    PASS = "oauth:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # "oauth:YourOauthToken" https://twitchapps.com/tmi/ <--- REQUIRED <--- REQUIRED
    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

  5. PhantomScripts created this gist Jul 21, 2017.
    102 changes: 102 additions & 0 deletions TwitchBot.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    # 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 #(.+) :(.+)$/)
    message = match && match[4]
    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" # IF YOU ARE CONFUSED ABOUT HOW TO ADD COMMANDS READ "help.txt" <--<--<--<--<--<--<--<--<--<--|
    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" # IF YOU ARE CONFUSED ABOUT HOW TO ADD COMMANDS READ "help.txt" <--<--<--<--<--<--<--<--<--<--|
    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"