first steps for the command handler

This commit is contained in:
gempir 2017-03-08 19:39:56 +01:00
parent 2701584d31
commit 1fc7a75d7e
5 changed files with 41 additions and 17 deletions

20
command/commands.go Normal file
View file

@ -0,0 +1,20 @@
package command
import (
"github.com/gempir/gempbotgo/twitch"
"github.com/op/go-logging"
)
type handler struct {
log logging.Logger
}
func NewHandler(logger logging.Logger) handler {
return handler{
log: logger,
}
}
func (h *handler) HandleMessage(msg twitch.Message) {
h.log.Debug(msg.Text)
}

10
main.go
View file

@ -8,11 +8,11 @@ import (
"github.com/op/go-logging"
"github.com/gempir/gempbotgo/twitch"
"gopkg.in/redis.v3"
"github.com/gempir/gempbotgo/command"
)
var (
cfg config
// Log logger from go-logging
Log logging.Logger
)
@ -41,7 +41,13 @@ func main() {
})
bot := twitch.NewBot(cfg.IrcAddress, cfg.IrcUser, cfg.IrcToken, Log, *rClient)
bot.CreateConnection()
go bot.CreateConnection()
cmdHandler := command.NewHandler(Log)
for msg := range bot.Messages {
cmdHandler.HandleMessage(msg)
}
}
func initLogger() logging.Logger {

View file

@ -12,7 +12,7 @@ import (
)
type bot struct {
messages chan message
Messages chan Message
ircAddress string
ircUser string
ircToken string
@ -30,7 +30,7 @@ var (
func NewBot(ircAddress string, ircUser string, ircToken string, logger logging.Logger, rClient redis.Client) bot {
return bot{
messages: make(chan message),
Messages: make(chan Message),
ircAddress: ircAddress,
ircUser: ircUser,
ircToken: ircToken,
@ -46,11 +46,10 @@ func (bot *bot) CreateConnection() {
bot.log.Error(err.Error())
return
}
bot.log.Debugf("new connection %s", conn.RemoteAddr())
bot.log.Debugf("new IRC connection %s", conn.RemoteAddr())
fmt.Fprintf(conn, "PASS %s\r\n", bot.ircToken)
fmt.Fprintf(conn, "NICK %s\r\n", bot.ircUser)
fmt.Fprintf(conn, "JOIN %s\r\n", "#" + bot.ircUser)
bot.log.Debugf("PASS %s\r\n", bot.ircToken)
go bot.joinDefault()
reader := bufio.NewReader(conn)
@ -86,7 +85,6 @@ func (bot *bot) joinDefault() {
}
func (bot *bot) parseMessage(msg string) {
bot.log.Debug(msg)
if !strings.Contains(msg, ".tmi.twitch.tv PRIVMSG ") {
return
@ -125,8 +123,8 @@ func (bot *bot) parseMessage(msg string) {
mod = true
}
user := newUser(username, tagMap["user-id"], tagMap["color"], tagMap["display-name"], mod, subscriber, turbo)
bot.messages <- newMessage(message, user, "#" + channel)
user := newUser(username, tagMap["User-id"], tagMap["color"], tagMap["display-name"], mod, subscriber, turbo)
bot.Messages <- newMessage(message, user, "#" + channel)
}
func (bot *bot) join(channel string) {

View file

@ -1,13 +1,13 @@
package twitch
type message struct {
Text string
User user
type Message struct {
Text string
User User
Channel string
}
func newMessage(text string, user user, channel string) message {
return message{
func newMessage(text string, user User, channel string) Message {
return Message{
Text: text,
User: user,
Channel: channel,

View file

@ -1,6 +1,6 @@
package twitch
type user struct {
type User struct {
Username string
UserId string
Color string
@ -11,8 +11,8 @@ type user struct {
Emotes map[string][]string
}
func newUser(username string, userId string, color string, displayName string, mod bool, subscriber bool, turbo bool) user {
return user{
func newUser(username string, userId string, color string, displayName string, mod bool, subscriber bool, turbo bool) User {
return User{
Username: username,
UserId: userId,
Color: color,