add whois command

This commit is contained in:
lyx0 2021-10-29 21:55:19 +02:00
parent 8ccac2d892
commit 242b5cad81
5 changed files with 92 additions and 0 deletions

61
pkg/api/ivr/whois.go Normal file
View file

@ -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
}
}

12
pkg/commands/whois.go Normal file
View file

@ -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)
}

View file

@ -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))
}

View file

@ -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"`
// }

View file

@ -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