From 242b5cad81d097e54eeb963d747a61168e303a85 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Fri, 29 Oct 2021 21:55:19 +0200 Subject: [PATCH] add whois command --- pkg/api/ivr/whois.go | 61 +++++++++++++++++++++++++++++++++++++++++ pkg/commands/whois.go | 12 ++++++++ pkg/db/addchannel.go | 1 + pkg/db/typings.go | 6 ++++ pkg/handlers/command.go | 12 ++++++++ 5 files changed, 92 insertions(+) create mode 100644 pkg/api/ivr/whois.go create mode 100644 pkg/commands/whois.go diff --git a/pkg/api/ivr/whois.go b/pkg/api/ivr/whois.go new file mode 100644 index 0000000..bad4faf --- /dev/null +++ b/pkg/api/ivr/whois.go @@ -0,0 +1,61 @@ +package ivr + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + log "github.com/sirupsen/logrus" +) + +// https://api.ivr.fi +type whoisApiResponse struct { + Id string `json:"id"` + DisplayName string `json:"displayName"` + Login string `json:"login"` + Bio string `json:"bio"` + ChatColor string `json:"chatColor"` + Partner bool `json:"partner"` + // Affiliate bool `json:"affiliate"` + Bot bool `json:"bot"` + CreatedAt string `json:"createdAt"` + Roles roles + Error string `json:"error"` +} + +type roles struct { + IsAffiliate bool `json:"isAffiliate"` + IsPartner bool `json:"isPartner"` + IsSiteAdmin bool `json:"isSiteAdmin"` + IsStaff bool `json:"isStaff"` +} + +// Userid returns the userID of a given user +func Whois(username string) string { + baseUrl := "https://api.ivr.fi/twitch/resolve" + + resp, err := http.Get(fmt.Sprintf("%s/%s", baseUrl, username)) + if err != nil { + log.Error(err) + } + + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Error(err) + } + + var responseObject whoisApiResponse + json.Unmarshal(body, &responseObject) + + reply := fmt.Sprintf("User: %s, ID: %s, Color: %s, Partner: %v, Affiliate: %v, Staff: %v, Admin: %v, Bot: %v, Bio: %v", responseObject.DisplayName, responseObject.Id, responseObject.ChatColor, responseObject.Roles.IsPartner, responseObject.Roles.IsAffiliate, responseObject.Roles.IsStaff, responseObject.Roles.IsSiteAdmin, responseObject.Bot, responseObject.Bio) + + // User not found + if responseObject.Error != "" { + return fmt.Sprintf(responseObject.Error + " FeelsBadMan") + } else { + return reply + } +} diff --git a/pkg/commands/whois.go b/pkg/commands/whois.go new file mode 100644 index 0000000..a913b02 --- /dev/null +++ b/pkg/commands/whois.go @@ -0,0 +1,12 @@ +package commands + +import ( + "github.com/lyx0/nourybot/cmd/bot" + "github.com/lyx0/nourybot/pkg/api/ivr" +) + +func Whois(target, user string, nb *bot.Bot) { + reply := ivr.Whois(user) + + nb.Send(target, reply) +} diff --git a/pkg/db/addchannel.go b/pkg/db/addchannel.go index 7c2a4e7..066b989 100644 --- a/pkg/db/addchannel.go +++ b/pkg/db/addchannel.go @@ -26,6 +26,7 @@ func AddChannel(target, channelName string, nb *bot.Bot) { log.Error(insertErr) return } + nb.TwitchClient.Join(channelName) nb.Send(target, fmt.Sprintf("Joined %s", channelName)) } diff --git a/pkg/db/typings.go b/pkg/db/typings.go index 408eed3..d49888f 100644 --- a/pkg/db/typings.go +++ b/pkg/db/typings.go @@ -4,3 +4,9 @@ type Channel struct { Name string `bson:"name,omitempty"` Connect bool `bson:"connect,omitempty"` } + +// type Command struct { +// Name string `bson:"name,omitempty"` +// Text string `bson:"text,omitempty"` +// Channel string `bson:"channel,omitempty"` +// } diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index dea733f..67c7488 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -429,6 +429,18 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) { return } + case "whois": + if msgLen == 1 { + nb.Send(target, "Usage: ()whois [user]") + return + } else if message.User.ID != "31437432" { + nb.Send(target, "You are not authorized to do that.") + return + } else { + commands.Whois(target, cmdParams[1], nb) + return + } + case "xd": commands.Xd(target, nb) return