From 3318352135c2c3cbf8fddf23d0bfdc2dbc1273e3 Mon Sep 17 00:00:00 2001 From: jay Date: Sat, 15 Sep 2018 23:48:40 +0800 Subject: [PATCH] fix twitch irc --- .gitignore | 1 + main.go | 5 +- module/twitch-irc/twitch-irc.go | 92 +++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 1d3ed4c..478c433 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ config.yml +.vscode diff --git a/main.go b/main.go index 6203c83..f4b4098 100644 --- a/main.go +++ b/main.go @@ -34,10 +34,7 @@ func main() { } defer db.Close() - err = twitchirc.InitIRC() - if err != nil { - log.Println(err) - } + go twitchirc.InitIRC() server.Run(strings.Join([]string{":", strconv.Itoa(config.GetConf().Port)}, "")) } diff --git a/module/twitch-irc/twitch-irc.go b/module/twitch-irc/twitch-irc.go index 3b8f83c..bd74992 100644 --- a/module/twitch-irc/twitch-irc.go +++ b/module/twitch-irc/twitch-irc.go @@ -1,8 +1,8 @@ package twitchirc import ( + "crypto/tls" "fmt" - "net" "time" "gopkg.in/irc.v2" @@ -15,25 +15,32 @@ var queue *QueueList var channels []string // InitIRC - -func InitIRC() (err error) { +func InitIRC() { conf := config.GetConf() - conn, err := net.Dial("tcp", conf.Twitch.ChatHost) + tlsConf := &tls.Config{} + conn, err := tls.Dial("tcp", conf.Twitch.ChatHost, tlsConf) + // conn, err := net.Dial("tcp", conf.Twitch.ChatHost) if err != nil { return } + defer conn.Close() + + channels = make([]string, 0) + queue = NewQueue() + runQueue() + config := irc.ClientConfig{ + Nick: "mtfos", + Pass: conf.Twitch.BotOauth, Handler: irc.HandlerFunc(ircHandle), } client = irc.NewClient(conn, config) err = client.Run() - - queue = NewQueue() - go runQueue() - - channels = make([]string, 0) - return + if err != nil { + fmt.Println("twitch chat connect fail") + } } // SendMessage - @@ -94,34 +101,53 @@ func LeaveChannel(ch string) { queue.Add(m) } -func runQueue() { - for { - if !queue.IsEmpty() { - m := queue.Get() - msg := &irc.Message{} - msg.Command = m.Command - msg.Params = m.Params - - if m.Command == "JOIN" { - if indexOf(channels, m.Params[0][1:]) != -1 { - continue - } - channels = append(channels, m.Params[0][1:]) - } else if m.Command == "PART" { - if indexOf(channels, m.Params[0][1:]) == -1 { - continue - } - idx := indexOf(channels, m.Params[0][1:]) - channels = append(channels[:idx], channels[idx+1:]...) - } - fmt.Println("< ", msg.String()) - client.WriteMessage(msg) +// LeaveAllChannel - +func LeaveAllChannel() { + for _, v := range channels { + m := &MsgObj{ + Command: "PART", + Params: []string{ + fmt.Sprintf("#%s", v), + }, } - - time.Sleep(time.Microsecond * 1500) + queue.Add(m) } } +func runQueue() { + go func() { + cnt := 0 + for { + if !queue.IsEmpty() { + m := queue.Get() + msg := &irc.Message{} + msg.Command = m.Command + msg.Params = m.Params + + if m.Command == "JOIN" { + if indexOf(channels, m.Params[0][1:]) != -1 { + continue + } + channels = append(channels, m.Params[0][1:]) + } else if m.Command == "PART" { + if indexOf(channels, m.Params[0][1:]) == -1 { + continue + } + idx := indexOf(channels, m.Params[0][1:]) + channels = append(channels[:idx], channels[idx+1:]...) + } + fmt.Println("< ", msg.String()) + client.WriteMessage(msg) + } + cnt++ + if cnt > 1800 { + // call rejoin + } + time.Sleep(time.Second * 1) + } + }() +} + func ircHandle(c *irc.Client, m *irc.Message) { fmt.Println("> ", m.String()) }