add new command changelanguage
This commit is contained in:
parent
8ed61ffc80
commit
5ad56f71bf
|
@ -52,7 +52,7 @@ func (h *handler) loadEnnoblements(servers []string) map[string]ennoblements {
|
|||
|
||||
lastEnnoblementAt, ok := h.lastEnnoblementAt[w]
|
||||
if !ok {
|
||||
lastEnnoblementAt = time.Now().Add(-60 * time.Minute)
|
||||
lastEnnoblementAt = time.Now().Add(-1 * time.Minute)
|
||||
}
|
||||
|
||||
m[w] = filterEnnoblements(es, lastEnnoblementAt)
|
||||
|
|
|
@ -31,6 +31,7 @@ const (
|
|||
DisableLostVillagesCommand Command = "disablelostvillages"
|
||||
ConqueredVillagesCommand Command = "conqueredvillages"
|
||||
DisableConqueredVillagesCommand Command = "disableconqueredvillages"
|
||||
ChangeLanguageCommand Command = "changelanguage"
|
||||
)
|
||||
|
||||
func (s *Session) handleAddGroupCommand(ctx commandCtx, m *discordgo.MessageCreate) {
|
||||
|
@ -82,6 +83,7 @@ func (s *Session) handleAddGroupCommand(ctx commandCtx, m *discordgo.MessageCrea
|
|||
},
|
||||
}))
|
||||
}
|
||||
|
||||
func (s *Session) handleDeleteGroupCommand(ctx commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has {
|
||||
return
|
||||
|
@ -891,3 +893,72 @@ func (s *Session) handleShowEnnobledBarbariansCommand(ctx commandCtx, m *discord
|
|||
}))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) handleChangeLanguageCommand(ctx commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has {
|
||||
return
|
||||
}
|
||||
|
||||
argsLength := len(args)
|
||||
if argsLength > 1 {
|
||||
s.sendUnknownCommandError(m.Author.Mention(), m.ChannelID, args[1:argsLength]...)
|
||||
return
|
||||
} else if argsLength < 1 {
|
||||
s.SendMessage(m.ChannelID,
|
||||
m.Author.Mention()+" "+ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "help.changelanguage",
|
||||
DefaultMessage: message.FallbackMsg("help.changelanguage",
|
||||
"**{{.Command}}** [{{.Languages}}] - change language."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": ChangeLanguageCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"Languages": getAvailableLanguages(),
|
||||
},
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
lang := args[0]
|
||||
valid := false
|
||||
for _, langTag := range message.LanguageTags() {
|
||||
if langTag.String() == lang {
|
||||
valid = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !valid {
|
||||
s.SendMessage(m.ChannelID,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "changeLanguage.languageNotSupported",
|
||||
DefaultMessage: message.FallbackMsg("changeLanguage.languageNotSupported",
|
||||
"{{.Mention}} Language not supported."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
ctx.server.Lang = lang
|
||||
if err := s.cfg.ServerRepository.Update(context.Background(), ctx.server); err != nil {
|
||||
s.SendMessage(m.ChannelID,
|
||||
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,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "changeLanguage.success",
|
||||
DefaultMessage: message.FallbackMsg("changeLanguage.success",
|
||||
"{{.Mention}} The language has been changed."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
|
|
@ -108,6 +108,9 @@ func (s *Session) handleNewMessage(_ *discordgo.Session, m *discordgo.MessageCre
|
|||
case TribeCommand.WithPrefix(s.cfg.CommandPrefix):
|
||||
s.handleTribeCommand(ctx, m, args...)
|
||||
|
||||
case ChangeLanguageCommand.WithPrefix(s.cfg.CommandPrefix):
|
||||
s.handleChangeLanguageCommand(ctx, m, args...)
|
||||
|
||||
case AddGroupCommand.WithPrefix(s.cfg.CommandPrefix):
|
||||
s.handleAddGroupCommand(ctx, m)
|
||||
case DeleteGroupCommand.WithPrefix(s.cfg.CommandPrefix):
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package discord
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/tribalwarshelp/dcbot/message"
|
||||
)
|
||||
|
||||
func getAvailableLanguages() string {
|
||||
langTags := []string{}
|
||||
for _, langTag := range message.LanguageTags() {
|
||||
langTags = append(langTags, langTag.String())
|
||||
}
|
||||
return strings.Join(langTags, " | ")
|
||||
}
|
|
@ -181,12 +181,13 @@ func (s *Session) handleHelpCommand(ctx commandCtx, m *discordgo.MessageCreate)
|
|||
|
||||
commandsForGuildAdmins2 := fmt.Sprintf(`
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
`,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "help.lostvillages",
|
||||
DefaultMessage: message.FallbackMsg("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."),
|
||||
"**{{.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."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": LostVillagesCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"GroupsCommand": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
|
@ -195,12 +196,21 @@ func (s *Session) handleHelpCommand(ctx commandCtx, m *discordgo.MessageCreate)
|
|||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "help.disablelostvillages",
|
||||
DefaultMessage: message.FallbackMsg("help.disablelostvillages",
|
||||
"*{{.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."),
|
||||
"**{{.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."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": DisableLostVillagesCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"GroupsCommand": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "help.changelanguage",
|
||||
DefaultMessage: message.FallbackMsg("help.changelanguage",
|
||||
"**{{.Command}}** [{{.Languages}}] - change language."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": ChangeLanguageCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"Languages": getAvailableLanguages(),
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
forAdmins := ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
|
@ -287,34 +297,34 @@ func (s *Session) handleTribeCommand(ctx commandCtx, m *discordgo.MessageCreate,
|
|||
filter.Sort = "rankAtt ASC"
|
||||
title = ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "tribe.title.sortedByODA",
|
||||
DefaultMessage: message.FallbackMsg("tribe.title.sortedByODA", "Sorted by ODA"),
|
||||
DefaultMessage: message.FallbackMsg("tribe.title.sortedByODA", "Ordered by ODA"),
|
||||
})
|
||||
case TopDefCommand:
|
||||
filter.RankDefGTE = 1
|
||||
filter.Sort = "rankDef ASC"
|
||||
title = ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "tribe.title.sortedByODD",
|
||||
DefaultMessage: message.FallbackMsg("tribe.title.sortedByODD", "Sorted by ODD"),
|
||||
DefaultMessage: message.FallbackMsg("tribe.title.sortedByODD", "Ordered by ODD"),
|
||||
})
|
||||
case TopSuppCommand:
|
||||
filter.RankSupGTE = 1
|
||||
filter.Sort = "rankSup ASC"
|
||||
title = ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "tribe.title.sortedByODS",
|
||||
DefaultMessage: message.FallbackMsg("tribe.title.sortedByODS", "Sorted by ODS"),
|
||||
DefaultMessage: message.FallbackMsg("tribe.title.sortedByODS", "Ordered by ODS"),
|
||||
})
|
||||
case TopTotalCommand:
|
||||
filter.RankTotalGTE = 1
|
||||
filter.Sort = "rankTotal ASC"
|
||||
title = ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "tribe.title.sortedByOD",
|
||||
DefaultMessage: message.FallbackMsg("tribe.title.sortedByOD", "Sorted by OD"),
|
||||
DefaultMessage: message.FallbackMsg("tribe.title.sortedByOD", "Ordered by OD"),
|
||||
})
|
||||
case TopPointsCommand:
|
||||
filter.Sort = "rank ASC"
|
||||
title = ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "tribe.title.sortedByPoints",
|
||||
DefaultMessage: message.FallbackMsg("tribe.title.sortedByPoints", "Sorted by points"),
|
||||
DefaultMessage: message.FallbackMsg("tribe.title.sortedByPoints", "Ordered by points"),
|
||||
})
|
||||
default:
|
||||
return
|
||||
|
|
|
@ -25,6 +25,10 @@ func NewLocalizer(l ...string) *i18n.Localizer {
|
|||
return i18n.NewLocalizer(bundle, append(l, lang.String())...)
|
||||
}
|
||||
|
||||
func LanguageTags() []language.Tag {
|
||||
return bundle.LanguageTags()
|
||||
}
|
||||
|
||||
func LoadMessageFiles(root string) error {
|
||||
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||
if path != root {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"help.title": "Help",
|
||||
"help.description": "Commands offered by the bot",
|
||||
"help.description": "Commands offered by the bot.",
|
||||
"help.footer": "",
|
||||
"help.forAllUsers": "For all users",
|
||||
"help.forAdmins": "For admins",
|
||||
|
@ -21,14 +21,15 @@
|
|||
"help.disableconqueredvillages": "**{{.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.disablelostvillages": "**{{.Command}}** [group id from {{.GroupsCommand}}] - disable notifications about lost villages.",
|
||||
"help.changelanguage": "**{{.Command}}** [{{.Languages}}] - change language.",
|
||||
|
||||
"tribe.invalidPage": "{{.Mention}} The page must be a number greater than 0.",
|
||||
"tribe.noTribeID": "{{.Mention}} You haven't entered the tribe ID.",
|
||||
"tribe.title.sortedByOD": "Sorted by OD",
|
||||
"tribe.title.sortedByODA": "Sorted by ODA",
|
||||
"tribe.title.sortedByODD": "Sorted by ODD",
|
||||
"tribe.title.sortedByODS": "Sorted by ODS",
|
||||
"tribe.title.sortedByPoints": "Sorted by points",
|
||||
"tribe.title.sortedByOD": "Ordered by OD",
|
||||
"tribe.title.sortedByODA": "Ordered by ODA",
|
||||
"tribe.title.sortedByODD": "Ordered by ODD",
|
||||
"tribe.title.sortedByODS": "Ordered by ODS",
|
||||
"tribe.title.sortedByPoints": "Ordered by points",
|
||||
"tribe.tribesNotFound": "{{.Mention}} Tribes not found.",
|
||||
"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",
|
||||
|
@ -81,6 +82,9 @@
|
|||
"showEnnobledBarbs.success_1": "{{.Mention}} Notifications about conquered barbarian villages will no longer show up.",
|
||||
"showEnnobledBarbs.success_2": "{{.Mention}} Enabled notifications about conquered barbarian villages.",
|
||||
|
||||
"changeLanguage.languageNotSupported": "{{.Mention}} Language not supported.",
|
||||
"changeLanguage.success": "{{.Mention}} The language has been changed.",
|
||||
|
||||
"cron.lostVillages.title": "Lost villages",
|
||||
"cron.conqueredVillages.title": "Conquered villages",
|
||||
"cron.checkEnnoblements.msgLine": "{{.NewOwner}} [{{.NewOwnerTribe}}] has conquered the village {{.Village}} (Old owner: {{.OldOwner}} [{{.OldOwnerTribe}}])",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"help.title": "Pomoc",
|
||||
"help.description": "Komendy oferowane przez bota",
|
||||
"help.description": "Komendy oferowane przez bota.",
|
||||
"help.footer": "",
|
||||
"help.forAllUsers": "Dla wszystkich",
|
||||
"help.forAdmins": "Dla adminów",
|
||||
|
@ -21,6 +21,7 @@
|
|||
"help.disableconqueredvillages": "**{{.Command}}** [id grupy z {{.GroupsCommand}}] - wyłącza powiadomienia o podbitych wioskach w danej grupie.",
|
||||
"help.lostvillages": "**{{.Command}}** [id grupy z {{.GroupsCommand}}] - zmienia kanał na którym będą się pojawiać informację o straconych wioskach w danej grupie. **WAŻNE!** Wywołaj tę komendę na kanale na którym chcesz dostawać te powiadomienia.",
|
||||
"help.disablelostvillages": "**{{.Command}}** [id grupy z {{.GroupsCommand}}] - wyłącza powiadomienia o straconych wioskach w danej grupie.",
|
||||
"help.changelanguage": "**{{.Command}}** [{{.Languages}}] - zmień język.",
|
||||
|
||||
"tribe.invalidPage": "{{.Mention}} Strona musi być liczbą większą od 0.",
|
||||
"tribe.noTribeID": "{{.Mention}} Nie wprowadziłeś ID plemienia.",
|
||||
|
@ -81,6 +82,9 @@
|
|||
"showEnnobledBarbs.success_1": "{{.Mention}} Powiadomienia o podbitych barbarkach nie będą się już dłużej pokazywały.",
|
||||
"showEnnobledBarbs.success_2": "{{.Mention}} Włączono powiadomienia o podbitych barbarkach.",
|
||||
|
||||
"changeLanguage.languageNotSupported": "{{.Mention}} Język nie jest obsługiwany.",
|
||||
"changeLanguage.success": "{{.Mention}} Język został zmieniony.",
|
||||
|
||||
"cron.lostVillages.title": "Stracone wioski",
|
||||
"cron.conqueredVillages.title": "Podbite wioski",
|
||||
"cron.checkEnnoblements.msgLine": "{{.NewOwner}} [{{.NewOwnerTribe}}] podbił wioskę {{.Village}} (Poprzedni właściciel: {{.OldOwner}} [{{.OldOwnerTribe}}])",
|
||||
|
|
Reference in New Issue