diff --git a/bot.go b/bot.go index 58c6142..7ca1d48 100644 --- a/bot.go +++ b/bot.go @@ -1,17 +1,11 @@ package main import ( - "fmt" - "html" "math" - "strings" - "text/tabwriter" "time" "github.com/go-errors/errors" tele "gopkg.in/telebot.v3" - - "git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/hostcmds" ) func isFromAdmin(sender *tele.User) bool { @@ -92,67 +86,6 @@ func logMiddleware(next tele.HandlerFunc) tele.HandlerFunc { } } -func handleStartCmd(c tele.Context) error { - if !isFromAdmin(c.Sender()) { - return c.Send("Hello, stranger :)") - } - - return c.Send("Hi :)") -} - -func handleUserInfoCmd(c tele.Context) error { - u := c.Sender() - if u == nil { - return c.Reply("Unknown.", tele.Silent) - } - - replyText := []string{ - `*User Info*`, - "```", - fmt.Sprintf(`ID: %d`, u.ID), - fmt.Sprintf(`Username: %s`, u.Username), - fmt.Sprintf(`FirstName: %s`, u.FirstName), - fmt.Sprintf(`LastName: %s`, u.LastName), - fmt.Sprintf(`LanguageCode: %s`, u.LanguageCode), - fmt.Sprintf(`IsBot: %t`, u.IsBot), - fmt.Sprintf(`IsPremium: %t`, u.IsPremium), - "```", - } - return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown}, tele.Silent) -} - -func handleChatInfoCmd(c tele.Context) error { - chat := c.Chat() - if chat == nil { - return c.Reply("Unknown.", tele.Silent) - } - loc := "" - if chat.ChatLocation != nil { - loc = chat.ChatLocation.Address - } - - replyText := []string{ - `*Chat Info*`, - "```", - fmt.Sprintf(`ID: %d`, chat.ID), - fmt.Sprintf(`Type: %s`, chat.Type), - fmt.Sprintf(`Title: %s`, chat.Title), - fmt.Sprintf(`FirstName: %s`, chat.FirstName), - fmt.Sprintf(`LastName: %s`, chat.LastName), - fmt.Sprintf(`Username: %s`, chat.Username), - fmt.Sprintf(`SlowMode: %d`, chat.SlowMode), - fmt.Sprintf(`StickerSet: %s`, chat.StickerSet), - fmt.Sprintf(`CanSetStickerSet: %t`, chat.CanSetStickerSet), - fmt.Sprintf(`LinkedChatID: %d`, chat.LinkedChatID), - fmt.Sprintf(`ChatLocation: %s`, loc), - fmt.Sprintf(`Private: %t`, chat.Private), - fmt.Sprintf(`Protected: %t`, chat.Protected), - fmt.Sprintf(`NoVoiceAndVideo: %t`, chat.NoVoiceAndVideo), - "```", - } - return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown}, tele.Silent) -} - func drawBar(progress float64, length int) string { barChars := []rune("ยท-=") @@ -171,20 +104,6 @@ func drawBar(progress float64, length int) string { return string(buf) } -func handleYearProgressCmd(c tele.Context) error { - yearStart := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local) - yearEnd := yearStart.AddDate(1, 0, 0) - yearDur := yearEnd.Sub(yearStart) - elapsed := time.Since(yearStart) - ratio := float64(elapsed) / float64(yearDur) - - replyText := fmt.Sprintf( - "\n%d is %2.0f%% complete.\n
%s
", - time.Now().Year(), ratio*100, drawBar(ratio, 20), - ) - return c.Reply(replyText, &tele.SendOptions{ParseMode: tele.ModeHTML}, tele.Silent) -} - func handleGeneralMessage(_ tele.Context) error { // Do nothing for now return nil @@ -197,46 +116,3 @@ func stickerFromID(id string) *tele.Sticker { }, } } - -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 [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("Status: %s\n", resp.Status), - fmt.Sprintf("Query Time: %s\n\n", resp.QueryTime), - "
",
-		html.EscapeString(replyBuf.String()),
-		"
", - } - return c.Reply(strings.Join(replyText, ""), &tele.SendOptions{ParseMode: tele.ModeHTML}, tele.Silent) -} diff --git a/botcmd_dig.go b/botcmd_dig.go new file mode 100644 index 0000000..80812e5 --- /dev/null +++ b/botcmd_dig.go @@ -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 [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("Status: %s\n", resp.Status), + fmt.Sprintf("Query Time: %s\n\n", resp.QueryTime), + "
",
+		html.EscapeString(replyBuf.String()),
+		"
", + } + return c.Reply(strings.Join(replyText, ""), &tele.SendOptions{ParseMode: tele.ModeHTML}, tele.Silent) +} diff --git a/botcmd_exchangerates.go b/botcmd_exchangerates.go index 36c5d1a..4e09e35 100644 --- a/botcmd_exchangerates.go +++ b/botcmd_exchangerates.go @@ -5,6 +5,7 @@ import ( "regexp" "strconv" "strings" + "time" "git.gensokyo.cafe/kkyy/mycurrencynet" "github.com/dustin/go-humanize" @@ -13,6 +14,25 @@ import ( tele "gopkg.in/telebot.v3" ) +var exchangeRates = mycurrencynet.New() + +func initExchangeRates() { + if err := exchangeRates.Update(); err != nil { + logger.Panicw("Failed to update exchange rates", "err", err) + } + logger.Info("Exchange rates updated") + + go exchangeRates.UpdateEvery( + time.Hour, + func(err error) { + logger.Errorw("Failed to update exchange rates", "err", err) + }, + func() { + logger.Info("Exchange rates updated") + }, + ) +} + func handleExchangeRateCmd(c tele.Context) error { msg := c.Message() if msg == nil { diff --git a/botcmd_misc.go b/botcmd_misc.go new file mode 100644 index 0000000..f35ed1a --- /dev/null +++ b/botcmd_misc.go @@ -0,0 +1,84 @@ +package main + +import ( + "fmt" + "strings" + "time" + + tele "gopkg.in/telebot.v3" +) + +func handleStartCmd(c tele.Context) error { + if !isFromAdmin(c.Sender()) { + return c.Send("Hello, stranger :)") + } + + return c.Send("Hi :)") +} + +func handleUserInfoCmd(c tele.Context) error { + u := c.Sender() + if u == nil { + return c.Reply("Unknown.", tele.Silent) + } + + replyText := []string{ + `*User Info*`, + "```", + fmt.Sprintf(`ID: %d`, u.ID), + fmt.Sprintf(`Username: %s`, u.Username), + fmt.Sprintf(`FirstName: %s`, u.FirstName), + fmt.Sprintf(`LastName: %s`, u.LastName), + fmt.Sprintf(`LanguageCode: %s`, u.LanguageCode), + fmt.Sprintf(`IsBot: %t`, u.IsBot), + fmt.Sprintf(`IsPremium: %t`, u.IsPremium), + "```", + } + return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown}, tele.Silent) +} + +func handleChatInfoCmd(c tele.Context) error { + chat := c.Chat() + if chat == nil { + return c.Reply("Unknown.", tele.Silent) + } + loc := "" + if chat.ChatLocation != nil { + loc = chat.ChatLocation.Address + } + + replyText := []string{ + `*Chat Info*`, + "```", + fmt.Sprintf(`ID: %d`, chat.ID), + fmt.Sprintf(`Type: %s`, chat.Type), + fmt.Sprintf(`Title: %s`, chat.Title), + fmt.Sprintf(`FirstName: %s`, chat.FirstName), + fmt.Sprintf(`LastName: %s`, chat.LastName), + fmt.Sprintf(`Username: %s`, chat.Username), + fmt.Sprintf(`SlowMode: %d`, chat.SlowMode), + fmt.Sprintf(`StickerSet: %s`, chat.StickerSet), + fmt.Sprintf(`CanSetStickerSet: %t`, chat.CanSetStickerSet), + fmt.Sprintf(`LinkedChatID: %d`, chat.LinkedChatID), + fmt.Sprintf(`ChatLocation: %s`, loc), + fmt.Sprintf(`Private: %t`, chat.Private), + fmt.Sprintf(`Protected: %t`, chat.Protected), + fmt.Sprintf(`NoVoiceAndVideo: %t`, chat.NoVoiceAndVideo), + "```", + } + return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown}, tele.Silent) +} + +func handleYearProgressCmd(c tele.Context) error { + yearStart := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local) + yearEnd := yearStart.AddDate(1, 0, 0) + yearDur := yearEnd.Sub(yearStart) + elapsed := time.Since(yearStart) + ratio := float64(elapsed) / float64(yearDur) + + replyText := fmt.Sprintf( + "\n%d is %2.0f%% complete.\n
%s
", + time.Now().Year(), ratio*100, drawBar(ratio, 20), + ) + return c.Reply(replyText, &tele.SendOptions{ParseMode: tele.ModeHTML}, tele.Silent) +} diff --git a/main.go b/main.go index 94df370..1d639d1 100644 --- a/main.go +++ b/main.go @@ -4,18 +4,18 @@ import ( "os" "os/signal" "syscall" - "time" "go.uber.org/zap" "go.uber.org/zap/zapcore" tele "gopkg.in/telebot.v3" - "git.gensokyo.cafe/kkyy/mycurrencynet" "git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/utils" ) -var logger *zap.SugaredLogger -var loglvl zap.AtomicLevel +var ( + logger *zap.SugaredLogger + loglvl zap.AtomicLevel +) func initLogger() { logCfg := zap.NewProductionConfig() @@ -29,25 +29,6 @@ func initLogger() { logger = l.Sugar() } -var exchangeRates = mycurrencynet.New() - -func initExchangeRates() { - if err := exchangeRates.Update(); err != nil { - logger.Panicw("Failed to update exchange rates", "err", err) - } - logger.Info("Exchange rates updated") - - go exchangeRates.UpdateEvery( - time.Hour, - func(err error) { - logger.Errorw("Failed to update exchange rates", "err", err) - }, - func() { - logger.Info("Exchange rates updated") - }, - ) -} - func runBot() { logger.Info("Bot initializing...") bot, err := initBot()