Merge branch 'feature/line' into develop

This commit is contained in:
Maxim Lebedev 2023-08-29 06:30:07 +06:00
commit 42ef399a5c
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
3 changed files with 120 additions and 6 deletions

View File

@ -0,0 +1,111 @@
// 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()),
},
})
}

View File

@ -75,13 +75,13 @@ func (h *Handler) ServeInteraction(s *discordgo.Session, i *discordgo.Interactio
return
}
printer := message.NewPrinter(language.Make(string(i.Locale)))
data := i.ApplicationCommandData()
if data.Name != InteractionID {
return
}
printer := message.NewPrinter(language.Make(string(i.Locale)))
respond := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{

13
main.go
View File

@ -13,6 +13,7 @@ import (
characterdiscorddelivery "source.toby3d.me/toby3d/alice/internal/character/delivery/discord"
"source.toby3d.me/toby3d/alice/internal/domain"
linediscorddelivery "source.toby3d.me/toby3d/alice/internal/line/delivery/discord"
searchdiscorddelivery "source.toby3d.me/toby3d/alice/internal/search/delivery/discord"
searchfsrepo "source.toby3d.me/toby3d/alice/internal/search/repository/memory"
searchusecase "source.toby3d.me/toby3d/alice/internal/search/usecase"
@ -51,21 +52,23 @@ func main() {
voicemailDiscordHandler := voicemaildiscorddelivery.NewHandler(voicemailsService, assets, logger, *config)
xDiscordHandler := xdiscorddelivery.NewHandler(assets, logger)
characterDiscordDelivery := characterdiscorddelivery.NewHandler(logger)
lineDiscordHandler := linediscorddelivery.NewHandler(logger)
searches := searchfsrepo.NewMemorySearchRepository()
searchService := searchusecase.NewSearchUseCase(searches, rand.NewSource(time.Now().UnixNano()))
searchDiscordDelivery := searchdiscorddelivery.NewHandler(searchService, assets, logger)
for _, h := range []any{
xDiscordHandler.ServeReady,
characterDiscordDelivery.ServeReady,
lineDiscordHandler.ServeReady,
searchDiscordDelivery.ServeReady,
voicemailDiscordHandler.ServeReady,
characterDiscordDelivery.ServeReady,
xDiscordHandler.ServeReady,
voicemailDiscordHandler.ServeMessage,
xDiscordHandler.ServeInteraction,
voicemailDiscordHandler.ServeInteraction,
characterDiscordDelivery.ServeInteraction,
lineDiscordHandler.ServeInteraction,
searchDiscordDelivery.ServeInteraction,
voicemailDiscordHandler.ServeInteraction,
xDiscordHandler.ServeInteraction,
} {
session.AddHandler(h)
}