Fix Whoing a channel and factoid timer

This commit is contained in:
Chris Sexton 2016-04-21 11:19:38 -04:00
parent e77c082db6
commit 8485ed9fe3
6 changed files with 81 additions and 18 deletions

View File

@ -178,13 +178,12 @@ func (b *bot) AddHandler(name string, h Handler) {
} }
func (b *bot) Who(channel string) []user.User { func (b *bot) Who(channel string) []user.User {
out := []user.User{} names := b.conn.Who(channel)
for _, u := range b.users { users := []user.User{}
if u.Name != b.Config().Nick { for _, n := range names {
out = append(out, u) users = append(users, user.New(n))
}
} }
return out return users
} }
var rootIndex string = ` var rootIndex string = `

View File

@ -31,6 +31,8 @@ type Connector interface {
SendMessage(channel, message string) SendMessage(channel, message string)
SendAction(channel, message string) SendAction(channel, message string)
Serve() Serve()
Who(string) []string
} }
// Interface used for compatibility with the Plugin interface // Interface used for compatibility with the Plugin interface

View File

@ -6,15 +6,12 @@ package user
// session // session
type User struct { type User struct {
// Current nickname known // Current nickname known
Name string Name string
// LastSeen DateTime
// Alternative nicknames seen
Alts []string
Parent string
Admin bool Admin bool
}
//bot *bot
func New(name string) User {
return User{
Name: name,
}
} }

View File

@ -274,3 +274,7 @@ func (i *Irc) buildMessage(inMsg irc.Msg) msg.Message {
return msg return msg
} }
func (i Irc) Who(channel string) []string {
return []string{}
}

View File

@ -609,13 +609,14 @@ func (p *Factoid) randomFact() *factoid {
// factTimer spits out a fact at a given interval and with given probability // factTimer spits out a fact at a given interval and with given probability
func (p *Factoid) factTimer(channel string) { func (p *Factoid) factTimer(channel string) {
duration := time.Duration(p.Bot.Config().Factoid.QuoteTime) * time.Minute duration := time.Duration(p.Bot.Config().Factoid.QuoteTime) * time.Second
myLastMsg := time.Now() myLastMsg := time.Now()
for { for {
time.Sleep(time.Duration(5) * time.Second) time.Sleep(time.Duration(5) * time.Second) // why 5?
lastmsg, err := p.Bot.LastMessage(channel) lastmsg, err := p.Bot.LastMessage(channel)
if err != nil { if err != nil {
// Probably no previous message to time off of
continue continue
} }
@ -627,6 +628,7 @@ func (p *Factoid) factTimer(channel string) {
if success && tdelta > duration && earlier { if success && tdelta > duration && earlier {
fact := p.randomFact() fact := p.randomFact()
if fact == nil { if fact == nil {
log.Println("Didn't find a random fact to say")
continue continue
} }

View File

@ -11,8 +11,10 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"strings" "strings"
"sync/atomic" "sync/atomic"
"time"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/msg"
@ -44,6 +46,25 @@ type slackUserInfoResp struct {
} `json:"user"` } `json:"user"`
} }
type slackChannelInfoResp struct {
Ok bool `json:"ok"`
Channel struct {
Id string `json:"id"`
Name string `json:"name"`
Created int64 `json:"created"`
Creator string `json:"creator"`
Members []string `json:"members"`
Topic struct {
Value string `json:"value"`
Creator string `json:"creator"`
LastSet int64 `json:"last_set"`
} `json:"topic"`
} `json:"channel"`
}
type slackMessage struct { type slackMessage struct {
Id uint64 `json:"id"` Id uint64 `json:"id"`
Type string `json:"type"` Type string `json:"type"`
@ -51,6 +72,7 @@ type slackMessage struct {
Channel string `json:"channel"` Channel string `json:"channel"`
Text string `json:"text"` Text string `json:"text"`
User string `json:"user"` User string `json:"user"`
Ts string `json:"ts"`
Error struct { Error struct {
Code uint64 `json:"code"` Code uint64 `json:"code"`
Msg string `json:"msg"` Msg string `json:"msg"`
@ -150,6 +172,12 @@ func (s *Slack) buildMessage(m slackMessage) msg.Message {
u := s.getUser(m.User) u := s.getUser(m.User)
// I think it's horseshit that I have to do this
ts := strings.Split(m.Ts, ".")
sec, _ := strconv.ParseInt(ts[0], 10, 64)
nsec, _ := strconv.ParseInt(ts[1], 10, 64)
tstamp := time.Unix(sec, nsec)
return msg.Message{ return msg.Message{
User: &user.User{ User: &user.User{
Name: u, Name: u,
@ -160,6 +188,7 @@ func (s *Slack) buildMessage(m slackMessage) msg.Message {
Command: isCmd, Command: isCmd,
Action: isAction, Action: isAction,
Host: string(m.Id), Host: string(m.Id),
Time: tstamp,
} }
} }
@ -212,6 +241,7 @@ func (s *Slack) getUser(id string) string {
resp.StatusCode, err) resp.StatusCode, err)
return "UNKNOWN" return "UNKNOWN"
} }
defer resp.Body.Close()
var userInfo slackUserInfoResp var userInfo slackUserInfoResp
err = json.NewDecoder(resp.Body).Decode(&userInfo) err = json.NewDecoder(resp.Body).Decode(&userInfo)
if err != nil { if err != nil {
@ -221,3 +251,32 @@ func (s *Slack) getUser(id string) string {
s.users[id] = userInfo.User.Name s.users[id] = userInfo.User.Name
return s.users[id] return s.users[id]
} }
// Who gets usernames out of a channel
func (s *Slack) Who(id string) []string {
log.Println("Who is queried for ", id)
u := s.url + "channels.info"
resp, err := http.PostForm(u,
url.Values{"token": {s.config.Slack.Token}, "channel": {id}})
if err != nil || resp.StatusCode != 200 {
log.Printf("Error posting user info request: %d %s",
resp.StatusCode, err)
return []string{}
}
defer resp.Body.Close()
var chanInfo slackChannelInfoResp
err = json.NewDecoder(resp.Body).Decode(&chanInfo)
if err != nil || !chanInfo.Ok {
log.Println("Error decoding response: ", err)
return []string{}
}
log.Printf("%#v", chanInfo.Channel)
handles := []string{}
for _, member := range chanInfo.Channel.Members {
handles = append(handles, s.getUser(member))
}
log.Printf("Returning %d handles", len(handles))
return handles
}