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
10 changes: 9 additions & 1 deletion cmd/discord/gateway/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,15 @@ func (c *eventContext) saveInteractionEvent(msg discordgo.Message, msgErr error,
}
}
func (c *eventContext) InteractionResponse(reply common.Reply) (discordgo.Message, error) {
return c.CreateMessage(c.Context, c.channelID, reply)
msg, err := c.CreateMessage(c.Context, c.channelID, reply)
// missing permissions with files - send a permissions error instead
if errors.Is(err, rest.ErrMissingPermissions) && len(reply.Peek().Files) > 0 {
msg, err = c.gw.rest.CreateMessage(c.Context, c.channelID, discordgo.MessageSend{
Content: c.localize("common_error_missing_permissions_images"),
Reference: reply.Peek().Reference,
}, nil)
}
return msg, err
}

func (c *eventContext) InteractionFollowUp(ctx context.Context, reply common.Reply) (discordgo.Message, error) {
Expand Down
23 changes: 22 additions & 1 deletion cmd/discord/router/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cufee/aftermath/cmd/discord/common"
"github.com/cufee/aftermath/cmd/discord/rest"
"github.com/cufee/aftermath/internal/database"
"github.com/cufee/aftermath/internal/retry"
"github.com/cufee/aftermath/internal/database/models"
"github.com/cufee/aftermath/internal/external/averages"
"github.com/cufee/aftermath/internal/localization"
Expand Down Expand Up @@ -122,7 +123,7 @@ func (c *routeContext) InteractionResponse(reply common.Reply) (discordgo.Messag
go c.saveInteractionEvent(discordgo.Message{}, c.Context.Err(), reply)
return discordgo.Message{}, c.Context.Err()
default:
msg, err := common.WithRetry(func() (discordgo.Message, error) {
res := retry.Retry(func() (discordgo.Message, error) {
// since we already finished handling the interaction, there is no need to use the handler context
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
Expand All @@ -135,7 +136,20 @@ func (c *routeContext) InteractionResponse(reply common.Reply) (discordgo.Messag
return discordgo.Message{}, err
}
return c.rest.UpdateInteractionResponse(ctx, c.interaction.AppID, c.interaction.Token, data.Interaction(), files)
}, 5, time.Second, func(err error) bool {
// do not retry on missing permissions, they will not change between retries
return errors.Is(err, rest.ErrMissingPermissions)
})
msg, err := res.Data, res.Err

// missing permissions with files - send a permissions error instead
if errors.Is(err, rest.ErrMissingPermissions) && len(files) > 0 {
permData := discordgo.InteractionResponseData{Content: c.localize("common_error_missing_permissions_images")}
retryCtx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
msg, err = c.rest.UpdateInteractionResponse(retryCtx, c.interaction.AppID, c.interaction.Token, permData, nil)
}

if errors.Is(err, rest.ErrUnknownInteraction) || errors.Is(err, rest.ErrUnknownWebhook) {
// Discord did not propagate the ack, this happens on low usage discord servers sometimes
message := data.Message()
Expand All @@ -144,6 +158,13 @@ func (c *routeContext) InteractionResponse(reply common.Reply) (discordgo.Messag
fallbackCtx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
msg, err = c.rest.CreateMessage(fallbackCtx, c.interaction.ChannelID, message, files)

// channel message also failed on permissions with files - send permissions error only
if errors.Is(err, rest.ErrMissingPermissions) && len(files) > 0 {
msg, err = c.rest.CreateMessage(fallbackCtx, c.interaction.ChannelID, discordgo.MessageSend{
Content: c.localize("common_error_missing_permissions_images"),
}, nil)
}
}

go c.saveInteractionEvent(msg, err, reply)
Expand Down
1 change: 1 addition & 0 deletions static/localization/en/discord.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ common_error_command_missing_permissions: "You don't have permission to use this
common_error_discord_outage: "It looks like Discord is having some temporary issues. Please try again in a few seconds."
common_error_missing_permissions_dm: "Aftermath is not able to send you a direct message."
common_error_missing_permissions_dm_mention_fmt: "Hey %s, Aftermath is not able to send you a direct message."
common_error_missing_permissions_images: "Aftermath is missing permissions to send images in this channel. Please make sure the bot has the **Attach Files** permission enabled."
common_error_payment_required: |-
This feature of Aftermath is only available for users with an active subscription.
You can subscribe by using the `/subscribe` command.
Expand Down
1 change: 1 addition & 0 deletions static/localization/pl/discord.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ common_error_command_missing_permissions: "Nie masz uprawnień do użycia tego p
common_error_discord_outage: "Wygląda na to, że Discord ma chwilowe problemy. Spróbuj ponownie za kilka sekund."
common_error_missing_permissions_dm: "Aftermath nie jest w stanie wysłać Ci bezpośredniej wiadomości."
common_error_missing_permissions_dm_mention_fmt: "Hej %s, Aftermath nie jest w stanie wysłać Ci bezpośredniej wiadomości."
common_error_missing_permissions_images: "Aftermath nie ma uprawnień do wysyłania obrazów na tym kanale. Upewnij się, że bot ma włączone uprawnienie **Attach Files**."
common_error_payment_required: |-
Ta funkcja Aftermath jest dostępna tylko dla użytkowników z aktywną subskrypcją.
Możesz subskrybować, używając polecenia `/subscribe`.
Expand Down
1 change: 1 addition & 0 deletions static/localization/pt-BR/discord.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ common_error_command_missing_permissions: "Você não tem permissão para usar e
common_error_discord_outage: "Parece que o Discord está com problemas temporários. Tente novamente em alguns segundos."
common_error_missing_permissions_dm: "O Aftermath não pode te enviar uma mensagem direta."
common_error_missing_permissions_dm_mention_fmt: "Olá %s, o Aftermath não pode lhe enviar uma mensagem direta."
common_error_missing_permissions_images: "O Aftermath não tem permissões para enviar imagens neste canal. Certifique-se de que o bot tenha a permissão **Attach Files** ativada."
common_error_payment_required: |-
Este recurso é exclusivo de assinantes.
Você pode ter uma assinatura pelo comando `/subscribe`.
Expand Down
Loading