2022-07-28 03:04:02 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-08-04 04:52:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-04 04:52:08 +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) {
|
2022-07-28 03:13:17 +00:00
|
|
|
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)
|
|
|
|
|
2022-07-28 03:13:17 +00:00
|
|
|
if len(x) > 2 {
|
2022-07-28 03:04:02 +00:00
|
|
|
s.ChannelMessageSend(m.ChannelID, "help: `!time TIMEZONE`")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-28 03:13:17 +00:00
|
|
|
now := time.Now()
|
2022-07-28 03:04:02 +00:00
|
|
|
|
2022-07-28 03:13:17 +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
|
|
|
}
|
|
|
|
|
2022-07-28 03:13:17 +00:00
|
|
|
s.ChannelMessageSend(m.ChannelID, fmt.Sprint(t))
|
2022-07-28 03:04:02 +00:00
|
|
|
}
|