refactor: move functions
This commit is contained in:
parent
658a041ff8
commit
522d253410
5 changed files with 163 additions and 147 deletions
55
botcmd_dig.go
Normal file
55
botcmd_dig.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
tele "gopkg.in/telebot.v3"
|
||||
|
||||
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/hostcmds"
|
||||
)
|
||||
|
||||
func handleDigCmd(c tele.Context) error {
|
||||
msg := c.Message()
|
||||
if msg == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
req, err := hostcmds.NewDigRequest(msg.Payload)
|
||||
if err != nil {
|
||||
return c.Reply(
|
||||
"Invalid arguments.\nUsage: `/dig <name> [type]`",
|
||||
&tele.SendOptions{ParseMode: tele.ModeMarkdown},
|
||||
tele.Silent,
|
||||
)
|
||||
}
|
||||
|
||||
resp, err := hostcmds.Dig(req)
|
||||
if err != nil {
|
||||
_ = c.Reply(stickerFromID(stickerPanic), tele.Silent)
|
||||
return err
|
||||
}
|
||||
|
||||
replyBuf := &strings.Builder{}
|
||||
tw := tabwriter.NewWriter(replyBuf, 0, 0, 2, ' ', 0)
|
||||
// Write header
|
||||
if len(resp.Records) > 0 {
|
||||
_, _ = tw.Write([]byte("Name\tTTL\tType\tData\n"))
|
||||
}
|
||||
// Write data
|
||||
for _, r := range resp.Records {
|
||||
_, _ = fmt.Fprintf(tw, "%s\t%d\t%s\t%s\n", r.Name, r.TTL, r.Type, r.Data)
|
||||
}
|
||||
_ = tw.Flush()
|
||||
|
||||
replyText := []string{
|
||||
fmt.Sprintf("<i>Status: <b>%s</b></i>\n", resp.Status),
|
||||
fmt.Sprintf("<i>Query Time: <b>%s</b></i>\n\n", resp.QueryTime),
|
||||
"<pre>",
|
||||
html.EscapeString(replyBuf.String()),
|
||||
"</pre>",
|
||||
}
|
||||
return c.Reply(strings.Join(replyText, ""), &tele.SendOptions{ParseMode: tele.ModeHTML}, tele.Silent)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue