From 1166fc732ec85ef2f0dea4095c6a8a29fc57083f Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Wed, 29 Apr 2020 17:36:34 -0400 Subject: [PATCH] admin: add setkey --- config/config.go | 8 ++++++++ plugins/admin/admin.go | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/config/config.go b/config/config.go index c75d72a..a818026 100644 --- a/config/config.go +++ b/config/config.go @@ -160,6 +160,14 @@ func (c *Config) Set(key, value string) error { return nil } +func (c *Config) SetMap(key string, values map[string]string) error { + b, err := json.Marshal(values) + if err != nil { + return err + } + return c.Set(key, string(b)) +} + func (c *Config) SetArray(key string, values []string) error { vals := strings.Join(values, ";;") return c.Set(key, vals) diff --git a/plugins/admin/admin.go b/plugins/admin/admin.go index 7e41916..80adab7 100644 --- a/plugins/admin/admin.go +++ b/plugins/admin/admin.go @@ -68,6 +68,7 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag } if strings.ToLower(body) == "reboot" { + p.bot.Send(conn, bot.Message, message.Channel, "brb") log.Info().Msgf("Got reboot command") os.Exit(0) } @@ -119,6 +120,15 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag } p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1])) return true + } else if parts[0] == "setkey" && len(parts) > 3 { + items := p.cfg.GetMap(parts[1], map[string]string{}) + items[parts[2]] = strings.Join(parts[3:], " ") + if err := p.cfg.SetMap(parts[1], items); err != nil { + p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set error: %s", err)) + return true + } + p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1])) + return true } if parts[0] == "get" && len(parts) == 2 && forbiddenKeys[parts[1]] {