NewIRCClient
Create a new IRC client for connecting to Twitch chat.
client := helix.NewIRCClient("bot_username", "oauth:your-token",
helix.WithMessageHandler(func(msg *helix.ChatMessage) {
fmt.Printf("[%s] %s: %s\n", msg.Channel, msg.User, msg.Text)
}),
)
NewIRCClientE
Create a new IRC client with error handling for invalid inputs.
client, err := helix.NewIRCClientE("bot_username", "oauth:your-token",
helix.WithAutoReconnect(true),
)
if err != nil {
log.Fatal(err)
}
Connect
Establish a connection to Twitch IRC.
ctx := context.Background()
if err := client.Connect(ctx); err != nil {
log.Fatal(err)
}
defer client.Close()
Join
Join a channel to receive messages.
if err := client.Join("channel_name"); err != nil {
log.Printf("Failed to join: %v", err)
}
Part
Leave a channel.
if err := client.Part("channel_name"); err != nil {
log.Printf("Failed to part: %v", err)
}
Say
Send a message to a channel.
if err := client.Say("channel_name", "Hello, chat!"); err != nil {
log.Printf("Failed to send: %v", err)
}
Reply
Reply to a specific message.
if err := client.Reply("channel_name", "message-id", "This is a reply!"); err != nil {
log.Printf("Failed to reply: %v", err)
}
Close
Close the IRC connection.
if err := client.Close(); err != nil {
log.Printf("Failed to close: %v", err)
}
Configuration Options
WithAutoReconnect
Enable or disable automatic reconnection.
helix.WithAutoReconnect(true)
WithReconnectDelay
Set the delay between reconnection attempts.
helix.WithReconnectDelay(5 * time.Second)
WithIRCURL
Set a custom WebSocket URL.
helix.WithIRCURL("wss://custom-irc.example.com")
Event Handlers
WithMessageHandler
Handle incoming chat messages.
helix.WithMessageHandler(func(msg *helix.ChatMessage) {
fmt.Printf("%s: %s\n", msg.User, msg.Text)
})
WithUserNoticeHandler
Handle user notices (subscriptions, raids, etc.).
helix.WithUserNoticeHandler(func(notice *helix.UserNotice) {
fmt.Printf("User notice: %s\n", notice.MsgID)
})
WithRoomStateHandler
Handle room state changes (slow mode, emote-only, etc.).
helix.WithRoomStateHandler(func(state *helix.RoomState) {
fmt.Printf("Room state updated: %s\n", state.Channel)
})
WithClearChatHandler
Handle timeout and ban events.
helix.WithClearChatHandler(func(clear *helix.ClearChat) {
fmt.Printf("User %s was timed out\n", clear.TargetUserID)
})
WithClearMessageHandler
Handle deleted messages.
helix.WithClearMessageHandler(func(clear *helix.ClearMessage) {
fmt.Printf("Message deleted: %s\n", clear.TargetMsgID)
})
WithWhisperHandler
Handle whisper (private) messages.
helix.WithWhisperHandler(func(whisper *helix.Whisper) {
fmt.Printf("Whisper from %s: %s\n", whisper.User, whisper.Text)
})
WithJoinHandler
Handle channel join events.
helix.WithJoinHandler(func(channel, user string) {
fmt.Printf("%s joined %s\n", user, channel)
})
WithPartHandler
Handle channel leave events.
helix.WithPartHandler(func(channel, user string) {
fmt.Printf("%s left %s\n", user, channel)
})
WithConnectHandler
Handle successful connection events.
helix.WithConnectHandler(func() {
fmt.Println("Connected to IRC!")
})
WithDisconnectHandler
Handle disconnection events.
helix.WithDisconnectHandler(func() {
fmt.Println("Disconnected from IRC")
})
WithReconnectHandler
Handle reconnection events.
helix.WithReconnectHandler(func() {
fmt.Println("Reconnected!")
// Rejoin channels
})
WithIRCErrorHandler
Handle errors.
helix.WithIRCErrorHandler(func(err error) {
log.Printf("IRC error: %v", err)
})
WithRawMessageHandler
Handle raw IRC messages for debugging.
helix.WithRawMessageHandler(func(raw string) {
fmt.Printf("Raw: %s\n", raw)
})
See Also
- IRC Client Examples - Complete code examples
- Chat Bot Example - Building a chat bot
- Chat API - Helix API chat endpoints