the addgroup command is fully translatable

This commit is contained in:
Dawid Wysokiński 2020-07-19 13:40:53 +02:00 committed by Kichiyaki
parent decb8c63a5
commit de1e78459a
3 changed files with 37 additions and 19 deletions

View File

@ -6,14 +6,16 @@ import (
"strconv" "strconv"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/tribalwarshelp/dcbot/message"
"github.com/tribalwarshelp/dcbot/models" "github.com/tribalwarshelp/dcbot/models"
"github.com/tribalwarshelp/dcbot/utils" "github.com/tribalwarshelp/dcbot/utils"
shared_models "github.com/tribalwarshelp/shared/models" shared_models "github.com/tribalwarshelp/shared/models"
) )
const ( const (
ObservationsPerGroup = 10 observationsPerGroup = 10
GroupsPerServer = 5 groupsPerServer = 5
) )
const ( const (
@ -30,22 +32,22 @@ const (
UnObserveConqueredVillagesCommand Command = "unobserveconqueredvillages" UnObserveConqueredVillagesCommand Command = "unobserveconqueredvillages"
) )
func (s *Session) handleAddGroupCommand(m *discordgo.MessageCreate) { func (s *Session) handleAddGroupCommand(ctx commandCtx, m *discordgo.MessageCreate) {
if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has {
return return
} }
server := &models.Server{ if len(ctx.server.Groups) >= groupsPerServer {
ID: m.GuildID,
}
if err := s.cfg.ServerRepository.Store(context.Background(), server); err != nil {
s.SendMessage(m.ChannelID, s.SendMessage(m.ChannelID,
fmt.Sprintf("%s Nie udało się dodać grupy", m.Author.Mention())) ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
return MessageID: "addGroup.groupLimitHasBeenReached",
} DefaultMessage: message.FallbackMsg("addGroup.groupLimitHasBeenReached", "{{.Mention}} The group limit has been reached ({{.Total}}/{{.Limit}})."),
if len(server.Groups) >= GroupsPerServer { TemplateData: map[string]interface{}{
s.SendMessage(m.ChannelID, "Mention": m.Author.Mention(),
m.Author.Mention()+fmt.Sprintf(` Osiągnięto limit grup na serwerze (%d/%d).`, GroupsPerServer, GroupsPerServer)) "Total": len(ctx.server.Groups),
"Limit": groupsPerServer,
},
}))
return return
} }
@ -53,17 +55,30 @@ func (s *Session) handleAddGroupCommand(m *discordgo.MessageCreate) {
ServerID: m.GuildID, ServerID: m.GuildID,
ShowEnnobledBarbarians: true, ShowEnnobledBarbarians: true,
} }
if err := s.cfg.GroupRepository.Store(context.Background(), group); err != nil { if err := s.cfg.GroupRepository.Store(context.Background(), group); err != nil {
s.SendMessage(m.ChannelID, s.SendMessage(m.ChannelID,
fmt.Sprintf("%s Nie udało się dodać grupy", m.Author.Mention())) ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "internalServerError",
DefaultMessage: message.FallbackMsg("internalServerError", "{{.Mention}} Internal server error occurred, please try again later."),
TemplateData: map[string]interface{}{
"Mention": m.Author.Mention(),
},
}))
return return
} }
s.SendMessage(m.ChannelID, s.SendMessage(m.ChannelID,
fmt.Sprintf("%s Utworzono nową grupę o ID %d.", m.Author.Mention(), group.ID)) ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "addGroup.success",
DefaultMessage: message.FallbackMsg("addGroup.success", "{{.Mention}} A new group has been created (ID: {{.ID}})."),
TemplateData: map[string]interface{}{
"Mention": m.Author.Mention(),
"ID": group.ID,
},
}))
} }
func (s *Session) handleDeleteGroupCommand(m *discordgo.MessageCreate, args ...string) { func (s *Session) handleDeleteGroupCommand(m *discordgo.MessageCreate, args ...string) {
if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has {
return return
} }
@ -356,9 +371,9 @@ func (s *Session) handleObserveCommand(m *discordgo.MessageCreate, args ...strin
return return
} }
if len(group.Observations) >= ObservationsPerGroup { if len(group.Observations) >= observationsPerGroup {
s.SendMessage(m.ChannelID, s.SendMessage(m.ChannelID,
m.Author.Mention()+fmt.Sprintf(` Osiągnięto limit plemion w grupie (%d/%d).`, ObservationsPerGroup, ObservationsPerGroup)) m.Author.Mention()+fmt.Sprintf(` Osiągnięto limit plemion w grupie (%d/%d).`, observationsPerGroup, observationsPerGroup))
return return
} }

View File

@ -109,7 +109,7 @@ func (s *Session) handleNewMessage(_ *discordgo.Session, m *discordgo.MessageCre
s.handleTribeCommand(ctx, m, args...) s.handleTribeCommand(ctx, m, args...)
case AddGroupCommand.WithPrefix(s.cfg.CommandPrefix): case AddGroupCommand.WithPrefix(s.cfg.CommandPrefix):
s.handleAddGroupCommand(m) s.handleAddGroupCommand(ctx, m)
case DeleteGroupCommand.WithPrefix(s.cfg.CommandPrefix): case DeleteGroupCommand.WithPrefix(s.cfg.CommandPrefix):
s.handleDeleteGroupCommand(m, args...) s.handleDeleteGroupCommand(m, args...)
case GroupsCommand.WithPrefix(s.cfg.CommandPrefix): case GroupsCommand.WithPrefix(s.cfg.CommandPrefix):

View File

@ -33,6 +33,9 @@
"tribe.exceededMaximumNumberOfPages": "{{.Mention}} You have exceeded the maximum number of pages ({{.Page}}/{{.MaxPage}}).", "tribe.exceededMaximumNumberOfPages": "{{.Mention}} You have exceeded the maximum number of pages ({{.Page}}/{{.MaxPage}}).",
"tribe.messageLine": "**{{.Index}}**. [``{{.PlayerName}}``]({{.PlayerURL}}) (Tribe: [``{{.TribeTag}}``]({{.TribeURL}}) | Rank: **{{.Rank}}** | Score: **{{.Score}}**)\n", "tribe.messageLine": "**{{.Index}}**. [``{{.PlayerName}}``]({{.PlayerURL}}) (Tribe: [``{{.TribeTag}}``]({{.TribeURL}}) | Rank: **{{.Rank}}** | Score: **{{.Score}}**)\n",
"addGroup.groupLimitHasBeenReached": "{{.Mention}} The group limit has been reached ({{.Total}}/{{.Limit}}).",
"addGroup.success": "{{.Mention}} A new group has been created (ID: {{.ID}}).",
"api.defaultError": "{{.Mention}} There was an error fetching data from the API, please try again later.", "api.defaultError": "{{.Mention}} There was an error fetching data from the API, please try again later.",
"pagination.labelDisplayedPage": "Page: {{.Page}} from {{.MaxPage}}", "pagination.labelDisplayedPage": "Page: {{.Page}} from {{.MaxPage}}",