From 80587d4a1e512015304e50a81a1383dc555aeb69 Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Tue, 31 Mar 2020 21:34:18 +0000 Subject: [PATCH] update set/list to config commands --- commands.go | 33 +++++++++++++++++++++++++++------ handlers.go | 11 +++-------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/commands.go b/commands.go index 35b5b01..4906e24 100644 --- a/commands.go +++ b/commands.go @@ -76,19 +76,38 @@ func (b *bot) handleFeedback(m chat1.MsgSummary) { } } +// handleConfigCommand dispatches config calls +func (b *bot) handleConfigCommand(m chat1.MsgSummary) { + args := strings.Fields(strings.ToLower(m.Content.Text.Body)) + if args[1] != "config" { + return + } + if len(args) >= 3 { + switch args[2] { + case "set": + b.handleSetCommand(m) + return + case "list": + b.handleListCommand(m) + return + } + } +} + // handleSetCommand processes all settings SET calls +// this should be called from b.handleConfigCommand() func (b *bot) handleSetCommand(m chat1.MsgSummary) { - b.debug("%s called set command in %s", m.Sender.Username, m.ConvID) // first normalize the text and extract the arguments args := strings.Fields(strings.ToLower(m.Content.Text.Body)) - if args[1] != "set" { + if args[2] != "set" { return } + b.debug("config set called by @%s in %s", m.Sender.Username, m.ConvID) switch len(args) { - case 4: - if args[2] == "url" { + case 5: + if args[3] == "url" { // first validate the URL - u, err := url.ParseRequestURI(args[3]) + u, err := url.ParseRequestURI(args[4]) if err != nil { eid := b.logError(err) b.k.ReactByConvID(m.ConvID, m.Id, "Error ID %s", eid) @@ -129,10 +148,12 @@ func (b *bot) handleSetCommand(m chat1.MsgSummary) { } // handleListCommand lists settings for the conversation +// this should be called from b.handleConfigCommand() func (b *bot) handleListCommand(m chat1.MsgSummary) { // first normalize the text and extract the arguments args := strings.Fields(strings.ToLower(m.Content.Text.Body)) - if args[0] != "list" { + if args[2] != "list" { return } + b.debug("config list called by @%s in %s", m.Sender.Username, m.ConvID) } diff --git a/handlers.go b/handlers.go index 3a60446..eec82e0 100644 --- a/handlers.go +++ b/handlers.go @@ -68,14 +68,9 @@ func (b *bot) chatHandler(m chat1.MsgSummary) { b.handleWelcome(m.ConvID) return } - // set commands - if hasCommandPrefix(m.Content.Text.Body, b.cmd(), b.k.Username, "set") { - b.handleSetCommand(m) - return - } - // list commands - if hasCommandPrefix(m.Content.Text.Body, b.cmd(), b.k.Username, "list") { - b.handleSetCommand(m) + // config commands + if hasCommandPrefix(m.Content.Text.Body, b.cmd(), b.k.Username, "config") { + b.handleConfigCommand(m) return } }