twitch

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2019 License: MIT Imports: 12 Imported by: 25

README

go-twitch-irc Build Status Coverage Status

This is an irc client for connecting to twitch. It handles the annoying stuff like irc tag parsing. I highly recommend reading the godoc below, but this readme gives a basic overview of the functionality.

godoc: https://godoc.org/github.com/gempir/go-twitch-irc

Getting Started

package main

import (
	"github.com/gempir/go-twitch-irc"
	"fmt"
)

func main() {
	client := twitch.NewClient("justinfan123123", "oauth:123123123")

	client.OnNewMessage(func(channel string, user twitch.User, message twitch.Message) {
		fmt.Println(message.Text)
	})

	client.Join("gempir")

	err := client.Connect()
	if err != nil {
		panic(err)
	}
}
Available Data

The 2 structs twitch.User and twitch.Message look like this:

type User struct {
	UserID      string
	Username    string
	DisplayName string
	UserType    string
	Color       string
	Badges      map[string]int
}

type Message struct {
	Type      MessageType
	Time      time.Time
	Action    bool
	Emotes    []*Emote
	Tags      map[string]string
	Text      string
	Raw       string
	ChannelID string
}

Channel is just a string like "lirik", note the absent #.

Client Methods

These are the available methods of the client so you can get your bot going:

func (c *Client) Say(channel, text string)
func (c *Client) Whisper(username, text string)
func (c *Client) Join(channel string)
func (c *Client) Depart(channel string)
func (c *Client) Userlist(channel string) ([]string, error)
func (c *Client) Connect() error
func (c *Client) Disconnect() error
Options

On your client you can configure multiple options:

client.IrcAddress = "127.0.0.1:3030" // for custom irc server
client.TLS = false // enabled by default, will connect to non TLS server of twitch when off or the given client.IrcAddress
client.SetupCmd = "LOGIN custom_command_here" // Send a custom command on successful IRC connection, before authentication.
Callbacks

These callbacks are available to pass to the client:

client.OnConnect(func() {})
client.OnNewWhisper(func(user twitch.User, message twitch.Message) {})
client.OnNewMessage(func(channel string, user twitch.User, message twitch.Message) {})
client.OnNewRoomstateMessage(func(channel string, user twitch.User, message twitch.Message) {})
client.OnNewClearchatMessage(func(channel string, user twitch.User, message twitch.Message) {})
client.OnNewUsernoticeMessage(func(channel string, user twitch.User, message twitch.Message) {})
client.OnNewNoticeMessage(func(channel string, user twitch.User, message twitch.Message) {})
client.OnNewUserstateMessage(func(channel string, user twitch.User, message twitch.Message) {})
client.OnUserJoin(func(channel, user string) {})
client.OnUserPart(func(channel, user string) {})
Message Types

If you ever need more than basic PRIVMSG, this might be for you. These are the 6 major message types currently supported:

PRIVMSG
WHISPER
ROOMSTATE
CLEARCHAT
USERNOTICE
NOTICE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClientDisconnected returned from Connect() when a Disconnect() was called
	ErrClientDisconnected = errors.New("client called Disconnect()")

	// ErrLoginAuthenticationFailed returned from Connect() when either the wrong or a malformed oauth token is used
	ErrLoginAuthenticationFailed = errors.New("login authentication failed")

	// ErrConnectionIsNotOpen is returned by Disconnect in case you call it without being connected
	ErrConnectionIsNotOpen = errors.New("connection is not open")

	// WriteBufferSize can be modified to change the write channel buffer size. Must be configured before NewClient is called to take effect
	WriteBufferSize = 512

	// ReadBufferSize can be modified to change the read channel buffer size. Must be configured before NewClient is called to take effect
	ReadBufferSize = 64
)

Functions

func ParseMessage

func ParseMessage(line string) (string, *User, *Message)

ParseMessage parse a raw ircv3 twitch

Types

type Client

type Client struct {
	IrcAddress string

	TLS bool

	// Option whether to send pings every `IdlePingInterval`. The IdlePingInterval is interrupted every time a message is received from the irc server
	// The variable may only be modified before calling Connect
	SendPings bool

	// IdlePingInterval is the interval at which to send a ping to the irc server to ensure the connection is alive.
	// The variable may only be modified before calling Connect
	IdlePingInterval time.Duration

	// PongTimeout is the time go-twitch-irc waits after sending a ping before issuing a reconnect
	// The variable may only be modified before calling Connect
	PongTimeout time.Duration

	// SetupCmd is the command that is ran on successful connection to Twitch. Useful if you are proxying or something to run a custom command on connect.
	// The variable must be modified before calling Connect or the command will not run.
	SetupCmd string
	// contains filtered or unexported fields
}

Client client to control your connection and attach callbacks

func NewClient

func NewClient(username, oauth string) *Client

NewClient to create a new client

func (*Client) Connect

func (c *Client) Connect() error

Connect connect the client to the irc server

func (*Client) Depart

func (c *Client) Depart(channel string)

Depart leave a twitch channel

func (*Client) Disconnect

func (c *Client) Disconnect() error

Disconnect close current connection

func (*Client) Join

func (c *Client) Join(channel string)

Join enter a twitch channel to read more messages

func (*Client) OnConnect

func (c *Client) OnConnect(callback func())

OnConnect attach callback to when a connection has been established

func (*Client) OnNewClearchatMessage

func (c *Client) OnNewClearchatMessage(callback func(channel string, user User, message Message))

OnNewClearchatMessage attach callback to new messages such as timeouts

func (*Client) OnNewMessage

func (c *Client) OnNewMessage(callback func(channel string, user User, message Message))

OnNewMessage attach callback to new standard chat messages

func (*Client) OnNewNoticeMessage

func (c *Client) OnNewNoticeMessage(callback func(channel string, user User, message Message))

OnNewNoticeMessage attach callback to new notice message such as hosts

func (*Client) OnNewRoomstateMessage

func (c *Client) OnNewRoomstateMessage(callback func(channel string, user User, message Message))

OnNewRoomstateMessage attach callback to new messages such as submode enabled

func (*Client) OnNewUnsetMessage

func (c *Client) OnNewUnsetMessage(callback func(rawMessage string))

OnNewUnsetMessage attaches callback to messages that didn't parse properly. Should only be used if you're debugging the message parsing

func (*Client) OnNewUsernoticeMessage

func (c *Client) OnNewUsernoticeMessage(callback func(channel string, user User, message Message))

OnNewUsernoticeMessage attach callback to new usernotice message such as sub, resub, and raids

func (*Client) OnNewUserstateMessage

func (c *Client) OnNewUserstateMessage(callback func(channel string, user User, message Message))

OnNewUserstateMessage attach callback to new userstate

func (*Client) OnNewWhisper

func (c *Client) OnNewWhisper(callback func(user User, message Message))

OnNewWhisper attach callback to new whisper

func (*Client) OnPingSent added in v1.1.0

func (c *Client) OnPingSent(callback func())

OnPingSent attaches callback that's called whenever the client sends out a ping message

func (*Client) OnPongReceived added in v1.1.0

func (c *Client) OnPongReceived(callback func())

OnPongReceived attaches callback that's called whenever the client receives a pong to one of its previously sent out ping messages

func (*Client) OnUserJoin

func (c *Client) OnUserJoin(callback func(channel, user string))

OnUserJoin attaches callback to user joins

func (*Client) OnUserPart

func (c *Client) OnUserPart(callback func(channel, user string))

OnUserPart attaches callback to user parts

func (*Client) Say

func (c *Client) Say(channel, text string)

Say write something in a chat

func (*Client) SetIRCToken

func (c *Client) SetIRCToken(ircToken string)

SetIRCToken updates the oauth token for this client used for authentication This will not cause a reconnect, but is meant more for "on next connect, use this new token" in case the old token has expired

func (*Client) Userlist

func (c *Client) Userlist(channel string) ([]string, error)

Userlist returns the userlist for a given channel

func (*Client) Whisper

func (c *Client) Whisper(username, text string)

Whisper write something in private to someone on twitch whispers are heavily spam protected so your message might get blocked because of this verify your bot to prevent this

type Emote

type Emote struct {
	Name  string
	ID    string
	Count int
}

Emote twitch emotes

type Message

type Message struct {
	Type      MessageType
	Time      time.Time
	Action    bool
	Emotes    []*Emote
	Tags      map[string]string
	Text      string
	Raw       string
	ChannelID string
}

Message data you receive from tmi

type MessageType

type MessageType int

MessageType different message types possible to receive via IRC

const (
	// UNSET is the default message type, for whenever a new message type is added by twitch that we don't parse yet
	UNSET MessageType = -1
	// WHISPER private messages
	WHISPER MessageType = 0
	// PRIVMSG standard chat message
	PRIVMSG MessageType = 1
	// CLEARCHAT timeout messages
	CLEARCHAT MessageType = 2
	// ROOMSTATE changes like sub mode
	ROOMSTATE MessageType = 3
	// USERNOTICE messages like subs, resubs, raids, etc
	USERNOTICE MessageType = 4
	// USERSTATE messages
	USERSTATE MessageType = 5
	// NOTICE messages like sub mode, host on
	NOTICE MessageType = 6
)

type User

type User struct {
	UserID      string
	Username    string
	DisplayName string
	UserType    string
	Color       string
	Badges      map[string]int
}

User data you receive from tmi

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL