From 634088712edc3cd1fbc5f4d140867adc4c497338 Mon Sep 17 00:00:00 2001 From: Kichiyaki Date: Sun, 19 Jul 2020 12:16:02 +0200 Subject: [PATCH] add initial i18n configuration --- discord/admin_commands.go | 42 ++++------------- discord/command.go | 10 ++++ discord/discord.go | 21 +++++++-- discord/public_commands.go | 71 +++++++++++++++++++---------- go.mod | 2 + go.sum | 21 ++++----- main.go | 11 +++++ message/bundle.go | 35 ++++++++++++++ message/helpers.go | 12 +++++ message/translations/active.en.json | 24 ++++++++++ models/server.go | 2 +- 11 files changed, 175 insertions(+), 76 deletions(-) create mode 100644 message/bundle.go create mode 100644 message/helpers.go create mode 100644 message/translations/active.en.json diff --git a/discord/admin_commands.go b/discord/admin_commands.go index d03fb72..4399fbc 100644 --- a/discord/admin_commands.go +++ b/discord/admin_commands.go @@ -31,10 +31,6 @@ const ( ) func (s *Session) handleAddGroupCommand(m *discordgo.MessageCreate) { - if m.GuildID == "" { - return - } - if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return } @@ -67,9 +63,6 @@ func (s *Session) handleAddGroupCommand(m *discordgo.MessageCreate) { fmt.Sprintf("%s Utworzono nową grupę o ID %d.", m.Author.Mention(), group.ID)) } func (s *Session) handleDeleteGroupCommand(m *discordgo.MessageCreate, args ...string) { - if m.GuildID == "" { - return - } if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return @@ -104,9 +97,7 @@ func (s *Session) handleDeleteGroupCommand(m *discordgo.MessageCreate, args ...s } func (s *Session) handleGroupsCommand(m *discordgo.MessageCreate) { - if m.GuildID == "" { - return - } + if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return } @@ -136,9 +127,6 @@ func (s *Session) handleGroupsCommand(m *discordgo.MessageCreate) { } func (s *Session) handleConqueredVillagesCommand(m *discordgo.MessageCreate, args ...string) { - if m.GuildID == "" { - return - } if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return @@ -181,9 +169,7 @@ func (s *Session) handleConqueredVillagesCommand(m *discordgo.MessageCreate, arg } func (s *Session) handleUnObserveConqueredVillagesCommand(m *discordgo.MessageCreate, args ...string) { - if m.GuildID == "" { - return - } + if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return } @@ -226,9 +212,7 @@ func (s *Session) handleUnObserveConqueredVillagesCommand(m *discordgo.MessageCr } func (s *Session) handleLostVillagesCommand(m *discordgo.MessageCreate, args ...string) { - if m.GuildID == "" { - return - } + if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return } @@ -271,9 +255,7 @@ func (s *Session) handleLostVillagesCommand(m *discordgo.MessageCreate, args ... } func (s *Session) handleUnObserveLostVillagesCommand(m *discordgo.MessageCreate, args ...string) { - if m.GuildID == "" { - return - } + if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return } @@ -319,9 +301,7 @@ func (s *Session) handleUnObserveLostVillagesCommand(m *discordgo.MessageCreate, } func (s *Session) handleObserveCommand(m *discordgo.MessageCreate, args ...string) { - if m.GuildID == "" { - return - } + if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return } @@ -396,9 +376,7 @@ func (s *Session) handleObserveCommand(m *discordgo.MessageCreate, args ...strin } func (s *Session) handleUnObserveCommand(m *discordgo.MessageCreate, args ...string) { - if m.GuildID == "" { - return - } + if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return } @@ -445,9 +423,7 @@ func (s *Session) handleUnObserveCommand(m *discordgo.MessageCreate, args ...str } func (s *Session) handleObservationsCommand(m *discordgo.MessageCreate, args ...string) { - if m.GuildID == "" { - return - } + if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return } @@ -546,9 +522,7 @@ func (s *Session) handleObservationsCommand(m *discordgo.MessageCreate, args ... } func (s *Session) handleShowEnnobledBarbariansCommand(m *discordgo.MessageCreate, args ...string) { - if m.GuildID == "" { - return - } + if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { return } diff --git a/discord/command.go b/discord/command.go index 30ea701..709ed59 100644 --- a/discord/command.go +++ b/discord/command.go @@ -1,5 +1,10 @@ package discord +import ( + "github.com/nicksnyder/go-i18n/v2/i18n" + "github.com/tribalwarshelp/dcbot/models" +) + type Command string func (cmd Command) String() string { @@ -9,3 +14,8 @@ func (cmd Command) String() string { func (cmd Command) WithPrefix(prefix string) string { return prefix + cmd.String() } + +type commandCtx struct { + server *models.Server + localizer *i18n.Localizer +} diff --git a/discord/discord.go b/discord/discord.go index 6729384..b64f3bf 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -1,10 +1,14 @@ package discord import ( + "context" "fmt" "strings" + "github.com/tribalwarshelp/dcbot/message" + "github.com/tribalwarshelp/dcbot/group" + "github.com/tribalwarshelp/dcbot/models" "github.com/tribalwarshelp/dcbot/observation" "github.com/tribalwarshelp/dcbot/server" "github.com/tribalwarshelp/golang-sdk/sdk" @@ -78,16 +82,27 @@ func (s *Session) UpdateStatus(status string) error { } func (s *Session) handleNewMessage(_ *discordgo.Session, m *discordgo.MessageCreate) { - if m.Author.ID == s.dg.State.User.ID || m.Author.Bot { + if m.Author.ID == s.dg.State.User.ID || m.Author.Bot || m.GuildID == "" { return } splitted := strings.Split(m.Content, " ") argsLength := len(splitted) - 1 args := splitted[1 : argsLength+1] + server := &models.Server{ + ID: m.GuildID, + Lang: message.GetDefaultLanguage().String(), + } + if err := s.cfg.ServerRepository.Store(context.Background(), server); err != nil { + return + } + ctx := commandCtx{ + server: server, + localizer: message.NewLocalizer(server.Lang), + } switch splitted[0] { case HelpCommand.WithPrefix(s.cfg.CommandPrefix): - s.handleHelpCommand(m) + s.handleHelpCommand(ctx, m) case AuthorCommand.WithPrefix(s.cfg.CommandPrefix): s.handleAuthorCommand(m) case TribeCommand.WithPrefix(s.cfg.CommandPrefix): @@ -155,7 +170,7 @@ func (s *Session) memberHasPermission(guildID string, userID string, permission } func (s *Session) sendUnknownCommandError(mention, channelID string, command ...string) { - s.SendMessage(channelID, mention+` Nieznana komenda: `+strings.Join(command, " ")) + s.SendMessage(channelID, mention+` Unknown command: `+strings.Join(command, " ")) } func (s *Session) IsGuildMember(guildID string) (bool, error) { diff --git a/discord/public_commands.go b/discord/public_commands.go index bb06717..95e5ff9 100644 --- a/discord/public_commands.go +++ b/discord/public_commands.go @@ -5,6 +5,9 @@ import ( "math" "strconv" + "github.com/tribalwarshelp/dcbot/message" + + "github.com/nicksnyder/go-i18n/v2/i18n" shared_models "github.com/tribalwarshelp/shared/models" "github.com/bwmarrin/discordgo" @@ -23,15 +26,15 @@ const ( AuthorCommand Command = "author" ) -func (s *Session) handleHelpCommand(m *discordgo.MessageCreate) { +func (s *Session) handleHelpCommand(ctx commandCtx, m *discordgo.MessageCreate) { tribeCMDWithPrefix := TribeCommand.WithPrefix(s.cfg.CommandPrefix) commandsForAll := fmt.Sprintf(` -- **%s %s** [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RA z plemion o podanych id -- **%s %s** [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RO z plemion o podanych id -- **%s %s** [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RW z plemion o podanych id -- **%s %s** [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największej liczbie pokonanych z plemion o podanych id -- **%s %s** [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największej liczbie punktów z plemion o podanych id -- **%s** - kontakt z autorem bota +- **%s %s** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by ODA. +- **%s %s** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by ODD. +- **%s %s** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by ODS. +- **%s %s** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by OD. +- **%s %s** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by points. +- **%s** - shows how to contact the author `, tribeCMDWithPrefix, TopAttCommand.String(), @@ -47,16 +50,15 @@ func (s *Session) handleHelpCommand(m *discordgo.MessageCreate) { ) commandsForGuildAdmins := fmt.Sprintf(` -- **%s** - tworzy nową grupę -- **%s** - lista grup -- **%s** [id grupy z %s] - usuwa grupę -- **%s** [id grupy z %s] - włącza/wyłącza wyświetlanie powiadomień o podbitych wioskach barbarzyńskich -- **%s** [id grupy z %s] [świat] [id plemienia] - dodaje plemię z danego świata do obserwowanych -- **%s** [id grupy z %s] - wyświetla wszystkie obserwowane plemiona -- **%s** [id grupy z %s] [id z %s] - usuwa plemię z obserwowanych -- **%s** [id grupy z %s] - ustawia kanał na którym będą wyświetlać się informacje o podbitych wioskach -- **%s** [id grupy z %s] - informacje o podbitych wioskach nie będą się już pojawiały -- **%s** [id grupy z %s] - ustawia kanał na którym będą wyświetlać się informacje o straconych wioskach +- **%s** - adds a new observation group. +- **%s** - shows you a list of groups created by this guild. +- **%s** [group id from %s] - deletes an observation group. +- **%s** [group id from %s] - enables/disables notifications about ennobling barbarian villages. +- **%s** [group id from %s] [server] [tribe id] - command adds a tribe to the observation group. +- **%s** [group id from %s] - shows a list of observed tribes by this group. +- **%s** [group id from %s] [id from %s] - removes a tribe to the observation group. +- **%s** [group id from %s] - changes the channel on which notifications about conquered village will show. IMPORTANT! Call this command on the channel you want to display these notifications. +- **%s** [group id from %s] - disable notifications about conquered villages. `, AddGroupCommand.WithPrefix(s.cfg.CommandPrefix), GroupsCommand.WithPrefix(s.cfg.CommandPrefix), @@ -75,23 +77,42 @@ func (s *Session) handleHelpCommand(m *discordgo.MessageCreate) { GroupsCommand.WithPrefix(s.cfg.CommandPrefix), UnObserveConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix), GroupsCommand.WithPrefix(s.cfg.CommandPrefix), - LostVillagesCommand.WithPrefix(s.cfg.CommandPrefix), - GroupsCommand.WithPrefix(s.cfg.CommandPrefix), ) commandsForGuildAdmins2 := fmt.Sprintf(` -- **%s** [id grupy z %s] - informacje o straconych wioskach nie będą się już pojawiały +- **%s** [group id from %s] - changes the channel on which notifications about lost village will show. IMPORTANT! Call this command on the channel you want to display these notifications. +- **%s** [group id from %s] - Disable notifications about lost villages. `, + LostVillagesCommand.WithPrefix(s.cfg.CommandPrefix), + GroupsCommand.WithPrefix(s.cfg.CommandPrefix), UnObserveLostVillagesCommand.WithPrefix(s.cfg.CommandPrefix), GroupsCommand.WithPrefix(s.cfg.CommandPrefix), ) + forAdmins := ctx.localizer.MustLocalize(&i18n.LocalizeConfig{ + MessageID: "help.forAdmins", + DefaultMessage: message.FallbackMsg("help.forAdmins", "For admins"), + }) + s.SendEmbed(m.ChannelID, NewEmbed(). - SetTitle("Pomoc"). - SetDescription("Komendy oferowane przez bota"). - AddField("Dla wszystkich", commandsForAll). - AddField("Dla adminów", commandsForGuildAdmins). - AddField("Dla adminów 2", commandsForGuildAdmins2). + SetTitle(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{ + MessageID: "help.title", + DefaultMessage: message.FallbackMsg("help.title", "Help"), + })). + SetDescription(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{ + MessageID: "help.description", + DefaultMessage: message.FallbackMsg("help.description", "Commands offered by bot"), + })). + SetFooter(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{ + MessageID: "help.footer", + DefaultMessage: message.FallbackMsg("help.footer", "Check bot website -> https://dcbot.tribalwarshelp.com/."), + })). + AddField(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{ + MessageID: "help.forAllUsers", + DefaultMessage: message.FallbackMsg("help.forAllUsers", "For all guild members."), + }), commandsForAll). + AddField(forAdmins, commandsForGuildAdmins). + AddField(forAdmins+" 2", commandsForGuildAdmins2). MessageEmbed) } diff --git a/go.mod b/go.mod index 13df344..4172af9 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,12 @@ require ( github.com/bwmarrin/discordgo v0.20.3 github.com/go-pg/pg/v10 v10.0.0-beta.2 github.com/joho/godotenv v1.3.0 + github.com/nicksnyder/go-i18n/v2 v2.0.3 github.com/pkg/errors v0.9.1 github.com/robfig/cron/v3 v3.0.1 github.com/segmentio/encoding v0.1.14 // indirect github.com/tribalwarshelp/golang-sdk v0.0.0-20200709112050-ba3a7ba145b1 github.com/tribalwarshelp/shared v0.0.0-20200707075151-722e4a520a3c golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 // indirect + golang.org/x/text v0.3.2 ) diff --git a/go.sum b/go.sum index e92a2b6..3d4d9b7 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/DataDog/sketches-go v0.0.0-20190923095040-43f19ad77ff7 h1:qELHH0AWCvf98Yf+CNIJx9vOZOfHFDDzgDRYsnNk/vs= github.com/DataDog/sketches-go v0.0.0-20190923095040-43f19ad77ff7/go.mod h1:Q5DbzQ+3AkgGwymQO7aZFNP7ns2lZKGtvRBzRXfdi60= @@ -74,6 +76,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/nicksnyder/go-i18n/v2 v2.0.3 h1:ks/JkQiOEhhuF6jpNvx+Wih1NIiXzUnZeZVnJuI8R8M= +github.com/nicksnyder/go-i18n/v2 v2.0.3/go.mod h1:oDab7q8XCYMRlcrBnaY/7B1eOectbvj6B1UPBT+p5jo= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo= github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -101,21 +105,8 @@ github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgh github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo= github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs= -github.com/tribalwarshelp/golang-sdk v0.0.0-20200623145613-0fb6430054d2 h1:8sp6dweIF6v6m9ORL8JXFMraxV29qcLeZyXZx4b9uwY= -github.com/tribalwarshelp/golang-sdk v0.0.0-20200623145613-0fb6430054d2/go.mod h1:ls5/l0IY/Aa12URZt8IRRYSR8xi3puOoWpgUTf6QNVY= -github.com/tribalwarshelp/golang-sdk v0.0.0-20200625131004-06b44e214642 h1:PD5bOsgnMAQwq4m2R19/dM5an56kEo0OSHm2R2gYgoY= -github.com/tribalwarshelp/golang-sdk v0.0.0-20200625131004-06b44e214642/go.mod h1:dM2bGl7OxfsejbDu+TSFie8XS1XD1WBc1/dEqbIRLUk= -github.com/tribalwarshelp/golang-sdk v0.0.0-20200629130313-0880d8996ba6 h1:+C127PqJZY5xS1gyWQAaSblxnktBxnFISzHsnEjPj0s= -github.com/tribalwarshelp/golang-sdk v0.0.0-20200629130313-0880d8996ba6/go.mod h1:J1nwE1gYoP8LJkb1sbL3OGqTp7u8RJZGpsKHYjS3+0Y= github.com/tribalwarshelp/golang-sdk v0.0.0-20200709112050-ba3a7ba145b1 h1:p+/OHcQQYXCmxjG+YTkXfeL4Oxku7t6sW0KK2Ne85V4= github.com/tribalwarshelp/golang-sdk v0.0.0-20200709112050-ba3a7ba145b1/go.mod h1:jQWbuz96EMQTuxhanSY1imtK4zEjPrB1cM5wCKkk7l4= -github.com/tribalwarshelp/shared v0.0.0-20200623144748-aa834a01dce6 h1:WZ1oxHysFtiPjHa2ADUqiGrzwcN3j0YpiVg/V2e/74o= -github.com/tribalwarshelp/shared v0.0.0-20200623144748-aa834a01dce6/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y= -github.com/tribalwarshelp/shared v0.0.0-20200625120510-6d18ee334662/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y= -github.com/tribalwarshelp/shared v0.0.0-20200625131045-74c5a9b3b4f0 h1:k1j0Nh2OIr1fTrCrbznjPAH+eRVpB3HYXM1sHErrayg= -github.com/tribalwarshelp/shared v0.0.0-20200625131045-74c5a9b3b4f0/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y= -github.com/tribalwarshelp/shared v0.0.0-20200629123803-0cd6cb6f1e87 h1:TMuZUk0wW+8dXSGVJVLRFFhLONKKYJRnLBFvbb9XTrE= -github.com/tribalwarshelp/shared v0.0.0-20200629123803-0cd6cb6f1e87/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y= github.com/tribalwarshelp/shared v0.0.0-20200707075151-722e4a520a3c h1:wenSReGHPux51q5s+9Ji0TOHCt8Zx6whxsjx8Iv7aZg= github.com/tribalwarshelp/shared v0.0.0-20200707075151-722e4a520a3c/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y= github.com/vmihailenco/bufpool v0.1.5/go.mod h1:fL9i/PRTuS7AELqAHwSU1Zf1c70xhkhGe/cD5ud9pJk= @@ -135,6 +126,7 @@ go.opentelemetry.io/otel v0.6.0/go.mod h1:jzBIgIzK43Iu1BpDAXwqOd6UPsSAk+ewVZ5ofS golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY= golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20191128160524-b544559bb6d1/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -152,6 +144,7 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190420063019-afa5a82059c6/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -168,6 +161,7 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -183,6 +177,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 218955c..cc9a77b 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,11 @@ import ( "log" "os" "os/signal" + "path" "syscall" + "github.com/tribalwarshelp/dcbot/message" + "github.com/tribalwarshelp/golang-sdk/sdk" _cron "github.com/tribalwarshelp/dcbot/cron" @@ -35,6 +38,14 @@ func init() { } func main() { + dir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + if err := message.LoadMessageFiles(path.Join(dir, "message", "translations")); err != nil { + log.Fatal(err) + } + db := pg.Connect(&pg.Options{ User: os.Getenv("DB_USER"), Password: os.Getenv("DB_PASSWORD"), diff --git a/message/bundle.go b/message/bundle.go new file mode 100644 index 0000000..b9e4b15 --- /dev/null +++ b/message/bundle.go @@ -0,0 +1,35 @@ +package message + +import ( + "os" + "path/filepath" + + "github.com/nicksnyder/go-i18n/v2/i18n" + + "golang.org/x/text/language" +) + +var lang = language.English +var bundle = i18n.NewBundle(lang) + +func SetDefaultLanguage(tag language.Tag) { + lang = tag + bundle = i18n.NewBundle(tag) +} + +func GetDefaultLanguage() language.Tag { + return lang +} + +func NewLocalizer(l ...string) *i18n.Localizer { + return i18n.NewLocalizer(bundle, append(l, lang.String())...) +} + +func LoadMessageFiles(root string) error { + return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + if path != root { + bundle.MustLoadMessageFile(path) + } + return nil + }) +} diff --git a/message/helpers.go b/message/helpers.go new file mode 100644 index 0000000..250a02b --- /dev/null +++ b/message/helpers.go @@ -0,0 +1,12 @@ +package message + +import "github.com/nicksnyder/go-i18n/v2/i18n" + +func FallbackMsg(id string, msg string) *i18n.Message { + return &i18n.Message{ + ID: id, + One: msg, + Many: msg, + Other: msg, + } +} diff --git a/message/translations/active.en.json b/message/translations/active.en.json new file mode 100644 index 0000000..298fc37 --- /dev/null +++ b/message/translations/active.en.json @@ -0,0 +1,24 @@ +{ + "help.title": "Help", + "help.description": "Commands offered by the bot", + "help.footer": "Check bot website -> https://dcbot.tribalwarshelp.com/", + "help.forAllUsers": "For all users", + "help.forAdmins": "For admins", + "help.tribe.topatt": "**{{.Command}}** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by ODA.", + "help.tribe.topdef": "**{{.Command}}** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by ODD.", + "help.tribe.topsupp": "**{{.Command}}** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by ODS.", + "help.tribe.toptotal": "**{{.Command}}** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by OD.", + "help.tribe.toppoints": "**{{.Command}}** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by points.", + "help.author": "**{{.Command}}** - shows how to contact the author.", + "help.addgroup": "**{{.Command}}** - adds a new observation group.", + "help.groups": "**{{.Command}}** - shows you a list of groups created by this guild.", + "help.deletegroup": "**{{.Command}}** [group id from {{.GroupsCommand}}] - deletes an observation group.", + "help.showennobledbarbs": "**{{.Command}}** [group id from {{.GroupsCommand}}] - enables/disables notifications about ennobling barbarian villages.", + "help.observe": "**{{.Command}}** [group id from {{.GroupsCommand}}] [server] [tribe id] - command adds a tribe to the observation group.", + "help.observations": "**{{.Command}}** [group id from {{.GroupsCommand}}] shows a list of observed tribes by this group.", + "help.unobserve": "**{{.Command}}** [group id from {{.GroupsCommand}}] [id from {{.ObservationsCommand}}] - removes a tribe to the observation group.", + "help.conqueredvillages": "**{{.Command}}** [group id from {{.GroupsCommand}}] - changes the channel on which notifications about conquered village will show. IMPORTANT! Call this command on the channel you want to display these notifications.", + "help.unobserveconqueredvillages": "**{{.Command}}** [group id from {{.GroupsCommand}}] - disable notifications about conquered villages.", + "help.lostvillages": "**{{.Command}}** [group id from {{.GroupsCommand}}] changes the channel on which notifications about lost village will show. IMPORTANT! Call this command on the channel you want to display these notifications.", + "help.unobservelostvillages": "**{{.Command}}** [group id from {{.GroupsCommand}}] - disable notifications about lost villages." +} diff --git a/models/server.go b/models/server.go index a1885e0..7940c23 100644 --- a/models/server.go +++ b/models/server.go @@ -4,9 +4,9 @@ type Server struct { tableName struct{} `pg:",alias:server"` ID string `pg:",pk" json:"id" gqlgen:"id"` + Lang string `pg:",use_zero"` Groups []*Group } - type ServerFilter struct { ID []string Limit int `urlstruct:",nowhere"`