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

Commit d763677

Browse files
author
Seraphim R.P
committed
Implement +biblebot and begin Bolls.life integration.
1 parent 652047d commit d763677

File tree

12 files changed

+361
-46
lines changed

12 files changed

+361
-46
lines changed

data/versions.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ func ImportVersions(db *gorm.DB) {
3030
for _, val := range oldVersionEntries {
3131
source := "bg"
3232

33-
if slices.StringInSlice(val.Abbreviation, []string{"BSB", "NHEB", "WBT"}) {
33+
if slices.StringInSlice(val.Abbreviation, []string{"BSB", "NHEB", "WBT", "ELXX", "LXX"}) {
3434
continue
3535
} else if slices.StringInSlice(val.Abbreviation, []string{"KJVA", "FBV"}) {
3636
source = "ab"
37-
} else if slices.StringInSlice(val.Abbreviation, []string{"ELXX", "LXX"}) {
38-
continue
3937
}
4038

4139
newVersion := &models.Version{

models/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Config struct {
1616
DecryptionKey string `yaml:"decryptionKey"`
1717
EncryptedFiles []string `yaml:"encryptedFiles"`
1818
IsTest bool `yaml:"isTest"`
19+
Version string `yaml:"version"`
1920
DB gorm.DB
2021
Languages map[string]Language
2122
}

models/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ type Context struct {
1616
UserID string `json:"userID"`
1717
ChannelID string `json:"channelID"`
1818
GuildID string `json:"guildID"`
19+
Version string `json:"version"`
1920
}

models/dcembed.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package models
22

33
// DiscordEmbed is a struct representing a typical Discord RichEmbed. This is so we can output a JSONified embed that the frontend can interpret into the actual object.
44
type DiscordEmbed struct {
5-
Title string `json:"title"`
6-
Description string `json:"description"`
7-
URL string `json:"url"`
8-
Colour int `json:"colour"`
9-
Footer footer `json:"footer"`
10-
Image media `json:"image"`
11-
Thumbnail media `json:"thumbnail"`
12-
Video media `json:"video"`
13-
Author author `json:"author"`
14-
Fields []field `json:"fields"`
5+
Title string `json:"title"`
6+
Description string `json:"description"`
7+
URL string `json:"url"`
8+
Colour int `json:"colour"`
9+
Footer footer `json:"footer"`
10+
Image media `json:"image"`
11+
Thumbnail media `json:"thumbnail"`
12+
Video media `json:"video"`
13+
Author author `json:"author"`
14+
Fields []EmbedField `json:"fields"`
1515
}
1616

1717
type footer struct {
@@ -29,7 +29,8 @@ type author struct {
2929
IconURL string `json:"icon_url"`
3030
}
3131

32-
type field struct {
32+
// EmbedField is a struct representing Discord embed fields.
33+
type EmbedField struct {
3334
Name string `json:"name"`
3435
Value string `json:"value"`
3536
Inline bool `json:"inline"`

models/language.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ func (lng Language) TranslatePlaceholdersInString(ctx *Context, str string) stri
232232
switch placeholder {
233233
case "<+>":
234234
placeholder = ctx.GuildPrefs.Prefix
235+
case "<truerandom>":
236+
placeholder = lng.GetCommandTranslation("TrueRandom")
237+
case "<dailyverse>":
238+
placeholder = lng.GetCommandTranslation("DailyVerse")
235239
default:
236240
purifiedQuery := strings.Title(placeholder[1 : len(placeholder)-1])
237241
possibleCommand := lng.GetCommandTranslation(purifiedQuery)

routes/commands/commands.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/gofiber/fiber/v2"
1414
"internal.kerygma.digital/kerygma-digital/biblebot/backend/models"
15+
"internal.kerygma.digital/kerygma-digital/biblebot/backend/routes/commands/information"
1516
"internal.kerygma.digital/kerygma-digital/biblebot/backend/routes/commands/settings"
1617
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/converters"
1718
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/embedify"
@@ -27,6 +28,7 @@ var (
2728
config *models.Config
2829

2930
vcr = settings.NewVersionCommandRouter()
31+
icr = information.NewInformationCommandRouter()
3032
)
3133

3234
// RegisterRouter registers routers related to command processing.
@@ -48,7 +50,7 @@ func commandHandler(c *fiber.Ctx) error {
4850
})
4951
}
5052

51-
res := vcr.Process(strings.Split(ctx.Body, " ")[1:], ctx)
53+
res := icr.Process(strings.Split(ctx.Body, " "), ctx)
5254

5355
return c.JSON(res)
5456
}
Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,116 @@
11
package information
22

33
import (
4+
"fmt"
5+
"strings"
46
"sync"
57

68
"internal.kerygma.digital/kerygma-digital/biblebot/backend/models"
9+
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/embedify"
710
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/slices"
811
)
912

10-
// HelpCommandRouter is a basic struct with functions to handle help-related commands.
11-
type HelpCommandRouter struct {
12-
Commands []models.Command
13+
// InformationCommandRouter is a basic struct with functions to handle help-related commands.
14+
type InformationCommandRouter struct {
15+
DefaultCommand models.Command
16+
Commands []models.Command
1317
}
1418

1519
var (
16-
// helpInstance is the singleton router used to process its respective commands
17-
helpInstance *HelpCommandRouter
18-
helpOnce sync.Once
19-
20-
helpCommand = models.Command{
21-
Command: "biblebot",
22-
Process: func(params []string, ctx *models.Context) *models.CommandResponse {
23-
return nil
24-
},
25-
}
20+
// infoInstance is the singleton router used to process its respective commands
21+
infoInstance *InformationCommandRouter
22+
infoOnce sync.Once
2623
)
2724

28-
// NewHelpCommandRouter creates a HelpCommandRouter if one does not already exist
29-
func NewHelpCommandRouter() *HelpCommandRouter {
30-
helpOnce.Do(func() {
31-
helpInstance = &HelpCommandRouter{
32-
Commands: []models.Command{helpCommand},
25+
// NewInformationCommandRouter creates a HelpCommandRouter if one does not already exist
26+
func NewInformationCommandRouter() *InformationCommandRouter {
27+
infoOnce.Do(func() {
28+
infoInstance = &InformationCommandRouter{
29+
DefaultCommand: infoBibleBot,
30+
Commands: []models.Command{},
3331
}
3432
})
3533

36-
return helpInstance
34+
return infoInstance
3735
}
3836

3937
// Process checks which command process to run given the inputed command & parameters
40-
func (cr *HelpCommandRouter) Process(params []string, ctx *models.Context) *models.CommandResponse {
41-
cm, ok := slices.FilterInterface(cr.Commands, func(cm interface{}) bool {
42-
cmd, ok := cm.(models.Command)
43-
return (params[0] == cmd.Command) && ok
44-
}).(models.Command)
45-
46-
if ok {
47-
// Strip first element of slice (is the command itself)
48-
return cm.Process(params[1:], ctx)
38+
func (cr *InformationCommandRouter) Process(params []string, ctx *models.Context) *models.CommandResponse {
39+
cmMatches := slices.FilterInterface(cr.Commands, func(cm interface{}) bool {
40+
if len(params) > 0 {
41+
cmd, ok := cm.(models.Command)
42+
return (params[0] == cmd.Command) && ok
43+
}
44+
45+
return false
46+
}).([]models.Command)
47+
48+
var cm models.Command
49+
if len(cmMatches) == 0 {
50+
cm = cr.DefaultCommand
51+
} else {
52+
cm = cmMatches[0]
4953
}
50-
return nil
54+
55+
if len(params) == 0 {
56+
return cm.Process([]string{}, ctx)
57+
}
58+
59+
return cm.Process(params[1:], ctx)
60+
}
61+
62+
var infoBibleBot = models.Command{
63+
Command: "biblebot",
64+
Process: func(params []string, ctx *models.Context) *models.CommandResponse {
65+
if len(params) > 0 {
66+
// TODO: implement rescue option
67+
return &models.CommandResponse{}
68+
} else {
69+
lng := ctx.Language
70+
title := strings.Replace(lng.GetRawString("BibleBot"), "<version>", ctx.Version, 1)
71+
desc := lng.GetString(ctx, "Credit")
72+
73+
commandListName := lng.GetString(ctx, "CommandListName")
74+
commandList := lng.GetString(ctx, "CommandList")
75+
76+
linksName := lng.GetString(ctx, "Links")
77+
links := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n\n**%s**", lng.GetRawString("Website"), lng.GetRawString("Copyrights"),
78+
lng.GetRawString("Code"), lng.GetRawString("Server"),
79+
lng.GetRawString("Terms"), lng.GetRawString("Usage"))
80+
81+
replacementMap := map[string]string{
82+
"<website>": "https://biblebot.xyz",
83+
"<copyrights>": "https://biblebot.xyz/copyrights",
84+
"<repository>": "https://internal.kerygma.digital",
85+
"<invite>": "https://discord.gg/H7ZyHqE",
86+
"<terms>": "https://biblebot.xyz/terms",
87+
}
88+
89+
for k, v := range replacementMap {
90+
links = strings.ReplaceAll(links, k, v)
91+
}
92+
93+
embed := embedify.Embedify("", title, desc, false, "")
94+
embed.Fields = append(embed.Fields, models.EmbedField{
95+
Name: commandListName,
96+
Value: commandList,
97+
Inline: false,
98+
})
99+
embed.Fields = append(embed.Fields, models.EmbedField{
100+
Name: "\u200B",
101+
Value: "—————————————",
102+
Inline: false,
103+
})
104+
embed.Fields = append(embed.Fields, models.EmbedField{
105+
Name: linksName,
106+
Value: links,
107+
Inline: false,
108+
})
109+
110+
return &models.CommandResponse{
111+
OK: true,
112+
Content: embed,
113+
}
114+
}
115+
},
51116
}

routes/verses/providers/apibible.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ func NewAPIBibleProvider(key string) *APIBibleProvider {
4040

4141
// GetVerse fetches a Verse based upon a Reference, for API.Bible versions.
4242
func (abp *APIBibleProvider) GetVerse(ref *models.Reference, titles bool, verseNumbers bool) (*models.Verse, error) {
43-
URL, err := url.Parse(fmt.Sprintf("https://api.scripture.api.bible/v1/bibles/%s/search", versionTable[ref.Version.Abbreviation]))
43+
URL, _ := url.Parse(fmt.Sprintf("https://api.scripture.api.bible/v1/bibles/%s/search", versionTable[ref.Version.Abbreviation]))
4444

4545
query := URL.Query()
4646
query.Set("query", ref.ToString())
4747
query.Set("limit", "1")
4848
URL.RawQuery = query.Encode()
4949

5050
client := &http.Client{}
51-
req, err := http.NewRequest("GET", URL.String(), nil)
51+
req, _ := http.NewRequest("GET", URL.String(), nil)
5252
req.Header.Add("api-key", abp.Key)
5353

5454
resp, err := client.Do(req)

0 commit comments

Comments
 (0)