feat: add /year_progress command

yeah i'm bored
This commit is contained in:
Yiyang Kang 2022-11-21 15:42:43 +08:00
parent d037f57560
commit f9ae020243
Signed by: kkyy
GPG Key ID: 80FD317ECAF06CC3
1 changed files with 12 additions and 0 deletions

12
bot.go
View File

@ -39,6 +39,7 @@ func initBot() (*tele.Bot, error) {
b.Handle("/me", handleUserInfoCmd) b.Handle("/me", handleUserInfoCmd)
b.Handle("/chat", handleChatInfoCmd) b.Handle("/chat", handleChatInfoCmd)
b.Handle("/year_progress", handleYearProgressCmd)
return b, nil return b, nil
} }
@ -197,3 +198,14 @@ func drawBarForTrafficRecord(r stats.VnstatTrafficRecord) string {
ratio := float64(effective) / 1024 / 1024 / 1024 / float64(max) ratio := float64(effective) / 1024 / 1024 / 1024 / float64(max)
return fmt.Sprintf("%s %2.0f%%", drawBar(ratio, 16), ratio*100) return fmt.Sprintf("%s %2.0f%%", drawBar(ratio, 16), ratio*100)
} }
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)
bar := fmt.Sprintf("```\n%s %2.0f%%\n```", drawBar(ratio, 16), ratio*100)
return c.Reply(bar, &tele.SendOptions{ParseMode: tele.ModeMarkdown})
}