Skip to content
This repository was archived by the owner on May 15, 2021. It is now read-only.

Commit e6a4385

Browse files
author
Seraphim R.P
committed
Implement embeds further and add translation helpers.
1 parent e64813a commit e6a4385

File tree

5 files changed

+48
-33
lines changed

5 files changed

+48
-33
lines changed

models/language.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,39 @@ type args struct {
209209
False string `json:"false"`
210210
}
211211

212-
func (lng Language) GetString(str string) string {
212+
func (lng Language) GetString(ctx *Context, str string, params []string) string {
213+
// TODO: Infer params from raw string, as they are always surrounded by <>. Making it a parameter is mildly inconvenient.
214+
rawStr := lng.GetRawString(str)
215+
216+
if params == nil {
217+
return rawStr
218+
}
219+
220+
for _, replaceQuery := range params {
221+
origQuery := replaceQuery
222+
223+
switch replaceQuery {
224+
case "<+>":
225+
replaceQuery = ctx.GuildPrefs.Prefix
226+
default:
227+
purifiedQuery := strings.Title(replaceQuery[1 : len(replaceQuery)-1])
228+
possibleCommand := lng.GetCommandTranslation(purifiedQuery)
229+
possibleArgument := lng.GetArgumentTranslation(purifiedQuery)
230+
231+
if possibleCommand != purifiedQuery {
232+
replaceQuery = possibleCommand
233+
} else if possibleArgument != purifiedQuery {
234+
replaceQuery = possibleArgument
235+
}
236+
}
237+
238+
rawStr = strings.ReplaceAll(rawStr, origQuery, replaceQuery)
239+
}
240+
241+
return rawStr
242+
}
243+
244+
func (lng Language) GetRawString(str string) string {
213245
v := reflect.ValueOf(lng.RawObject)
214246

215247
for i := 0; i < v.NumField(); i++ {

models/response.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ package models
22

33
// CommandResponse is a model for command output.
44
type CommandResponse struct {
5-
OK bool `json:"ok"`
6-
Content string `json:"content"`
7-
Thumbnail string `json:"thumbnail"`
8-
Pages []page `json:"pages"`
9-
}
10-
11-
type page struct {
12-
Title string `json:"title"`
13-
Content string `json:"content"`
5+
OK bool `json:"ok"`
6+
Content *DiscordEmbed `json:"content"`
7+
Pages []*DiscordEmbed `json:"pages"`
148
}
159

1610
// VerseResponse is a model for command output.

routes/commands/commands.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"internal.kerygma.digital/kerygma-digital/biblebot/backend/models"
1515
"internal.kerygma.digital/kerygma-digital/biblebot/backend/routes/commands/settings"
1616
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/converters"
17+
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/embedify"
1718
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/slices"
1819
)
1920

@@ -43,7 +44,7 @@ func commandHandler(c *fiber.Ctx) error {
4344

4445
return c.JSON(&models.CommandResponse{
4546
OK: false,
46-
Content: err.Error(),
47+
Content: embedify.Embedify("Backend", "401 Error - Unauthorized Access", err.Error(), true, ""),
4748
})
4849
}
4950

routes/commands/settings/versions.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package settings
33
import (
44
"fmt"
55
"sort"
6-
"strings"
76
"sync"
87

98
"internal.kerygma.digital/kerygma-digital/biblebot/backend/models"
9+
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/embedify"
1010
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/slices"
1111
)
1212

@@ -78,7 +78,7 @@ var verDefault = models.Command{
7878

7979
return &models.CommandResponse{
8080
OK: true,
81-
Content: content,
81+
Content: embedify.Embedify("", "+version", content, false, ""),
8282
}
8383
},
8484
}
@@ -106,16 +106,10 @@ var verSet = models.Command{
106106
}
107107

108108
response.OK = true
109-
response.Content = ctx.Language.GetString("SetVersionSuccess")
109+
response.Content = embedify.Embedify("", "+version set", ctx.Language.GetString(ctx, "SetVersionSuccess", nil), false, "")
110110
} else {
111111
response.OK = false
112-
113-
content := ctx.Language.GetString("SetVersionFail")
114-
content = strings.Replace(content, "<+>", ctx.GuildPrefs.Prefix, 1)
115-
content = strings.Replace(content, "<version>", ctx.Language.GetCommandTranslation("Version"), 1)
116-
content = strings.Replace(content, "<list>", ctx.Language.GetCommandTranslation("List"), 1)
117-
118-
response.Content = content
112+
response.Content = embedify.Embedify("", "+version set", ctx.Language.GetString(ctx, "SetVersionFail", []string{"<+>", "<version>", "<list>"}), false, "")
119113
}
120114

121115
return &response
@@ -145,16 +139,10 @@ var verSetServer = models.Command{
145139
}
146140

147141
response.OK = true
148-
response.Content = ctx.Language.GetString("SetGuildVersionSuccess")
142+
response.Content = embedify.Embedify("", "+version setserver", ctx.Language.GetString(ctx, "SetGuildVersionSuccess", nil), false, "")
149143
} else {
150144
response.OK = false
151-
152-
content := ctx.Language.GetString("SetGuildVersionFail")
153-
content = strings.Replace(content, "<+>", ctx.GuildPrefs.Prefix, 1)
154-
content = strings.Replace(content, "<version>", ctx.Language.GetCommandTranslation("Version"), 1)
155-
content = strings.Replace(content, "<list>", ctx.Language.GetCommandTranslation("List"), 1)
156-
157-
response.Content = content
145+
response.Content = embedify.Embedify("", "+version setserver", ctx.Language.GetString(ctx, "SetGuildVersionFail", []string{"<+>", "<version>", "<list>"}), false, "")
158146
}
159147

160148
return &response

utils/embedify/embedify.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import (
44
"fmt"
55

66
"internal.kerygma.digital/kerygma-digital/biblebot/backend/models"
7-
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/logger"
87
)
98

109
const (
1110
NORMAL_COLOR = 6709986
1211
ERROR_COLOR = 16723502
1312
)
1413

15-
func Embedify(author string, title string, description string, isError bool, copyright string) (*models.DiscordEmbed, error) {
14+
func Embedify(author string, title string, description string, isError bool, copyright string) *models.DiscordEmbed {
1615
var embed models.DiscordEmbed
1716

1817
embed.Colour = NORMAL_COLOR
@@ -29,19 +28,20 @@ func Embedify(author string, title string, description string, isError bool, cop
2928

3029
if author != "" {
3130
embed.Author.Name = author
31+
embed.Author.IconURL = embed.Footer.IconURL
3232
}
3333

3434
if description != "" {
3535
if len(description) < 2049 {
3636
embed.Description = description
3737
} else {
38-
return nil, logger.LogWithError("embedify", "description is too long", nil)
38+
return nil
3939
}
4040
}
4141

4242
if copyright != "" {
4343
embed.Footer.Text = fmt.Sprintf("%s // %s", copyright, embed.Footer.Text)
4444
}
4545

46-
return &embed, nil
46+
return &embed
4747
}

0 commit comments

Comments
 (0)