change color command to take in a user parameter

This commit is contained in:
lyx0 2021-12-19 02:46:08 +01:00
parent 89509ee58a
commit 79f4ccb6b2
3 changed files with 101 additions and 11 deletions

53
\ Normal file
View file

@ -0,0 +1,53 @@
package commands
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/lyx0/nourybot/cmd/bot"
log "github.com/sirupsen/logrus"
)
// https://api.ivr.fi
type colorApiResponse struct {
Id string `json:"id"`
DisplayName string `json:"displayName"`
ChatColor string `json:"chatColor"`
Error string `json:"error"`
}
// Userid returns the userID of a given user
func Color(username, target string, nb *bot.Bot) 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 colorApiResponse
json.Unmarshal(body, &responseObject)
// time string format 2011-05-19T00:28:28.310449Z
// discard everything after T
reply := fmt.Sprintf("%s color is %s",
responseObject.DisplayName,
responseObject.ChatColor,
)
// User not found
if responseObject.Error != "" {
return fmt.Sprintf(responseObject.Error + " FeelsBadMan")
}
nb.Send(target, reply)
}

View file

@ -1,16 +1,53 @@
package commands package commands
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http"
"github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/cmd/bot"
log "github.com/sirupsen/logrus"
) )
// Color responds with a users Twitch username color. // https://api.ivr.fi
func Color(message twitch.PrivateMessage, nb *bot.Bot) { type colorApiResponse struct {
reply := fmt.Sprintf("@%v, your color is %v", message.User.DisplayName, message.User.Color) Id string `json:"id"`
DisplayName string `json:"displayName"`
nb.Send(message.Channel, reply) ChatColor string `json:"chatColor"`
Error string `json:"error"`
}
// Userid returns the userID of a given user
func Color(username, target string, nb *bot.Bot) {
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 colorApiResponse
json.Unmarshal(body, &responseObject)
// time string format 2011-05-19T00:28:28.310449Z
// discard everything after T
reply := fmt.Sprintf("%s color is %s",
responseObject.DisplayName,
responseObject.ChatColor,
)
// User not found
if responseObject.Error != "" {
nb.Send(target, "Something went wrong... FeelsBadMan")
}
nb.Send(target, reply)
} }

View file

@ -107,7 +107,7 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
return return
case "color": case "color":
commands.Color(message, nb) commands.Color(cmdParams[1], target, nb)
return return
case "commands": case "commands":
@ -246,9 +246,9 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
db.AddChannel(target, cmdParams[1], nb) db.AddChannel(target, cmdParams[1], nb)
return return
case "mycolor": //. case "mycolor":
commands.Color(message, nb) //. commands.Color(message, nb)
return //. return
case "nourybot": case "nourybot":
commands.Help(target, nb) commands.Help(target, nb)