bb/command/time.go

64 lines
981 B
Go
Raw Normal View History

2022-07-28 03:04:02 +00:00
package command
import (
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
type (
TimeHandler struct {
config Config
}
2022-07-28 03:04:02 +00:00
)
func NewTimeHandler() *TimeHandler {
2022-07-28 04:43:08 +00:00
return new(TimeHandler)
2022-07-28 03:04:02 +00:00
}
func (h *TimeHandler) SetConfig(config Config) {
h.config = config
}
2022-07-28 03:04:02 +00:00
func (h *TimeHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
var (
t time.Time
tz string
)
2022-07-28 03:04:02 +00:00
if m.Author.ID == s.State.User.ID {
return
}
if !strings.HasPrefix(m.Content, "!time") {
return
}
x := strings.SplitN(m.Content, " ", 2)
if len(x) > 2 {
2022-07-28 03:04:02 +00:00
s.ChannelMessageSend(m.ChannelID, "help: `!time TIMEZONE`")
return
}
now := time.Now()
2022-07-28 03:04:02 +00:00
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
2022-07-28 03:04:02 +00:00
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprint(t))
2022-07-28 03:04:02 +00:00
}