bot can now talk and report status in chat'
This commit is contained in:
parent
acd2c96d56
commit
9c8e7031b0
3 changed files with 24 additions and 16 deletions
|
@ -10,13 +10,15 @@ import (
|
|||
|
||||
type handler struct {
|
||||
admin string
|
||||
bot twitch.Bot
|
||||
startTime time.Time
|
||||
log logging.Logger
|
||||
}
|
||||
|
||||
func NewHandler(admin string, startTime time.Time, logger logging.Logger) handler {
|
||||
func NewHandler(admin string, bot twitch.Bot, startTime time.Time, logger logging.Logger) handler {
|
||||
return handler{
|
||||
admin: admin,
|
||||
bot: bot,
|
||||
startTime: startTime,
|
||||
log: logger,
|
||||
}
|
||||
|
@ -25,7 +27,7 @@ func NewHandler(admin string, startTime time.Time, logger logging.Logger) handle
|
|||
func (h *handler) HandleCommand(msg twitch.Message) {
|
||||
if msg.User.Username == strings.ToLower(h.admin) {
|
||||
uptime := formatDiff(diff(h.startTime, time.Now()))
|
||||
h.log.Debug(h.admin + ", uptime: " + uptime)
|
||||
h.bot.Say(h.admin + ", uptime: " + uptime, msg.Channel)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
9
main.go
9
main.go
|
@ -4,14 +4,15 @@ import (
|
|||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/op/go-logging"
|
||||
"github.com/gempir/gempbotgo/twitch"
|
||||
"gopkg.in/redis.v3"
|
||||
|
||||
"github.com/gempir/gempbotgo/twitch"
|
||||
"github.com/gempir/gempbotgo/command"
|
||||
"strings"
|
||||
"github.com/gempir/gempbotgo/filelog"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -50,7 +51,7 @@ func main() {
|
|||
go bot.CreateConnection()
|
||||
|
||||
fileLogger := filelog.NewFileLogger(cfg.LogPath, Log)
|
||||
cmdHandler := command.NewHandler(cfg.Admin, startTime, Log)
|
||||
cmdHandler := command.NewHandler(cfg.Admin, bot, startTime, Log)
|
||||
|
||||
|
||||
for msg := range bot.Messages {
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
"gopkg.in/redis.v3"
|
||||
)
|
||||
|
||||
type bot struct {
|
||||
type Bot struct {
|
||||
Messages chan Message
|
||||
ircAddress string
|
||||
ircUser string
|
||||
|
@ -28,8 +28,8 @@ var (
|
|||
actionReg2 = regexp.MustCompile(`([\x{0001}]+)`)
|
||||
)
|
||||
|
||||
func NewBot(ircAddress string, ircUser string, ircToken string, logger logging.Logger, rClient redis.Client) bot {
|
||||
return bot{
|
||||
func NewBot(ircAddress string, ircUser string, ircToken string, logger logging.Logger, rClient redis.Client) Bot {
|
||||
return Bot{
|
||||
Messages: make(chan Message),
|
||||
ircAddress: ircAddress,
|
||||
ircUser: ircUser,
|
||||
|
@ -39,7 +39,12 @@ func NewBot(ircAddress string, ircUser string, ircToken string, logger logging.L
|
|||
}
|
||||
}
|
||||
|
||||
func (bot *bot) CreateConnection() {
|
||||
func (bot *Bot) Say(text string, channel string) {
|
||||
bot.log.Infof("PRIVMSG %s :%s", channel, text)
|
||||
fmt.Fprintf(*mainConn, "PRIVMSG %s :%s\r\n", channel, text)
|
||||
}
|
||||
|
||||
func (bot *Bot) CreateConnection() {
|
||||
conn, err := net.Dial("tcp", bot.ircAddress)
|
||||
mainConn = &conn
|
||||
if err != nil {
|
||||
|
@ -47,9 +52,9 @@ func (bot *bot) CreateConnection() {
|
|||
return
|
||||
}
|
||||
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)
|
||||
fmt.Fprintf(*mainConn, "PASS %s\r\n", bot.ircToken)
|
||||
fmt.Fprintf(*mainConn, "NICK %s\r\n", bot.ircUser)
|
||||
fmt.Fprintf(*mainConn, "JOIN %s\r\n", "#" + bot.ircUser)
|
||||
go bot.joinDefault()
|
||||
|
||||
reader := bufio.NewReader(conn)
|
||||
|
@ -71,7 +76,7 @@ func (bot *bot) CreateConnection() {
|
|||
defer bot.CreateConnection()
|
||||
}
|
||||
|
||||
func (bot *bot) joinDefault() {
|
||||
func (bot *Bot) joinDefault() {
|
||||
val, err := bot.rClient.HGetAll("channels").Result()
|
||||
if err != nil {
|
||||
bot.log.Error(err.Error())
|
||||
|
@ -84,7 +89,7 @@ func (bot *bot) joinDefault() {
|
|||
}
|
||||
}
|
||||
|
||||
func (bot *bot) parseMessage(msg string) {
|
||||
func (bot *Bot) parseMessage(msg string) {
|
||||
|
||||
if !strings.Contains(msg, ".tmi.twitch.tv PRIVMSG ") {
|
||||
return
|
||||
|
@ -127,7 +132,7 @@ func (bot *bot) parseMessage(msg string) {
|
|||
bot.Messages <- newMessage(message, user, channel)
|
||||
}
|
||||
|
||||
func (bot *bot) join(channel string) {
|
||||
func (bot *Bot) join(channel string) {
|
||||
bot.log.Info("JOIN " + channel)
|
||||
fmt.Fprintf(*mainConn, "JOIN %s\r\n", channel)
|
||||
}
|
Loading…
Add table
Reference in a new issue