|  | 
| 1 | 1 | package information | 
| 2 | 2 | 
 | 
| 3 | 3 | import ( | 
|  | 4 | +	"fmt" | 
|  | 5 | +	"strings" | 
| 4 | 6 | 	"sync" | 
| 5 | 7 | 
 | 
| 6 | 8 | 	"internal.kerygma.digital/kerygma-digital/biblebot/backend/models" | 
|  | 9 | +	"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/embedify" | 
| 7 | 10 | 	"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/slices" | 
| 8 | 11 | ) | 
| 9 | 12 | 
 | 
| 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 | 
| 13 | 17 | } | 
| 14 | 18 | 
 | 
| 15 | 19 | 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 | 
| 26 | 23 | ) | 
| 27 | 24 | 
 | 
| 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{}, | 
| 33 | 31 | 		} | 
| 34 | 32 | 	}) | 
| 35 | 33 | 
 | 
| 36 |  | -	return helpInstance | 
|  | 34 | +	return infoInstance | 
| 37 | 35 | } | 
| 38 | 36 | 
 | 
| 39 | 37 | // 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] | 
| 49 | 53 | 	} | 
| 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 | +	}, | 
| 51 | 116 | } | 
0 commit comments