From d9ce580015acfb688c9eb55ae3c5b6f9cea12209 Mon Sep 17 00:00:00 2001 From: Sergey Date: Sat, 4 May 2024 05:47:50 +0300 Subject: [PATCH] feat: add discord /missing proper command --- pkg/reporters/discord/discord.go | 23 ++++++++++++++++++----- pkg/reporters/discord/help.go | 9 +-------- pkg/reporters/discord/missing.go | 13 +------------ pkg/reporters/discord/params.go | 9 +-------- 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/pkg/reporters/discord/discord.go b/pkg/reporters/discord/discord.go index 4c2d666..c080ead 100644 --- a/pkg/reporters/discord/discord.go +++ b/pkg/reporters/discord/discord.go @@ -9,6 +9,7 @@ import ( statePkg "main/pkg/state" templatesPkg "main/pkg/templates" types "main/pkg/types" + "main/pkg/utils" "strings" "sync" "time" @@ -199,16 +200,28 @@ func (reporter *Reporter) Send(report *types.Report) error { } func (reporter *Reporter) BotRespond(s *discordgo.Session, i *discordgo.InteractionCreate, text string) { - err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + chunks := utils.SplitStringIntoChunks(text, 2000) + firstChunk, rest := chunks[0], chunks[1:] + + if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: text, + Content: firstChunk, }, - }) - - if err != nil { + }); err != nil { reporter.Logger.Error().Err(err).Msg("Error sending response") } + + for index, chunk := range rest { + if _, err := s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{ + Content: chunk, + }); err != nil { + reporter.Logger.Error(). + Int("chunk", index). + Err(err). + Msg("Error sending followup message") + } + } } func (reporter *Reporter) SerializeDate(date time.Time) string { diff --git a/pkg/reporters/discord/help.go b/pkg/reporters/discord/help.go index 8bea503..81392e2 100644 --- a/pkg/reporters/discord/help.go +++ b/pkg/reporters/discord/help.go @@ -24,14 +24,7 @@ func (reporter *Reporter) GetHelpCommand() *Command { return } - if err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: template, - }, - }); err != nil { - reporter.Logger.Error().Err(err).Msg("Error sending help") - } + reporter.BotRespond(s, i, template) }, } } diff --git a/pkg/reporters/discord/missing.go b/pkg/reporters/discord/missing.go index d1fae1e..429fd41 100644 --- a/pkg/reporters/discord/missing.go +++ b/pkg/reporters/discord/missing.go @@ -66,18 +66,7 @@ func (reporter *Reporter) GetMissingCommand() *Command { return } - chunks := utils.SplitStringIntoChunks(template, 2000) - - for _, chunk := range chunks { - if err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: chunk, - }, - }); err != nil { - reporter.Logger.Error().Err(err).Msg("Error sending missing") - } - } + reporter.BotRespond(s, i, template) }, } } diff --git a/pkg/reporters/discord/params.go b/pkg/reporters/discord/params.go index 2d509b8..abd5214 100644 --- a/pkg/reporters/discord/params.go +++ b/pkg/reporters/discord/params.go @@ -36,14 +36,7 @@ func (reporter *Reporter) GetParamsCommand() *Command { return } - if err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: template, - }, - }); err != nil { - reporter.Logger.Error().Err(err).Msg("Error sending params") - } + reporter.BotRespond(s, i, template) }, } }