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
.vscode

View File

@ -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)}, ""))
}

View File

@ -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,7 +101,22 @@ func LeaveChannel(ch string) {
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() {
go func() {
cnt := 0
for {
if !queue.IsEmpty() {
m := queue.Get()
@ -117,9 +139,13 @@ func runQueue() {
fmt.Println("< ", msg.String())
client.WriteMessage(msg)
}
time.Sleep(time.Microsecond * 1500)
cnt++
if cnt > 1800 {
// call rejoin
}
time.Sleep(time.Second * 1)
}
}()
}
func ircHandle(c *irc.Client, m *irc.Message) {