all repos — paprika @ 1082dd587023cb5c21c88497d9a0e3665915bae2

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
25func (Seen) Execute(m *irc.Message) (string, error) {
26	// we just saw this user so.
27	LastSeen.Store(m.Name, time.Now())
28
29	if strings.HasPrefix(m.Params[1], ".seen") {
30		params := strings.Split(m.Trailing(), " ")
31		if len(params) == 1 {
32			return ".seen nickname", nil
33		}
34
35		if seen, ok := LastSeen.Load(params[1]); ok {
36			return fmt.Sprintf(
37				"\x02%s\x02 was last seen: %s",
38				params[1], humanize.Time(seen.(time.Time)),
39			), nil
40		} else {
41			return "I have not seen " + params[1], nil
42		}
43	}
44
45	return "", NoReply
46}