Browse Source

Initial Commit

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main v0.0.1
jolheiser 1 year ago
commit
92856c2e34
Signed by: jolheiser GPG Key ID: B853ADA5DA7BBF7A
  1. 2
      .gitignore
  2. 7
      LICENSE
  3. 9
      Makefile
  4. 11
      README.md
  5. 39
      embed.go
  6. 52
      fields.go
  7. 3
      go.mod
  8. 17
      mention.go
  9. 27
      webhook.go
  10. 71
      webhook_test.go

2
.gitignore

@ -0,0 +1,2 @@
# GoLand
.idea/

7
LICENSE

@ -0,0 +1,7 @@
Copyright 2020 John Olheiser
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

9
Makefile

@ -0,0 +1,9 @@
GO ?= go
.PHONY: fmt
fmt:
$(GO) fmt ./...
.PHONY: test
test:
$(GO) test -race ./...

11
README.md

@ -0,0 +1,11 @@
# Disco
A Discord webhook library. That's it.
Not a full REST API, not a bot maker. Just a webhook/embed library.
[example](webhook_test.go)
## License
[MIT](LICENSE)

39
embed.go

@ -0,0 +1,39 @@
package disco
import "time"
// Type is the type of embed
type Type string
// Embed Type
const (
TypeRich Type = "rich"
TypeImage Type = "image"
TypeVideo Type = "video"
TypeGifv Type = "gifv"
TypeArticle Type = "article"
TypeLink Type = "link"
)
// An Embed is sent in a message or webhook
type Embed struct {
Title string `json:"title,omitempty"`
Type Type `json:"type,omitempty"`
Description string `json:"description,omitempty"`
URL string `json:"url,omitempty"`
Timestamp *time.Time `json:"timestamp,omitempty"`
Color int `json:"color,omitempty"`
Footer *Footer `json:"footer,omitempty"`
Image *Image `json:"image,omitempty"`
Thumbnail *Thumbnail `json:"thumbnail,omitempty"`
Video *Video `json:"video,omitempty"`
Provider *Provider `json:"provider,omitempty"`
Author *Author `json:"author,omitempty"`
Fields []*Field `json:"fields,omitempty"`
}
// Now just wraps time.Now and returns a pointer that works with Embed.Timestamp
func Now() *time.Time {
now := time.Now()
return &now
}

52
fields.go

@ -0,0 +1,52 @@
package disco
// Author is the author of an embed
type Author struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
IconURL string `json:"icon_url,omitempty"`
ProxyIconURL string `json:"proxy_icon_url,omitempty"`
}
// Field is a field in an embed
type Field struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
Inline bool `json:"inline,omitempty"`
}
// Footer is the footer of an embed
type Footer struct {
Text string `json:"text,omitempty"`
IconURL string `json:"icon_url,omitempty"`
ProxyIconURL string `json:"proxy_icon_url,omitempty"`
}
// Image is the image of an embed
type Image struct {
URL string `json:"url,omitempty"`
ProxyURL string `json:"proxy_url,omitempty"`
Height int `json:"height,omitempty"`
Width int `json:"width,omitempty"`
}
// Provider is the provider of an embed
type Provider struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
}
// Thumbnail is the thumbnail of an embed
type Thumbnail struct {
URL string `json:"url,omitempty"`
ProxyURL string `json:"proxy_url,omitempty"`
Height int `json:"height,omitempty"`
Width int `json:"width,omitempty"`
}
// Video is the video in an embed
type Video struct {
URL string `json:"url,omitempty"`
Height int `json:"height,omitempty"`
Width int `json:"width,omitempty"`
}

3
go.mod

@ -0,0 +1,3 @@
module go.jolheiser.com/disco
go 1.15

17
mention.go

@ -0,0 +1,17 @@
package disco
// Parse is the parse types of AllowedMentions
type Parse string
const (
Roles Parse = "roles"
Users Parse = "users"
Everyone Parse = "everyone"
)
// AllowedMentions is the allowed mentions to parse in a Webhook
type AllowedMentions struct {
Parse []Parse `json:"parse"`
Roles []string `json:"roles,omitempty"`
Users []string `json:"users,omitempty"`
}

27
webhook.go

@ -0,0 +1,27 @@
package disco
import (
"bytes"
"encoding/json"
"net/http"
)
// Webhook is a Discord webhook
type Webhook struct {
Content string `json:"content"`
Username string `json:"username,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
TTS bool `json:"tts,omitempty"`
Embeds []*Embed `json:"embeds,omitempty"`
AllowedMentions *AllowedMentions `json:"allowed_mentions,omitempty"`
}
// Send is a basic convenience for minimum effort
func (w *Webhook) Send(webhookURL string) (*http.Response, error) {
payload, err := json.Marshal(w)
if err != nil {
return nil, err
}
return http.Post(webhookURL, "application/json", bytes.NewBuffer(payload))
}

71
webhook_test.go

@ -0,0 +1,71 @@
package disco
import (
"os"
"testing"
)
func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func TestWebhook(t *testing.T) {
u := os.Getenv("DISCO_WEBHOOK")
if u == "" {
return
}
webhook := Webhook{
Content: "@everyone Webhook Content! (No Ping)",
Username: "Disco",
AvatarURL: "https://gitea.com/user/avatar/jolheiser/-1",
Embeds: []*Embed{
{
URL: "https://gitea.com/jolheiser/disco",
Type: TypeRich,
Title: "Disco Webhook",
Description: "A webhook created with disco",
Timestamp: Now(),
Color: 0x007D96,
Author: &Author{
Name: "jolheiser",
URL: "https://gitea.com/jolheiser",
IconURL: "https://gitea.com/user/avatar/jolheiser/-1",
},
Footer: &Footer{
Text: "Webhook Footer",
IconURL: "https://gitea.com/user/avatar/jolheiser/-1",
},
Image: &Image{
URL: "https://gitea.com/user/avatar/jolheiser/-1",
},
Fields: []*Field{
{
Name: "Library",
Value: "Disco",
Inline: true,
},
{
Name: "Usage",
Value: "Discord embeds/webhooks",
Inline: true,
},
},
},
},
AllowedMentions: &AllowedMentions{
Parse: []Parse{Users},
},
}
resp, err := webhook.Send(u)
if err != nil {
t.Log(err)
t.FailNow()
}
if resp.StatusCode != 204 {
t.Logf("Non-204 status returned: %d", resp.StatusCode)
t.FailNow()
}
}
Loading…
Cancel
Save