bot should send only one message per channel with all conquers/losess

This commit is contained in:
Dawid Wysokiński 2020-07-15 14:46:18 +02:00 committed by Kichiyaki
parent de90699827
commit 68babe0762
2 changed files with 35 additions and 34 deletions

View File

@ -113,12 +113,13 @@ func (h *handler) checkLastEnnoblements() {
if group.ConqueredVillagesChannelID == "" && group.LostVillagesChannelID == "" { if group.ConqueredVillagesChannelID == "" && group.LostVillagesChannelID == "" {
continue continue
} }
lostVillagesMsg := &discord.EmbedMessage{}
conqueredVillagesMsg := &discord.EmbedMessage{}
for _, observation := range group.Observations { for _, observation := range group.Observations {
ennoblements, ok := ennoblementsByServerKey[observation.Server] ennoblements, ok := ennoblementsByServerKey[observation.Server]
langVersion := utils.FindLangVersionByTag(langVersions, utils.LanguageTagFromWorldName(observation.Server)) langVersion := utils.FindLangVersionByTag(langVersions, utils.LanguageTagFromWorldName(observation.Server))
if ok && langVersion != nil && langVersion.Host != "" { if ok && langVersion != nil && langVersion.Host != "" {
if group.LostVillagesChannelID != "" { if group.LostVillagesChannelID != "" {
msg := &discord.EmbedMessage{}
for _, ennoblement := range ennoblements.getLostVillagesByTribe(observation.TribeID) { for _, ennoblement := range ennoblements.getLostVillagesByTribe(observation.TribeID) {
if !isPlayerTribeNil(ennoblement.NewOwner) && if !isPlayerTribeNil(ennoblement.NewOwner) &&
group.Observations.Contains(observation.Server, ennoblement.NewOwner.Tribe.ID) { group.Observations.Contains(observation.Server, ennoblement.NewOwner.Tribe.ID) {
@ -130,22 +131,11 @@ func (h *handler) checkLastEnnoblements() {
ennoblement: ennoblement, ennoblement: ennoblement,
t: messageTypeLost, t: messageTypeLost,
} }
msg.Append(newMessage(newMsgDataConfig).String()) lostVillagesMsg.Append(newMessage(newMsgDataConfig).String())
}
if !msg.IsEmpty() {
h.discord.SendEmbed(group.LostVillagesChannelID,
discord.
NewEmbed().
SetTitle("Stracone wioski").
SetColor(colorLostVillage).
SetFields(msg.ToMessageEmbedFields()).
SetTimestamp(formatDateOfConquest(time.Now())).
MessageEmbed)
} }
} }
if group.ConqueredVillagesChannelID != "" { if group.ConqueredVillagesChannelID != "" {
msg := &discord.EmbedMessage{}
for _, ennoblement := range ennoblements.getConqueredVillagesByTribe(observation.TribeID) { for _, ennoblement := range ennoblements.getConqueredVillagesByTribe(observation.TribeID) {
isBarbarian := isPlayerNil(ennoblement.OldOwner) || ennoblement.OldOwner.ID == 0 isBarbarian := isPlayerNil(ennoblement.OldOwner) || ennoblement.OldOwner.ID == 0
if (!isPlayerTribeNil(ennoblement.OldOwner) && if (!isPlayerTribeNil(ennoblement.OldOwner) &&
@ -159,21 +149,32 @@ func (h *handler) checkLastEnnoblements() {
ennoblement: ennoblement, ennoblement: ennoblement,
t: messageTypeConquer, t: messageTypeConquer,
} }
msg.Append(newMessage(newMsgDataConfig).String()) conqueredVillagesMsg.Append(newMessage(newMsgDataConfig).String())
}
if !msg.IsEmpty() {
h.discord.SendEmbed(group.ConqueredVillagesChannelID,
discord.
NewEmbed().
SetTitle("Podbite wioski").
SetColor(colorConqueredVillage).
SetFields(msg.ToMessageEmbedFields()).
SetTimestamp(formatDateOfConquest(time.Now())).
MessageEmbed)
} }
} }
} }
} }
if group.ConqueredVillagesChannelID != "" && !conqueredVillagesMsg.IsEmpty() {
h.discord.SendEmbed(group.ConqueredVillagesChannelID,
discord.
NewEmbed().
SetTitle("Stracone wioski").
SetColor(colorLostVillage).
SetFields(conqueredVillagesMsg.ToMessageEmbedFields()).
SetTimestamp(formatDateOfConquest(time.Now())).
MessageEmbed)
}
if group.LostVillagesChannelID != "" && !lostVillagesMsg.IsEmpty() {
h.discord.SendEmbed(group.LostVillagesChannelID,
discord.
NewEmbed().
SetTitle("Stracone wioski").
SetColor(colorLostVillage).
SetFields(lostVillagesMsg.ToMessageEmbedFields()).
SetTimestamp(formatDateOfConquest(time.Now())).
MessageEmbed)
}
} }
log.Printf("checkLastEnnoblements: finished in %s", time.Since(start).String()) log.Printf("checkLastEnnoblements: finished in %s", time.Since(start).String())

View File

@ -253,36 +253,36 @@ func (e *Embed) TruncateFooter() *Embed {
} }
type EmbedMessage struct { type EmbedMessage struct {
Chunks []string chunks []string
Index int index int
mutex sync.Mutex mutex sync.Mutex
} }
func (msg *EmbedMessage) IsEmpty() bool { func (msg *EmbedMessage) IsEmpty() bool {
return len(msg.Chunks) == 0 return len(msg.chunks) == 0
} }
func (msg *EmbedMessage) Append(m string) { func (msg *EmbedMessage) Append(m string) {
msg.mutex.Lock() msg.mutex.Lock()
defer msg.mutex.Unlock() defer msg.mutex.Unlock()
for len(msg.Chunks) < msg.Index+1 { for len(msg.chunks) < msg.index+1 {
msg.Chunks = append(msg.Chunks, "") msg.chunks = append(msg.chunks, "")
} }
if len(m)+len(msg.Chunks[msg.Index]) > EmbedLimitFieldValue { if len(m)+len(msg.chunks[msg.index]) > EmbedLimitFieldValue {
msg.Chunks = append(msg.Chunks, m) msg.chunks = append(msg.chunks, m)
msg.Index++ msg.index++
return return
} }
msg.Chunks[msg.Index] += m msg.chunks[msg.index] += m
} }
func (msg *EmbedMessage) ToMessageEmbedFields() []*discordgo.MessageEmbedField { func (msg *EmbedMessage) ToMessageEmbedFields() []*discordgo.MessageEmbedField {
msg.mutex.Lock() msg.mutex.Lock()
defer msg.mutex.Unlock() defer msg.mutex.Unlock()
fields := []*discordgo.MessageEmbedField{} fields := []*discordgo.MessageEmbedField{}
for _, chunk := range msg.Chunks { for _, chunk := range msg.chunks {
fields = append(fields, &discordgo.MessageEmbedField{ fields = append(fields, &discordgo.MessageEmbedField{
Name: "-", Name: "-",
Value: chunk, Value: chunk,