2020-01-24 03:17:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/bot"
|
|
|
|
"github.com/diamondburned/arikawa/v3/bot/extras/middlewares"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
2020-01-24 03:17:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2020-11-13 03:02:52 +00:00
|
|
|
// 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.
|
2020-05-14 21:20:23 +00:00
|
|
|
sub.ChangeCommandInfo("Goroutines", "", "Prints the current number of Goroutines")
|
2020-01-24 03:17:03 +00:00
|
|
|
|
2020-11-13 03:02:52 +00:00
|
|
|
sub.Hide(d.Die)
|
|
|
|
sub.AddMiddleware(d.Die, middlewares.AdminOnly(d.Context))
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ~go goroutines
|
2020-07-29 23:29:01 +00:00
|
|
|
func (d *Debug) Goroutines(*gateway.MessageCreateEvent) (string, error) {
|
2020-01-26 09:08:53 +00:00
|
|
|
return fmt.Sprintf(
|
2020-01-24 03:17:03 +00:00
|
|
|
"goroutines: %d",
|
|
|
|
runtime.NumGoroutine(),
|
2020-01-26 09:08:53 +00:00
|
|
|
), nil
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ~go GOOS
|
2020-07-29 23:29:01 +00:00
|
|
|
func (d *Debug) GOOS(*gateway.MessageCreateEvent) (string, error) {
|
2020-01-26 09:08:53 +00:00
|
|
|
return strings.Title(runtime.GOOS), nil
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ~go GC
|
2020-07-29 23:29:01 +00:00
|
|
|
func (d *Debug) GC(*gateway.MessageCreateEvent) (string, error) {
|
2020-01-24 03:17:03 +00:00
|
|
|
runtime.GC()
|
2020-01-26 09:08:53 +00:00
|
|
|
return "Done.", nil
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ~go die
|
|
|
|
// This command will be hidden from ~help by default.
|
2020-05-14 21:20:23 +00:00
|
|
|
func (d *Debug) Die(m *gateway.MessageCreateEvent) error {
|
2020-01-24 03:17:03 +00:00
|
|
|
log.Fatalln("User", m.Author.Username, "killed the bot x_x")
|
|
|
|
return nil
|
|
|
|
}
|