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,7 +101,22 @@ func LeaveChannel(ch string) {
queue.Add(m) queue.Add(m)
} }
// LeaveAllChannel -
func LeaveAllChannel() {
for _, v := range channels {
m := &MsgObj{
Command: "PART",
Params: []string{
fmt.Sprintf("#%s", v),
},
}
queue.Add(m)
}
}
func runQueue() { func runQueue() {
go func() {
cnt := 0
for { for {
if !queue.IsEmpty() { if !queue.IsEmpty() {
m := queue.Get() m := queue.Get()
@ -117,9 +139,13 @@ func runQueue() {
fmt.Println("< ", msg.String()) fmt.Println("< ", msg.String())
client.WriteMessage(msg) client.WriteMessage(msg)
} }
cnt++
time.Sleep(time.Microsecond * 1500) 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) {