🎨 Format of the code

This commit is contained in:
Maxim Lebedev 2023-08-29 06:26:38 +06:00
parent 7a97fa48ba
commit 1831742dcc
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
3 changed files with 54 additions and 21 deletions

View File

@ -1,15 +1,47 @@
// 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,
@ -19,7 +51,7 @@ func NewHandler(logger *log.Logger) *Handler {
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: "line",
Name: InteractionID,
NameLocalizations: &map[discordgo.Locale]string{
discordgo.Russian: "линия",
},
@ -46,32 +78,34 @@ func (h *Handler) ServeReady(s *discordgo.Session, r *discordgo.Ready) {
}
func (h *Handler) ServeInteraction(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type != discordgo.InteractionApplicationCommand {
return
}
data := i.ApplicationCommandData()
if data.Name != "line" {
if data.Name != InteractionID {
return
}
printer := message.NewPrinter(language.Make(string(i.Locale)))
if len(data.Options) == 0 {
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
go s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You must describe your line.",
Content: printer.Sprintf("You must describe the subject of the line."),
Flags: discordgo.MessageFlagsEphemeral,
},
}); err != nil {
h.logger.Println("cannot make interaction response:", err)
}
})
return
}
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
go s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "(Line: " + data.Options[0].StringValue() + ")",
Content: printer.Sprintf("(Line: %s)", data.Options[0].StringValue()),
},
}); err != nil {
h.logger.Println("cannot make interaction response:", err)
}
})
}

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{

15
main.go
View File

@ -13,10 +13,10 @@ 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"
linediscorddelivery "source.toby3d.me/toby3d/alice/internal/line/delivery/discord"
voicemaildiscorddelivery "source.toby3d.me/toby3d/alice/internal/voicemail/delivery/discord"
voicemailfsrepo "source.toby3d.me/toby3d/alice/internal/voicemail/repository/fs"
voicemailusecase "source.toby3d.me/toby3d/alice/internal/voicemail/usecase"
@ -52,24 +52,23 @@ func main() {
voicemailDiscordHandler := voicemaildiscorddelivery.NewHandler(voicemailsService, assets, logger, *config)
xDiscordHandler := xdiscorddelivery.NewHandler(assets, logger)
characterDiscordDelivery := characterdiscorddelivery.NewHandler(logger)
lineDiscordHandler := linediscorddelivery.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,
lineDiscordHandler.ServeInteraction,
voicemailDiscordHandler.ServeInteraction,
characterDiscordDelivery.ServeInteraction,
lineDiscordHandler.ServeInteraction,
searchDiscordDelivery.ServeInteraction,
voicemailDiscordHandler.ServeInteraction,
xDiscordHandler.ServeInteraction,
} {
session.AddHandler(h)
}