add http api
This commit is contained in:
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module git.sunturtle.xyz/zephyr/horsebot
|
|||||||
go 1.24.1
|
go 1.24.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117053525-5a1194358bf4
|
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117063336-b22b77c53578
|
||||||
github.com/disgoorg/disgo v0.19.0-rc.15
|
github.com/disgoorg/disgo v0.19.0-rc.15
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
16
go.sum
16
go.sum
@@ -1,17 +1,5 @@
|
|||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260116044610-b0c555f547e4 h1:YJEGZG/EnxE5Tr6EZMGxikXDF6QKKHrH9Bp1dLoWXkk=
|
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117063336-b22b77c53578 h1:FI54vRfCtesXfcwi4oHo86nB/x/2T+ZrZvyFVKiQSYA=
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260116044610-b0c555f547e4/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117063336-b22b77c53578/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117022630-19fb713aaa10 h1:soAAeJR/omxESc0ne1ItNxTpTv7ahyr4HbjU3PjEIvM=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117022630-19fb713aaa10/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117030110-cb1c51db05bb h1:iFPbu8i9sRakUnvRX9UzBd/RsVQkf6Nn3sOG/8F6QKM=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117030110-cb1c51db05bb/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117043014-a86aa0daebfe h1:fgTarB04AHOKsi/G50fszjCG1pV4u799pxf43zmJk/8=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117043014-a86aa0daebfe/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117050435-e6032f995f00 h1:L7h4Guiibjm7A0RK1/carM3CCsdOsOJwOqWBLgFF0H0=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117050435-e6032f995f00/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117053235-43d02b4b00ee h1:cURyTAeJbX7zaWiaWQZS8m5LZe+T4vv7GL6j400TKnc=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117053235-43d02b4b00ee/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117053525-5a1194358bf4 h1:T2nFw6bz4JrvWkoy4UsM2pWOC0ayXzbVr9Meg+HhT7w=
|
|
||||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260117053525-5a1194358bf4/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/disgoorg/disgo v0.19.0-rc.15 h1:x0NsV2gcbdjwuztsg2wYXw76p1Cpc8f6ByDrkPcfQtU=
|
github.com/disgoorg/disgo v0.19.0-rc.15 h1:x0NsV2gcbdjwuztsg2wYXw76p1Cpc8f6ByDrkPcfQtU=
|
||||||
|
|||||||
51
main.go
51
main.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@@ -18,20 +19,41 @@ import (
|
|||||||
"github.com/disgoorg/disgo/discord"
|
"github.com/disgoorg/disgo/discord"
|
||||||
"github.com/disgoorg/disgo/handler"
|
"github.com/disgoorg/disgo/handler"
|
||||||
"github.com/disgoorg/disgo/handler/middleware"
|
"github.com/disgoorg/disgo/handler/middleware"
|
||||||
|
"github.com/disgoorg/disgo/httpserver"
|
||||||
"github.com/disgoorg/disgo/rest"
|
"github.com/disgoorg/disgo/rest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
appID string
|
|
||||||
pubkey string
|
|
||||||
tokenFile string
|
tokenFile string
|
||||||
|
// http api options
|
||||||
|
addr string
|
||||||
|
route string
|
||||||
|
pubkey string
|
||||||
|
// logging options
|
||||||
|
level slog.Level
|
||||||
|
textfmt string
|
||||||
)
|
)
|
||||||
flag.StringVar(&appID, "id", "", "Discord application ID")
|
|
||||||
flag.StringVar(&pubkey, "key", "", "Discord public key")
|
|
||||||
flag.StringVar(&tokenFile, "token", "", "`file` containing the Discord bot token")
|
flag.StringVar(&tokenFile, "token", "", "`file` containing the Discord bot token")
|
||||||
|
flag.StringVar(&addr, "http", "", "`address` to bind HTTP API server")
|
||||||
|
flag.StringVar(&route, "route", "/interactions/callback", "`path` to serve HTTP API calls")
|
||||||
|
flag.StringVar(&pubkey, "key", "", "Discord public key")
|
||||||
|
flag.TextVar(&level, "log", slog.LevelInfo, "slog logging `level`")
|
||||||
|
flag.StringVar(&textfmt, "log-format", "text", "slog logging `format`, text or json")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
var lh slog.Handler
|
||||||
|
switch textfmt {
|
||||||
|
case "text":
|
||||||
|
lh = slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: level})
|
||||||
|
case "json":
|
||||||
|
lh = slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: level})
|
||||||
|
default:
|
||||||
|
fmt.Fprintf(os.Stderr, "invalid log format %q, must be text or json", textfmt)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
slog.SetDefault(slog.New(lh))
|
||||||
|
|
||||||
token, err := os.ReadFile(tokenFile)
|
token, err := os.ReadFile(tokenFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("reading token", slog.Any("err", err))
|
slog.Error("reading token", slog.Any("err", err))
|
||||||
@@ -51,8 +73,25 @@ func main() {
|
|||||||
r.ButtonComponent("/{id}", skillButton)
|
r.ButtonComponent("/{id}", skillButton)
|
||||||
})
|
})
|
||||||
|
|
||||||
slog.Info("connect", slog.String("disgo", disgo.Version))
|
opts := []bot.ConfigOpt{bot.WithDefaultGateway(), bot.WithEventListeners(r)}
|
||||||
client, err := disgo.New(string(token), bot.WithDefaultGateway(), bot.WithEventListeners(r))
|
if addr != "" {
|
||||||
|
if pubkey == "" {
|
||||||
|
slog.Error("Discord public key must be provided when using HTTP API")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
opts = append(opts, bot.WithHTTPServerConfigOpts(pubkey,
|
||||||
|
httpserver.WithAddress(addr),
|
||||||
|
httpserver.WithURL(route),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("connect",
|
||||||
|
slog.String("disgo", disgo.Version),
|
||||||
|
slog.Bool("http", addr != ""),
|
||||||
|
slog.String("address", addr),
|
||||||
|
slog.String("route", route),
|
||||||
|
)
|
||||||
|
client, err := disgo.New(string(token), opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("building bot", slog.Any("err", err))
|
slog.Error("building bot", slog.Any("err", err))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user