initial commit

This commit is contained in:
Jeremy Baxter 2026-03-12 12:46:52 +13:00
commit 2806c44bd2
3 changed files with 72 additions and 0 deletions

13
COPYING Normal file
View file

@ -0,0 +1,13 @@
Copyright (c) 2026 Jeremy Baxter. All rights reserved.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module git.baxters.nz/jeremy/webhook
go 1.25.7

56
main.go Normal file
View file

@ -0,0 +1,56 @@
// webhook: execute a Discord webhook
// Copyright (c) 2026 Jeremy Baxter. All rights reserved.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package main
import (
"errors"
"strings"
"encoding/json"
"net/http"
)
var baseURL = "https://discord.com/api/webhooks/"
type Embed struct {
Title string `json:"title"`
Text string `json:"description"`
Type string `json:"type"`
}
type WebhookRequest struct {
Name string `json:"username"`
Embeds [1]Embed `json:"embeds"`
}
func Execute(suffix, username, title, text string) (err error) {
embed := Embed{title, text, "rich"}
w := WebhookRequest{username, [1]Embed{embed}}
body, err := json.Marshal(w)
if err != nil {
return
}
resp, err := http.Post(baseURL + suffix, "application/json",
strings.NewReader(string(body)))
if resp.StatusCode < 200 || resp.StatusCode > 299 {
err = errors.New(resp.Status)
}
return
}