Simplify presence module and make better readable

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-08-02 13:34:19 +02:00
parent d7fa78acdf
commit 19c1c4d7b5
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"math" "math"
"strings"
"time" "time"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -87,7 +86,7 @@ func (m modPresence) cronUpdatePresence() {
// @attr fallback_text required string "" What to set the text to when no stream is found (`playing <text>`) // @attr fallback_text required string "" What to set the text to when no stream is found (`playing <text>`)
status := m.attrs.MustString("fallback_text", nil) status := m.attrs.MustString("fallback_text", nil)
if nextStream != nil { if nextStream != nil {
status = fmt.Sprintf("in: %s", m.durationToHumanReadable(time.Since(*nextStream))) status = m.durationToHumanReadable(time.Since(*nextStream))
} }
if err := m.discord.UpdateGameStatus(0, status); err != nil { if err := m.discord.UpdateGameStatus(0, status); err != nil {
@ -99,24 +98,18 @@ func (m modPresence) cronUpdatePresence() {
func (m modPresence) durationToHumanReadable(d time.Duration) string { func (m modPresence) durationToHumanReadable(d time.Duration) string {
d = time.Duration(math.Abs(float64(d))) d = time.Duration(math.Abs(float64(d)))
if d > presenceTimeDay { if d > presenceTimeDay {
return fmt.Sprintf("%.0f Tagen", math.Round(float64(d)/float64(presenceTimeDay))) return fmt.Sprintf("in %.0f Tagen", math.Round(float64(d)/float64(presenceTimeDay)))
} }
var elements []string if d > time.Hour {
return fmt.Sprintf("in %.0f Stunden", math.Round(float64(d)/float64(time.Hour)))
for div, req := range map[time.Duration]bool{
time.Hour: true,
time.Minute: true,
} {
if d < div && !req {
continue
} }
pt := d / div if d > time.Minute {
d -= pt * div return fmt.Sprintf("in %.0f Minuten", math.Round(float64(d)/float64(time.Minute)))
elements = append(elements, fmt.Sprintf("%.2d", pt))
} }
return strings.Join(elements, ":") return "gleich"
} }