112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
// TODO(toby3d): save lines in a public channel for references?
|
|
package discord
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
"golang.org/x/text/language"
|
|
"golang.org/x/text/message"
|
|
"golang.org/x/text/message/catalog"
|
|
|
|
"source.toby3d.me/toby3d/alice/internal/domain"
|
|
)
|
|
|
|
type Handler struct {
|
|
logger *log.Logger
|
|
}
|
|
|
|
const InteractionID string = "line"
|
|
|
|
func init() {
|
|
for _, entry := range [...]domain.CatalogEntry{
|
|
{
|
|
Tag: language.English, Key: "You must describe the subject of the line.",
|
|
Message: "You must describe the subject of the line.",
|
|
},
|
|
{
|
|
Tag: language.Russian, Key: "You must describe the subject of the line.",
|
|
Message: "Вы должны описать предмет линии.",
|
|
},
|
|
{Tag: language.English, Key: "(Line: %s)", Message: "(Line: %s)"},
|
|
{Tag: language.Russian, Key: "(Line: %s)", Message: "(Линия: %s)"},
|
|
} {
|
|
switch msg := entry.Message.(type) {
|
|
case string:
|
|
message.SetString(entry.Tag, entry.Key, msg)
|
|
case catalog.Message:
|
|
message.Set(entry.Tag, entry.Key, msg)
|
|
case []catalog.Message:
|
|
message.Set(entry.Tag, entry.Key, msg...)
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewHandler(logger *log.Logger) *Handler {
|
|
return &Handler{
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) ServeReady(s *discordgo.Session, r *discordgo.Ready) {
|
|
for i := range r.Guilds {
|
|
if _, err := s.ApplicationCommandCreate(s.State.User.ID, r.Guilds[i].ID, &discordgo.ApplicationCommand{
|
|
Name: InteractionID,
|
|
NameLocalizations: &map[discordgo.Locale]string{
|
|
discordgo.Russian: "линия",
|
|
},
|
|
Description: "request a complete content ban",
|
|
DescriptionLocalizations: &map[discordgo.Locale]string{
|
|
discordgo.Russian: "запросить полный запрет контента",
|
|
},
|
|
Options: []*discordgo.ApplicationCommandOption{{
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
Name: "description",
|
|
NameLocalizations: map[discordgo.Locale]string{
|
|
discordgo.Russian: "описание",
|
|
},
|
|
Description: "description of the content to be banned",
|
|
DescriptionLocalizations: map[discordgo.Locale]string{
|
|
discordgo.Russian: "описание контента который требуется запретить",
|
|
},
|
|
Required: true,
|
|
}},
|
|
}); err != nil {
|
|
h.logger.Println("cannot register command:", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (h *Handler) ServeInteraction(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
if i.Type != discordgo.InteractionApplicationCommand {
|
|
return
|
|
}
|
|
|
|
data := i.ApplicationCommandData()
|
|
|
|
if data.Name != InteractionID {
|
|
return
|
|
}
|
|
|
|
printer := message.NewPrinter(language.Make(string(i.Locale)))
|
|
|
|
if len(data.Options) == 0 {
|
|
go s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
Data: &discordgo.InteractionResponseData{
|
|
Content: printer.Sprintf("You must describe the subject of the line."),
|
|
Flags: discordgo.MessageFlagsEphemeral,
|
|
},
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
go s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
Data: &discordgo.InteractionResponseData{
|
|
Content: printer.Sprintf("(Line: %s)", data.Options[0].StringValue()),
|
|
},
|
|
})
|
|
}
|