From 19c1c4d7b5b2687786ab99c0fadd81bb90f01dff Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 2 Aug 2021 13:34:19 +0200 Subject: [PATCH] Simplify presence module and make better readable Signed-off-by: Knut Ahlers --- mod_presence.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/mod_presence.go b/mod_presence.go index afffc33..38aa7a5 100644 --- a/mod_presence.go +++ b/mod_presence.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "math" - "strings" "time" "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 `) status := m.attrs.MustString("fallback_text", 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 { @@ -99,24 +98,18 @@ func (m modPresence) cronUpdatePresence() { func (m modPresence) durationToHumanReadable(d time.Duration) string { d = time.Duration(math.Abs(float64(d))) + 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 - - for div, req := range map[time.Duration]bool{ - time.Hour: true, - time.Minute: true, - } { - if d < div && !req { - continue - } - - pt := d / div - d -= pt * div - elements = append(elements, fmt.Sprintf("%.2d", pt)) + if d > time.Hour { + return fmt.Sprintf("in %.0f Stunden", math.Round(float64(d)/float64(time.Hour))) } - return strings.Join(elements, ":") + if d > time.Minute { + return fmt.Sprintf("in %.0f Minuten", math.Round(float64(d)/float64(time.Minute))) + } + + return "gleich" }