2022-08-23 06:45:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/diamondburned/arikawa/v3/api"
|
2022-12-10 14:03:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/api/cmdroute"
|
2022-08-23 06:45:07 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/api/webhook"
|
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
|
|
|
)
|
|
|
|
|
2022-10-02 02:40:19 +00:00
|
|
|
var commands = []api.CreateCommandData{
|
|
|
|
{
|
|
|
|
Name: "ping",
|
|
|
|
Description: "ping pong!",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "echo",
|
|
|
|
Description: "echo back the argument",
|
|
|
|
Options: []discord.CommandOption{
|
|
|
|
&discord.StringOption{
|
|
|
|
OptionName: "argument",
|
|
|
|
Description: "what's echoed back",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "thonk",
|
|
|
|
Description: "biiiig thonk",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-08-23 06:45:07 +00:00
|
|
|
func main() {
|
|
|
|
token := os.Getenv("BOT_TOKEN")
|
|
|
|
if token == "" {
|
|
|
|
log.Fatalln("No $BOT_TOKEN given.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
webhookAddr = os.Getenv("WEBHOOK_ADDR")
|
|
|
|
webhookPubkey = os.Getenv("WEBHOOK_PUBKEY")
|
|
|
|
)
|
|
|
|
|
|
|
|
if webhookAddr != "" {
|
2022-12-10 14:03:04 +00:00
|
|
|
state := state.NewAPIOnlyState(token, nil)
|
2022-08-23 06:45:07 +00:00
|
|
|
|
2023-11-04 08:48:17 +00:00
|
|
|
if err := cmdroute.OverwriteCommands(state, commands); err != nil {
|
|
|
|
log.Fatalln("cannot update commands:", err)
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
h := newHandler(state)
|
2022-08-23 06:45:07 +00:00
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
if err := overwriteCommands(state); err != nil {
|
2022-08-23 06:45:07 +00:00
|
|
|
log.Fatalln("cannot update commands:", err)
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
srv, err := webhook.NewInteractionServer(webhookPubkey, h)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("cannot create interaction server:", err)
|
|
|
|
}
|
|
|
|
|
2022-08-23 06:45:07 +00:00
|
|
|
log.Println("listening and serving at", webhookAddr+"/")
|
|
|
|
log.Fatalln(http.ListenAndServe(webhookAddr, srv))
|
|
|
|
} else {
|
2022-12-10 14:03:04 +00:00
|
|
|
state := state.New("Bot " + token)
|
|
|
|
state.AddIntents(gateway.IntentGuilds)
|
|
|
|
state.AddHandler(func(*gateway.ReadyEvent) {
|
|
|
|
me, _ := state.Me()
|
2022-08-23 06:45:07 +00:00
|
|
|
log.Println("connected to the gateway as", me.Tag())
|
|
|
|
})
|
|
|
|
|
2023-11-04 08:48:17 +00:00
|
|
|
if err := cmdroute.OverwriteCommands(state, commands); err != nil {
|
2022-08-23 06:45:07 +00:00
|
|
|
log.Fatalln("cannot update commands:", err)
|
|
|
|
}
|
|
|
|
|
2023-11-04 08:48:17 +00:00
|
|
|
h := newHandler(state)
|
|
|
|
state.AddInteractionHandler(h)
|
|
|
|
|
2022-08-23 06:45:07 +00:00
|
|
|
if err := h.s.Connect(context.Background()); err != nil {
|
|
|
|
log.Fatalln("cannot connect:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func overwriteCommands(s *state.State) error {
|
2022-12-10 14:03:04 +00:00
|
|
|
return cmdroute.OverwriteCommands(s, commands)
|
2022-08-23 06:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type handler struct {
|
2022-12-10 14:03:04 +00:00
|
|
|
*cmdroute.Router
|
2022-08-23 06:45:07 +00:00
|
|
|
s *state.State
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
func newHandler(s *state.State) *handler {
|
|
|
|
h := &handler{s: s}
|
|
|
|
|
|
|
|
h.Router = cmdroute.NewRouter()
|
|
|
|
// Automatically defer handles if they're slow.
|
|
|
|
h.Use(cmdroute.Deferrable(s, cmdroute.DeferOpts{}))
|
|
|
|
h.AddFunc("ping", h.cmdPing)
|
|
|
|
h.AddFunc("echo", h.cmdEcho)
|
|
|
|
h.AddFunc("thonk", h.cmdThonk)
|
|
|
|
|
|
|
|
return h
|
2022-08-23 06:45:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
func (h *handler) cmdPing(ctx context.Context, cmd cmdroute.CommandData) *api.InteractionResponseData {
|
|
|
|
return &api.InteractionResponseData{
|
|
|
|
Content: option.NewNullableString("Pong!"),
|
2022-08-23 06:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
func (h *handler) cmdEcho(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
|
2022-08-23 06:45:07 +00:00
|
|
|
var options struct {
|
|
|
|
Arg string `discord:"argument"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := data.Options.Unmarshal(&options); err != nil {
|
|
|
|
return errorResponse(err)
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
return &api.InteractionResponseData{
|
|
|
|
Content: option.NewNullableString(options.Arg),
|
|
|
|
AllowedMentions: &api.AllowedMentions{}, // don't mention anyone
|
2022-08-23 06:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
func (h *handler) cmdThonk(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
|
|
|
|
time.Sleep(time.Duration(3+rand.Intn(5)) * time.Second)
|
|
|
|
return &api.InteractionResponseData{
|
|
|
|
Content: option.NewNullableString("https://tenor.com/view/thonk-thinking-sun-thonk-sun-thinking-sun-gif-14999983"),
|
2022-08-23 06:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
func errorResponse(err error) *api.InteractionResponseData {
|
|
|
|
return &api.InteractionResponseData{
|
|
|
|
Content: option.NewNullableString("**Error:** " + err.Error()),
|
|
|
|
Flags: discord.EphemeralMessage,
|
|
|
|
AllowedMentions: &api.AllowedMentions{ /* none */ },
|
2022-08-23 06:45:07 +00:00
|
|
|
}
|
|
|
|
}
|