gempir / go-twitch-irc

go irc client for twitch.tv
MIT License
349 stars 58 forks source link

ErrReconnect without additional information #196

Closed happsie closed 7 months ago

happsie commented 7 months ago

Hello!

Not sure if I'm not understanding at all, but this is my code I am trying to use to connect to twitch IRC using this library.

func (ti *TwitchIRC) StartIRC(channels []string) error {
    if ti.client != nil {
        return nil
    }
    ti.client = twitch.NewClient(username, token)
    ti.client.OnConnect(ti.onConnect)
    ti.client.OnPrivateMessage(ti.onPrivateMessage)
    ti.client.Join(channels...)
    err := ti.client.Connect()
    if err != nil {
        ti.container.GetLogger().Error("error connecting to IRC", err)
        return err
    }
    return nil
}

func (ti *TwitchIRC) onConnect() {
    ti.container.GetLogger().Info("connected!")
}

func (ti *TwitchIRC) onPrivateMessage(message twitch.PrivateMessage) {
    ti.container.GetLogger().Info(message.Message)
}

After some debugging, I see that I constantly hit the error reconnect. I am however not sure why and don't see any errors. Is there something I'm missing?

pajlada commented 7 months ago

The error with the string "reconnect" is an internal error that should never be returned to you as a user See https://github.com/gempir/go-twitch-irc/blob/master/client.go#L717-L727 where errReconnect should be caught and not returned

Your code looks good, that's basically what the readme & the cmd/pingpong/pingpong.go example looks like

Common disconnects happen due to bad tokens, could you try connecting anonymously for now? basically ti.client = twitch.NewClient("justinfan123456", "oauth:123123123")

happsie commented 7 months ago

Thanks for your reply!

Seems like anonymous client works fine. What format should the token be in? I currently have it as oauth:bearer token. Having it as oauth:token gives login authentication failed

pajlada commented 7 months ago

It must be oauth:abc123 where abc123 is your User Access Token that has chat reading (and optionally writing) permissions

pajlada commented 7 months ago

The flow I use for my bots is: Use an Authorization code grant flow, sign into my website with the bot account. That forwards my browser to web server/bot service, it then uses the authorization code to get a User Access Token (access_token in the response), and a Refresh Code (refresh_code in the response). The User Access Token is used to connect to chat, and the Refresh Code is used to refresh the User Access Token when it has expired.

happsie commented 7 months ago

Thank you!

Was a permissions problem, did not think of adding them as scopes :D