2022-08-24 14:06:00 +00:00
|
|
|
package handler
|
2022-07-28 03:04:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-08-23 16:52:36 +00:00
|
|
|
"git.kill0.net/chill9/beepboop/bot"
|
2022-07-28 03:04:02 +00:00
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-08-04 04:52:08 +00:00
|
|
|
TimeHandler struct {
|
2022-08-23 16:52:36 +00:00
|
|
|
config bot.Config
|
2022-08-27 14:00:35 +00:00
|
|
|
Name string
|
2022-08-04 04:52:08 +00:00
|
|
|
}
|
2022-07-28 03:04:02 +00:00
|
|
|
)
|
|
|
|
|
2022-08-27 14:00:35 +00:00
|
|
|
func NewTimeHandler(s string) *TimeHandler {
|
|
|
|
h := new(TimeHandler)
|
|
|
|
h.Name = s
|
|
|
|
return h
|
2022-07-28 03:04:02 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 16:52:36 +00:00
|
|
|
func (h *TimeHandler) SetConfig(config bot.Config) {
|
2022-08-04 04:52:08 +00:00
|
|
|
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
|
|
|
}
|