refactor, add default values to messageData constructor
This commit is contained in:
parent
a724fe29b9
commit
4115558ee7
65
cron/cron.go
65
cron/cron.go
|
@ -2,18 +2,14 @@ package cron
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tribalwarshelp/golang-sdk/sdk"
|
"github.com/tribalwarshelp/golang-sdk/sdk"
|
||||||
shared_models "github.com/tribalwarshelp/shared/models"
|
|
||||||
|
|
||||||
"github.com/tribalwarshelp/dcbot/discord"
|
"github.com/tribalwarshelp/dcbot/discord"
|
||||||
"github.com/tribalwarshelp/dcbot/server"
|
"github.com/tribalwarshelp/dcbot/server"
|
||||||
"github.com/tribalwarshelp/dcbot/tribalwars"
|
|
||||||
"github.com/tribalwarshelp/dcbot/tribe"
|
"github.com/tribalwarshelp/dcbot/tribe"
|
||||||
"github.com/tribalwarshelp/dcbot/utils"
|
|
||||||
|
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
)
|
)
|
||||||
|
@ -61,6 +57,7 @@ func (h *handler) loadEnnoblements(worlds []string) map[string]ennoblements {
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("%s: %s", w, err.Error())
|
log.Printf("%s: %s", w, err.Error())
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
m[w] = filterEnnoblements(es, h.since)
|
m[w] = filterEnnoblements(es, h.since)
|
||||||
}
|
}
|
||||||
|
@ -115,63 +112,3 @@ func (h *handler) checkEnnoblements() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatDateOfConquest(loc *time.Location, t time.Time) string {
|
|
||||||
return t.In(loc).Format("15:04:05")
|
|
||||||
}
|
|
||||||
|
|
||||||
type messageData struct {
|
|
||||||
world string
|
|
||||||
date string
|
|
||||||
village string
|
|
||||||
oldOwnerName string
|
|
||||||
oldOwnerTribeTag string
|
|
||||||
newOwnerName string
|
|
||||||
newOwnerTribeTag string
|
|
||||||
}
|
|
||||||
|
|
||||||
func newMessageData(world string, ennoblement *shared_models.Ennoblement) messageData {
|
|
||||||
data := messageData{
|
|
||||||
date: formatDateOfConquest(utils.GetLocation(tribalwars.LanguageCodeFromWorldName(world)), ennoblement.EnnobledAt),
|
|
||||||
world: world,
|
|
||||||
}
|
|
||||||
if !isVillageNil(ennoblement.Village) {
|
|
||||||
data.village = fmt.Sprintf("%s (%d|%d)", ennoblement.Village.Name, ennoblement.Village.X, ennoblement.Village.Y)
|
|
||||||
}
|
|
||||||
if !isPlayerNil(ennoblement.OldOwner) {
|
|
||||||
data.oldOwnerName = ennoblement.OldOwner.Name
|
|
||||||
}
|
|
||||||
if !isPlayerTribeNil(ennoblement.OldOwner) {
|
|
||||||
data.oldOwnerTribeTag = ennoblement.OldOwner.Tribe.Tag
|
|
||||||
}
|
|
||||||
if !isPlayerNil(ennoblement.NewOwner) {
|
|
||||||
data.newOwnerName = ennoblement.NewOwner.Name
|
|
||||||
}
|
|
||||||
if !isPlayerTribeNil(ennoblement.NewOwner) {
|
|
||||||
data.newOwnerTribeTag = ennoblement.NewOwner.Tribe.Tag
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
func formatMsgAboutVillageLost(msgData messageData) string {
|
|
||||||
|
|
||||||
return fmt.Sprintf(`**%s** %s: Wioska %s (właściciel: %s [%s]) została stracona na rzecz gracza %s (%s)`,
|
|
||||||
msgData.world,
|
|
||||||
msgData.date,
|
|
||||||
msgData.village,
|
|
||||||
msgData.oldOwnerName,
|
|
||||||
msgData.oldOwnerTribeTag,
|
|
||||||
msgData.newOwnerName,
|
|
||||||
msgData.newOwnerTribeTag)
|
|
||||||
}
|
|
||||||
|
|
||||||
func formatMsgAboutVillageConquest(msgData messageData) string {
|
|
||||||
return fmt.Sprintf(`**%s** %s: Gracz %s (%s) podbił wioskę %s od gracza %s (%s)`,
|
|
||||||
msgData.world,
|
|
||||||
msgData.date,
|
|
||||||
msgData.newOwnerName,
|
|
||||||
msgData.newOwnerTribeTag,
|
|
||||||
msgData.village,
|
|
||||||
msgData.oldOwnerName,
|
|
||||||
msgData.oldOwnerTribeTag)
|
|
||||||
}
|
|
||||||
|
|
|
@ -28,3 +28,7 @@ func isPlayerTribeNil(player *shared_models.Player) bool {
|
||||||
func isVillageNil(village *shared_models.Village) bool {
|
func isVillageNil(village *shared_models.Village) bool {
|
||||||
return village == nil
|
return village == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatDateOfConquest(loc *time.Location, t time.Time) string {
|
||||||
|
return t.In(loc).Format("15:04:05")
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
package cron
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/tribalwarshelp/dcbot/utils"
|
||||||
|
shared_models "github.com/tribalwarshelp/shared/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type messageData struct {
|
||||||
|
world string
|
||||||
|
date string
|
||||||
|
village string
|
||||||
|
oldOwnerName string
|
||||||
|
oldOwnerTribeTag string
|
||||||
|
newOwnerName string
|
||||||
|
newOwnerTribeTag string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMessageData(world string, ennoblement *shared_models.Ennoblement) messageData {
|
||||||
|
data := messageData{
|
||||||
|
date: formatDateOfConquest(utils.GetLocation(utils.LanguageCodeFromWorldName(world)), ennoblement.EnnobledAt),
|
||||||
|
world: world,
|
||||||
|
village: "-",
|
||||||
|
oldOwnerName: "-",
|
||||||
|
oldOwnerTribeTag: "-",
|
||||||
|
newOwnerName: "-",
|
||||||
|
newOwnerTribeTag: "-",
|
||||||
|
}
|
||||||
|
if !isVillageNil(ennoblement.Village) {
|
||||||
|
data.village = fmt.Sprintf("%s (%d|%d)", ennoblement.Village.Name, ennoblement.Village.X, ennoblement.Village.Y)
|
||||||
|
}
|
||||||
|
if !isPlayerNil(ennoblement.OldOwner) {
|
||||||
|
data.oldOwnerName = ennoblement.OldOwner.Name
|
||||||
|
}
|
||||||
|
if !isPlayerTribeNil(ennoblement.OldOwner) {
|
||||||
|
data.oldOwnerTribeTag = ennoblement.OldOwner.Tribe.Tag
|
||||||
|
}
|
||||||
|
if !isPlayerNil(ennoblement.NewOwner) {
|
||||||
|
data.newOwnerName = ennoblement.NewOwner.Name
|
||||||
|
}
|
||||||
|
if !isPlayerTribeNil(ennoblement.NewOwner) {
|
||||||
|
data.newOwnerTribeTag = ennoblement.NewOwner.Tribe.Tag
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatMsgAboutVillageLost(msgData messageData) string {
|
||||||
|
return fmt.Sprintf(`**%s** %s: Wioska %s gracza %s [%s] została stracona na rzecz %s (%s)`,
|
||||||
|
msgData.world,
|
||||||
|
msgData.date,
|
||||||
|
msgData.village,
|
||||||
|
msgData.oldOwnerName,
|
||||||
|
msgData.oldOwnerTribeTag,
|
||||||
|
msgData.newOwnerName,
|
||||||
|
msgData.newOwnerTribeTag)
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatMsgAboutVillageConquest(msgData messageData) string {
|
||||||
|
return fmt.Sprintf(`**%s** %s: Gracz %s (%s) podbił wioskę %s od gracza %s (%s)`,
|
||||||
|
msgData.world,
|
||||||
|
msgData.date,
|
||||||
|
msgData.newOwnerName,
|
||||||
|
msgData.newOwnerTribeTag,
|
||||||
|
msgData.village,
|
||||||
|
msgData.oldOwnerName,
|
||||||
|
msgData.oldOwnerTribeTag)
|
||||||
|
}
|
|
@ -1,12 +1,5 @@
|
||||||
package discord
|
package discord
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Command string
|
type Command string
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -30,59 +23,3 @@ func (cmd Command) String() string {
|
||||||
func (cmd Command) WithPrefix(prefix string) string {
|
func (cmd Command) WithPrefix(prefix string) string {
|
||||||
return prefix + cmd.String()
|
return prefix + cmd.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) sendHelpMessage(channelID string) {
|
|
||||||
embed := &discordgo.MessageEmbed{
|
|
||||||
Author: &discordgo.MessageEmbedAuthor{},
|
|
||||||
Color: discordEmbedColor,
|
|
||||||
Title: "Pomoc",
|
|
||||||
Description: "Komendy oferowane przez bota",
|
|
||||||
Fields: []*discordgo.MessageEmbedField{
|
|
||||||
&discordgo.MessageEmbedField{
|
|
||||||
Name: "Dla wszystkich",
|
|
||||||
Value: fmt.Sprintf(`
|
|
||||||
- %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RA z plemion o podanych id
|
|
||||||
- %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RO z plemion o podanych id
|
|
||||||
- %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RW z plemion o podanych id
|
|
||||||
- %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największej liczbie pokonanych z plemion o podanych id
|
|
||||||
- %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największej liczbie punktów z plemion o podanych id
|
|
||||||
`,
|
|
||||||
TopAttCommand.WithPrefix(s.cfg.CommandPrefix),
|
|
||||||
TopDefCommand.WithPrefix(s.cfg.CommandPrefix),
|
|
||||||
TopSuppCommand.WithPrefix(s.cfg.CommandPrefix),
|
|
||||||
TopTotalCommand.WithPrefix(s.cfg.CommandPrefix),
|
|
||||||
TopPointsCommand.WithPrefix(s.cfg.CommandPrefix)),
|
|
||||||
Inline: false,
|
|
||||||
},
|
|
||||||
&discordgo.MessageEmbedField{
|
|
||||||
Name: "Dla adminów",
|
|
||||||
Value: fmt.Sprintf(`
|
|
||||||
- %s [świat] [id] - dodaje plemię z danego świata do obserwowanych
|
|
||||||
- %s - wyświetla wszystkie obserwowane plemiona
|
|
||||||
- %s [id z %s] - usuwa plemię z obserwowanych
|
|
||||||
- %s - ustawia kanał na którym będą wyświetlać się informacje o straconych wioskach
|
|
||||||
- %s - ustawia kanał na którym będą wyświetlać się informacje o podbitych wioskach
|
|
||||||
`,
|
|
||||||
AddCommand.WithPrefix(s.cfg.CommandPrefix),
|
|
||||||
ListCommand.WithPrefix(s.cfg.CommandPrefix),
|
|
||||||
DeleteCommand.WithPrefix(s.cfg.CommandPrefix),
|
|
||||||
ListCommand.WithPrefix(s.cfg.CommandPrefix),
|
|
||||||
LostVillagesCommand.WithPrefix(s.cfg.CommandPrefix),
|
|
||||||
ConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix)),
|
|
||||||
Inline: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Footer: &discordgo.MessageEmbedFooter{
|
|
||||||
Text: "https://dawid-wysokinski.pl/",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
s.dg.ChannelMessageSendEmbed(channelID, embed)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Session) sendUnknownCommandError(mention, channelID string, command ...string) {
|
|
||||||
s.SendMessage(channelID, mention+` Nieznana komenda: `+strings.Join(command, " "))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Session) SendMessage(channelID, message string) {
|
|
||||||
s.dg.ChannelMessageSend(channelID, message)
|
|
||||||
}
|
|
||||||
|
|
|
@ -391,7 +391,7 @@ func (s *Session) handleListCommand(m *discordgo.MessageCreate) {
|
||||||
Color: discordEmbedColor,
|
Color: discordEmbedColor,
|
||||||
Fields: []*discordgo.MessageEmbedField{
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
&discordgo.MessageEmbedField{
|
&discordgo.MessageEmbedField{
|
||||||
Name: "ID - świat - ID plemienia",
|
Name: "Indeks. ID - świat - ID plemienia",
|
||||||
Value: msg,
|
Value: msg,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -437,3 +437,59 @@ func (s *Session) memberHasPermission(guildID string, userID string, permission
|
||||||
|
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Session) sendHelpMessage(channelID string) {
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Author: &discordgo.MessageEmbedAuthor{},
|
||||||
|
Color: discordEmbedColor,
|
||||||
|
Title: "Pomoc",
|
||||||
|
Description: "Komendy oferowane przez bota",
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
|
&discordgo.MessageEmbedField{
|
||||||
|
Name: "Dla wszystkich",
|
||||||
|
Value: fmt.Sprintf(`
|
||||||
|
- %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RA z plemion o podanych id
|
||||||
|
- %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RO z plemion o podanych id
|
||||||
|
- %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RW z plemion o podanych id
|
||||||
|
- %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największej liczbie pokonanych z plemion o podanych id
|
||||||
|
- %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największej liczbie punktów z plemion o podanych id
|
||||||
|
`,
|
||||||
|
TopAttCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||||
|
TopDefCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||||
|
TopSuppCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||||
|
TopTotalCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||||
|
TopPointsCommand.WithPrefix(s.cfg.CommandPrefix)),
|
||||||
|
Inline: false,
|
||||||
|
},
|
||||||
|
&discordgo.MessageEmbedField{
|
||||||
|
Name: "Dla adminów",
|
||||||
|
Value: fmt.Sprintf(`
|
||||||
|
- %s [świat] [id] - dodaje plemię z danego świata do obserwowanych
|
||||||
|
- %s - wyświetla wszystkie obserwowane plemiona
|
||||||
|
- %s [id z %s] - usuwa plemię z obserwowanych
|
||||||
|
- %s - ustawia kanał na którym będą wyświetlać się informacje o straconych wioskach
|
||||||
|
- %s - ustawia kanał na którym będą wyświetlać się informacje o podbitych wioskach
|
||||||
|
`,
|
||||||
|
AddCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||||
|
ListCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||||
|
DeleteCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||||
|
ListCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||||
|
LostVillagesCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||||
|
ConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix)),
|
||||||
|
Inline: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Footer: &discordgo.MessageEmbedFooter{
|
||||||
|
Text: "https://dawid-wysokinski.pl/",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
s.dg.ChannelMessageSendEmbed(channelID, embed)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) sendUnknownCommandError(mention, channelID string, command ...string) {
|
||||||
|
s.SendMessage(channelID, mention+` Nieznana komenda: `+strings.Join(command, " "))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) SendMessage(channelID, message string) {
|
||||||
|
s.dg.ChannelMessageSend(channelID, message)
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package tribalwars
|
package utils
|
||||||
|
|
||||||
func LanguageCodeFromWorldName(world string) string {
|
func LanguageCodeFromWorldName(world string) string {
|
||||||
if len(world) < 2 {
|
if len(world) < 2 {
|
Reference in New Issue