2021-01-27 17:43:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-06-10 23:48:32 +00:00
|
|
|
"context"
|
2021-01-27 17:43:38 +00:00
|
|
|
"log"
|
2022-10-02 02:40:19 +00:00
|
|
|
"math/rand"
|
2021-01-27 17:43:38 +00:00
|
|
|
"os"
|
2022-12-10 14:03:04 +00:00
|
|
|
"os/signal"
|
2022-10-02 02:40:19 +00:00
|
|
|
"time"
|
2021-01-27 17:43:38 +00:00
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/api"
|
2022-12-10 14:03:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/api/cmdroute"
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
2021-10-10 22:44:31 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/state"
|
2021-08-08 20:20:54 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
2021-01-27 17:43:38 +00:00
|
|
|
)
|
|
|
|
|
2022-10-02 02:40:19 +00:00
|
|
|
// To run, do `BOT_TOKEN="TOKEN HERE" go run .`
|
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
2021-01-27 17:43:38 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
token := os.Getenv("BOT_TOKEN")
|
|
|
|
if token == "" {
|
|
|
|
log.Fatalln("No $BOT_TOKEN given.")
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
h := newHandler(state.New("Bot " + token))
|
|
|
|
h.s.AddInteractionHandler(h)
|
2022-10-02 02:40:19 +00:00
|
|
|
h.s.AddIntents(gateway.IntentGuilds)
|
|
|
|
h.s.AddHandler(func(*gateway.ReadyEvent) {
|
|
|
|
me, _ := h.s.Me()
|
|
|
|
log.Println("connected to the gateway as", me.Tag())
|
|
|
|
})
|
|
|
|
|
2023-11-04 08:48:17 +00:00
|
|
|
if err := cmdroute.OverwriteCommands(h.s, commands); err != nil {
|
2022-10-02 02:40:19 +00:00
|
|
|
log.Fatalln("cannot update commands:", err)
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := h.s.Connect(ctx); err != nil {
|
2022-10-02 02:40:19 +00:00
|
|
|
log.Fatalln("cannot connect:", err)
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 17:43:38 +00:00
|
|
|
|
2022-10-02 02:40:19 +00:00
|
|
|
type handler struct {
|
2022-12-10 14:03:04 +00:00
|
|
|
*cmdroute.Router
|
2022-10-02 02:40:19 +00:00
|
|
|
s *state.State
|
|
|
|
}
|
2021-01-27 17:43:38 +00:00
|
|
|
|
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-10-02 02:40:19 +00:00
|
|
|
}
|
2021-01-27 17:43:38 +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-10-02 02:40:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-27 17:43:38 +00:00
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
func (h *handler) cmdEcho(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
|
2022-10-02 02:40:19 +00:00
|
|
|
var options struct {
|
|
|
|
Arg string `discord:"argument"`
|
2021-01-27 17:43:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 02:40:19 +00:00
|
|
|
if err := data.Options.Unmarshal(&options); err != nil {
|
|
|
|
return errorResponse(err)
|
2021-01-27 17:43:38 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 14:03:04 +00:00
|
|
|
return &api.InteractionResponseData{
|
|
|
|
Content: option.NewNullableString(options.Arg),
|
|
|
|
AllowedMentions: &api.AllowedMentions{}, // don't mention anyone
|
2021-01-27 17:43:38 +00:00
|
|
|
}
|
2022-10-02 02:40:19 +00:00
|
|
|
}
|
2021-01-27 17:43:38 +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-10-02 02:40:19 +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 */ },
|
2021-01-27 17:43:38 +00:00
|
|
|
}
|
|
|
|
}
|