fix app crash when sdk returns nil pointer
This commit is contained in:
parent
913cbf7ff0
commit
c944cd5e53
|
@ -75,7 +75,7 @@ func formatMsgLink(text string, url string) string {
|
||||||
if url == "" {
|
if url == "" {
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("[%s](%s)", text, url)
|
return fmt.Sprintf("[``%s``](%s)", text, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatMsgAboutVillageLost(msgData messageData) string {
|
func formatMsgAboutVillageLost(msgData messageData) string {
|
||||||
|
@ -88,7 +88,7 @@ func formatMsgAboutVillageLost(msgData messageData) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatMsgAboutVillageConquest(msgData messageData) string {
|
func formatMsgAboutVillageConquest(msgData messageData) string {
|
||||||
return fmt.Sprintf(`Gracz %s (%s) podbił wioskę %s od gracza %s (%s)`,
|
return fmt.Sprintf("Gracz %s (%s) podbił wioskę %s od gracza %s (%s)",
|
||||||
formatMsgLink(msgData.newOwnerName, msgData.newOwnerURL),
|
formatMsgLink(msgData.newOwnerName, msgData.newOwnerURL),
|
||||||
formatMsgLink(msgData.newOwnerTribeTag, msgData.newOwnerTribeURL),
|
formatMsgLink(msgData.newOwnerTribeTag, msgData.newOwnerTribeURL),
|
||||||
formatMsgLink(msgData.village, msgData.villageURL),
|
formatMsgLink(msgData.village, msgData.villageURL),
|
||||||
|
|
|
@ -6,8 +6,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"log"
|
|
||||||
|
|
||||||
shared_models "github.com/tribalwarshelp/shared/models"
|
shared_models "github.com/tribalwarshelp/shared/models"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
|
@ -107,7 +105,7 @@ func (s *Session) handleTribeCommand(m *discordgo.MessageCreate, args ...string)
|
||||||
ids := []int{}
|
ids := []int{}
|
||||||
for _, arg := range args[3:argsLength] {
|
for _, arg := range args[3:argsLength] {
|
||||||
id, err := strconv.Atoi(arg)
|
id, err := strconv.Atoi(arg)
|
||||||
if err != nil {
|
if err != nil || id <= 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ids = append(ids, id)
|
ids = append(ids, id)
|
||||||
|
@ -160,22 +158,33 @@ func (s *Session) handleTribeCommand(m *discordgo.MessageCreate, args ...string)
|
||||||
Tribe: true,
|
Tribe: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.SendMessage(m.ChannelID, fmt.Sprintf("%s Nie udało się wygenerować listy.", m.Author.Mention()))
|
s.SendMessage(m.ChannelID,
|
||||||
|
fmt.Sprintf("%s Wystąpił błąd podczas pobierania danych z API, prosimy spróbować później.", m.Author.Mention()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if playersList.Total == 0 {
|
if playersList == nil || playersList.Total == 0 {
|
||||||
s.SendMessage(m.ChannelID, fmt.Sprintf("%s Nie znaleziono plemion o podanych ID.", m.Author.Mention()))
|
s.SendMessage(m.ChannelID, fmt.Sprintf("%s Nie znaleziono graczy należących do plemion o podanych ID.", m.Author.Mention()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
totalPages := int(math.Ceil(float64(playersList.Total) / float64(limit)))
|
||||||
|
if page > totalPages {
|
||||||
|
s.SendMessage(m.ChannelID, fmt.Sprintf("%s Przekroczyłeś limit stron (%d/%d).", m.Author.Mention(), page, totalPages))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
langVersion, err := s.cfg.API.LangVersions.Read(utils.LanguageCodeFromWorldName(world))
|
langTag := utils.LanguageCodeFromWorldName(world)
|
||||||
if err != nil {
|
langVersion, err := s.cfg.API.LangVersions.Read(langTag)
|
||||||
s.SendMessage(m.ChannelID, fmt.Sprintf("%s Nie udało się wygenerować listy.", m.Author.Mention()))
|
if err != nil || langVersion == nil {
|
||||||
|
s.SendMessage(m.ChannelID, fmt.Sprintf("%s Nie znaleziono wersji językowej: %s.", m.Author.Mention(), langTag))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := &EmbedMessage{}
|
msg := &EmbedMessage{}
|
||||||
for i, player := range playersList.Items {
|
for i, player := range playersList.Items {
|
||||||
|
if player == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
rank := 0
|
rank := 0
|
||||||
score := 0
|
score := 0
|
||||||
switch command {
|
switch command {
|
||||||
|
@ -196,17 +205,23 @@ func (s *Session) handleTribeCommand(m *discordgo.MessageCreate, args ...string)
|
||||||
score = player.Points
|
score = player.Points
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.Append(fmt.Sprintf("**%d**. [%s](%s) (Plemię: [%s](%s) | Ranking ogólny: **%d** | Wynik: **%d**)\n",
|
tribeTag := "-"
|
||||||
|
tribeURL := "-"
|
||||||
|
if player.Tribe != nil {
|
||||||
|
tribeTag = player.Tribe.Tag
|
||||||
|
tribeURL = utils.FormatTribeURL(world, langVersion.Host, player.Tribe.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.Append(fmt.Sprintf("**%d**. [``%s``](%s) (Plemię: [``%s``](%s) | Ranking ogólny: **%d** | Wynik: **%d**)\n",
|
||||||
offset+i+1,
|
offset+i+1,
|
||||||
player.Name,
|
player.Name,
|
||||||
utils.FormatPlayerURL(world, langVersion.Host, player.ID),
|
utils.FormatPlayerURL(world, langVersion.Host, player.ID),
|
||||||
player.Tribe.Tag,
|
tribeTag,
|
||||||
utils.FormatTribeURL(world, langVersion.Host, player.Tribe.ID),
|
tribeURL,
|
||||||
rank,
|
rank,
|
||||||
score))
|
score))
|
||||||
}
|
}
|
||||||
|
|
||||||
totalPages := int(math.Round(float64(playersList.Total) / float64(limit)))
|
|
||||||
s.SendEmbed(m.ChannelID, NewEmbed().
|
s.SendEmbed(m.ChannelID, NewEmbed().
|
||||||
SetTitle(title).
|
SetTitle(title).
|
||||||
SetDescription("A oto lista!").
|
SetDescription("A oto lista!").
|
||||||
|
@ -308,7 +323,6 @@ func (s *Session) handleObserveCommand(m *discordgo.MessageCreate, args ...strin
|
||||||
}
|
}
|
||||||
err = s.cfg.ServerRepository.Store(context.Background(), dcServer)
|
err = s.cfg.ServerRepository.Store(context.Background(), dcServer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
|
||||||
s.SendMessage(m.ChannelID, m.Author.Mention()+` Nie udało się dodać plemienia do obserwowanych.`)
|
s.SendMessage(m.ChannelID, m.Author.Mention()+` Nie udało się dodać plemienia do obserwowanych.`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -324,7 +338,6 @@ func (s *Session) handleObserveCommand(m *discordgo.MessageCreate, args ...strin
|
||||||
ServerID: dcServer.ID,
|
ServerID: dcServer.ID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
|
||||||
s.SendMessage(m.ChannelID, m.Author.Mention()+` Nie udało się dodać plemienia do obserwowanych.`)
|
s.SendMessage(m.ChannelID, m.Author.Mention()+` Nie udało się dodać plemienia do obserwowanych.`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package discord
|
package discord
|
||||||
|
|
||||||
import "github.com/bwmarrin/discordgo"
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
// Constants for message embed character limits
|
// Constants for message embed character limits
|
||||||
const (
|
const (
|
||||||
|
@ -251,10 +255,13 @@ func (e *Embed) TruncateFooter() *Embed {
|
||||||
type EmbedMessage struct {
|
type EmbedMessage struct {
|
||||||
Chunks []string
|
Chunks []string
|
||||||
Index int
|
Index int
|
||||||
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *EmbedMessage) Append(m string) {
|
func (msg *EmbedMessage) Append(m string) {
|
||||||
if len(msg.Chunks) < msg.Index+1 {
|
msg.mutex.Lock()
|
||||||
|
defer msg.mutex.Unlock()
|
||||||
|
for len(msg.Chunks) < msg.Index+1 {
|
||||||
msg.Chunks = append(msg.Chunks, "")
|
msg.Chunks = append(msg.Chunks, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,6 +275,8 @@ func (msg *EmbedMessage) Append(m string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *EmbedMessage) ToMessageEmbedFields() []*discordgo.MessageEmbedField {
|
func (msg *EmbedMessage) ToMessageEmbedFields() []*discordgo.MessageEmbedField {
|
||||||
|
msg.mutex.Lock()
|
||||||
|
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{
|
||||||
|
|
Reference in New Issue
Block a user