github.com/diamondburned/arikawa/v2@v2.1.0/_example/advanced_bot/debug.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"runtime"
     7  	"strings"
     8  
     9  	"github.com/diamondburned/arikawa/v2/bot"
    10  	"github.com/diamondburned/arikawa/v2/bot/extras/middlewares"
    11  	"github.com/diamondburned/arikawa/v2/gateway"
    12  )
    13  
    14  // Flag for administrators only.
    15  type Debug struct {
    16  	Context *bot.Context
    17  }
    18  
    19  // Setup demonstrates the CanSetup interface. This function will never be parsed
    20  // as a callback of any event.
    21  func (d *Debug) Setup(sub *bot.Subcommand) {
    22  	// Set a custom command (e.g. "!go ..."):
    23  	sub.Command = "go"
    24  	// Set a custom description:
    25  	sub.Description = "Print Go debugging variables"
    26  
    27  	// Manually set the usage for each function.
    28  
    29  	// Those methods can take in a regular Go method reference.
    30  	sub.ChangeCommandInfo(d.GOOS, "GOOS", "Prints the current operating system")
    31  	sub.ChangeCommandInfo(d.GC, "GC", "Triggers the garbage collector")
    32  	// They could also take in the raw name.
    33  	sub.ChangeCommandInfo("Goroutines", "", "Prints the current number of Goroutines")
    34  
    35  	sub.Hide(d.Die)
    36  	sub.AddMiddleware(d.Die, middlewares.AdminOnly(d.Context))
    37  }
    38  
    39  // ~go goroutines
    40  func (d *Debug) Goroutines(*gateway.MessageCreateEvent) (string, error) {
    41  	return fmt.Sprintf(
    42  		"goroutines: %d",
    43  		runtime.NumGoroutine(),
    44  	), nil
    45  }
    46  
    47  // ~go GOOS
    48  func (d *Debug) GOOS(*gateway.MessageCreateEvent) (string, error) {
    49  	return strings.Title(runtime.GOOS), nil
    50  }
    51  
    52  // ~go GC
    53  func (d *Debug) GC(*gateway.MessageCreateEvent) (string, error) {
    54  	runtime.GC()
    55  	return "Done.", nil
    56  }
    57  
    58  // ~go die
    59  // This command will be hidden from ~help by default.
    60  func (d *Debug) Die(m *gateway.MessageCreateEvent) error {
    61  	log.Fatalln("User", m.Author.Username, "killed the bot x_x")
    62  	return nil
    63  }