1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-03-28 21:09:27 +00:00

Bot: Added Run(); updated examples

This commit is contained in:
diamondburned 2021-02-23 21:40:44 -08:00
parent 3b5a4ed94d
commit 3713c9d404
3 changed files with 58 additions and 16 deletions

View file

@ -49,6 +49,38 @@ This example demonstrates the PreHandler feature of the state library.
PreHandler calls all handlers that are registered (separately from the session), PreHandler calls all handlers that are registered (separately from the session),
calling them before the state is updated. calling them before the state is updated.
### Bare Minimum Bot
The least amount of code for a basic ping-pong bot. It's similar to Serenity's
Discord bot example in the README.
```go
package main
import (
"os"
"github.com/diamondburned/arikawa/v2/bot"
"github.com/diamondburned/arikawa/v2/gateway"
)
func main() {
bot.Run(os.Getenv("DISCORD_TOKEN"), &Bot{},
func(ctx *bot.Context) error {
ctx.HasPrefix = bot.NewPrefix("!")
},
)
}
type Bot struct {
Ctx *bot.Context
}
func (b *Bot) Ping(*gateway.MessageCreateEvent) (string, error) {
return "Pong!", nil
}
```
### [Advanced Bot](https://github.com/diamondburned/arikawa/tree/v2/_example/advanced_bot) ### [Advanced Bot](https://github.com/diamondburned/arikawa/tree/v2/_example/advanced_bot)
A complex example demonstrating the reflect-based command router that's A complex example demonstrating the reflect-based command router that's

View file

@ -19,7 +19,7 @@ func main() {
commands := &Bot{} commands := &Bot{}
wait, err := bot.Start(token, commands, func(ctx *bot.Context) error { bot.Run(token, commands, func(ctx *bot.Context) error {
ctx.HasPrefix = bot.NewPrefix("!", "~") ctx.HasPrefix = bot.NewPrefix("!", "~")
ctx.EditableCommands = true ctx.EditableCommands = true
@ -32,18 +32,4 @@ func main() {
return nil return nil
}) })
if err != nil {
log.Fatalln(err)
}
log.Println("Bot started")
// As of this commit, wait() will block until SIGINT or fatal. The past
// versions close on call, but this one will block.
// If for some reason you want the Cancel() function, manually make a new
// context.
if err := wait(); err != nil {
log.Fatalln("Gateway fatal error:", err)
}
} }

View file

@ -147,7 +147,15 @@ func Start(
token string, cmd interface{}, token string, cmd interface{},
opts func(*Context) error) (wait func() error, err error) { opts func(*Context) error) (wait func() error, err error) {
s, err := state.New("Bot " + token) if token == "" {
return nil, errors.New("token is not given")
}
if !strings.HasPrefix(token, "Bot ") {
token = "Bot " + token
}
s, err := state.New(token)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to create a dgo session") return nil, errors.Wrap(err, "failed to create a dgo session")
} }
@ -188,6 +196,22 @@ func Start(
}, nil }, nil
} }
// Run starts the bot, prints a message into the console, and blocks until
// SIGINT. "Bot" is prepended into the token automatically, similar to Start.
// The function will call os.Exit(1) on an initialization or cleanup error.
func Run(token string, cmd interface{}, opts func(*Context) error) {
wait, err := Start(token, cmd, opts)
if err != nil {
log.Fatalln("failed to start:", err)
}
log.Println("Bot is running.")
if err := wait(); err != nil {
log.Fatalln("cleanup error:", err)
}
}
// Wait blocks until SIGINT. // Wait blocks until SIGINT.
func Wait() { func Wait() {
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)