cleanup
This commit is contained in:
parent
2f02cbe32d
commit
2adeb4f686
|
@ -13,8 +13,8 @@ func (cmd Command) String() string {
|
|||
return string(cmd)
|
||||
}
|
||||
|
||||
func (cmd Command) WithPrefix(prefix string) Command {
|
||||
return Command(prefix + cmd.String())
|
||||
func (cmd Command) WithPrefix(prefix string) string {
|
||||
return prefix + cmd.String()
|
||||
}
|
||||
|
||||
type commandCtx struct {
|
||||
|
@ -22,25 +22,19 @@ type commandCtx struct {
|
|||
localizer *i18n.Localizer
|
||||
}
|
||||
|
||||
type commandHandler struct {
|
||||
cmd Command
|
||||
requireAdmPermissions bool
|
||||
fn func(ctx *commandCtx, m *discordgo.MessageCreate, args ...string)
|
||||
type commandHandler interface {
|
||||
cmd() Command
|
||||
requireAdmPermissions() bool
|
||||
execute(ctx *commandCtx, m *discordgo.MessageCreate, args ...string)
|
||||
}
|
||||
|
||||
type commandHandlers []*commandHandler
|
||||
type commandHandlers []commandHandler
|
||||
|
||||
func (hs commandHandlers) find(cmd Command) *commandHandler {
|
||||
func (hs commandHandlers) find(prefix, cmd string) commandHandler {
|
||||
for _, h := range hs {
|
||||
if h.cmd == cmd {
|
||||
if h.cmd().WithPrefix(prefix) == cmd {
|
||||
return h
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type commandHandlerInterface interface {
|
||||
cmd() Command
|
||||
requireAdmPermissions() bool
|
||||
execute(ctx *commandCtx, m *discordgo.MessageCreate, args ...string)
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ type commandCoordsTranslation struct {
|
|||
*Session
|
||||
}
|
||||
|
||||
var _ commandHandlerInterface = &commandCoordsTranslation{}
|
||||
var _ commandHandler = &commandCoordsTranslation{}
|
||||
|
||||
func (c *commandCoordsTranslation) cmd() Command {
|
||||
return CoordsTranslationCommand
|
||||
|
@ -78,54 +78,11 @@ func (c *commandCoordsTranslation) execute(ctx *commandCtx, m *discordgo.Message
|
|||
}))
|
||||
}
|
||||
|
||||
func (s *Session) handleCoordsTranslationCommand(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
argsLength := len(args)
|
||||
if argsLength != 1 {
|
||||
s.SendMessage(m.ChannelID,
|
||||
m.Author.Mention()+" "+ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpCoordsTranslation,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpCoordsTranslation,
|
||||
"**{{.Command}}** [server] - enables coords translation feature."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": CoordsTranslationCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
serverKey := args[0]
|
||||
server, err := s.cfg.API.Server.Read(serverKey, nil)
|
||||
if err != nil || server == nil {
|
||||
s.SendMessage(m.ChannelID,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.CoordsTranslationServerNotFound,
|
||||
DefaultMessage: message.FallbackMsg(message.CoordsTranslationServerNotFound, "{{.Mention}} Server not found."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
ctx.server.CoordsTranslation = serverKey
|
||||
go s.cfg.ServerRepository.Update(context.Background(), ctx.server)
|
||||
|
||||
s.SendMessage(m.ChannelID,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.CoordsTranslationSuccess,
|
||||
DefaultMessage: message.FallbackMsg(message.CoordsTranslationSuccess,
|
||||
"{{.Mention}} Coords translation feature has been enabled."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
type commandDisableCoordsTranslation struct {
|
||||
*Session
|
||||
}
|
||||
|
||||
var _ commandHandlerInterface = &commandDisableCoordsTranslation{}
|
||||
var _ commandHandler = &commandDisableCoordsTranslation{}
|
||||
|
||||
func (c *commandDisableCoordsTranslation) cmd() Command {
|
||||
return DisableCoordsTranslationCommand
|
||||
|
@ -148,21 +105,6 @@ func (c *commandDisableCoordsTranslation) execute(ctx *commandCtx, m *discordgo.
|
|||
}))
|
||||
}
|
||||
|
||||
func (s *Session) handleDisableCoordsTranslationCommand(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
ctx.server.CoordsTranslation = ""
|
||||
go s.cfg.ServerRepository.Update(context.Background(), ctx.server)
|
||||
|
||||
s.SendMessage(m.ChannelID,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.DisableCoordsTranslationSuccess,
|
||||
DefaultMessage: message.FallbackMsg(message.DisableCoordsTranslationSuccess,
|
||||
"{{.Mention}} Coords translation feature has been disabled."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
func (s *Session) translateCoords(ctx *commandCtx, m *discordgo.MessageCreate) {
|
||||
if ctx.server.CoordsTranslation == "" {
|
||||
return
|
||||
|
@ -213,8 +155,6 @@ func (s *Session) translateCoords(ctx *commandCtx, m *discordgo.MessageCreate) {
|
|||
|
||||
msg.Append(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.CoordsTranslationMessage,
|
||||
DefaultMessage: message.FallbackMsg(message.CoordsTranslationMessage,
|
||||
"{{.Village}} owned by {{.Player}} (Tribe: {{.Tribe}})."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Village": BuildLink(village.FullName(), villageURL),
|
||||
"Player": BuildLink(playerName, playerURL),
|
||||
|
@ -225,8 +165,7 @@ func (s *Session) translateCoords(ctx *commandCtx, m *discordgo.MessageCreate) {
|
|||
|
||||
s.SendEmbed(m.ChannelID, NewEmbed().
|
||||
SetTitle(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.CoordsTranslationTitle,
|
||||
DefaultMessage: message.FallbackMsg(message.CoordsTranslationTitle, "Villages"),
|
||||
MessageID: message.CoordsTranslationTitle,
|
||||
})).
|
||||
SetFields(msg.ToMessageEmbedFields()))
|
||||
}
|
||||
|
|
|
@ -54,93 +54,24 @@ func New(cfg SessionConfig) (*Session, error) {
|
|||
|
||||
func (s *Session) init() error {
|
||||
s.handlers = commandHandlers{
|
||||
&commandHandler{
|
||||
cmd: HelpCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleHelpCommand,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: AuthorCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleAuthorCommand,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: TribeCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleTribeCommand,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: ChangeLanguageCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleChangeLanguageCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: AddGroupCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleAddGroupCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: DeleteGroupCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleDeleteGroupCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleGroupsCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: ObserveCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleObserveCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: DeleteObservationCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleDeleteObservationCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: ObservationsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleObservationsCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: ConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleConqueredVillagesCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: DisableConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleDisableConqueredVillagesCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: LostVillagesCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleLostVillagesCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: DisableLostVillagesCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleDisableLostVillagesCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: ShowEnnobledBarbariansCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleShowEnnobledBarbariansCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: ShowInternalsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleShowInternalsCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: CoordsTranslationCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleCoordsTranslationCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHandler{
|
||||
cmd: DisableCoordsTranslationCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
fn: s.handleDisableCoordsTranslationCommand,
|
||||
requireAdmPermissions: true,
|
||||
},
|
||||
&commandHelp{s},
|
||||
&commandAuthor{s},
|
||||
&commandTribe{s},
|
||||
&commandChangeLanguage{s},
|
||||
&commandAddGroup{s},
|
||||
&commandDeleteGroup{s},
|
||||
&commandGroups{s},
|
||||
&commandObserve{s},
|
||||
&commandDeleteObservation{s},
|
||||
&commandObservations{s},
|
||||
&commandConqueredVillages{s},
|
||||
&commandDisableConqueredVillages{s},
|
||||
&commandLostVillages{s},
|
||||
&commandDisableLostVillages{s},
|
||||
&commandShowEnnobledBarbarians{s},
|
||||
&commandShowInternals{s},
|
||||
&commandCoordsTranslation{s},
|
||||
&commandDisableCoordsTranslation{s},
|
||||
}
|
||||
|
||||
s.dg.AddHandler(s.handleNewMessage)
|
||||
|
@ -222,10 +153,9 @@ func (s *Session) handleNewMessage(_ *discordgo.Session, m *discordgo.MessageCre
|
|||
localizer: message.NewLocalizer(svr.Lang),
|
||||
}
|
||||
|
||||
cmd := Command(parts[0])
|
||||
h := s.handlers.find(cmd)
|
||||
h := s.handlers.find(s.cfg.CommandPrefix, parts[0])
|
||||
if h != nil {
|
||||
if h.requireAdmPermissions {
|
||||
if h.requireAdmPermissions() {
|
||||
if m.GuildID == "" {
|
||||
return
|
||||
}
|
||||
|
@ -238,13 +168,13 @@ func (s *Session) handleNewMessage(_ *discordgo.Session, m *discordgo.MessageCre
|
|||
WithFields(logrus.Fields{
|
||||
"serverID": svr.ID,
|
||||
"lang": svr.Lang,
|
||||
"command": cmd,
|
||||
"command": parts[0],
|
||||
"args": args,
|
||||
"authorID": m.Author.ID,
|
||||
"authorUsername": m.Author.Username,
|
||||
}).
|
||||
Info("handleNewMessage: Executing command...")
|
||||
h.fn(ctx, m, args...)
|
||||
Infof("handleNewMessage: Executing command %s...", parts[0])
|
||||
h.execute(ctx, m, args...)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -32,7 +32,7 @@ type commandHelp struct {
|
|||
*Session
|
||||
}
|
||||
|
||||
var _ commandHandlerInterface = &commandHelp{}
|
||||
var _ commandHandler = &commandHelp{}
|
||||
|
||||
func (c *commandHelp) cmd() Command {
|
||||
return HelpCommand
|
||||
|
@ -54,31 +54,31 @@ func (c *commandHelp) execute(ctx *commandCtx, m *discordgo.MessageCreate, args
|
|||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTribeTopODA,
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": TribeCommand.WithPrefix(c.cfg.CommandPrefix) + " " + TopODACommand,
|
||||
"Command": TribeCommand.WithPrefix(c.cfg.CommandPrefix) + " " + TopODACommand.String(),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTribeTopODD,
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": TribeCommand.WithPrefix(c.cfg.CommandPrefix) + " " + TopODDCommand,
|
||||
"Command": TribeCommand.WithPrefix(c.cfg.CommandPrefix) + " " + TopODDCommand.String(),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTribeTopODS,
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": TribeCommand.WithPrefix(c.cfg.CommandPrefix) + " " + TopODSCommand,
|
||||
"Command": TribeCommand.WithPrefix(c.cfg.CommandPrefix) + " " + TopODSCommand.String(),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTribeTopOD,
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": TribeCommand.WithPrefix(c.cfg.CommandPrefix) + " " + TopODCommand,
|
||||
"Command": TribeCommand.WithPrefix(c.cfg.CommandPrefix) + " " + TopODCommand.String(),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTribeTopPoints,
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": TribeCommand.WithPrefix(c.cfg.CommandPrefix) + " " + TopPointsCommand,
|
||||
"Command": TribeCommand.WithPrefix(c.cfg.CommandPrefix) + " " + TopPointsCommand.String(),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
|
@ -233,248 +233,11 @@ func (c *commandHelp) execute(ctx *commandCtx, m *discordgo.MessageCreate, args
|
|||
AddField(forAdmins+" 2", commandsForGuildAdmins2))
|
||||
}
|
||||
|
||||
func (s *Session) handleHelpCommand(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
commandsForAll := fmt.Sprintf(`
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
`,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTribeTopODA,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpTribeTopODA,
|
||||
"**{{.Command}}** [server] [page] [tribe id or tribe tag, you can enter more than one] - generates a player list from selected tribes ordered by ODA."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": TribeCommand.WithPrefix(s.cfg.CommandPrefix) + " " + TopODACommand,
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTribeTopODD,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpTribeTopODD,
|
||||
"**{{.Command}}** [server] [page] [tribe id or tribe tag, you can enter more than one] - generates a player list from selected tribes ordered by ODD."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": TribeCommand.WithPrefix(s.cfg.CommandPrefix) + " " + TopODDCommand,
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTribeTopODS,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpTribeTopODS,
|
||||
"**{{.Command}}** [server] [page] [tribe id or tribe tag, you can enter more than one] - generates a player list from selected tribes ordered by ODS."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": TribeCommand.WithPrefix(s.cfg.CommandPrefix) + " " + TopODSCommand,
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTribeTopOD,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpTribeTopOD,
|
||||
"**{{.Command}}** [server] [page] [tribe id or tribe tag, you can enter more than one] - generates a player list from selected tribes ordered by OD."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": TribeCommand.WithPrefix(s.cfg.CommandPrefix) + " " + TopODCommand,
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTribeTopPoints,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpTribeTopPoints,
|
||||
"**{{.Command}}** [server] [page] [tribe id or tribe tag, you can enter more than one] - generates a player list from selected tribes ordered by points."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": TribeCommand.WithPrefix(s.cfg.CommandPrefix) + " " + TopPointsCommand,
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpAuthor,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpAuthor,
|
||||
"**{{.Command}}** - shows how to get in touch with the author."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": AuthorCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
commandsForGuildAdmins := fmt.Sprintf(`
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
`,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpAddGroup,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpAddGroup,
|
||||
"**{{.Command}}** - adds a new observation group."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": AddGroupCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpGroups,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpGroups,
|
||||
"**{{.Command}}** - shows you a list of groups created by this server."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpDeleteGroup,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpDeleteGroup,
|
||||
"**{{.Command}}** [group id from {{.GroupsCommand}}] - deletes an observation group."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": DeleteGroupCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"GroupsCommand": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpShowEnnobledBarbs,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpShowEnnobledBarbs,
|
||||
"**{{.Command}}** [group id from {{.GroupsCommand}}] - enables/disables notifications about ennobling barbarian villages."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": ShowEnnobledBarbariansCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"GroupsCommand": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpObserve,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpObserve,
|
||||
"**{{.Command}}** [group id from {{.GroupsCommand}}] [server] [tribe id or tribe tag] - adds a tribe to the observation group."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": ObserveCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"GroupsCommand": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpGroups,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpGroups,
|
||||
"**{{.Command}}** [group id from {{.GroupsCommand}}] - shows a list of monitored tribes added to this group."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": ObservationsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"GroupsCommand": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpDeleteObservation,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpDeleteObservation,
|
||||
"**{{.Command}}** [group id from {{.GroupsCommand}}] [id from {{.ObservationsCommand}}] - removes a tribe from the observation group."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": DeleteObservationCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"ObservationsCommand": ObservationsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"GroupsCommand": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpConqueredVillages,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpConqueredVillages,
|
||||
"**{{.Command}}** [group id from {{.GroupsCommand}}] - sets the channel on which notifications about conquered village will be displayed. IMPORTANT! Call this command on the channel you want to display these notifications."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": ConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"GroupsCommand": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpDisableConqueredVillages,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpDisableConqueredVillages,
|
||||
"**{{.Command}}** [group id from {{.GroupsCommand}}] - disables notifications about conquered villages."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": DisableConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"GroupsCommand": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
commandsForGuildAdmins2 := fmt.Sprintf(`
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
- %s
|
||||
`,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpLostVillages,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpLostVillages,
|
||||
"**{{.Command}}** [group id from {{.GroupsCommand}}] - sets the channel on which notifications about lost village will be displayed. 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),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpDisableLostVillages,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpDisableLostVillages,
|
||||
"**{{.Command}}** [group id from {{.GroupsCommand}}] - sets the channel on which notifications about lost village will be displayed. 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: message.HelpShowInternals,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpShowInternals,
|
||||
"**{{.Command}}** [group id from {{.GroupsCommand}}] - enables/disables notifications about in-group/in-tribe conquering."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": ShowInternalsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"GroupsCommand": GroupsCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpChangageLanguage,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpChangageLanguage,
|
||||
"**{{.Command}}** [{{.Languages}}] - changes language."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": ChangeLanguageCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
"Languages": getAvailableLanguages(),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpCoordsTranslation,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpCoordsTranslation,
|
||||
"**{{.Command}}** [server] - enables coords translation feature."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": CoordsTranslationCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpDisableCoordsTranslation,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpDisableCoordsTranslation,
|
||||
"**{{.Command}}** - disables coords translation feature."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": DisableCoordsTranslationCommand.WithPrefix(s.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
forAdmins := ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpForAdmins,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpForAdmins, "For admins"),
|
||||
})
|
||||
|
||||
s.SendEmbed(m.ChannelID, NewEmbed().
|
||||
SetTitle(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpTitle,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpTitle, "Help"),
|
||||
})).
|
||||
SetURL("https://dcbot.tribalwarshelp.com/").
|
||||
SetDescription(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpDescription,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpDescription, "Command list"),
|
||||
})).
|
||||
AddField(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpForAllUsers,
|
||||
DefaultMessage: message.FallbackMsg(message.HelpForAllUsers, "For everyone"),
|
||||
}), commandsForAll).
|
||||
AddField(forAdmins, commandsForGuildAdmins).
|
||||
AddField(forAdmins+" 2", commandsForGuildAdmins2))
|
||||
}
|
||||
|
||||
type commandAuthor struct {
|
||||
*Session
|
||||
}
|
||||
|
||||
var _ commandHandlerInterface = &commandAuthor{}
|
||||
var _ commandHandler = &commandAuthor{}
|
||||
|
||||
func (c *commandAuthor) cmd() Command {
|
||||
return AuthorCommand
|
||||
|
@ -490,17 +253,11 @@ func (c *commandAuthor) execute(ctx *commandCtx, m *discordgo.MessageCreate, arg
|
|||
m.Author.Mention()))
|
||||
}
|
||||
|
||||
func (s *Session) handleAuthorCommand(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
s.SendMessage(m.ChannelID,
|
||||
fmt.Sprintf("%s Discord: Kichiyaki#2064 | https://dwysokinski.me/#contact.",
|
||||
m.Author.Mention()))
|
||||
}
|
||||
|
||||
type commandTribe struct {
|
||||
*Session
|
||||
}
|
||||
|
||||
var _ commandHandlerInterface = &commandTribe{}
|
||||
var _ commandHandler = &commandTribe{}
|
||||
|
||||
func (c *commandTribe) cmd() Command {
|
||||
return TribeCommand
|
||||
|
@ -710,221 +467,3 @@ func (c *commandTribe) execute(ctx *commandCtx, m *discordgo.MessageCreate, args
|
|||
},
|
||||
})))
|
||||
}
|
||||
|
||||
func (s *Session) handleTribeCommand(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
argsLength := len(args)
|
||||
if argsLength < 3 {
|
||||
return
|
||||
}
|
||||
|
||||
command := Command(args[0])
|
||||
server := args[1]
|
||||
page, err := strconv.Atoi(args[2])
|
||||
if err != nil || page <= 0 {
|
||||
s.SendMessage(m.ChannelID, ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.TribeInvalidPage,
|
||||
DefaultMessage: message.FallbackMsg(message.TribeInvalidPage, "{{.Mention}} The page must be a number greater than 0."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
return
|
||||
}
|
||||
var ids []int
|
||||
var tags []string
|
||||
for _, arg := range args[3:argsLength] {
|
||||
trimmed := strings.TrimSpace(arg)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
id, err := strconv.Atoi(trimmed)
|
||||
if err != nil || id <= 0 {
|
||||
tags = append(tags, trimmed)
|
||||
} else {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
if len(ids) == 0 && len(tags) == 0 {
|
||||
s.SendMessage(m.ChannelID, ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.TribeNoTribeID,
|
||||
DefaultMessage: message.FallbackMsg(message.TribeNoTribeID, "{{.Mention}} At least one tribe id/tag is required."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
exists := true
|
||||
limit := 10
|
||||
offset := (page - 1) * limit
|
||||
filter := &twmodel.PlayerFilter{
|
||||
Exists: &exists,
|
||||
TribeFilter: &twmodel.TribeFilter{
|
||||
Or: &twmodel.TribeFilterOr{
|
||||
ID: ids,
|
||||
Tag: tags,
|
||||
},
|
||||
},
|
||||
}
|
||||
title := ""
|
||||
sort := ""
|
||||
switch command {
|
||||
case TopODACommand:
|
||||
filter.RankAttGTE = 1
|
||||
sort = "rankAtt ASC"
|
||||
title = ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.TribeTitleOrderedByODA,
|
||||
DefaultMessage: message.FallbackMsg(message.TribeTitleOrderedByODA, "Ordered by ODA"),
|
||||
})
|
||||
case TopODDCommand:
|
||||
filter.RankDefGTE = 1
|
||||
sort = "rankDef ASC"
|
||||
title = ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.TribeTitleOrderedByODD,
|
||||
DefaultMessage: message.FallbackMsg(message.TribeTitleOrderedByODD, "Ordered by ODD"),
|
||||
})
|
||||
case TopODSCommand:
|
||||
filter.RankSupGTE = 1
|
||||
sort = "rankSup ASC"
|
||||
title = ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.TribeTitleOrderedByODS,
|
||||
DefaultMessage: message.FallbackMsg(message.TribeTitleOrderedByODS, "Ordered by ODS"),
|
||||
})
|
||||
case TopODCommand:
|
||||
filter.RankTotalGTE = 1
|
||||
sort = "rankTotal ASC"
|
||||
title = ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.TribeTitleOrderedByOD,
|
||||
DefaultMessage: message.FallbackMsg(message.TribeTitleOrderedByOD, "Ordered by OD"),
|
||||
})
|
||||
case TopPointsCommand:
|
||||
sort = "rank ASC"
|
||||
title = ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.TribeTitleOrderedByPoints,
|
||||
DefaultMessage: message.FallbackMsg(message.TribeTitleOrderedByPoints, "Ordered by points"),
|
||||
})
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
playerList, err := s.cfg.API.Player.Browse(server,
|
||||
limit,
|
||||
offset,
|
||||
[]string{sort},
|
||||
filter,
|
||||
&sdk.PlayerInclude{
|
||||
Tribe: true,
|
||||
})
|
||||
if err != nil {
|
||||
s.SendMessage(m.ChannelID,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.ApiDefaultError,
|
||||
DefaultMessage: message.FallbackMsg(message.ApiDefaultError,
|
||||
"{{.Mention}} Can't fetch data from the API at the moment, please try again later."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
return
|
||||
}
|
||||
if playerList == nil || playerList.Total == 0 {
|
||||
s.SendMessage(m.ChannelID, ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.TribeTribesNotFound,
|
||||
DefaultMessage: message.FallbackMsg(message.TribeTribesNotFound, "{{.Mention}} Tribes not found."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
return
|
||||
}
|
||||
totalPages := int(math.Ceil(float64(playerList.Total) / float64(limit)))
|
||||
if page > totalPages {
|
||||
s.SendMessage(m.ChannelID, ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.TribeExceededMaximumNumberOfPages,
|
||||
DefaultMessage: message.FallbackMsg(message.TribeExceededMaximumNumberOfPages,
|
||||
"{{.Mention}} You have exceeded the maximum number of pages ({{.Page}}/{{.MaxPage}})."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
"Page": page,
|
||||
"MaxPage": totalPages,
|
||||
},
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
code := twmodel.VersionCodeFromServerKey(server)
|
||||
version, err := s.cfg.API.Version.Read(code)
|
||||
if err != nil || version == nil {
|
||||
s.SendMessage(m.ChannelID, ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.InternalServerError,
|
||||
DefaultMessage: message.FallbackMsg(message.InternalServerError,
|
||||
"{{.Mention}} An internal server error has occurred, please try again later."),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
msg := &MessageEmbedFieldBuilder{}
|
||||
for i, player := range playerList.Items {
|
||||
if player == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
rank := 0
|
||||
score := 0
|
||||
switch command {
|
||||
case TopODACommand:
|
||||
rank = player.RankAtt
|
||||
score = player.ScoreAtt
|
||||
case TopODDCommand:
|
||||
rank = player.RankDef
|
||||
score = player.ScoreDef
|
||||
case TopODSCommand:
|
||||
rank = player.RankSup
|
||||
score = player.ScoreSup
|
||||
case TopODCommand:
|
||||
rank = player.RankTotal
|
||||
score = player.ScoreTotal
|
||||
case TopPointsCommand:
|
||||
rank = player.Rank
|
||||
score = player.Points
|
||||
}
|
||||
|
||||
tribeTag := "-"
|
||||
tribeURL := "-"
|
||||
if player.Tribe != nil {
|
||||
tribeTag = player.Tribe.Tag
|
||||
tribeURL = twurlbuilder.BuildTribeURL(server, version.Host, player.Tribe.ID)
|
||||
}
|
||||
|
||||
msg.Append(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.TribeMessageLine,
|
||||
DefaultMessage: message.FallbackMsg(message.TribeMessageLine,
|
||||
"**{{.Index}}**. [`{{.PlayerName}}`]({{.PlayerURL}}) (Tribe: [`{{.TribeTag}}`]({{.TribeURL}}) | Rank: **{{.Rank}}** | Score: **{{.Score}}**)\n"),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Index": offset + i + 1,
|
||||
"PlayerName": player.Name,
|
||||
"PlayerURL": twurlbuilder.BuildPlayerURL(server, version.Host, player.ID),
|
||||
"TribeTag": tribeTag,
|
||||
"TribeURL": tribeURL,
|
||||
"Rank": rank,
|
||||
"Score": humanize.Comma(int64(score)),
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
s.SendEmbed(m.ChannelID, NewEmbed().
|
||||
SetTitle(title).
|
||||
SetFields(msg.ToMessageEmbedFields()).
|
||||
SetFooter(ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.PaginationLabelDisplayedPage,
|
||||
DefaultMessage: message.FallbackMsg(message.PaginationLabelDisplayedPage, "{{.Page}} of {{.MaxPage}}"),
|
||||
TemplateData: map[string]interface{}{
|
||||
"Page": page,
|
||||
"MaxPage": totalPages,
|
||||
},
|
||||
})))
|
||||
}
|
||||
|
|
Reference in New Issue