add a command handler interface, refactor - coords translation commands
This commit is contained in:
parent
fee7b101cd
commit
0d669e0c01
|
@ -38,3 +38,9 @@ func (hs commandHandlers) find(cmd Command) *commandHandler {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type commandHandlerInterface interface {
|
||||
cmd() Command
|
||||
requireAdmPermissions() bool
|
||||
execute(ctx *commandCtx, m *discordgo.MessageCreate, args ...string)
|
||||
}
|
||||
|
|
|
@ -22,6 +22,62 @@ const (
|
|||
|
||||
var coordsRegex = regexp.MustCompile(`(\d+)\|(\d+)`)
|
||||
|
||||
type commandCoordsTranslation struct {
|
||||
*Session
|
||||
}
|
||||
|
||||
var _ commandHandlerInterface = &commandCoordsTranslation{}
|
||||
|
||||
func (c *commandCoordsTranslation) cmd() Command {
|
||||
return CoordsTranslationCommand
|
||||
}
|
||||
|
||||
func (c *commandCoordsTranslation) requireAdmPermissions() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *commandCoordsTranslation) execute(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
argsLength := len(args)
|
||||
if argsLength != 1 {
|
||||
c.SendMessage(
|
||||
m.ChannelID,
|
||||
m.Author.Mention()+" "+ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.HelpCoordsTranslation,
|
||||
TemplateData: map[string]interface{}{
|
||||
"Command": c.cmd().WithPrefix(c.cfg.CommandPrefix),
|
||||
},
|
||||
}),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
serverKey := args[0]
|
||||
server, err := c.cfg.API.Server.Read(serverKey, nil)
|
||||
if err != nil || server == nil {
|
||||
c.SendMessage(
|
||||
m.ChannelID,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.CoordsTranslationServerNotFound,
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.server.CoordsTranslation = serverKey
|
||||
go c.cfg.ServerRepository.Update(context.Background(), ctx.server)
|
||||
|
||||
c.SendMessage(m.ChannelID,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.CoordsTranslationSuccess,
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
func (s *Session) handleCoordsTranslationCommand(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
argsLength := len(args)
|
||||
if argsLength != 1 {
|
||||
|
@ -65,6 +121,33 @@ func (s *Session) handleCoordsTranslationCommand(ctx *commandCtx, m *discordgo.M
|
|||
}))
|
||||
}
|
||||
|
||||
type commandDisableCoordsTranslation struct {
|
||||
*Session
|
||||
}
|
||||
|
||||
var _ commandHandlerInterface = &commandDisableCoordsTranslation{}
|
||||
|
||||
func (c *commandDisableCoordsTranslation) cmd() Command {
|
||||
return DisableCoordsTranslationCommand
|
||||
}
|
||||
|
||||
func (c *commandDisableCoordsTranslation) requireAdmPermissions() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *commandDisableCoordsTranslation) execute(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
ctx.server.CoordsTranslation = ""
|
||||
go c.cfg.ServerRepository.Update(context.Background(), ctx.server)
|
||||
|
||||
c.SendMessage(m.ChannelID,
|
||||
ctx.localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: message.DisableCoordsTranslationSuccess,
|
||||
TemplateData: map[string]interface{}{
|
||||
"Mention": m.Author.Mention(),
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
func (s *Session) handleDisableCoordsTranslationCommand(ctx *commandCtx, m *discordgo.MessageCreate, args ...string) {
|
||||
ctx.server.CoordsTranslation = ""
|
||||
go s.cfg.ServerRepository.Update(context.Background(), ctx.server)
|
||||
|
|
|
@ -279,7 +279,7 @@ func (b *MessageEmbedFieldBuilder) ToMessageEmbedFields() []*discordgo.MessageEm
|
|||
}
|
||||
for i, chunk := range b.chunks {
|
||||
fields = append(fields, &discordgo.MessageEmbedField{
|
||||
Name: name + strconv.Itoa(i+1),
|
||||
Name: name + " " + strconv.Itoa(i+1),
|
||||
Value: chunk,
|
||||
})
|
||||
}
|
||||
|
|
Reference in New Issue