all repos — paprika @ 5d7c1b2e1cb095849165e6c4b2c8ec469886b5a1

go rewrite of taigabot

plugins/lastfm.go (view raw)

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