Skip to content

Commit

Permalink
get language in event (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonNekoGH authored Mar 20, 2024
1 parent 1d99d7e commit 6084293
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
18 changes: 11 additions & 7 deletions internal/bots/discord/listeners/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package listeners

import (
"fmt"
"github.com/nekomeowww/insights-bot/pkg/i18n"
"strings"

"github.com/disgoorg/disgo/discord"
Expand All @@ -26,18 +27,21 @@ type NewListenersParam struct {

Logger *logger.Logger
SmrQueue *smrqueue.Queue
I18n *i18n.I18n
}

type Listeners struct {
logger *logger.Logger
smrQueue *smrqueue.Queue
i18n *i18n.I18n
}

func NewListeners() func(param NewListenersParam) *Listeners {
return func(param NewListenersParam) *Listeners {
return &Listeners{
logger: param.Logger,
smrQueue: param.SmrQueue,
i18n: param.I18n,
}
}
}
Expand All @@ -52,14 +56,15 @@ func (b *Listeners) smrCmd(event *events.ApplicationCommandInteractionCreate, da

b.logger.Info(fmt.Sprintf("discord: command received: /smr %s", urlString))

lang := event.Locale().String()

// url check
err, originErr := smr.CheckUrl(urlString)
if err != nil {
if smr.IsUrlCheckError(err) {
err = event.CreateMessage(
discord.NewMessageCreateBuilder().
// TODO: i18n support for discord
SetContent(smr.FormatUrlCheckError(err, bot.FromPlatformDiscord, "", nil)).
SetContent(smr.FormatUrlCheckError(err, bot.FromPlatformDiscord, lang, nil)).
Build(),
)
if err != nil {
Expand All @@ -69,7 +74,7 @@ func (b *Listeners) smrCmd(event *events.ApplicationCommandInteractionCreate, da
return
}

err = event.CreateMessage(discord.NewMessageCreateBuilder().SetContent("出现了一些问题,可以再试试?").Build())
err = event.CreateMessage(discord.NewMessageCreateBuilder().SetContent(b.i18n.TWithLanguage(lang, "commands.groups.summarization.commands.smr.failedToRead")).Build())
if err != nil {
b.logger.Warn("discord: failed to send error message", zap.Error(err), zap.NamedError("original_error", originErr))
}
Expand All @@ -79,7 +84,7 @@ func (b *Listeners) smrCmd(event *events.ApplicationCommandInteractionCreate, da

// must reply the interaction as soon as possible
err = event.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("请稍等,量子速读中...").
SetContent(b.i18n.TWithLanguage(lang, "commands.groups.summarization.commands.smr.reading")).
Build())
if err != nil {
b.logger.Warn("discord: failed to send response message", zap.Error(err))
Expand All @@ -90,13 +95,12 @@ func (b *Listeners) smrCmd(event *events.ApplicationCommandInteractionCreate, da
Platform: bot.FromPlatformDiscord,
URL: urlString,
ChannelID: event.Channel().ID().String(),
// TODO: support i18n for discord
Language: "zh-CN",
Language: lang,
})
if err != nil {
b.logger.Warn("discord: failed to add task", zap.Error(err))

err = event.CreateMessage(discord.NewMessageCreateBuilder().SetContent("出现了一些问题,可以再试试?").Build())
err = event.CreateMessage(discord.NewMessageCreateBuilder().SetContent("commands.groups.summarization.commands.smr.failedToRead").Build())
if err != nil {
b.logger.Warn("discord: failed to send error message", zap.Error(err))
}
Expand Down
3 changes: 1 addition & 2 deletions internal/bots/slack/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ func (h *Handlers) PostCommandInfo(ctx *gin.Context) {
URL: urlString,
ChannelID: body.ChannelID,
TeamID: body.TeamID,
// TODO: support i18n for discord
Language: "zh-CN",
Language: user.Locale,
})
if err != nil {
h.logger.Warn("slack: failed to add task", zap.Error(err))
Expand Down
1 change: 0 additions & 1 deletion internal/services/smr/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ func (s *Service) processor(info types.TaskInfo) {
smrResult, err := s.model.SummarizeInputURL(ctx, info.URL, info.Platform)
if err != nil {
s.logger.Warn("smr service: summarization failed", zap.Error(err))
// TODO: support i18n for discord
errStr := s.processError(err, lo.Ternary(info.Language == "", "en", info.Language))
s.sendResult(nil, info, errStr)

Expand Down

0 comments on commit 6084293

Please sign in to comment.