From 3713c9d404ea50f754ece227c578f793c3cfb0dc Mon Sep 17 00:00:00 2001 From: diamondburned Date: Tue, 23 Feb 2021 21:40:44 -0800 Subject: [PATCH] Bot: Added Run(); updated examples --- README.md | 32 ++++++++++++++++++++++++++++++++ _example/advanced_bot/main.go | 16 +--------------- bot/ctx.go | 26 +++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a47a690..ddf6916 100644 --- a/README.md +++ b/README.md @@ -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), 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) A complex example demonstrating the reflect-based command router that's diff --git a/_example/advanced_bot/main.go b/_example/advanced_bot/main.go index 69d2cdd..135a834 100644 --- a/_example/advanced_bot/main.go +++ b/_example/advanced_bot/main.go @@ -19,7 +19,7 @@ func main() { 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.EditableCommands = true @@ -32,18 +32,4 @@ func main() { 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) - } } diff --git a/bot/ctx.go b/bot/ctx.go index 4dd530b..864983e 100644 --- a/bot/ctx.go +++ b/bot/ctx.go @@ -147,7 +147,15 @@ func Start( token string, cmd interface{}, 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 { return nil, errors.Wrap(err, "failed to create a dgo session") } @@ -188,6 +196,22 @@ func Start( }, 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. func Wait() { sigs := make(chan os.Signal, 1)