This repository has been archived on 2022-10-02. You can view files and clone it, but cannot push or open issues or pull requests.
dcbot-old/discord/coords_translation.go

152 lines
4.8 KiB
Go
Raw Normal View History

2020-08-09 15:20:23 +00:00
package discord
import (
"context"
"github.com/tribalwarshelp/shared/tw/twmodel"
"github.com/tribalwarshelp/shared/tw/twurlbuilder"
2020-08-09 15:20:23 +00:00
"regexp"
"github.com/bwmarrin/discordgo"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/tribalwarshelp/golang-sdk/sdk"
2020-08-09 15:20:23 +00:00
"github.com/tribalwarshelp/dcbot/message"
"github.com/tribalwarshelp/dcbot/util/twutil"
2020-08-09 15:20:23 +00:00
)
const (
coordsLimit = 20
CoordsTranslationCommand Command = "coordstranslation"
DisableCoordsTranslationCommand Command = "disablecoordstranslation"
)
var coordsRegex = regexp.MustCompile(`(\d+)\|(\d+)`)
func (s *Session) handleCoordsTranslationCommand(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
2020-08-09 15:20:23 +00:00
argsLength := len(args)
if argsLength != 1 {
2020-08-09 15:20:23 +00:00
s.SendMessage(m.ChannelID,
m.Author.Mention()+" "+ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: message.HelpCoordsTranslation,
DefaultMessage: message.FallbackMsg(message.HelpCoordsTranslation,
2020-09-03 13:13:49 +00:00
"**{{.Command}}** [server] - enables coords translation feature."),
2020-08-09 15:20:23 +00:00
TemplateData: map[string]interface{}{
"Command": CoordsTranslationCommand.WithPrefix(s.cfg.CommandPrefix),
},
}))
return
}
serverKey := args[0]
server, err := s.cfg.API.Server.Read(serverKey, nil)
2020-08-09 15:20:23 +00:00
if err != nil || server == nil {
s.SendMessage(m.ChannelID,
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: message.CoordsTranslationServerNotFound,
DefaultMessage: message.FallbackMsg(message.CoordsTranslationServerNotFound, "{{.Mention}} Server not found."),
2020-08-09 15:20:23 +00:00
TemplateData: map[string]interface{}{
"Mention": m.Author.Mention(),
},
}))
return
}
ctx.server.CoordsTranslation = serverKey
go s.cfg.ServerRepository.Update(context.Background(), ctx.server)
s.SendMessage(m.ChannelID,
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: message.CoordsTranslationSuccess,
DefaultMessage: message.FallbackMsg(message.CoordsTranslationSuccess,
2020-08-09 15:20:23 +00:00
"{{.Mention}} Coords translation feature has been enabled."),
TemplateData: map[string]interface{}{
"Mention": m.Author.Mention(),
},
}))
}
func (s *Session) handleDisableCoordsTranslationCommand(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
2020-08-09 15:20:23 +00:00
ctx.server.CoordsTranslation = ""
go s.cfg.ServerRepository.Update(context.Background(), ctx.server)
s.SendMessage(m.ChannelID,
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: message.DisableCoordsTranslationSuccess,
DefaultMessage: message.FallbackMsg(message.DisableCoordsTranslationSuccess,
2020-08-09 15:20:23 +00:00
"{{.Mention}} Coords translation feature has been disabled."),
TemplateData: map[string]interface{}{
"Mention": m.Author.Mention(),
},
}))
}
func (s *Session) translateCoords(ctx *commandCtx, m *discordgo.MessageCreate) {
2020-08-09 15:20:23 +00:00
if ctx.server.CoordsTranslation == "" {
return
}
coords := coordsRegex.FindAllString(m.Content, -1)
2020-08-09 15:20:23 +00:00
coordsLen := len(coords)
if coordsLen > 0 {
version, err := s.cfg.API.Version.Read(twmodel.VersionCodeFromServerKey(ctx.server.CoordsTranslation))
if err != nil || version == nil {
2020-08-09 15:20:23 +00:00
return
}
if coordsLen > coordsLimit {
coords = coords[0:coordsLimit]
}
list, err := s.cfg.API.Village.Browse(ctx.server.CoordsTranslation,
0,
0,
[]string{},
&twmodel.VillageFilter{
2020-08-09 15:20:23 +00:00
XY: coords,
},
&sdk.VillageInclude{
2020-08-09 15:20:23 +00:00
Player: true,
PlayerInclude: sdk.PlayerInclude{
Tribe: true,
},
},
)
2021-05-06 15:15:10 +00:00
if err != nil || list == nil || len(list.Items) <= 0 {
2020-08-09 15:20:23 +00:00
return
}
msg := &MessageEmbed{}
2020-08-09 15:20:23 +00:00
for _, village := range list.Items {
villageURL := twurlbuilder.BuildVillageURL(ctx.server.CoordsTranslation, version.Host, village.ID)
2020-08-09 15:20:23 +00:00
playerName := "-"
playerURL := ""
2021-05-06 13:25:09 +00:00
if !twutil.IsPlayerNil(village.Player) {
2020-08-09 15:20:23 +00:00
playerName = village.Player.Name
playerURL = twurlbuilder.BuildPlayerURL(ctx.server.CoordsTranslation, version.Host, village.Player.ID)
2020-08-09 15:20:23 +00:00
}
tribeName := "-"
tribeURL := ""
2021-05-06 13:25:09 +00:00
if !twutil.IsPlayerTribeNil(village.Player) {
2020-08-09 15:20:23 +00:00
tribeName = village.Player.Tribe.Name
tribeURL = twurlbuilder.BuildTribeURL(ctx.server.CoordsTranslation, version.Host, village.Player.Tribe.ID)
2020-08-09 15:20:23 +00:00
}
msg.Append(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: message.CoordsTranslationMessage,
DefaultMessage: message.FallbackMsg(message.CoordsTranslationMessage,
2020-08-09 15:20:23 +00:00
"{{.Village}} owned by {{.Player}} (Tribe: {{.Tribe}})."),
TemplateData: map[string]interface{}{
"Village": BuildLink(village.FullName(), villageURL),
"Player": BuildLink(playerName, playerURL),
"Tribe": BuildLink(tribeName, tribeURL),
2020-08-09 15:20:23 +00:00
},
2020-08-09 15:50:32 +00:00
}) + "\n")
2020-08-09 15:20:23 +00:00
}
s.SendEmbed(m.ChannelID, NewEmbed().
SetTitle(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: message.CoordsTranslationTitle,
DefaultMessage: message.FallbackMsg(message.CoordsTranslationTitle, "Villages"),
2020-08-09 15:20:23 +00:00
})).
SetFields(msg.ToMessageEmbedFields()).
MessageEmbed)
}
}