Sean

Weekly Brain Dump #36

- 490 words

At a Glance

Building a Twitch Bot in Opal

The whole point of my Ruby on the frontend journey has been to work towards building this specific widget. I want to learn how to build Twitch chat bots so that I can build a little task tracker for my wife.

So far getting a connection and being able to join a channel has been surprisingly easy. I didn’t expect that out of something owned by Amazon. That’s really all I’ve had the chance for this week as it’s been hectic here.

Here’s the basic class I threw together to get it working and joining my channel. It still needs to do any level of message parsing and responding to events, but its surprisingly simple!

module Twitch
  class Client
    attr_reader :socket
    def initialize
      @auth_token = "$YOUR_AUTH_TOKEN"
      @username = "hell_rok"
      @channel = "#hell_rok"
    end

    def start
      Browser::Socket.new("wss://irc-ws.chat.twitch.tv:443") do |socket|
        @socket = socket
        socket.on :open do
          login
        end

        socket.on :message do |event|
          messages = Message.from_event(event)
          puts "== EVENT =="
          puts messages.map(&:message)
        end
      end
    end

    def login
      socket.puts("CAP REQ :twitch.tv/tags twitch.tv/commands");
      socket.puts("PASS oauth:#{@auth_token}");
      socket.puts("NICK #{@username}");
      socket.puts("JOIN #{@channel}");
    end
  end

  class Message
    def self.from_event(event)
      puts "-- FROM EVENT --"
      event.data.lines.map do |message|
        new(message)
      end
    end

    attr_reader :message
    def initialize(message)
      @message = message
    end
  end
end

You can create any game mechanic once you understand this: A great introduction into thinking about designing using a framework.

Offpunk Manifesto: A great decsription of what Offpunk is.

2-Player Legend of Zelda Romhack: Someone modded the original Legend of Zelda to be multiplayer.

There’s a libcurl.dll in my system32: A funny little story.

Archive of interesting links


Comments