all repos — paprika @ 4c9cf50d74fcdfe5ee2cbc65607417058e874e80

go rewrite of taigabot

plugins/profile.go (view raw)

 1// User profile plugins: desktop, homescreen, selfie, battlestation, etc.
 2package plugins
 3
 4import (
 5	"fmt"
 6	"strings"
 7
 8	"git.icyphox.sh/paprika/database"
 9	"gopkg.in/irc.v3"
10)
11
12func init() {
13	Register(Profile{})
14}
15
16type Profile struct{}
17
18func (Profile) Triggers() []string {
19	return []string{
20		".selfie",
21		".desktop", ".dt",
22		".hs", ".homescreen", ".home",
23		".bs", ".battlestation", ".keyb",
24	}
25}
26
27func (Profile) Execute(m *irc.Message) (string, error) {
28	parts := strings.SplitN(m.Trailing(), " ", 2)
29	trigger := parts[0]
30
31	var key string
32
33	switch trigger {
34	case ".desktop", ".dt":
35		key = "desktop"
36	case ".hs", ".homescreen", ".home":
37		key = "homescreen"
38	case ".bs", ".battlestation", ".keyb":
39		key = "battlestation"
40	default:
41		// Strip the '.'
42		key = trigger[1:]
43	}
44
45	if len(parts) == 1 {
46		val, err := database.DB.Get([]byte(fmt.Sprintf(
47			"%s/%s",
48			key,
49			strings.ToLower(m.Prefix.Name),
50		)))
51		if err != nil {
52			return fmt.Sprintf(
53				"Error fetching %s. Use '%s <link>' to set it.", key, trigger,
54			), err
55		}
56		return fmt.Sprintf("\x02%s\x02: %s", m.Prefix.Name, string(val)), nil
57	} else if len(parts) == 2 {
58		// Querying @nick's thing.
59		if strings.HasPrefix(parts[1], "@") {
60			val, err := database.DB.Get([]byte(fmt.Sprintf(
61				"%s/%s",
62				key,
63				parts[1][1:],
64			)))
65			if err != nil {
66				return fmt.Sprintf("Error fetching %s", key), err
67			}
68			return fmt.Sprintf("\x02%s\x02: %s", parts[1][1:], string(val)), nil
69		}
70		// User wants to set the thing.
71		value := parts[1]
72
73		err := database.DB.Set(
74			[]byte(fmt.Sprintf("%s/%s", key, strings.ToLower(m.Prefix.Name))),
75			[]byte(value),
76		)
77		if err != nil {
78			return fmt.Sprintf("Error saving %s", key), err
79		}
80		return fmt.Sprintf("Saved your %s successfully", key), nil
81	}
82	return "", nil
83}