You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.2 KiB
97 lines
2.2 KiB
|
5 hours ago
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"log"
|
||
|
|
"os"
|
||
|
|
"strings"
|
||
|
|
"encoding/json"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/gempir/go-twitch-irc/v4"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Secret struct {
|
||
|
|
ClientSecret string
|
||
|
|
RefreshToken string
|
||
|
|
}
|
||
|
|
|
||
|
|
type Periodic struct {
|
||
|
|
Seconds int
|
||
|
|
Message string
|
||
|
|
}
|
||
|
|
|
||
|
|
type Config struct {
|
||
|
|
ClientNick string
|
||
|
|
Channel string
|
||
|
|
Commands map[string]string
|
||
|
|
Periodic []Periodic
|
||
|
|
}
|
||
|
|
|
||
|
|
func LoadJSON[T any](file string) T {
|
||
|
|
data, err := os.ReadFile(file)
|
||
|
|
if err != nil { log.Fatal(err) }
|
||
|
|
|
||
|
|
var config T
|
||
|
|
err = json.Unmarshal(data, &config)
|
||
|
|
if err != nil { log.Fatal(err) }
|
||
|
|
|
||
|
|
return config
|
||
|
|
}
|
||
|
|
|
||
|
|
func RunPeriodic(message Periodic, channel string, client *twitch.Client) {
|
||
|
|
for {
|
||
|
|
time.Sleep(time.Duration(message.Seconds) * time.Second)
|
||
|
|
client.Say(channel, message.Message)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
SECRETS := LoadJSON[Secret]("secret.json")
|
||
|
|
CONFIG := LoadJSON[Config]("config.json")
|
||
|
|
|
||
|
|
// or client := twitch.NewAnonymousClient() for an anonymous user (no write capabilities)
|
||
|
|
client := twitch.NewClient(CONFIG.ClientNick, SECRETS.ClientSecret)
|
||
|
|
|
||
|
|
client.OnPrivateMessage(func(message twitch.PrivateMessage) {
|
||
|
|
cmd, found := strings.CutPrefix(message.Message, "!")
|
||
|
|
reply, valid_cmd := CONFIG.Commands[strings.ToLower(cmd)]
|
||
|
|
|
||
|
|
if found {
|
||
|
|
if valid_cmd {
|
||
|
|
client.Reply(message.Channel, message.ID, reply)
|
||
|
|
} else if !valid_cmd {
|
||
|
|
client.Reply(message.Channel, message.ID, "Invalid command. Use !help to see what's available.")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
client.OnSelfJoinMessage(func (message twitch.UserJoinMessage) {
|
||
|
|
log.Println("Join", message.Channel, "as", message.User, "successful")
|
||
|
|
})
|
||
|
|
|
||
|
|
client.OnConnect(func () {
|
||
|
|
log.Println("Connected. Joining", CONFIG.Channel)
|
||
|
|
client.Join(CONFIG.Channel)
|
||
|
|
})
|
||
|
|
|
||
|
|
client.OnPingMessage(func (message twitch.PingMessage) {
|
||
|
|
log.Println("PING!", message.Message)
|
||
|
|
})
|
||
|
|
|
||
|
|
client.OnPongMessage(func (message twitch.PongMessage) {
|
||
|
|
log.Println("PONG!", message.Message)
|
||
|
|
})
|
||
|
|
|
||
|
|
client.OnNoticeMessage(func (message twitch.NoticeMessage) {
|
||
|
|
log.Println("NOTICE", message.Channel, message.Message)
|
||
|
|
})
|
||
|
|
|
||
|
|
for _, perodic := range(CONFIG.Periodic) {
|
||
|
|
go RunPeriodic(perodic, CONFIG.Channel, client)
|
||
|
|
}
|
||
|
|
|
||
|
|
log.Println("Connecting to Twitch...")
|
||
|
|
err := client.Connect()
|
||
|
|
if err != nil { log.Fatal(err) }
|
||
|
|
}
|