Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/discord/commands/public/career.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ func careerCommandHandler(ctx common.Context) error {
}

ioptions.AccountID = accountID
button, saveErr := ioptions.refreshButton(ctx, ctx.ID())
button, saveErr := ioptions.actionRow(ctx, ctx.ID(), true)
if saveErr != nil {
// nil button will not cause an error and will be ignored
log.Err(err).Str("interactionId", ctx.ID()).Str("command", "session").Msg("failed to save discord interaction")
log.Err(saveErr).Str("interactionId", ctx.ID()).Str("command", ctx.ID()).Msg("failed to save discord interaction")
}

var buf bytes.Buffer
Expand Down
57 changes: 44 additions & 13 deletions cmd/discord/commands/public/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"strings"
"unicode"
"unicode/utf8"

"github.com/cufee/aftermath/internal/external/wargaming"
"github.com/cufee/aftermath/internal/json"
Expand Down Expand Up @@ -37,15 +39,15 @@ func (o statsOptions) fromInteraction(data models.DiscordInteraction) (statsOpti
return o, json.Unmarshal([]byte(raw), &o)
}

func (o statsOptions) refreshButton(ctx common.Context, id string) (discordgo.MessageComponent, error) {
func (o statsOptions) interactionButton(ctx common.Context, eventID string, result string) (models.DiscordInteraction, error) {
encoded, err := json.Marshal(o)
if err != nil {
return nil, err
return models.DiscordInteraction{}, err
}

interaction := models.DiscordInteraction{
Result: "generated-refresh-button",
EventID: id,
Result: result,
EventID: eventID,
Locale: ctx.Locale(),
UserID: ctx.User().ID,
GuildID: ctx.RawInteraction().GuildID,
Expand All @@ -58,12 +60,31 @@ func (o statsOptions) refreshButton(ctx common.Context, id string) (discordgo.Me
interaction.MessageID = ctx.RawInteraction().Message.ID
}

interaction, err = ctx.Core().Database().CreateDiscordInteraction(ctx.Ctx(), interaction)
return ctx.Core().Database().CreateDiscordInteraction(ctx.Ctx(), interaction)
}

func (o statsOptions) actionRow(ctx common.Context, refreshEventID string, includeSession bool) (discordgo.MessageComponent, error) {
refresh, err := o.interactionButton(ctx, refreshEventID, "generated-refresh-button")
if err != nil {
return nil, err
}

return newStatsRefreshButton(interaction), nil
row := discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
newStatsRefreshButton(refresh),
},
}
if !includeSession {
return row, nil
}

session, sessionErr := o.interactionButton(ctx, "session", "generated-session-button")
if sessionErr != nil {
return row, sessionErr
}

row.Components = append(row.Components, newStatsSessionButton(ctx, session))
return row, nil
}

var validNicknameSeparatorChars = []string{"!", "@", "#", "$", "%", "^", "&", "*", "-", "=", "+"}
Expand Down Expand Up @@ -170,12 +191,22 @@ func realmSelectButtons(ctx common.Context, id string, accounts []fetch.AccountW
return ctx.Reply().Hint("stats_bad_nickname_input_hint").Component(row).Text(strings.Join(message, "\n")), nil
}

func newStatsRefreshButton(data models.DiscordInteraction) discordgo.MessageComponent {
return discordgo.ActionsRow{
Components: []discordgo.MessageComponent{discordgo.Button{
Style: discordgo.SecondaryButton,
Emoji: emoji.Refresh(),
CustomID: fmt.Sprintf("refresh_stats_from_button#%s", data.ID),
}},
func newStatsRefreshButton(data models.DiscordInteraction) discordgo.Button {
return discordgo.Button{
Style: discordgo.SecondaryButton,
Emoji: emoji.Refresh(),
CustomID: fmt.Sprintf("refresh_stats_from_button#%s", data.ID),
}
}

func newStatsSessionButton(ctx common.Context, data models.DiscordInteraction) discordgo.Button {
label := ctx.Localize("command_session_name")
r, size := utf8.DecodeRuneInString(label)
label = string(unicode.ToUpper(r)) + label[size:]

return discordgo.Button{
Style: discordgo.SecondaryButton,
Label: label,
CustomID: fmt.Sprintf("refresh_stats_from_button#%s", data.ID),
}
}
4 changes: 2 additions & 2 deletions cmd/discord/commands/public/my.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ func init() {
}

ioptions.AccountID = accountID
button, saveErr := ioptions.refreshButton(ctx, subcommand)
button, saveErr := ioptions.actionRow(ctx, subcommand, subcommand != "session")
if saveErr != nil {
// nil button will not cause an error and will be ignored
log.Err(err).Str("interactionId", ctx.ID()).Str("command", "session").Msg("failed to save discord interaction")
log.Err(saveErr).Str("interactionId", ctx.ID()).Str("command", subcommand).Msg("failed to save discord interaction")
}

var buf bytes.Buffer
Expand Down
4 changes: 2 additions & 2 deletions cmd/discord/commands/public/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ func init() {
}

ioptions.AccountID = accountID
button, saveErr := ioptions.refreshButton(ctx, ctx.ID())
button, saveErr := ioptions.actionRow(ctx, ctx.ID(), false)
if saveErr != nil {
// nil button will not cause an error and will be ignored
log.Err(err).Str("interactionId", ctx.ID()).Str("command", "session").Msg("failed to save discord interaction")
log.Err(saveErr).Str("interactionId", ctx.ID()).Str("command", "session").Msg("failed to save discord interaction")
}

var buf bytes.Buffer
Expand Down
5 changes: 3 additions & 2 deletions cmd/discord/commands/public/stats_interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ func init() {
return ctx.Reply().IsError(common.UserError).Send("stats_refresh_interaction_error_expired")
}

button, saveErr := ioptions.refreshButton(ctx, interaction.EventID)
includeSession := interaction.EventID != "session" && interaction.EventID != "replay"
button, saveErr := ioptions.actionRow(ctx, interaction.EventID, includeSession)
if saveErr != nil {
// nil button will not cause an error and will be ignored
log.Err(err).Str("interactionId", ctx.ID()).Str("command", "session").Msg("failed to save discord interaction")
log.Err(saveErr).Str("interactionId", ctx.ID()).Str("command", interaction.EventID).Msg("failed to save discord interaction")
}

var buf bytes.Buffer
Expand Down
Loading