spawn new connection after 50 channels per connection

This commit is contained in:
gempir 2020-12-05 18:32:21 +01:00
parent 8f76f05d2d
commit 613d9081e6
2 changed files with 87 additions and 51 deletions

View file

@ -14,7 +14,7 @@ func (b *Bot) handlePrivateMessage(message twitch.PrivateMessage) {
if contains(b.cfg.Admins, message.User.Name) {
if strings.HasPrefix(message.Message, "!justlog status") || strings.HasPrefix(message.Message, "!status") {
uptime := humanize.TimeSince(b.startTime)
b.twitchClient.Say(message.Channel, message.User.DisplayName+", uptime: "+uptime)
b.Say(message.Channel, message.User.DisplayName+", uptime: "+uptime)
}
if strings.HasPrefix(message.Message, "!justlog join ") {
b.handleJoin(message)
@ -31,17 +31,17 @@ func (b *Bot) handleJoin(message twitch.PrivateMessage) {
users, err := b.helixClient.GetUsersByUsernames(strings.Split(input, ","))
if err != nil {
log.Error(err)
b.twitchClient.Say(message.Channel, message.User.DisplayName+", something went wrong requesting the userids")
b.Say(message.Channel, message.User.DisplayName+", something went wrong requesting the userids")
}
ids := []string{}
for _, user := range users {
ids = append(ids, user.ID)
log.Infof("[bot] joining %s", user.Login)
b.twitchClient.Join(user.Login)
b.Join(user.Login)
}
b.cfg.AddChannels(ids...)
b.twitchClient.Say(message.Channel, fmt.Sprintf("%s, added channels: %v", message.User.DisplayName, ids))
b.Say(message.Channel, fmt.Sprintf("%s, added channels: %v", message.User.DisplayName, ids))
}
func (b *Bot) handleMessageType(message twitch.PrivateMessage) {

View file

@ -1,6 +1,7 @@
package bot
import (
"math/rand"
"strings"
"time"
@ -17,12 +18,17 @@ type Bot struct {
startTime time.Time
cfg *config.Config
helixClient *helix.Client
twitchClient *twitch.Client
fileLogger *filelog.Logger
worker []*worker
channels map[string]helix.UserData
messageTypesToLog map[string][]twitch.MessageType
}
type worker struct {
client *twitch.Client
joinedChannels []string
}
// NewBot create new bot instance
func NewBot(cfg *config.Config, helixClient *helix.Client, fileLogger *filelog.Logger) *Bot {
channels, err := helixClient.GetUsersByUserIds(cfg.Channels)
@ -35,13 +41,19 @@ func NewBot(cfg *config.Config, helixClient *helix.Client, fileLogger *filelog.L
helixClient: helixClient,
fileLogger: fileLogger,
channels: channels,
worker: []*worker{},
}
}
func (b *Bot) Say(channel, text string) {
randomIndex := rand.Intn(len(b.worker))
b.worker[randomIndex].client.Say(channel, text)
}
// Connect startup the logger and bot
func (b *Bot) Connect() {
b.startTime = time.Now()
b.twitchClient = twitch.NewClient(b.cfg.Username, "oauth:"+b.cfg.OAuth)
client := b.newClient()
b.UpdateMessageTypesToLog()
b.initialJoins()
@ -51,7 +63,71 @@ func (b *Bot) Connect() {
log.Info("[bot] joining as user " + b.cfg.Username)
}
b.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
log.Fatal(client.Connect())
}
func (b *Bot) shouldLog(channelName string, receivedMsgType twitch.MessageType) bool {
for _, msgType := range b.messageTypesToLog[channelName] {
if msgType == receivedMsgType {
return true
}
}
return false
}
// UpdateMessageTypesToLog reload the config
func (b *Bot) UpdateMessageTypesToLog() {
messageTypesToLog := make(map[string][]twitch.MessageType)
for _, channel := range b.channels {
if _, ok := b.cfg.ChannelConfigs[channel.ID]; ok && b.cfg.ChannelConfigs[channel.ID].MessageTypes != nil {
messageTypesToLog[channel.Login] = b.cfg.ChannelConfigs[channel.ID].MessageTypes
} else {
messageTypesToLog[channel.Login] = []twitch.MessageType{twitch.PRIVMSG, twitch.CLEARCHAT, twitch.USERNOTICE}
}
}
b.messageTypesToLog = messageTypesToLog
}
func (b *Bot) Depart(channelNames ...string) {
for _, channelName := range channelNames {
log.Info("[bot] leaving " + channelName)
for _, worker := range b.worker {
worker.client.Depart(channelName)
}
}
}
func (b *Bot) Join(channelNames ...string) {
for _, channel := range channelNames {
joined := false
for _, worker := range b.worker {
if len(worker.joinedChannels) < 50 {
log.Info("[bot] joining " + channel)
worker.client.Join(channel)
worker.joinedChannels = append(worker.joinedChannels, channel)
joined = true
break
}
}
if !joined {
client := b.newClient()
go client.Connect()
b.Join(channel)
}
}
}
func (b *Bot) newClient() *twitch.Client {
client := twitch.NewClient(b.cfg.Username, "oauth:"+b.cfg.OAuth)
b.worker = append(b.worker, &worker{client, []string{}})
log.Infof("[bot] creating new twitch connection, new total: %d", len(b.worker))
client.OnPrivateMessage(func(message twitch.PrivateMessage) {
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
@ -78,7 +154,7 @@ func (b *Bot) Connect() {
b.handlePrivateMessage(message)
})
b.twitchClient.OnUserNoticeMessage(func(message twitch.UserNoticeMessage) {
client.OnUserNoticeMessage(func(message twitch.UserNoticeMessage) {
log.Debug(message.Raw)
go func() {
@ -118,7 +194,7 @@ func (b *Bot) Connect() {
})
b.twitchClient.OnClearChatMessage(func(message twitch.ClearChatMessage) {
client.OnClearChatMessage(func(message twitch.ClearChatMessage) {
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
@ -143,51 +219,11 @@ func (b *Bot) Connect() {
}()
})
log.Fatal(b.twitchClient.Connect())
}
func (b *Bot) shouldLog(channelName string, receivedMsgType twitch.MessageType) bool {
for _, msgType := range b.messageTypesToLog[channelName] {
if msgType == receivedMsgType {
return true
}
}
return false
}
// UpdateMessageTypesToLog reload the config
func (b *Bot) UpdateMessageTypesToLog() {
messageTypesToLog := make(map[string][]twitch.MessageType)
for _, channel := range b.channels {
if _, ok := b.cfg.ChannelConfigs[channel.ID]; ok && b.cfg.ChannelConfigs[channel.ID].MessageTypes != nil {
messageTypesToLog[channel.Login] = b.cfg.ChannelConfigs[channel.ID].MessageTypes
} else {
messageTypesToLog[channel.Login] = []twitch.MessageType{twitch.PRIVMSG, twitch.CLEARCHAT, twitch.USERNOTICE}
}
}
b.messageTypesToLog = messageTypesToLog
}
func (b *Bot) Depart(channelNames ...string) {
for _, channelName := range channelNames {
log.Info("[bot] leaving " + channelName)
b.twitchClient.Depart(channelName)
}
}
func (b *Bot) Join(channelNames ...string) {
for _, channel := range channelNames {
log.Info("[bot] joining " + channel)
b.twitchClient.Join(channel)
}
return client
}
func (b *Bot) initialJoins() {
for _, channel := range b.channels {
log.Info("[bot] joining " + channel.Login)
b.twitchClient.Join(channel.Login)
b.Join(channel.Login)
}
}