1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-01 12:34:28 +00:00
arikawa/_example/advanced_bot/debug.go
diamondburned 273fcf1418 Bot: Added subcommand aliases and better setup API
This commit adds subcommand aliases as well as additional code in
HelpGenerate to cover for both subcommand and command aliases.

A breaking change is that {,Must}RegisterSubcommandCustom methods are
now replaced with normal {,Must}RegisterSubcommand methods. This is
because they now use variadic strings, which could take 0, 1 or more
arguments.

This commit also allows AddMiddleware and similar methods to be given a
method directly:

    sub.Plumb(cmds.PlumbedHandler)
    sub.AddMiddleware(cmds.PlumbedHandler, cmds.plumbMiddleware)

This change closes issue #146.
2020-11-12 19:02:52 -08:00

64 lines
1.6 KiB
Go

package main
import (
"fmt"
"log"
"runtime"
"strings"
"github.com/diamondburned/arikawa/v2/bot"
"github.com/diamondburned/arikawa/v2/bot/extras/middlewares"
"github.com/diamondburned/arikawa/v2/gateway"
)
// Flag for administrators only.
type Debug struct {
Context *bot.Context
}
// Setup demonstrates the CanSetup interface. This function will never be parsed
// as a callback of any event.
func (d *Debug) Setup(sub *bot.Subcommand) {
// Set a custom command (e.g. "!go ..."):
sub.Command = "go"
// Set a custom description:
sub.Description = "Print Go debugging variables"
// Manually set the usage for each function.
// Those methods can take in a regular Go method reference.
sub.ChangeCommandInfo(d.GOOS, "GOOS", "Prints the current operating system")
sub.ChangeCommandInfo(d.GC, "GC", "Triggers the garbage collector")
// They could also take in the raw name.
sub.ChangeCommandInfo("Goroutines", "", "Prints the current number of Goroutines")
sub.Hide(d.Die)
sub.AddMiddleware(d.Die, middlewares.AdminOnly(d.Context))
}
// ~go goroutines
func (d *Debug) Goroutines(*gateway.MessageCreateEvent) (string, error) {
return fmt.Sprintf(
"goroutines: %d",
runtime.NumGoroutine(),
), nil
}
// ~go GOOS
func (d *Debug) GOOS(*gateway.MessageCreateEvent) (string, error) {
return strings.Title(runtime.GOOS), nil
}
// ~go GC
func (d *Debug) GC(*gateway.MessageCreateEvent) (string, error) {
runtime.GC()
return "Done.", nil
}
// ~go die
// This command will be hidden from ~help by default.
func (d *Debug) Die(m *gateway.MessageCreateEvent) error {
log.Fatalln("User", m.Author.Username, "killed the bot x_x")
return nil
}