all repos — paprika @ ed37b272f5791d139f3031bd35c8b3c41592f356

go rewrite of taigabot

plugins/introduction.go (view raw)

 1package plugins
 2
 3import (
 4	"fmt"
 5	"strings"
 6
 7	"git.icyphox.sh/paprika/database"
 8	"github.com/dgraph-io/badger/v3"
 9	"gopkg.in/irc.v3"
10)
11
12func init() {
13	Register(Introduction{})
14}
15
16type Introduction struct{}
17
18func (Introduction) Triggers() []string {
19	return []string{".intro"}
20}
21
22func (Introduction) Execute(m *irc.Message) (string, error) {
23	param := strings.SplitN(m.Trailing(), " ", 2)
24	userKey := database.ToKey("introduction", m.Name)
25	if len(param) != 2 {
26		intro, err := database.DB.Get(userKey)
27		if err == badger.ErrKeyNotFound {
28			return fmt.Sprintf("Usage: %s <intro text>", param[0]), nil
29		} else if err != nil {
30			return "Unknown Error Checking for your intro.", err
31		} else {
32			return string(intro), nil
33		}
34	}
35
36	err := database.DB.Set(userKey, []byte(param[1]))
37	if err != nil {
38		return "[Introduction] Failed to set introduction string.", nil
39	}
40
41	return "", NoReply
42}
43
44func GetIntro (m *irc.Message) (string, error) {
45	intro, err := database.DB.Get(database.ToKey("introduction", m.Name))
46	if err == badger.ErrKeyNotFound {
47		return "", NoReply
48	} else if err != nil {
49		return "", err
50	} else {
51		return string(intro), nil
52	}
53}