Started REPL env for custom interactive shell
ONLY active when bot is running - Arguments [--watch] displays how many usses of each trigger and how many uses over the lifetime ofthe bot Commands - title [get/set] <newTitle> - category [get/set] <newCategory> - MUST be a legit Twitch.tv one - yet to come [cmd add|del|edit] [trigger] [response] (NOTE if you want Emotes you will have to type the 'name' in for twitch chat to recognise it)
This commit is contained in:
96
cmd/cli/main.go
Normal file
96
cmd/cli/main.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
twitchapi "streambot_twitch/internal/twitchapi"
|
||||
"streambot_twitch/internal/commands"
|
||||
"streambot_twitch/internal/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// env setup
|
||||
clientID := os.Getenv("TWITCH_CLIENT_ID")
|
||||
clientSecret := os.Getenv("TWITCH_CLIENT_SECRET")
|
||||
streamerChan := os.Getenv("CHANNEL")
|
||||
userToken := os.Getenv("STREAMER_OAUTH")
|
||||
|
||||
if clientID == "" || clientSecret == "" || streamerChan == "" {
|
||||
log.Fatal("Set TWITCH_CLIENT_ID, TWITCH_CLIENT_SECRET or cHANNEL")
|
||||
}
|
||||
|
||||
// init config, commands, usage
|
||||
if err := config.Load(); err != nil {log.Fatal(err)}
|
||||
if err := commands.LoadDefaultCommands(); err != nil {log.Fatal(err)}
|
||||
if err := commands.LoadCustomCommands(); err != nil {lof.Fatal(err)}
|
||||
if err := commands.LoadUsage(); err != nil {log.Fatal(err)}
|
||||
|
||||
// init api client
|
||||
apiClient, err := twitchapi.NewClient(clientID, clientSecret)
|
||||
if err != nil {log.Fatalf("API client: %v", err)}
|
||||
if userToken != "" {
|
||||
apiClient.SetUserAccessToken(userToken)
|
||||
}
|
||||
|
||||
// CLI flags
|
||||
watch := flag.Bool("watch", false, "refresh stats periodically")
|
||||
interval := flag.Duration("interval", 5*time.Minute, "refresh interval for watch mode")
|
||||
flag.Parse()
|
||||
args := flag.Args()
|
||||
|
||||
if len(args) == 0 || strings.ToLower(args[0]) == "shell" {
|
||||
repl(apiClient, streamerChan, *watch, *interval)
|
||||
return
|
||||
}
|
||||
|
||||
dispatch(apiClient, streamerChan, args, *watch, *interval)
|
||||
}
|
||||
|
||||
// repl interactive prompt
|
||||
func repl(apiClient *twitchapi.Client, streamerChan string, watch bool, interval time.Duration) {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
fmt.Print("bot:> ")
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
continue
|
||||
}
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
if line == "exit" || line == "quit" {
|
||||
fmt.Println("bye")
|
||||
return
|
||||
}
|
||||
parts := strings.Fields(line)
|
||||
dispatch(apiClient, streamerChan, parts, watch, interval)
|
||||
}
|
||||
}
|
||||
|
||||
// dispatch handles one command invocation
|
||||
func dispatch(apiClient *twitchapi.Client, streamerChan string, args []string, watch bool, interval time.Duration) {
|
||||
cmd := strings.ToLower(args[0])
|
||||
switch cmd {
|
||||
case "stats":
|
||||
if watch {
|
||||
printUsage()
|
||||
t := time.NewTicker(interval)
|
||||
for range t.C {
|
||||
printUsage()
|
||||
}
|
||||
} else {
|
||||
printUsage()
|
||||
}
|
||||
|
||||
case "title":
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user