From dab9ff05aa0c7b0bddfdfe19c78fcaaab9d2094b Mon Sep 17 00:00:00 2001 From: lyx0 Date: Mon, 18 Oct 2021 20:33:51 +0200 Subject: [PATCH] add userid command --- pkg/api/ivr/userid.go | 39 +++++++++++++++++++++++++++++++++++++++ pkg/commands/userid.go | 11 +++++++++++ pkg/handlers/command.go | 16 ++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 pkg/api/ivr/userid.go create mode 100644 pkg/commands/userid.go diff --git a/pkg/api/ivr/userid.go b/pkg/api/ivr/userid.go new file mode 100644 index 0000000..0e78323 --- /dev/null +++ b/pkg/api/ivr/userid.go @@ -0,0 +1,39 @@ +package ivr + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" +) + +// https://api.ivr.fi +type uidApiResponse struct { + Id string `json:"id"` + Error string `json:"error"` +} + +func Userid(username string) string { + resp, err := http.Get(fmt.Sprintf("https://api.ivr.fi/twitch/resolve/%s", username)) + if err != nil { + log.Fatalln(err) + } + + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + + var responseObject uidApiResponse + json.Unmarshal(body, &responseObject) + + // User not found + if responseObject.Error != "" { + return fmt.Sprintf(responseObject.Error + " FeelsBadMan") + } else { + return fmt.Sprintf(responseObject.Id) + } +} diff --git a/pkg/commands/userid.go b/pkg/commands/userid.go new file mode 100644 index 0000000..2cdaf0e --- /dev/null +++ b/pkg/commands/userid.go @@ -0,0 +1,11 @@ +package commands + +import ( + "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/pkg/api/ivr" +) + +func Userid(channel string, target string, client *twitch.Client) { + reply := ivr.Userid(target) + client.Say(channel, reply) +} diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index c902abd..8a1a164 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -220,6 +220,22 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u commands.Uptime(message.Channel, cmdParams[1], twitchClient) return } + case "uid": + if msgLen == 1 { + twitchClient.Say(message.Channel, "Usage: ()userid [username]") + return + } else { + commands.Userid(message.Channel, cmdParams[1], twitchClient) + return + } + case "userid": + if msgLen == 1 { + twitchClient.Say(message.Channel, "Usage: ()userid [username]") + return + } else { + commands.Userid(message.Channel, cmdParams[1], twitchClient) + return + } case "weather": if msgLen == 1 { twitchClient.Say(message.Channel, "Usage: ()weather [location]")