From cb592edddf5245a340bb3551b4a8fc1703f830b4 Mon Sep 17 00:00:00 2001 From: Daniel Aberger Date: Sun, 24 Apr 2016 14:20:20 +0200 Subject: [PATCH] bugfixes and slash command implementation --- main.go | 2 ++ modules/modules.go | 2 ++ modules/twitch.go | 26 +++++++++++++++++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 46030b4..b4eea05 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,8 @@ func webhookHandler(w http.ResponseWriter, req *http.Request) { UserName: req.Form.Get("user_name"), Text: req.Form.Get("text"), TriggerWord: req.Form.Get("trigger_word"), + Command: req.Form.Get("command"), + ResponseURL: req.Form.Get("response_url"), } modules.HandleMessage(&whp) } diff --git a/modules/modules.go b/modules/modules.go index 4784fda..32397ea 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -24,6 +24,8 @@ type WebhookPayload struct { TriggerWord string `json:"trigger_word"` UserID string `json:"user_id"` UserName string `json:"user_name"` + Command string `json:"command"` + ResponseURL string `json:"response_url"` } var ( diff --git a/modules/twitch.go b/modules/twitch.go index ea29a75..d35e953 100644 --- a/modules/twitch.go +++ b/modules/twitch.go @@ -144,17 +144,29 @@ func twitchHandleMessage(payload *WebhookPayload) { if len(tok) < 1 { return } + if payload.Command == "/twitch" { + log.Print("tok: ") + log.Println(tok) + handleCommand(tok, "@"+payload.UserName, true) + } switch tok[0] { case "!twitch": - handleCommand(tok, "#"+payload.ChannelName) - case "/twitch": - handleCommand(tok, "@"+payload.UserName) + handleCommand(tok, "#"+payload.ChannelName, false) default: } } -func handleCommand(tok []string, target string) { +func handleCommand(tok []string, target string, isSlashCommand bool) { + var tokMod int + if isSlashCommand { + tokMod = 1 + if tok[1-tokMod] == "" { + tok = make([]string, 0, 0) + } + } else { + tokMod = 0 + } switch len(tok) { - case 1: + case 1 - tokMod: onlinestreams := 0 for streamname, _ := range twitch { var so TwitchStreamObject @@ -172,8 +184,8 @@ func handleCommand(tok []string, target string) { if onlinestreams == 0 { SayCh <- GeneratePayload(target, "", "All streams offline", "Twitch_Bot") } - case 2: - streamname := tok[1] + case 2 - tokMod: + streamname := tok[1-tokMod] var so TwitchStreamObject var co TwitchChannelObject so = getTwitchStreamObject(streamname)