89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
|
|
auth "streambot_twitch/internal/auth"
|
|
"streambot_twitch/internal/chat"
|
|
"streambot_twitch/internal/commands"
|
|
"streambot_twitch/internal/points"
|
|
twitchapi "streambot_twitch/internal/twitchapi"
|
|
tests "streambot_twitch/internal/tests"
|
|
twitch "github.com/gempir/go-twitch-irc/v4"
|
|
)
|
|
|
|
func main() {
|
|
go func() {
|
|
if err := auth.StartOAuthServer(":8080"); err != nil {
|
|
log.Fatalf("OAuth server error: %v", err)
|
|
}
|
|
}()
|
|
|
|
_ = godotenv.Load()
|
|
|
|
log.Println("OAuth server started on port 8080")
|
|
|
|
botUsername := os.Getenv("BOT_USERNAME")
|
|
botAccess := os.Getenv("BOT_ACCESS_TOKEN")
|
|
botRefresh := os.Getenv("BOT_REFRESH_TOKEN")
|
|
channel := os Getenv("CHANNEL")
|
|
clientID := os.Getenv("TWITCH_CLIENT_ID")
|
|
clientSecret := os.Getenv("TWITCH_CLIENT_SECRET")
|
|
channelAccess := os.Getenv("STREAMER_ACCESS_TOKEN")
|
|
channelRefresh := os.Getenv("STREAMER_REFRESH_TOKEN")
|
|
botChannel := botUsername
|
|
|
|
if botUsername == "" || botAccess == "" || channel == "" || clientID == "" || clientSecret == "" {
|
|
log.Fatal("Please set Bot_Username, Bot_OAuth, Channel, Twitch_Client_ID, and Twitch_Client_Secret env vars")
|
|
}
|
|
|
|
// init Twitch API client
|
|
apiClient, err := twitchapi.NewClient(clientID, clientSecret)
|
|
if err != nil {
|
|
log.Fatalf("Failed to create Twitch API client: %v", err)
|
|
}
|
|
|
|
// run Tests
|
|
if err != nil {
|
|
log.Fatalf("Startup Test failed: %v", err)
|
|
}
|
|
|
|
botClient := twitch.NewClient(botUsername, botOAuth)
|
|
|
|
if err := tests.TestIRCConnection(botClient, []string{botChannel, streamerChannel}); err != nil {
|
|
log.Fatalf("Startup test failed: %v", err)
|
|
}
|
|
|
|
log.Println("All startup tests passed. Starting bot. . .")
|
|
// END of TESTS
|
|
|
|
// load chat points
|
|
if err := points.LoadPoints(); err != nil {
|
|
log.Fatalf("Failed to load points: %v", err)
|
|
}
|
|
|
|
// make sure points are saved on exit
|
|
defer points.SavePoints()
|
|
|
|
// load custom commands
|
|
if err := commands.LoadCommands(); err != nil {
|
|
log.Fatalf("Failed to Load Custom Commands: %v", err)
|
|
}
|
|
|
|
// Create Twitch IRC client
|
|
client := twitch.NewClient(botUsername, botOAuth)
|
|
client.Join(channel)
|
|
|
|
// Setup Chat handler
|
|
client.OnPrivateMessage(func(message twitch.PrivateMessage) {
|
|
chat.HandleMessage(client, apiClient, message, botUserName)
|
|
})
|
|
|
|
if err := client.Connect(); err != nil {
|
|
log.Fatalf("Error connecting to Twitch Chat: %v", err)
|
|
}
|
|
|
|
|
|
}
|