all repos — paprika @ seen-cmd

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/paprika/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	// TODO: removing .np from here until we figure out how
20	// it can co-exist with Last.fm
21	return []string{".lbz"}
22}
23
24func (Listenbrainz) Execute(m *irc.Message) (string, error) {
25	parts := strings.SplitN(m.Trailing(), " ", 2)
26	trigger := parts[0]
27
28	switch trigger {
29	case ".lbz":
30		if len(parts) == 2 {
31			arg := parts[1]
32			err := listenbrainz.Setup(arg, m.Prefix.Name)
33			if err != nil {
34				return "Database error", err
35			}
36			return "Successfully set Listenbrainz username", nil
37		} else {
38			return "Usage: .lbz <username>", nil
39		}
40	case ".np":
41		user, err := listenbrainz.GetUser(m.Prefix.Name)
42		if err == badger.ErrKeyNotFound {
43			return "User not found. Set it using '.lbz <username>'", nil
44		} else if err != nil {
45			return "Database error", err
46		}
47
48		np, err := listenbrainz.NowPlaying(user)
49		if err != nil {
50			return "Listenbrainz error", err
51		}
52
53		return np, nil
54	}
55
56	panic("Unreachable!")
57}