mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-16 03:44:26 +00:00
33 lines
687 B
Go
33 lines
687 B
Go
|
package state_test
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"log"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
|
||
|
"github.com/diamondburned/arikawa/v3/gateway"
|
||
|
"github.com/diamondburned/arikawa/v3/state"
|
||
|
)
|
||
|
|
||
|
func Example() {
|
||
|
s := state.New("Bot " + os.Getenv("DISCORD_TOKEN"))
|
||
|
s.AddIntents(gateway.IntentGuilds | gateway.IntentGuildMessages)
|
||
|
s.AddHandler(func(m *gateway.MessageCreateEvent) {
|
||
|
log.Printf("%s: %s", m.Author.Username, m.Content)
|
||
|
})
|
||
|
|
||
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||
|
defer cancel()
|
||
|
|
||
|
if err := s.Open(ctx); err != nil {
|
||
|
log.Println("cannot open:", err)
|
||
|
}
|
||
|
|
||
|
<-ctx.Done() // block until Ctrl+C
|
||
|
|
||
|
if err := s.Close(); err != nil {
|
||
|
log.Println("cannot close:", err)
|
||
|
}
|
||
|
}
|