The scripts can now lie in any level of the /scripts folder
MODIFY
internal/config/config.go
internal/config/config.go
@@ -87,6 +87,7 @@ type Config struct {
}
var cachedConfigDir string
+var cachedScriptDir string
//GetConfigFile returns the absolute path to the configuration file or an error
//in case of failure.
@@ -100,6 +101,17 @@ func GetConfigFile() (string, error) {
return filepath.Join(configDir, "config.json"), nil
}
+//GetScriptDirectory returns the path at which all the external scripts should
+//lie.
+func GetScriptDirectory() string {
+ if cachedScriptDir == "" {
+ //We'll just make the assumption, that the config dir has already been
+ //initialized at that point and time in the application.
+ cachedScriptDir = filepath.Join(cachedConfigDir, "scripts")
+ }
+ return cachedScriptDir
+}
+
//GetConfigDirectory is the parent directory in the os, that contains the
//settings for the application.
func GetConfigDirectory() (string, error) {

MODIFY
internal/scripting/javascript.go
internal/scripting/javascript.go
@@ -2,7 +2,6 @@ package scripting
import (
"fmt"
- "io/ioutil"
"os"
"path/filepath"
"strings"
@@ -19,8 +18,8 @@ type JavaScriptEngine struct {
}
// New instantiates a new scripting engine
-func New() (e *JavaScriptEngine) {
- e = &JavaScriptEngine{
+func New() (engine *JavaScriptEngine) {
+ engine = &JavaScriptEngine{
vm: otto.New(),
}
@@ -28,40 +27,37 @@ func New() (e *JavaScriptEngine) {
}
// LoadScripts implements Engine
-func (e *JavaScriptEngine) LoadScripts(dirname string) (err error) {
- files, err := ioutil.ReadDir(dirname)
- if err != nil {
- return
- }
-
- for _, file := range files {
- if file.IsDir() {
- continue
+func (engine *JavaScriptEngine) LoadScripts(dirname string) (err error) {
+ err = filepath.Walk(dirname, func(path string, fileInfo os.FileInfo, err error) error {
+ if fileInfo.IsDir() {
+ return nil
}
- if !strings.HasSuffix(file.Name(), ".js") {
- continue
+ if !strings.HasSuffix(fileInfo.Name(), ".js") {
+ return nil
}
- path := filepath.Join(dirname, file.Name())
- f, err := os.Open(path)
+ file, err := os.Open(path)
if err != nil {
return errors.Wrap(err, path)
}
- _, err = e.vm.Run(f)
+ _, err = engine.vm.Run(file)
if err != nil {
return errors.Wrap(err, "failed to run script")
}
- }
- return
+ return nil
+ })
+
+ return err
}
// OnMessage implements Engine
-func (e *JavaScriptEngine) OnMessage(oldText string) (newText string) {
- v, err := e.vm.Run(fmt.Sprintf(`onMessage("%s")`, oldText))
- if err != nil {
+func (engine *JavaScriptEngine) OnMessage(oldText string) (newText string) {
+ jsValue, jsError := engine.vm.Run(fmt.Sprintf(`onMessage("%s")`, oldText))
+ if jsError != nil {
+ //TODO Return error?
return oldText
}
- return v.String()
+ return jsValue.String()
}

MODIFY
internal/ui/window.go
internal/ui/window.go
@@ -3,7 +3,6 @@ package ui
import (
"fmt"
"io"
- "path/filepath"
"regexp"
"sort"
"strings"
@@ -87,11 +86,7 @@ func NewWindow(discord *discordgo.Session) (*Window, error) {
scripting: scripting.New(),
}
- configDir, err := config.GetConfigDirectory()
- if err != nil {
- return nil, err
- }
- if err := window.scripting.LoadScripts(filepath.Join(configDir, "scripts", "js")); err != nil {
+ if err := window.scripting.LoadScripts(config.GetScriptDirectory()); err != nil {
return nil, err
}
@@ -135,6 +130,7 @@ func NewWindow(discord *discordgo.Session) (*Window, error) {
if selectedGuildNode != nil {
selectedGuildNode.SetColor(tcell.ColorWhite)
}
+
selectedGuildNode = guildNode
selectedGuildNode.SetColor(tcell.ColorTeal)
@@ -429,7 +425,14 @@ func NewWindow(discord *discordgo.Session) (*Window, error) {
go window.editMessage(window.selectedChannel.ID, *window.editingMessageID, messageToSend)
window.exitMessageEditMode()
} else {
- go discord.ChannelMessageSend(window.selectedChannel.ID, window.scripting.OnMessage(messageToSend))
+ go func() {
+ _, sendError := discord.ChannelMessageSend(window.selectedChannel.ID, window.scripting.OnMessage(messageToSend))
+ if sendError != nil {
+ window.app.QueueUpdateDraw(func() {
+ window.ShowErrorDialog("Error sending message: " + sendError.Error())
+ })
+ }
+ }()
}
} else {
if window.editingMessageID != nil {

The scripts can now lie in any level of the /scripts folder