github.com/diamondburned/arikawa@v1.3.14/_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/bot" 10 "github.com/diamondburned/arikawa/bot/extras/middlewares" 11 "github.com/diamondburned/arikawa/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 sub.ChangeCommandInfo("GOOS", "GOOS", "Prints the current operating system") 30 sub.ChangeCommandInfo("GC", "GC", "Triggers the garbage collector") 31 sub.ChangeCommandInfo("Goroutines", "", "Prints the current number of Goroutines") 32 33 sub.Hide("Die") 34 sub.AddMiddleware("Die", middlewares.AdminOnly(d.Context)) 35 } 36 37 // ~go goroutines 38 func (d *Debug) Goroutines(*gateway.MessageCreateEvent) (string, error) { 39 return fmt.Sprintf( 40 "goroutines: %d", 41 runtime.NumGoroutine(), 42 ), nil 43 } 44 45 // ~go GOOS 46 func (d *Debug) GOOS(*gateway.MessageCreateEvent) (string, error) { 47 return strings.Title(runtime.GOOS), nil 48 } 49 50 // ~go GC 51 func (d *Debug) GC(*gateway.MessageCreateEvent) (string, error) { 52 runtime.GC() 53 return "Done.", nil 54 } 55 56 // ~go die 57 // This command will be hidden from ~help by default. 58 func (d *Debug) Die(m *gateway.MessageCreateEvent) error { 59 log.Fatalln("User", m.Author.Username, "killed the bot x_x") 60 return nil 61 }