all repos — paprika @ 4713a97cc5df6d8f4d9bd48f83fa1d8429ac58d4

go rewrite of taigabot

add intro command and JOIN handler. (#20)

Anthony DeDominic adedomin@gmail.com
Sat, 15 Jan 2022 21:13:05 -0500
commit

4713a97cc5df6d8f4d9bd48f83fa1d8429ac58d4

parent

0d44a1ca43ce1e4219583adbfc2ce3a534cd806b

2 files changed, 61 insertions(+), 7 deletions(-)

jump to
M main.gomain.go

@@ -12,12 +12,7 @@ "git.icyphox.sh/paprika/plugins"

"gopkg.in/irc.v3" ) -func handleChatMessage(c *irc.Client, m *irc.Message) { - // Trim leading and trailing spaces to not trip up our - // plugins. - m.Params[1] = strings.TrimSpace(m.Params[1]) - response, err := plugins.ProcessTrigger(m) - +func handleChatMessage(c *irc.Client, m *irc.Message, response string, err error) { if errors.Is(err, plugins.NoReply) { return }

@@ -59,8 +54,15 @@ func ircHandler(c *irc.Client, m *irc.Message) {

switch m.Command { case "001": c.Write(config.SplitChannelList(config.C.Channels)) + case "JOIN": + response, err := plugins.GetIntro(m) + handleChatMessage(c, m, response, err) case "PRIVMSG": - handleChatMessage(c, m) + // Trim leading and trailing spaces to not trip up our + // plugins. + m.Params[1] = strings.TrimSpace(m.Params[1]) + response, err := plugins.ProcessTrigger(m) + handleChatMessage(c, m, response, err) } }
A plugins/introduction.go

@@ -0,0 +1,52 @@

+package plugins + +import ( + "fmt" + "strings" + + "git.icyphox.sh/paprika/database" + "github.com/dgraph-io/badger/v3" + "gopkg.in/irc.v3" +) + +func init() { + Register(Introduction{}) +} + +type Introduction struct{} + +func (Introduction) Triggers() []string { + return []string{".intro"} +} + +func (Introduction) Execute(m *irc.Message) (string, error) { + param := strings.SplitN(m.Trailing(), " ", 2) + if len(param) != 2 { + intro, err := database.DB.Get([]byte(m.Name)) + if err == badger.ErrKeyNotFound { + return fmt.Sprintf("Usage: %s <intro text>", param[0]), nil + } else if err != nil { + return "Unknown Error Checking for your intro.", err + } else { + return string(intro), nil + } + } + + err := database.DB.Set([]byte(m.Name), []byte(param[1])) + if err != nil { + return "[Introduction] Failed to set introduction string.", nil + } + + return "", NoReply +} + +func GetIntro (m *irc.Message) (string, error) { + intro, err := database.DB.Get([]byte(m.Name)) + if err == badger.ErrKeyNotFound { + return "", NoReply + } else if err != nil { + return "", err + } else { + return string(intro), nil + } +}