2021-05-12 05:36:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-06-10 23:48:32 +00:00
|
|
|
"context"
|
2021-05-12 05:36:03 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/api"
|
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v3/session"
|
2021-08-08 20:20:54 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
2021-05-12 05:36:03 +00:00
|
|
|
)
|
|
|
|
|
2021-10-03 20:31:48 +00:00
|
|
|
// To run, do `GUILD_ID="GUILD ID" BOT_TOKEN="TOKEN HERE" go run .`
|
2021-05-12 05:36:03 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
guildID := discord.GuildID(mustSnowflakeEnv("GUILD_ID"))
|
|
|
|
|
|
|
|
token := os.Getenv("BOT_TOKEN")
|
|
|
|
if token == "" {
|
|
|
|
log.Fatalln("No $BOT_TOKEN given.")
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := session.New("Bot " + token)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Session failed:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-03 20:31:48 +00:00
|
|
|
app, err := s.CurrentApplication()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed to get application ID:", err)
|
|
|
|
}
|
|
|
|
appID := app.ID
|
|
|
|
|
2021-05-12 05:36:03 +00:00
|
|
|
s.AddHandler(func(e *gateway.InteractionCreateEvent) {
|
2021-09-09 23:06:44 +00:00
|
|
|
if e.Type == discord.CommandInteraction {
|
2021-05-12 05:36:03 +00:00
|
|
|
// Send a message with a button back on slash commands.
|
|
|
|
data := api.InteractionResponse{
|
|
|
|
Type: api.MessageInteractionWithSource,
|
|
|
|
Data: &api.InteractionResponseData{
|
2021-08-08 20:20:54 +00:00
|
|
|
Content: option.NewNullableString("This is a message with a button!"),
|
|
|
|
Components: &[]discord.Component{
|
2021-09-09 23:06:44 +00:00
|
|
|
&discord.ActionRowComponent{
|
2021-05-12 05:36:03 +00:00
|
|
|
Components: []discord.Component{
|
2021-09-09 23:06:44 +00:00
|
|
|
&discord.ButtonComponent{
|
2021-05-12 05:36:03 +00:00
|
|
|
Label: "Hello World!",
|
|
|
|
CustomID: "first_button",
|
|
|
|
Emoji: &discord.ButtonEmoji{
|
|
|
|
Name: "👋",
|
|
|
|
},
|
|
|
|
Style: discord.PrimaryButton,
|
|
|
|
},
|
2021-09-09 23:06:44 +00:00
|
|
|
&discord.ButtonComponent{
|
2021-05-12 05:36:03 +00:00
|
|
|
Label: "Secondary",
|
|
|
|
CustomID: "second_button",
|
|
|
|
Style: discord.SecondaryButton,
|
|
|
|
},
|
2021-09-09 23:06:44 +00:00
|
|
|
&discord.ButtonComponent{
|
2021-05-12 05:36:03 +00:00
|
|
|
Label: "Success",
|
|
|
|
CustomID: "success_button",
|
|
|
|
Style: discord.SuccessButton,
|
|
|
|
},
|
2021-09-09 23:06:44 +00:00
|
|
|
&discord.ButtonComponent{
|
2021-05-12 05:36:03 +00:00
|
|
|
Label: "Danger",
|
|
|
|
CustomID: "danger_button",
|
|
|
|
Style: discord.DangerButton,
|
|
|
|
},
|
2021-09-09 23:06:44 +00:00
|
|
|
&discord.ButtonComponent{
|
2021-05-12 05:36:03 +00:00
|
|
|
Label: "Link",
|
|
|
|
URL: "https://google.com",
|
|
|
|
Style: discord.LinkButton,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.RespondInteraction(e.ID, e.Token, data); err != nil {
|
|
|
|
log.Println("failed to send interaction callback:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 23:06:44 +00:00
|
|
|
if e.Type != discord.ComponentInteraction {
|
2021-05-12 05:36:03 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-09 23:06:44 +00:00
|
|
|
customID := e.Data.(*discord.ComponentInteractionData).CustomID
|
2021-05-12 05:36:03 +00:00
|
|
|
data := api.InteractionResponse{
|
|
|
|
Type: api.UpdateMessage,
|
|
|
|
Data: &api.InteractionResponseData{
|
2021-09-09 23:06:44 +00:00
|
|
|
Content: option.NewNullableString("Custom ID: " + customID),
|
2021-05-12 05:36:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.RespondInteraction(e.ID, e.Token, data); err != nil {
|
|
|
|
log.Println("failed to send interaction callback:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
s.AddIntents(gateway.IntentGuilds)
|
|
|
|
s.AddIntents(gateway.IntentGuildMessages)
|
2021-05-12 05:36:03 +00:00
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
if err := s.Open(context.Background()); err != nil {
|
2021-05-12 05:36:03 +00:00
|
|
|
log.Fatalln("failed to open:", err)
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
log.Println("Gateway connected. Getting all guild commands.")
|
|
|
|
|
|
|
|
commands, err := s.GuildCommands(appID, guildID)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("failed to get guild commands:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, command := range commands {
|
|
|
|
log.Println("Existing command", command.Name, "found.")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCommands := []api.CreateCommandData{
|
|
|
|
{
|
|
|
|
Name: "buttons",
|
|
|
|
Description: "Send an interactable message.",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, command := range newCommands {
|
|
|
|
_, err := s.CreateGuildCommand(appID, guildID, command)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("failed to create guild command:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block forever.
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustSnowflakeEnv(env string) discord.Snowflake {
|
|
|
|
s, err := discord.ParseSnowflake(os.Getenv(env))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Invalid snowflake for $%s: %v", env, err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|