This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
/
channels_list_handler.go
78 lines (69 loc) · 2.22 KB
/
channels_list_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package command_handlers
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/oppzippy/BoostRequestBot/boost_request/repository"
)
type ChannelsListHandler struct {
bundle *i18n.Bundle
repo repository.Repository
}
func NewChannelsListHandler(bundle *i18n.Bundle, repo repository.Repository) *ChannelsListHandler {
return &ChannelsListHandler{
bundle: bundle,
repo: repo,
}
}
func (h *ChannelsListHandler) Handle(event *discordgo.InteractionCreate, options map[string]*discordgo.ApplicationCommandInteractionDataOption) (*discordgo.InteractionResponse, error) {
localizer := i18n.NewLocalizer(h.bundle, string(event.Locale))
channels, err := h.repo.GetBoostRequestChannels(event.GuildID)
if err != nil {
return nil, fmt.Errorf("failed to fetch list of boost request channels: %w", err)
}
sb := strings.Builder{}
for i, brc := range channels {
options := make([]string, 0, 2)
if brc.SkipsBuyerDM {
options = append(options, "doesn't dm buyer")
}
if brc.UsesBuyerMessage {
options = append(options, "reacts directly to buyer's message")
}
if len(options) == 0 {
options = append(options, "none")
}
// TODO localize
sb.WriteString("**Channel ")
sb.WriteString(fmt.Sprintf("%d", i+1))
sb.WriteString("**\nFrontend Channel: <#")
sb.WriteString(brc.FrontendChannelID)
sb.WriteString(">\nBackend Channel: <#")
sb.WriteString(brc.BackendChannelID)
sb.WriteString(">\nOptions: ")
sb.WriteString(strings.Join(options, ", "))
sb.WriteString("\n")
}
if sb.Len() == 0 {
return &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: localizer.MustLocalize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "NoBoostRequestChannelsExist",
Other: "There are no boost request channels.",
},
}),
Flags: uint64(discordgo.MessageFlagsEphemeral),
},
}, nil
}
return &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: strings.TrimSpace(sb.String()),
Flags: uint64(discordgo.MessageFlagsEphemeral),
},
}, nil
}