mirror of
https://github.com/Fluffy-Bean/Lynxie.git
synced 2025-05-22 19:34:57 +00:00
Add Ping, Debug and Animal commands
This commit is contained in:
parent
2f30bec05c
commit
f9526ba5d4
6 changed files with 252 additions and 2 deletions
81
commands/meta.go
Normal file
81
commands/meta.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Fluffy-Bean/lynxie/app"
|
||||
)
|
||||
|
||||
func RegisterMetaCommands(a *app.App) {
|
||||
a.RegisterCommand("ping", registerPong(a))
|
||||
a.RegisterCommand("debug", registerDebug(a))
|
||||
}
|
||||
|
||||
func registerPong(a *app.App) func(h *app.Handler, args []string) {
|
||||
return func(h *app.Handler, args []string) {
|
||||
var options struct {
|
||||
latency bool
|
||||
}
|
||||
|
||||
cmd := flag.NewFlagSet("pong", flag.ContinueOnError)
|
||||
cmd.BoolVar(&options.latency, "latency", false, "Display the latency of ping")
|
||||
cmd.Parse(args)
|
||||
|
||||
if options.latency {
|
||||
h.Session.ChannelMessageSend(
|
||||
h.Message.ChannelID,
|
||||
fmt.Sprintf("Pong! %dms", h.Session.HeartbeatLatency().Milliseconds()),
|
||||
)
|
||||
} else {
|
||||
h.Session.ChannelMessageSend(
|
||||
h.Message.ChannelID,
|
||||
"Pong!",
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func registerDebug(a *app.App) func(h *app.Handler, args []string) {
|
||||
return func(h *app.Handler, args []string) {
|
||||
modified := false
|
||||
revision := "-"
|
||||
tags := "-"
|
||||
_go := strings.TrimPrefix(runtime.Version(), "go")
|
||||
gcCount := runtime.MemStats{}.NumGC
|
||||
localTime := time.Now().Local().Format("2006-01-02 15:04:05")
|
||||
|
||||
info, _ := debug.ReadBuildInfo()
|
||||
for _, setting := range info.Settings {
|
||||
switch setting.Key {
|
||||
case "vcs.revision":
|
||||
revision = setting.Value
|
||||
case "vcs.modified":
|
||||
modified = setting.Value == "true"
|
||||
case "-tags":
|
||||
tags = strings.ReplaceAll(setting.Value, ",", " ")
|
||||
}
|
||||
}
|
||||
|
||||
if modified {
|
||||
revision += " (uncommitted changes)"
|
||||
}
|
||||
|
||||
h.Session.ChannelMessageSend(
|
||||
h.Message.ChannelID,
|
||||
fmt.Sprintf(
|
||||
"``` Revision :: %s\nBuild Tags :: %s\nGo version :: %s\n OS/Arch :: %s\n GC Count :: %d\nLocal Time :: %s```",
|
||||
revision,
|
||||
tags,
|
||||
_go,
|
||||
runtime.GOOS+"/"+runtime.GOARCH,
|
||||
gcCount,
|
||||
localTime,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
46
commands/tinyfox.go
Normal file
46
commands/tinyfox.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Fluffy-Bean/lynxie/app"
|
||||
)
|
||||
|
||||
func RegisterTinyfoxCommands(a *app.App) {
|
||||
a.RegisterCommand("animal", registerAnimal(a))
|
||||
}
|
||||
|
||||
func registerAnimal(a *app.App) func(h *app.Handler, args []string) {
|
||||
client := http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
return func(h *app.Handler, args []string) {
|
||||
var options struct {
|
||||
animal string
|
||||
}
|
||||
|
||||
cmd := flag.NewFlagSet("pong", flag.ContinueOnError)
|
||||
cmd.StringVar(&options.animal, "animal", "serval", "Get an image of an animal!")
|
||||
cmd.Parse(args)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "https://api.tinyfox.dev/img?animal="+options.animal, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
h.Session.ChannelFileSend(
|
||||
h.Message.ChannelID,
|
||||
"animal__"+options.animal+".png",
|
||||
res.Body,
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue