all repos — paprika @ 7284194835942c15cd83b83e1247c7cf86cbd74e

go rewrite of taigabot

plugins/listenbrainz.go (view raw)

 1// Listenbrainz now-playing info.
 2package plugins
 3
 4import (
 5	"strings"
 6
 7	"git.icyphox.sh/taigobot/plugins/listenbrainz"
 8	"github.com/dgraph-io/badger/v3"
 9	"gopkg.in/irc.v3"
10)
11
12func init() {
13	Register(Listenbrainz{})
14}
15
16type Listenbrainz struct{}
17
18func (Listenbrainz) Triggers() []string {
19	return []string{".np", ".lbz"}
20}
21
22func (Listenbrainz) Execute(m *irc.Message) (string, error) {
23	parts := strings.SplitN(m.Trailing(), " ", 2)
24	trigger := parts[0]
25
26	switch trigger {
27	case ".lbz":
28		if len(parts) == 2 {
29			arg := parts[1]
30			err := listenbrainz.Setup(arg, m.Prefix.Name)
31			if err != nil {
32				return "Database error", err
33			}
34			return "Successfully set Listenbrainz username", nil
35		} else {
36			return "Usage: .lbz <username>", nil
37		}
38	case ".np":
39		user, err := listenbrainz.GetUser(m.Prefix.Name)
40		if err == badger.ErrKeyNotFound {
41			return "User not found. Set it using '.lbz <username>'", nil
42		} else if err != nil {
43			return "Database error", err
44		}
45
46		np, err := listenbrainz.NowPlaying(user)
47		if err != nil {
48			return "Listenbrainz error", err
49		}
50
51		return np, nil
52	}
53
54	return "", nil
55}