Add twitchat module and dup finder handler
This commit is contained in:
parent
5352ef8434
commit
601747352a
8 changed files with 152 additions and 4 deletions
|
@ -6,8 +6,11 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
version = "3.0.2"
|
||||
version = "4.0.0"
|
||||
twitchUser = "galchedbot"
|
||||
twitchIRCRoom = "galched"
|
||||
discordTokenPath = "./tokens/.discordtoken"
|
||||
twitchTokenPath = "./tokens/.twitchtoken"
|
||||
subdayDataPath = "./backups/subday"
|
||||
|
||||
// Permitted roles in discord for subday
|
||||
|
@ -21,6 +24,9 @@ type (
|
|||
Settings struct {
|
||||
Version string
|
||||
DiscordToken string
|
||||
TwitchUser string
|
||||
TwitchIRCRoom string
|
||||
TwitchToken string
|
||||
SubdayDataPath string
|
||||
PermittedRoles []string
|
||||
}
|
||||
|
@ -31,10 +37,17 @@ func New() (*Settings, error) {
|
|||
if err != nil {
|
||||
log.Print("settings: cannot read discord token file", err)
|
||||
}
|
||||
twitchToken, err := ioutil.ReadFile(twitchTokenPath)
|
||||
if err != nil {
|
||||
log.Print("settings: cannot read twitch token file", err)
|
||||
}
|
||||
|
||||
return &Settings{
|
||||
Version: version,
|
||||
DiscordToken: string(discordToken),
|
||||
TwitchToken: string(twitchToken),
|
||||
TwitchUser: twitchUser,
|
||||
TwitchIRCRoom: twitchIRCRoom,
|
||||
SubdayDataPath: subdayDataPath,
|
||||
PermittedRoles: []string{subRole1, subRole2, galchedRole, smorcRole},
|
||||
}, nil
|
||||
|
|
48
modules/twitchat/duphandler.go
Normal file
48
modules/twitchat/duphandler.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package twitchat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/gempir/go-twitch-irc"
|
||||
)
|
||||
|
||||
type (
|
||||
dupHandler struct {
|
||||
lastMessage string
|
||||
counter int
|
||||
dupMinimal int
|
||||
}
|
||||
)
|
||||
|
||||
const DupMinimal = 3
|
||||
|
||||
func DupHandler() MessageHandler {
|
||||
return &dupHandler{
|
||||
lastMessage: "",
|
||||
counter: 0,
|
||||
dupMinimal: DupMinimal,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *dupHandler) IsValid(m string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *dupHandler) Handle(ch string, u *twitch.User, m *twitch.Message, client *twitch.Client) {
|
||||
data := strings.Fields(m.Text)
|
||||
for i := range data {
|
||||
if data[i] == h.lastMessage {
|
||||
h.counter++
|
||||
} else {
|
||||
if h.counter >= h.dupMinimal {
|
||||
msg := fmt.Sprintf("%d %s подряд", h.counter, h.lastMessage)
|
||||
client.Say(ch, msg)
|
||||
log.Print("chat: ", msg)
|
||||
}
|
||||
h.counter = 1
|
||||
h.lastMessage = data[i]
|
||||
}
|
||||
}
|
||||
}
|
12
modules/twitchat/handlers.go
Normal file
12
modules/twitchat/handlers.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package twitchat
|
||||
|
||||
import (
|
||||
"github.com/gempir/go-twitch-irc"
|
||||
)
|
||||
|
||||
type (
|
||||
MessageHandler interface {
|
||||
IsValid(string) bool
|
||||
Handle(ch string, u *twitch.User, m *twitch.Message, client *twitch.Client)
|
||||
}
|
||||
)
|
53
modules/twitchat/twitchat.go
Normal file
53
modules/twitchat/twitchat.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package twitchat
|
||||
|
||||
import (
|
||||
"galched-bot/modules/settings"
|
||||
|
||||
"github.com/gempir/go-twitch-irc"
|
||||
_ "github.com/gempir/go-twitch-irc"
|
||||
)
|
||||
|
||||
type (
|
||||
TwitchIRC struct {
|
||||
username string
|
||||
chat *twitch.Client
|
||||
handlers []MessageHandler
|
||||
}
|
||||
)
|
||||
|
||||
func New(s *settings.Settings) (*TwitchIRC, error) {
|
||||
var irc = new(TwitchIRC)
|
||||
|
||||
irc.username = s.TwitchUser
|
||||
|
||||
irc.handlers = append(irc.handlers, DupHandler())
|
||||
|
||||
irc.chat = twitch.NewClient(s.TwitchUser, s.TwitchToken)
|
||||
irc.chat.OnNewMessage(irc.MessageHandler)
|
||||
irc.chat.Join(s.TwitchIRCRoom)
|
||||
|
||||
return irc, nil
|
||||
}
|
||||
|
||||
func (c *TwitchIRC) Start() error {
|
||||
go func() {
|
||||
err := c.chat.Connect()
|
||||
_ = err // no point in error because disconnect will be called anyway
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *TwitchIRC) Stop() error {
|
||||
return c.chat.Disconnect()
|
||||
}
|
||||
|
||||
func (c *TwitchIRC) MessageHandler(ch string, u twitch.User, m twitch.Message) {
|
||||
if u.Username == c.username {
|
||||
return
|
||||
}
|
||||
for i := range c.handlers {
|
||||
if c.handlers[i].IsValid(m.Text) {
|
||||
c.handlers[i].Handle(ch, &u, &m, c.chat)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue