From 85b2e2b99b0b3af83d2c58666fa813f3f8474245 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Wed, 27 Jul 2022 22:13:17 -0500 Subject: [PATCH] remove argument requirement for time command --- command/time.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/command/time.go b/command/time.go index f392024..312ed16 100644 --- a/command/time.go +++ b/command/time.go @@ -23,7 +23,10 @@ func NewTimeHandler() *TimeHandler { } func (h *TimeHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { - var tz string + var ( + t time.Time + tz string + ) if m.Author.ID == s.State.User.ID { return @@ -35,20 +38,25 @@ func (h *TimeHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { x := strings.SplitN(m.Content, " ", 2) - if len(x) != 2 { + if len(x) > 2 { s.ChannelMessageSend(m.ChannelID, "help: `!time TIMEZONE`") return } - tz = x[1] - - loc, err := time.LoadLocation(tz) - if err != nil { - log.Warnf("failed to load location: %s", err) - return - } - now := time.Now() - s.ChannelMessageSend(m.ChannelID, fmt.Sprint(now.In(loc))) + if len(x) == 2 { + tz = x[1] + loc, err := time.LoadLocation(tz) + if err != nil { + log.Warnf("failed to load location: %s", err) + s.ChannelMessageSend(m.ChannelID, err.Error()) + return + } + t = now.In(loc) + } else { + t = now + } + + s.ChannelMessageSend(m.ChannelID, fmt.Sprint(t)) }