fix twitch irc

This commit is contained in:
Jay 2018-09-15 23:48:40 +08:00
parent 16190c49c4
commit 3318352135
3 changed files with 61 additions and 37 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
config.yml config.yml
.vscode

View File

@ -34,10 +34,7 @@ func main() {
} }
defer db.Close() defer db.Close()
err = twitchirc.InitIRC() go twitchirc.InitIRC()
if err != nil {
log.Println(err)
}
server.Run(strings.Join([]string{":", strconv.Itoa(config.GetConf().Port)}, "")) server.Run(strings.Join([]string{":", strconv.Itoa(config.GetConf().Port)}, ""))
} }

View File

@ -1,8 +1,8 @@
package twitchirc package twitchirc
import ( import (
"crypto/tls"
"fmt" "fmt"
"net"
"time" "time"
"gopkg.in/irc.v2" "gopkg.in/irc.v2"
@ -15,25 +15,32 @@ var queue *QueueList
var channels []string var channels []string
// InitIRC - // InitIRC -
func InitIRC() (err error) { func InitIRC() {
conf := config.GetConf() 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 { if err != nil {
return return
} }
defer conn.Close()
channels = make([]string, 0)
queue = NewQueue()
runQueue()
config := irc.ClientConfig{ config := irc.ClientConfig{
Nick: "mtfos",
Pass: conf.Twitch.BotOauth,
Handler: irc.HandlerFunc(ircHandle), Handler: irc.HandlerFunc(ircHandle),
} }
client = irc.NewClient(conn, config) client = irc.NewClient(conn, config)
err = client.Run() err = client.Run()
if err != nil {
queue = NewQueue() fmt.Println("twitch chat connect fail")
go runQueue() }
channels = make([]string, 0)
return
} }
// SendMessage - // SendMessage -
@ -94,34 +101,53 @@ func LeaveChannel(ch string) {
queue.Add(m) queue.Add(m)
} }
func runQueue() { // LeaveAllChannel -
for { func LeaveAllChannel() {
if !queue.IsEmpty() { for _, v := range channels {
m := queue.Get() m := &MsgObj{
msg := &irc.Message{} Command: "PART",
msg.Command = m.Command Params: []string{
msg.Params = m.Params fmt.Sprintf("#%s", v),
},
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)
} }
queue.Add(m)
time.Sleep(time.Microsecond * 1500)
} }
} }
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) { func ircHandle(c *irc.Client, m *irc.Message) {
fmt.Println("> ", m.String()) fmt.Println("> ", m.String())
} }