1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-10-31 20:14:21 +00:00
arikawa/0-examples/commands/main.go
diamondburned 17b9c73ce3
gateway: Refactor for a better concurrent API
This commit refactors the whole package gateway as well as utils/ws
(formerly utils/wsutil) and voice/voicegateway. The new refactor
utilizes a design pattern involving a concurrent loop and an arriving
event channel.

An additional change was made to the way gateway events are typed.
Before, pretty much any type will satisfy a gateway event type, since
the actual type was just interface{}. The new refactor defines a
concrete interface that events can implement:

    type Event interface {
        Op() OpCode
        EventType() EventType
    }

Using this interface, the user can easily add custom gateway events
independently of the library without relying on string maps. This adds a
lot of type safety into the library and makes type-switching on Event
types much more reasonable.

Gateway error callbacks are also almost entirely removed in favor of
custom gateway events. A catch-all can easily be added like this:

    s.AddHandler(func(err error) {
        log.Println("gateway error:, err")
    })
2021-11-25 14:46:41 -08:00

89 lines
2 KiB
Go

package main
import (
"context"
"log"
"os"
"github.com/diamondburned/arikawa/v3/api"
"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"
)
// To run, do `GUILD_ID="GUILD ID" BOT_TOKEN="TOKEN HERE" go run .`
func main() {
guildID := discord.GuildID(mustSnowflakeEnv("GUILD_ID"))
token := os.Getenv("BOT_TOKEN")
if token == "" {
log.Fatalln("No $BOT_TOKEN given.")
}
s := state.New("Bot " + token)
app, err := s.CurrentApplication()
if err != nil {
log.Fatalln("Failed to get application ID:", err)
}
s.AddHandler(func(e *gateway.InteractionCreateEvent) {
data := api.InteractionResponse{
Type: api.MessageInteractionWithSource,
Data: &api.InteractionResponseData{
Content: option.NewNullableString("Pong!"),
},
}
if err := s.RespondInteraction(e.ID, e.Token, data); err != nil {
log.Println("failed to send interaction callback:", err)
}
})
s.AddIntents(gateway.IntentGuilds)
s.AddIntents(gateway.IntentGuildMessages)
if err := s.Open(context.Background()); err != nil {
log.Fatalln("failed to open:", err)
}
defer s.Close()
log.Println("Gateway connected. Getting all guild commands.")
commands, err := s.GuildCommands(app.ID, 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: "ping",
Description: "Basic ping command.",
},
}
for _, command := range newCommands {
_, err := s.CreateGuildCommand(app.ID, 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
}