This repository has been archived on 2022-03-09. You can view files and clone it, but cannot push or open issues or pull requests.
snoo2nebby/discordwebhook.go

61 lines
1.3 KiB
Go

package main
import (
"bytes"
"encoding/json"
"net/http"
)
// Webhook is the parent structure fired off to Discord.
type Webhook struct {
Embeds []Embed `json:"embeds,omitifempty"`
}
// EmbedField is an individual field being embedded in a message.
type EmbedField struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline"`
}
// EmbedFooter is the message footer.
type EmbedFooter struct {
Text string `json:"text"`
IconURL string `json:"icon_url"`
}
// Embed is a set of embedded fields and a footer.
type Embed struct {
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
Fields []EmbedField `json:"fields"`
Footer EmbedFooter `json:"footer"`
}
// Send returns a request for a Discord webhook.
func Send(whurl string, w Webhook) *http.Request {
data, err := json.Marshal(&w)
if err != nil {
panic(err)
}
req, err := http.NewRequest(http.MethodPost, whurl, bytes.NewBuffer(data))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
return req
}
// Validate validates the response from Discord.
func Validate(resp *http.Response) error {
if resp.StatusCode != http.StatusOK {
return NewError(http.StatusOK, resp)
}
return nil
}