From 7d683a2acec70f087b69031d5cce36ae59f85e78 Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Thu, 14 May 2020 14:20:23 -0700 Subject: [PATCH] Examples: Updated advanced_bot to the new API --- _example/advanced_bot/bot.go | 19 ++++++++++--------- _example/advanced_bot/debug.go | 19 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/_example/advanced_bot/bot.go b/_example/advanced_bot/bot.go index 16e1ebe..bb9dd17 100644 --- a/_example/advanced_bot/bot.go +++ b/_example/advanced_bot/bot.go @@ -10,6 +10,7 @@ import ( "github.com/diamondburned/arikawa/bot" "github.com/diamondburned/arikawa/bot/extras/arguments" + "github.com/diamondburned/arikawa/bot/extras/middlewares" "github.com/diamondburned/arikawa/discord" "github.com/diamondburned/arikawa/gateway" ) @@ -19,6 +20,10 @@ type Bot struct { Ctx *bot.Context } +func (bot *Bot) Setup(sub *bot.Subcommand) { + sub.AddMiddleware("GuildInfo", middlewares.GuildOnly(bot.Ctx)) +} + // Help prints the default help message. func (bot *Bot) Help(m *gateway.MessageCreateEvent) (string, error) { return bot.Ctx.Help(), nil @@ -39,9 +44,7 @@ func (bot *Bot) Ping(m *gateway.MessageCreateEvent) error { } // Say demonstrates how arguments.Flag could be used without the flag library. -func (bot *Bot) Say( - m *gateway.MessageCreateEvent, f *arguments.Flag) (string, error) { - +func (bot *Bot) Say(m *gateway.MessageCreateEvent, f *arguments.Flag) (string, error) { args := f.String() if args == "" { // Empty message, ignore @@ -53,15 +56,15 @@ func (bot *Bot) Say( // GuildInfo demonstrates the use of command flags, in this case the GuildOnly // flag. -func (bot *Bot) GーGuildInfo(m *gateway.MessageCreateEvent) (string, error) { - g, err := bot.Ctx.Guild(m.GuildID) +func (bot *Bot) GuildInfo(m *gateway.MessageCreateEvent) (string, error) { + g, err := bot.Ctx.GuildWithCount(m.GuildID) if err != nil { return "", fmt.Errorf("Failed to get guild: %v", err) } return fmt.Sprintf( "Your guild is %s, and its maximum members is %d", - g.Name, g.MaxMembers, + g.Name, g.ApproximateMembers, ), nil } @@ -98,9 +101,7 @@ func (bot *Bot) Repeat(m *gateway.MessageCreateEvent) (string, error) { // Embed is a simple embed creator. Its purpose is to demonstrate the usage of // the ParseContent interface, as well as using the stdlib flag package. -func (bot *Bot) Embed( - m *gateway.MessageCreateEvent, f *arguments.Flag) (*discord.Embed, error) { - +func (bot *Bot) Embed(m *gateway.MessageCreateEvent, f *arguments.Flag) (*discord.Embed, error) { fs := arguments.NewFlagSet() var ( diff --git a/_example/advanced_bot/debug.go b/_example/advanced_bot/debug.go index 3ced069..78c3c11 100644 --- a/_example/advanced_bot/debug.go +++ b/_example/advanced_bot/debug.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/diamondburned/arikawa/bot" + "github.com/diamondburned/arikawa/bot/extras/middlewares" "github.com/diamondburned/arikawa/gateway" ) @@ -25,14 +26,12 @@ func (d *Debug) Setup(sub *bot.Subcommand) { // Manually set the usage for each function. - sub.ChangeCommandInfo("GOOS", "", - "Prints the current operating system") + sub.ChangeCommandInfo("GOOS", "GOOS", "Prints the current operating system") + sub.ChangeCommandInfo("GC", "GC", "Triggers the garbage collecto") + sub.ChangeCommandInfo("Goroutines", "", "Prints the current number of Goroutines") - sub.ChangeCommandInfo("GC", "", - "Triggers the garbage collecto") - - sub.ChangeCommandInfo("Goroutines", "", - "Prints the current number of Goroutines") + sub.Hide("Die") + sub.AddMiddleware("Die", middlewares.AdminOnly(d.Context)) } // ~go goroutines @@ -44,19 +43,19 @@ func (d *Debug) Goroutines(m *gateway.MessageCreateEvent) (string, error) { } // ~go GOOS -func (d *Debug) RーGOOS(m *gateway.MessageCreateEvent) (string, error) { +func (d *Debug) GOOS(m *gateway.MessageCreateEvent) (string, error) { return strings.Title(runtime.GOOS), nil } // ~go GC -func (d *Debug) RーGC(m *gateway.MessageCreateEvent) (string, error) { +func (d *Debug) GC(m *gateway.MessageCreateEvent) (string, error) { runtime.GC() return "Done.", nil } // ~go die // This command will be hidden from ~help by default. -func (d *Debug) AーDie(m *gateway.MessageCreateEvent) error { +func (d *Debug) Die(m *gateway.MessageCreateEvent) error { log.Fatalln("User", m.Author.Username, "killed the bot x_x") return nil }