mineauth/_hooks/discord/discord.go

77 lines
1.2 KiB
Go

package main
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"net/http"
"os"
)
//go:embed webhook.txt
var webhookURL string
type profile struct {
ID string `json:"id"`
Name string `json:"name"`
IP string `json:"ip"`
}
func (p *profile) UUID() string {
return fmt.Sprintf("%s-%s-%s-%s-%s", p.ID[:8], p.ID[8:12], p.ID[12:16], p.ID[16:20], p.ID[20:32])
}
func main() {
var profile profile
if err := json.NewDecoder(os.Stdin).Decode(&profile); err != nil {
panic(err)
}
payloadHook := webhook{
Username: profile.Name,
Embeds: []embed{
{
Fields: []field{
{
Name: "UUID",
Value: profile.UUID(),
},
{
Name: "IP",
Value: profile.IP,
},
},
},
},
}
payload, err := json.Marshal(payloadHook)
if err != nil {
panic(err)
}
res, err := http.Post(webhookURL, "application/json", bytes.NewReader(payload))
if err != nil {
panic(err)
}
if res.StatusCode != http.StatusNoContent {
panic(fmt.Errorf("received non-200 status: %s", res.Status))
}
}
type webhook struct {
Username string `json:"username"`
Embeds []embed `json:"embeds"`
}
type embed struct {
Fields []field `json:"fields"`
}
type field struct {
Name string `json:"name"`
Value string `json:"value"`
}