gempir / go-twitch-irc

go irc client for twitch.tv
MIT License
357 stars 59 forks source link

Joining Room causes panic: assignment to entry in nil map #94

Closed dyindude closed 5 years ago

dyindude commented 5 years ago

While working on my bot, I'd like to be able to have the bot join a Room instead of the default chatroom for the channel.

Referencing the twitch docs: https://dev.twitch.tv/docs/irc/chat-rooms/ the format for specifying a channel for a room is: #chatrooms:<channel ID>:<room UUID>

However, in trying to set this up for a Room I've created on my channel, the following error is encountered:

twitchbot_1  | chatrooms:166880276:803f38a5-edcd-4985-a3cf-641837524716                                                                   [0/9525]
twitchbot_1  | panic: assignment to entry in nil map
twitchbot_1  |
twitchbot_1  | goroutine 1 [running]:
twitchbot_1  | github.com/gempir/go-twitch-irc.(*Client).handleLine(0xc000150140, 0xc000194070, 0x67, 0x0, 0x1)
twitchbot_1  |  /go/src/github.com/gempir/go-twitch-irc/client.go:646 +0x326
twitchbot_1  | github.com/gempir/go-twitch-irc.(*Client).startParser(0xc000150140, 0x639400, 0xc000150140)
twitchbot_1  |  /go/src/github.com/gempir/go-twitch-irc/client.go:501 +0x112
twitchbot_1  | github.com/gempir/go-twitch-irc.(*Client).makeConnection(0xc000150140, 0xc000071e40, 0xc00008c900, 0x40c700, 0x61c740)
twitchbot_1  |  /go/src/github.com/gempir/go-twitch-irc/client.go:351 +0x21a
twitchbot_1  | github.com/gempir/go-twitch-irc.(*Client).Connect(0xc000150140, 0xc00001205f, 0x38)
twitchbot_1  |  /go/src/github.com/gempir/go-twitch-irc/client.go:301 +0x121
twitchbot_1  | main.main()

Here's a quick mockup of code to reproduce the issue:

package main

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

func main() {
    twitchClient := twitch.NewClient(os.Getenv("TWITCH_USERNAME"), os.Getenv("TWITCH_OAUTH"))
    twitchClient.Join("chatrooms:166880276:803f38a5-edcd-4985-a3cf-641837524716")

    err := twitchClient.Connect()
    if err != nil {
        panic(err)
    }
}
dyindude commented 5 years ago

Doing some debugging, I see that the server-side syntax is the same for the room (expected):

:dyinbot!dyinbot@dyinbot.tmi.twitch.tv JOIN #chatrooms:166880276:803f38a5-edcd-4985-a3cf-641837524716
:dyinbot.tmi.twitch.tv 353 dyinbot = #chatrooms:166880276:803f38a5-edcd-4985-a3cf-641837524716 :dyinbot

:dyinbot!dyinbot@dyinbot.tmi.twitch.tv JOIN #dyindude
:dyinbot.tmi.twitch.tv 353 dyinbot = #dyindude :dyinbot

I have traced this behavior to the parseNames function in message.go:

func parseNames(text string) (string, []string) {
    lines := strings.Split(text, ":")
    channelDirty := strings.Split(lines[1], "#")
    channel := strings.Trim(channelDirty[1], " ")
    users := strings.Split(lines[2], " ")

    return channel, users
}

which produces an invalid chatroom name for a Room (which has : in its name) when a Client tries to update its channelUserlist

I am working on a PR that corrects this behavior