mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-27 17:23:00 +00:00
Examples: Updated advanced_bot to the new API
This commit is contained in:
parent
729979088c
commit
7d683a2ace
|
@ -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 (
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue