karaokards/main.go

88 lines
2.7 KiB
Go
Executable File

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"time"
builtins "git.martyn.berlin/martyn/karaokards/internal/builtins"
irc "git.martyn.berlin/martyn/karaokards/internal/irc"
rgb "github.com/foresthoffman/rgblog"
)
type customStringsStruct struct {
Strings []string `json:"strings,omitempty"`
}
var selectablePrompts []string
var customStrings customStringsStruct
func readBonusStrings() []string {
ex, err := os.Executable()
if err != nil {
return []string{}
fmt.Println("Could not read `strings.json`, will only have builtin prompts. File reading error", err)
}
exPath := filepath.Dir(ex)
data, err := ioutil.ReadFile(exPath + "/strings.json")
if err != nil {
fmt.Println("Could not read `strings.json`, will only have builtin prompts. File reading error", err)
return []string{}
}
err = json.Unmarshal(data, &customStrings)
if err != nil {
fmt.Println("Could not unmarshal `strings.json`, will only have builtin prompts. Unmarshal error", err)
return []string{}
}
fmt.Println("Read ", len(customStrings.Strings), " prompts from `strings.json`")
return customStrings.Strings
}
func main() {
rand.Seed(time.Now().UnixNano())
for _, val := range builtins.Karaokards {
selectablePrompts = append(selectablePrompts, val)
}
for _, val := range readBonusStrings() {
selectablePrompts = append(selectablePrompts, val)
}
fmt.Println(len(selectablePrompts), " prompts available.")
oauthPath := ""
if os.Getenv("TWITCH_OAUTH_JSON") != "" {
if _, err := os.Stat(os.Getenv("TWITCH_OAUTH_JSON")); os.IsNotExist(err) {
os.Exit(1)
}
oauthPath = os.Getenv("TWITCH_OAUTH_JSON")
} else {
if _, err := os.Stat(os.Getenv("HOME") + "/.twitch/oauth.json"); os.IsNotExist(err) {
rgb.YPrintf("[%s] Warning %s doesn't exist, trying %s next!\n", irc.TimeStamp(), os.Getenv("HOME")+"/.twitch/oauth.json", "/etc/twitch/oauth.json")
if _, err := os.Stat("/etc/twitch/oauth.json"); os.IsNotExist(err) {
rgb.YPrintf("[%s] Error %s doesn't exist either, bailing!\n", irc.TimeStamp(), "/etc/twitch/oauth.json")
os.Exit(1)
}
oauthPath = "/etc/twitch/oauth.json"
} else {
oauthPath = os.Getenv("HOME") + "/.twitch/oauth.json"
}
}
// Replace the channel name, bot name, and the path to the private directory with your respective
// values.
myBot := irc.KardBot{
Channel: "imartynontwitch",
MsgRate: time.Duration(20/30) * time.Millisecond,
Name: "Karaokards",
Port: "6667",
PrivatePath: oauthPath,
Server: "irc.chat.twitch.tv",
Prompts: selectablePrompts,
}
myBot.Start()
}