From 2806c44bd225fceaaeb606b41ccdf5b5f6dd7055 Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Thu, 12 Mar 2026 12:46:52 +1300 Subject: [PATCH] initial commit --- COPYING | 13 +++++++++++++ go.mod | 3 +++ main.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 COPYING create mode 100644 go.mod create mode 100644 main.go diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..3e9c833 --- /dev/null +++ b/COPYING @@ -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. \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..50ca823 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.baxters.nz/jeremy/webhook + +go 1.25.7 diff --git a/main.go b/main.go new file mode 100644 index 0000000..0b597e7 --- /dev/null +++ b/main.go @@ -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 +}