all repos — paprika @ 5e7478268e4bce3b6747afe20282d3bd2fe0a7a5

go rewrite of taigabot

plugins/seen.go (view raw)

 1package plugins
 2
 3import (
 4	"fmt"
 5	"strings"
 6	"sync"
 7	"time"
 8
 9	"github.com/dustin/go-humanize"
10	"gopkg.in/irc.v3"
11)
12
13func init() {
14	Register(Seen{})
15}
16
17type Seen struct{}
18
19func (Seen) Triggers() []string {
20	return []string{".seen", ""}
21}
22
23var LastSeen sync.Map
24
25type LastSeenInfo struct {
26	// The last message the user sent.
27	Message string
28	// The last time this user was seen.
29	Time time.Time
30}
31
32func (Seen) Execute(m *irc.Message) (string, error) {
33	LastSeen.Store(m.Name, LastSeenInfo{
34		Message: m.Trailing(),
35		// We just saw the user, so.
36		Time: time.Now(),
37	})
38
39	if strings.HasPrefix(m.Params[1], ".seen") {
40		params := strings.Split(m.Trailing(), " ")
41		if len(params) == 1 {
42			return "Usage: .seen <nickname>", nil
43		}
44
45		if seen, ok := LastSeen.Load(params[1]); ok {
46			humanized := humanize.Time(seen.(LastSeenInfo).Time)
47
48			// Don't want "now ago".
49			if humanized != "now" {
50				humanized = humanized + " ago"
51			}
52
53			return fmt.Sprintf(
54				"\x02%s\x02 was last seen %s, saying: %s",
55				params[1], humanized,
56				seen.(LastSeenInfo).Message,
57			), nil
58		} else {
59			return "I have not seen " + params[1], nil
60		}
61	}
62
63	return "", NoReply
64}