Skip to content

Commit

Permalink
refactor(FAQ): single source of faq message reply
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Jan 6, 2025
1 parent 553e9d8 commit 21cecd2
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 138 deletions.
3 changes: 3 additions & 0 deletions data/lang/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ discord.command:
Die Frage, nach der du gesucht hast, konnte nicht gefunden werden.
> %s
Versuche es erneut oder schau dir alle Fragen an indem du keine Frage in der suche angibst.
msg.error: |-
Es gab einen Fehler beim Laden aller Fragen :(
Bitte rufen sie ihren lokalen Administrator, falls der Fehler weiterhin auftreten sollte.
info:
base: info
Expand Down
3 changes: 3 additions & 0 deletions data/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ discord.command:
The question you searched for was not found.
> %s
Check again or list all questions by not searching anything.
msg.error: |-
There was an error while loading all questions :(
Please contact your local administrator if this problem persists.
info:
base: info
Expand Down
5 changes: 4 additions & 1 deletion modules/faq/chatCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ func (cmd Chat) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) {

if i.Type == discordgo.InteractionApplicationCommandAutocomplete {
cmd.handleAutocomplete(question)
} else if question == "" {
cmd.ReplyComplex(cmd.getAllFAQsMessage())
return
} else {
cmd.handleCommand(question)
cmd.ReplyComplex(cmd.getFAQMessage(question))
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/faq/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func (c Component) Handle(s *discordgo.Session, i *discordgo.InteractionCreate)
case "show_question":
if c.RequireOriginalAuthor() {
question := strings.Join(ids[:len(ids)-2], ".")
c.handleShowQuestion(question)
c.ReplyComplexUpdate(c.getFAQMessage(question))
}
return
case "all_questions":
if c.RequireOriginalAuthor() {
c.handleAllQuestions()
c.ReplyComplexUpdate(c.getAllFAQsMessage())
}
return
default:
Expand Down
94 changes: 94 additions & 0 deletions modules/faq/faqBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/bwmarrin/discordgo"
"github.com/cake4everyone/cake4everybot/data/lang"
"github.com/cake4everyone/cake4everybot/database"
"github.com/cake4everyone/cake4everybot/logger"
"github.com/cake4everyone/cake4everybot/util"
Expand Down Expand Up @@ -55,3 +56,96 @@ func (faq faqBase) getAllFAQs() (map[string]string, error) {
lastFAQTime = time.Now()
return lastFAQs[faq.Interaction.GuildID], nil
}

func (faq faqBase) getFAQMessage(question string) (msg *discordgo.InteractionResponseData) {
msg, faqs, final := faq.getBaseMessage()
if final {
return
}
msg.Embeds[0].Title = question

answer, ok := faqs[question]
if !ok {
e := util.SimpleEmbedf(0, lang.GetDefault(tp+"msg.question_not_found"), question)[0]
msg.Embeds[0].Title = e.Title
msg.Embeds[0].Description = e.Description
msg.Flags = discordgo.MessageFlagsEphemeral
return
}
msg.Embeds[0].Description = answer

var components []discordgo.MessageComponent
components = append(components, util.CreateButtonComponent(
"faq.all_questions",
lang.GetDefault(tp+"msg.button.all_questions"),
discordgo.SecondaryButton,
util.GetConfigComponentEmoji("faq.all_questions"),
))
components = append(components, util.CloseButtonComponent())
msg.Components = []discordgo.MessageComponent{discordgo.ActionsRow{Components: components}}

return
}

func (faq faqBase) getAllFAQsMessage() (msg *discordgo.InteractionResponseData) {
msg, faqs, final := faq.getBaseMessage()
if final {
return
}

var components []discordgo.MessageComponent
var i int
for question := range faqs {
i++
util.AddEmbedField(
msg.Embeds[0],
fmt.Sprintf("%d", i),
question,
true,
)
components = append(components, util.CreateButtonComponent(
fmt.Sprintf("faq.show_question.%s", question),
fmt.Sprint(i),
discordgo.PrimaryButton,
nil,
))
}
msg.Components = []discordgo.MessageComponent{discordgo.ActionsRow{Components: components}}

return
}

// getBaseMessage is a helper function to get the base message for the FAQs. It
// returns the an prefilled message with one embed.
//
// It also returns and checks the FAQs. If final is true, then the message is
// ready to send. This may be caused by an error or similar.
func (faq faqBase) getBaseMessage() (msg *discordgo.InteractionResponseData, faqs map[string]string, final bool) {
msg = &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{{
Color: 0xFAB1FD,
Title: "FAQs",
Footer: util.EmbedFooter(faq.Session, tp+"display"),
}},
}

faqs, err := faq.getAllFAQs()
if err != nil {
log.Printf("ERROR: getting all FAQs: %v", err)
msg.Embeds[0].Description = lang.GetDefault(tp + "msg.error")
msg.Flags = discordgo.MessageFlagsEphemeral
final = true
return
}

if len(faqs) == 0 {
e := util.SimpleEmbed(0, lang.GetDefault(tp+"msg.no_questions"))[0]
msg.Embeds[0].Title = e.Title
msg.Embeds[0].Description = e.Description
msg.Flags = discordgo.MessageFlagsEphemeral
final = true
return
}

return
}
34 changes: 0 additions & 34 deletions modules/faq/handleAllQuestions.go

This file was deleted.

52 changes: 0 additions & 52 deletions modules/faq/handleCommand.go

This file was deleted.

44 changes: 0 additions & 44 deletions modules/faq/handleShowQuestion.go

This file was deleted.

19 changes: 14 additions & 5 deletions util/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,27 @@ func SplitToEmbedFields[S []E, E any](s *discordgo.Session, elements S, color in
return embeds
}

// SetEmbedFooter takes a pointer to an embeds and sets the standard footer with the given name.
// SetEmbedFooter takes a pointer to an embeds and sets the standard footer with
// the given name. See [EmbedFooter].
//
// sectionName:
// translation key for the name
func SetEmbedFooter(s *discordgo.Session, sectionName string, e *discordgo.MessageEmbed) {
botName := viper.GetString("discord.name")
name := lang.Get(sectionName, lang.FallbackLang())

if e == nil {
e = &discordgo.MessageEmbed{}
}
e.Footer = &discordgo.MessageEmbedFooter{
e.Footer = EmbedFooter(s, sectionName)
}

// EmbedFooter returns the standard footer for an embed. See [SetEmbedFooter].
//
// sectionName:
// translation key for the name
func EmbedFooter(s *discordgo.Session, sectionName string) *discordgo.MessageEmbedFooter {
botName := viper.GetString("discord.name")
name := lang.Get(sectionName, lang.FallbackLang())

return &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf("%s > %s", botName, name),
IconURL: s.State.User.AvatarURL(""),
}
Expand Down

0 comments on commit 21cecd2

Please sign in to comment.