make importable library

This commit is contained in:
Jeremy Baxter 2026-03-12 13:01:46 +13:00
parent 2806c44bd2
commit ca0327561b

56
main.go
View file

@ -1,56 +0,0 @@
// 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
}