0 Replies - 1979 Views - Last Post: 03 October 2017 - 10:17 AM Rate Topic: -----

#1 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 91
  • Joined: 02-May 12

Golang twitch bot

Posted 03 October 2017 - 10:17 AM

Hi guys, I'm trying to create a twitch bot in go, but I'm having difficulties understanding what's going on with the code.

I've followed this tutorial: https://dinosaurscod...itch-bot-in-go/

Here's the source:
package main

import "net"
import "strings"
import "bufio"
import "net/textproto"
import "fmt"

func main() {
	conn, err := net.Dial("tcp", "irc.chat.twitch.tv:6667")
	if err != nil {
		panic(err)
	}

	conn.Write([]byte("PASS " + "oauth:secret" + "\r\n"))
	conn.Write([]byte("NICK " + "LazyTwitchBot" + "\r\n"))
	conn.Write([]byte("JOIN " + "#lazyvulpes" + "\r\n"))
	defer conn.Close()

	// handles reading from the connection
	tp := textproto.NewReader(bufio.NewReader(conn))

	// listens/responds to chat messages
	for {
		msg, err := tp.ReadLine()
		if err != nil {
			panic(err)
		}
		fmt.Println(msg)
		// split the msg by spaces
		msgParts := strings.Split(msg, " ")

		// if the msg contains PING you're required to
		// respond with PONG else you get kicked
		if msgParts[0] == "PING" {
			conn.Write([]byte("PONG " + msgParts[1]))
			continue
		}

		// if msg contains PRIVMSG then respond
		if msgParts[1] == "PRIVMSG" {
			// echo back the same message
			conn.Write([]byte("PRIVMSG " + msgParts[2] + " " + msgParts[3] + "\r\n"))
		}
	}
}



Right now, the bot just responds with the first word any user writes in the chat, this is of course a useless behaviour.

I'm trying to make it so that when a user types in a certain predetermined command, the bot responds accordantly.

I've tried doing stuff like:
if msgParts[3]=="command"{
	conn.Write([]byte("PRIVMSG " + msgParts[2] + " " + "Command response" + "\r\n"))
}


But it doesn't seem to work, another thing that confuses me, is that, when I type in the same message twice, the bot no longer responds, but if I type in a different message, it responds again.

My end goal is to make a interface between twitch and discord. I have a music bot in my discord which I would like my twitch viewers to be able to control via the twitch chat. So my bot should take the commands from twitch and relay to my discord bot, using the discord chat.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1