all repos — paprika @ 1c51f1922297e7136d39f49dc2b7d002a25da1e4

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	if len(param) != 2 {
25		intro, err := database.DB.Get([]byte(m.Name))
26		if err == badger.ErrKeyNotFound {
27			return fmt.Sprintf("Usage: %s <intro text>", param[0]), nil
28		} else if err != nil {
29			return "Unknown Error Checking for your intro.", err
30		} else {
31			return string(intro), nil
32		}
33	}
34
35	err := database.DB.Set([]byte(m.Name), []byte(param[1]))
36	if err != nil {
37		return "[Introduction] Failed to set introduction string.", nil
38	}
39
40	return "", NoReply
41}
42
43func GetIntro (m *irc.Message) (string, error) {
44	intro, err := database.DB.Get([]byte(m.Name))
45	if err == badger.ErrKeyNotFound {
46		return "", NoReply
47	} else if err != nil {
48		return "", err
49	} else {
50		return string(intro), nil
51	}
52}