From de1e78459a58912435b502d8ae39afccfdcdc686 Mon Sep 17 00:00:00 2001 From: Kichiyaki Date: Sun, 19 Jul 2020 13:40:53 +0200 Subject: [PATCH] the addgroup command is fully translatable --- discord/admin_commands.go | 51 +++++++++++++++++++---------- discord/discord.go | 2 +- message/translations/active.en.json | 3 ++ 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/discord/admin_commands.go b/discord/admin_commands.go index 4399fbc..c61362f 100644 --- a/discord/admin_commands.go +++ b/discord/admin_commands.go @@ -6,14 +6,16 @@ import ( "strconv" "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/utils" shared_models "github.com/tribalwarshelp/shared/models" ) const ( - ObservationsPerGroup = 10 - GroupsPerServer = 5 + observationsPerGroup = 10 + groupsPerServer = 5 ) const ( @@ -30,22 +32,22 @@ const ( 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 { return } - server := &models.Server{ - ID: m.GuildID, - } - if err := s.cfg.ServerRepository.Store(context.Background(), server); err != nil { + if len(ctx.server.Groups) >= groupsPerServer { s.SendMessage(m.ChannelID, - fmt.Sprintf("%s Nie udało się dodać grupy", m.Author.Mention())) - return - } - if len(server.Groups) >= GroupsPerServer { - s.SendMessage(m.ChannelID, - m.Author.Mention()+fmt.Sprintf(` Osiągnięto limit grup na serwerze (%d/%d).`, GroupsPerServer, GroupsPerServer)) + ctx.localizer.MustLocalize(&i18n.LocalizeConfig{ + MessageID: "addGroup.groupLimitHasBeenReached", + DefaultMessage: message.FallbackMsg("addGroup.groupLimitHasBeenReached", "{{.Mention}} The group limit has been reached ({{.Total}}/{{.Limit}})."), + TemplateData: map[string]interface{}{ + "Mention": m.Author.Mention(), + "Total": len(ctx.server.Groups), + "Limit": groupsPerServer, + }, + })) return } @@ -53,17 +55,30 @@ func (s *Session) handleAddGroupCommand(m *discordgo.MessageCreate) { ServerID: m.GuildID, ShowEnnobledBarbarians: true, } + if err := s.cfg.GroupRepository.Store(context.Background(), group); err != nil { 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 } 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) { - if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return } @@ -356,9 +371,9 @@ func (s *Session) handleObserveCommand(m *discordgo.MessageCreate, args ...strin return } - if len(group.Observations) >= ObservationsPerGroup { + if len(group.Observations) >= observationsPerGroup { 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 } diff --git a/discord/discord.go b/discord/discord.go index e5c6690..f5e770f 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -109,7 +109,7 @@ func (s *Session) handleNewMessage(_ *discordgo.Session, m *discordgo.MessageCre s.handleTribeCommand(ctx, m, args...) case AddGroupCommand.WithPrefix(s.cfg.CommandPrefix): - s.handleAddGroupCommand(m) + s.handleAddGroupCommand(ctx, m) case DeleteGroupCommand.WithPrefix(s.cfg.CommandPrefix): s.handleDeleteGroupCommand(m, args...) case GroupsCommand.WithPrefix(s.cfg.CommandPrefix): diff --git a/message/translations/active.en.json b/message/translations/active.en.json index b46e4cd..fd8bf53 100644 --- a/message/translations/active.en.json +++ b/message/translations/active.en.json @@ -33,6 +33,9 @@ "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", + "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.", "pagination.labelDisplayedPage": "Page: {{.Page}} from {{.MaxPage}}",