This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
/
log_channel_set_handler.go
64 lines (56 loc) · 1.92 KB
/
log_channel_set_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
package command_handlers
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/oppzippy/BoostRequestBot/boost_request/repository"
)
type LogChannelSetHandler struct {
bundle *i18n.Bundle
repo repository.Repository
discord *discordgo.Session
}
func NewLogChannelSetHandler(bundle *i18n.Bundle, repo repository.Repository, discord *discordgo.Session) *LogChannelSetHandler {
return &LogChannelSetHandler{
bundle: bundle,
repo: repo,
discord: discord,
}
}
func (h *LogChannelSetHandler) Handle(event *discordgo.InteractionCreate, options map[string]*discordgo.ApplicationCommandInteractionDataOption) (*discordgo.InteractionResponse, error) {
localizer := i18n.NewLocalizer(h.bundle, string(event.Locale))
channel := options["channel"].ChannelValue(h.discord)
if channel.Type != discordgo.ChannelTypeGuildText {
return &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: localizer.MustLocalize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "MustBeTextChannel",
Other: "The specified channel must be a text channel.",
},
}),
Flags: uint64(discordgo.MessageFlagsEphemeral),
},
}, nil
}
err := h.repo.InsertLogChannel(event.GuildID, channel.ID)
if err != nil {
return nil, fmt.Errorf("error setting boost request log channel: %w", err)
}
return &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: localizer.MustLocalize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "BoostRequestLogChannelSet",
Other: "Boost request log channel set to {{.Channel}}.",
},
TemplateData: map[string]string{
"Channel": channel.Mention(),
},
}),
Flags: uint64(discordgo.MessageFlagsEphemeral),
},
}, nil
}