Compare commits
No commits in common. "d037f575608bfba69abd87505022f8769ec1429e" and "b429137d20490304ec7770632dd789aa28e231cc" have entirely different histories.
d037f57560
...
b429137d20
5
Makefile
5
Makefile
|
@ -1,9 +1,6 @@
|
|||
build: lint
|
||||
build:
|
||||
CGO_ENABLED=0 go build -v -trimpath -ldflags='-s -w'
|
||||
|
||||
lint:
|
||||
golangci-lint run ./...
|
||||
|
||||
pack: build
|
||||
tar --owner root --group root -c tgbot_misaka_5882f7 | zstdmt -15v > tgbot_misaka_5882f7.tar.zst
|
||||
|
||||
|
|
37
bot.go
37
bot.go
|
@ -2,12 +2,10 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
"github.com/samber/lo"
|
||||
tele "gopkg.in/telebot.v3"
|
||||
|
||||
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/stats"
|
||||
|
@ -98,12 +96,11 @@ func handleTrafficCmd(c tele.Context) error {
|
|||
// This month's traffic, if present
|
||||
if len(monthlyTraffic) > 0 {
|
||||
row := monthlyTraffic[len(monthlyTraffic)-1]
|
||||
|
||||
responseParts = append(
|
||||
responseParts,
|
||||
fmt.Sprintf("This month so far:` %s`", fmtTraffic(row)),
|
||||
)
|
||||
bar := fmt.Sprintf("\n```\n%s\n```", drawBarForTrafficRecord(row))
|
||||
responseParts = append(responseParts, bar)
|
||||
}
|
||||
|
||||
var respText string
|
||||
|
@ -117,8 +114,12 @@ func handleTrafficCmd(c tele.Context) error {
|
|||
}
|
||||
|
||||
func fmtTraffic(r stats.VnstatTrafficRecord) string {
|
||||
effective := lo.Max([]uint64{r.Rx, r.Tx})
|
||||
return fmt.Sprintf("%.2f GiB", float64(effective)/1024/1024/1024)
|
||||
biggest := r.Rx
|
||||
if r.Tx > biggest {
|
||||
biggest = r.Tx
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%.2f GiB", float64(biggest)/1024/1024/1024)
|
||||
}
|
||||
|
||||
func handleUserInfoCmd(c tele.Context) error {
|
||||
|
@ -173,27 +174,3 @@ func handleChatInfoCmd(c tele.Context) error {
|
|||
}
|
||||
return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown})
|
||||
}
|
||||
|
||||
func drawBar(progress float64, length int) string {
|
||||
barChars := []rune("░▒▓█")
|
||||
|
||||
if length <= 0 {
|
||||
return ""
|
||||
}
|
||||
step := 1 / float64(length)
|
||||
buf := make([]rune, length)
|
||||
for i := 0; i < length; i++ {
|
||||
fill := (progress - float64(i)*step) / step
|
||||
fill = math.Min(math.Max(fill, 0), 1)
|
||||
idx := int(math.Round(fill * float64(len(barChars)-1)))
|
||||
buf[i] = barChars[idx]
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func drawBarForTrafficRecord(r stats.VnstatTrafficRecord) string {
|
||||
effective := lo.Max([]uint64{r.Rx, r.Tx})
|
||||
max := config.MonthlyTrafficLimitGiB
|
||||
ratio := float64(effective) / 1024 / 1024 / 1024 / float64(max)
|
||||
return fmt.Sprintf("%s %2.0f%%", drawBar(ratio, 16), ratio*100)
|
||||
}
|
||||
|
|
23
cfg.go
23
cfg.go
|
@ -13,8 +13,7 @@ type Config struct {
|
|||
AdminUIDs map[int64]struct{}
|
||||
TGBotToken string
|
||||
|
||||
WatchedInterface string
|
||||
MonthlyTrafficLimitGiB int
|
||||
WatchedInterface string
|
||||
}
|
||||
|
||||
var config *Config
|
||||
|
@ -29,9 +28,8 @@ func LoadCfg() error {
|
|||
cfg.TGBotToken = token
|
||||
|
||||
adminUIDsEnv := os.Getenv("TG_ADMIN_UIDS")
|
||||
adminUIDs := lo.FilterMap(strings.Split(adminUIDsEnv, ","), func(s string, _ int) (string, bool) {
|
||||
trimmed := strings.TrimSpace(s)
|
||||
return trimmed, trimmed != ""
|
||||
adminUIDs := lo.Filter(strings.Split(adminUIDsEnv, ","), func(s string, _ int) bool {
|
||||
return strings.Trim(s, "\t ") != ""
|
||||
})
|
||||
cfg.AdminUIDs = make(map[int64]struct{}, len(adminUIDs))
|
||||
for _, uidStr := range adminUIDs {
|
||||
|
@ -42,18 +40,11 @@ func LoadCfg() error {
|
|||
cfg.AdminUIDs[uid] = struct{}{}
|
||||
}
|
||||
|
||||
cfg.WatchedInterface = "eth0"
|
||||
if iface := os.Getenv("TG_WATCHED_INTERFACE"); iface != "" {
|
||||
cfg.WatchedInterface = iface
|
||||
}
|
||||
|
||||
cfg.MonthlyTrafficLimitGiB = 1000
|
||||
if trafficLimitStr := os.Getenv("TG_MONTHLY_TRAFFIC_LIMIT_GIB"); trafficLimitStr != "" {
|
||||
var err error
|
||||
if cfg.MonthlyTrafficLimitGiB, err = strconv.Atoi(trafficLimitStr); err != nil {
|
||||
return errors.New("invalid traffic limit: " + trafficLimitStr)
|
||||
}
|
||||
iface := os.Getenv("TG_WATCHED_INTERFACE")
|
||||
if iface == "" {
|
||||
iface = "eth0"
|
||||
}
|
||||
cfg.WatchedInterface = iface
|
||||
|
||||
config = &cfg
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue