Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
NAME=bo
CONFIG_PATH=./config
LOKI_URL="http://localhost:3100/loki/api/v1/push"
OTEL_EXPORTER_OTLP_INSECURE="true"
OTEL_RESOURCE_ATTRIBUTES_BOBY="service.name=bo,service.namespace=bo,deployment.environment=prod"
OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317"
OTEL_EXPORTER_OTLP_PROTOCOL=grpc
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ src/main/config/**
.devcontainer/devcontainer.json
.vscode/launch.json
src/main/logging.log
.env
19 changes: 19 additions & 0 deletions src/command/admin.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package command

import (
"encoding/json"
"io"
)

// Config for admin commands.
type AdminConfig struct {
Enabled bool // Whether admin commands are enabled.
}

// ImAdminTrigger is a trigger to use for an ImAdmin command.
const ImAdminTrigger = "imadmin"

Expand All @@ -15,6 +25,15 @@ const IsAdminTrigger = "isadmin"
// Repo is a URL to this project's repository. Useful for showing with help information.
const Repo = "https://github.com/BKrajancic/boby"

func GetAdminConfigs(reader io.Reader) (AdminConfig, error) {
bytes, err := io.ReadAll(reader)
if err != nil {
return AdminConfig{}, err
}
var config AdminConfig
return config, json.Unmarshal(bytes, &config)
}

// AdminCommands returns an array of commands for handling admins.
func AdminCommands() []Command {
return []Command{
Expand Down
16 changes: 15 additions & 1 deletion src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/BKrajancic/boby/m/v2/src/utils"
)

const adminConfigFilepath = "admin_config.json"
const jsonFilepath = "json_getter_config.json"
const regexpFilepath = "regexp_scraper_config.json"
const goqueryFilepath = "goquery_scraper_config.json"
Expand Down Expand Up @@ -159,7 +160,7 @@ func MakeExampleDir(dir string) error {
// ConfiguredBot uses files in configDir to return a bot ready for usage.
// This bot is not attached to any storage or services.
func ConfiguredBot(configDir string, storage *storage.Storage) ([]command.Command, error) {
commands := command.AdminCommands()
commands := []command.Command{}

file, err := os.Open(path.Join(configDir, jsonFilepath))
if err != nil {
Expand Down Expand Up @@ -243,6 +244,19 @@ func ConfiguredBot(configDir string, storage *storage.Storage) ([]command.Comman
commands = append(commands, oxfordCommandInfo)
}

file, err = os.Open(path.Join(configDir, adminConfigFilepath))
if err != nil {
return commands, err
}

adminConfig, err := command.GetAdminConfigs(bufio.NewReader(file))
if err != nil {
return commands, err
}
if adminConfig.Enabled {
commands = append(commands, command.AdminCommands()...)
}

// TODO: Helptext is hardcoded for discord, and is therefore a leaky abstraction.
renderCmd := command.Command{
Trigger: "render",
Expand Down
24 changes: 17 additions & 7 deletions src/service/discordservice/discord_subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,23 @@ func (d *DiscordSubject) helpExec(conversation service.Conversation, user servic
Value: command.Repo,
})

return sink(
conversation,
service.Message{
Title: "Help",
Fields: fields,
},
)
const batchSize = 25
for i := 0; i < len(fields); i += batchSize {
batch := fields[i:min(i+batchSize, len(fields))]
err := sink(
conversation,
service.Message{
Title: "Help",
Fields: batch,
},
)

if err != nil {
return err
}
}

return nil
}

func (d *DiscordSubject) handleMessageError(m *discordgo.Message, event string, err error) {
Expand Down
Loading