fix: respond to commands even when one of translations is missing
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Dawid Wysokiński 2023-07-09 06:23:36 +02:00
parent a8f309c299
commit 9dbfd76c89
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
3 changed files with 105 additions and 16 deletions

View File

@ -69,8 +69,6 @@ func New() *cli.Command {
return fmt.Errorf("couldn't create file (path=%s): %w", healthyFilePath, err)
}
logger.Info("Bot is up and running")
waitForSignal(c.Context)
return nil

View File

@ -143,6 +143,7 @@ func (b *Bot) registerCronJobs() error {
func (b *Bot) handleSessionReady(s *discordgo.Session, _ *discordgo.Ready) {
_ = s.UpdateGameStatus(0, "Tribal Wars")
b.logger.Info("Bot is up and running")
}
func (b *Bot) logCommands(_ *discordgo.Session, i *discordgo.InteractionCreate) {

View File

@ -658,6 +658,10 @@ func (c *groupCommand) buildSubcommandDelete() (*discordgo.ApplicationCommandOpt
}
func (c *groupCommand) handle(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type != discordgo.InteractionApplicationCommand {
return
}
cmdData := i.ApplicationCommandData()
if cmdData.Name != c.name() {
@ -718,7 +722,13 @@ func (c *groupCommand) handleCreate(ctx context.Context, s *discordgo.Session, i
},
})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", successMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", successMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}
@ -742,6 +752,7 @@ func (c *groupCommand) optionsToCreateGroupParams(
internals := false
barbarians := false
languageTag := ""
for _, opt := range options {
switch opt.Name {
case "version":
@ -760,6 +771,7 @@ func (c *groupCommand) optionsToCreateGroupParams(
barbarians = opt.BoolValue()
}
}
return domain.NewCreateGroupParams(
guildID,
version,
@ -804,7 +816,13 @@ func (c *groupCommand) handleList(ctx context.Context, s *discordgo.Session, i *
MessageID: langMessageID,
})
if localizeErr != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", langMessageID), zap.Error(localizeErr))
c.logger.Error("no message with the specified id", zap.String("id", langMessageID), zap.Error(localizeErr))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, localizeErr),
},
})
return
}
@ -821,7 +839,13 @@ func (c *groupCommand) handleList(ctx context.Context, s *discordgo.Session, i *
},
})
if localizeErr != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", fieldValueMessageID), zap.Error(localizeErr))
c.logger.Error("no message with the specified id", zap.String("id", fieldValueMessageID), zap.Error(localizeErr))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, localizeErr),
},
})
return
}
@ -835,7 +859,13 @@ func (c *groupCommand) handleList(ctx context.Context, s *discordgo.Session, i *
titleMessageID := "cmd.group.list.embed.title"
title, err := c.localizer.Localize(locale, &i18n.LocalizeConfig{MessageID: titleMessageID})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", titleMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", titleMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}
@ -893,7 +923,13 @@ func (c *groupCommand) handleDetails(ctx context.Context, s *discordgo.Session,
MessageID: langMessageID,
})
if localizeErr != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", langMessageID), zap.Error(localizeErr))
c.logger.Error("no message with the specified id", zap.String("id", langMessageID), zap.Error(localizeErr))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, localizeErr),
},
})
return
}
@ -911,7 +947,13 @@ func (c *groupCommand) handleDetails(ctx context.Context, s *discordgo.Session,
},
})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", descriptionMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", descriptionMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
@ -978,7 +1020,13 @@ func (c *groupCommand) handleSetLanguage(ctx context.Context, s *discordgo.Sessi
successMessageID := "cmd.group.set.language.success"
successMsg, err := c.localizer.Localize(locale, &i18n.LocalizeConfig{MessageID: successMessageID})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", successMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", successMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}
@ -1021,7 +1069,13 @@ func (c *groupCommand) handleSetChannelGains(ctx context.Context, s *discordgo.S
successMessageID := "cmd.group.set.channel-gains.success"
successMsg, err := c.localizer.Localize(locale, &i18n.LocalizeConfig{MessageID: successMessageID})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", successMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", successMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}
@ -1064,7 +1118,13 @@ func (c *groupCommand) handleSetChannelLosses(ctx context.Context, s *discordgo.
successMessageID := "cmd.group.set.channel-losses.success"
successMsg, err := c.localizer.Localize(locale, &i18n.LocalizeConfig{MessageID: successMessageID})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", successMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", successMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}
@ -1107,7 +1167,13 @@ func (c *groupCommand) handleSetInternals(ctx context.Context, s *discordgo.Sess
successMessageID := "cmd.group.set.internals.success"
successMsg, err := c.localizer.Localize(locale, &i18n.LocalizeConfig{MessageID: successMessageID})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", successMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", successMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}
@ -1150,7 +1216,13 @@ func (c *groupCommand) handleSetBarbarians(ctx context.Context, s *discordgo.Ses
successMessageID := "cmd.group.set.barbarians.success"
successMsg, err := c.localizer.Localize(locale, &i18n.LocalizeConfig{MessageID: successMessageID})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", successMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", successMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}
@ -1205,7 +1277,13 @@ func (c *groupCommand) handleTribeAdd(ctx context.Context, s *discordgo.Session,
successMessageID := "cmd.group.tribe.add.success"
successMsg, err := c.localizer.Localize(locale, &i18n.LocalizeConfig{MessageID: successMessageID})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", successMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", successMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}
@ -1248,7 +1326,13 @@ func (c *groupCommand) handleTribeRemove(ctx context.Context, s *discordgo.Sessi
successMessageID := "cmd.group.tribe.remove.success"
successMsg, err := c.localizer.Localize(locale, &i18n.LocalizeConfig{MessageID: successMessageID})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", successMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", successMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}
@ -1277,7 +1361,13 @@ func (c *groupCommand) handleDelete(ctx context.Context, s *discordgo.Session, i
successMessageID := "cmd.group.delete.success"
successMsg, err := c.localizer.Localize(locale, &i18n.LocalizeConfig{MessageID: successMessageID})
if err != nil {
c.logger.Error("no message with the specified identifier", zap.String("id", successMessageID), zap.Error(err))
c.logger.Error("no message with the specified id", zap.String("id", successMessageID), zap.Error(err))
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: c.localizer.LocalizeError(locale, err),
},
})
return
}