Skip to content

Commit

Permalink
Adding raid notifications support & timeout reboot
Browse files Browse the repository at this point in the history
  • Loading branch information
kabessao committed Mar 10, 2024
1 parent 1bd2fe5 commit e281853
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 17 deletions.
10 changes: 10 additions & 0 deletions config_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,13 @@ oauth_password: 'YOUR TWITCH OAUTH'
#
#grab_emotes: true

# With this option you can get notifications whenever a raid just arrived.
# You can also set a max amount of users for the message to appear
# (the idea is to implement all of user notice messages, but this will do for now)
#
# default: none
#
#user_notice_message:
# -
# type: raid # for now there's only raid. More in the future
# min: 100 # only raids with more than 100 users will be shown.
35 changes: 21 additions & 14 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

type Config struct {

// mandatory
WebhookUrl string `yaml:"webhook_url"`
TwitchClientId string `yaml:"twitch_client_id"`
Expand All @@ -18,25 +19,31 @@ type Config struct {
Channel string `yaml:"channel"`

// optionals
Channels []string `yaml:"channels"`
SendAllMessages bool `yaml:"send_all_messages"`
PreventPing bool `yaml:"prevent_ping"`
ShowBitGifters interface{} `yaml:"show_bit_gifters"`
ShowHyperChat interface{} `yaml:"show_hyber_chat"`
OutputLog bool `yaml:"output_log"`
ModActions bool `yaml:"mod_actions"`
FilterBadges []string `yaml:"filter_badges"`
FilterUsernames []string `yaml:"filter_usernames"`
Blacklist []string `yaml:"blacklist"`
FilterMessages []string `yaml:"filter_messages"`
GrabEmotes bool `yaml:"grab_emotes"`
UseExternalEmotes bool `yaml:"use_external_emotes"`
OnStreamStatus string `yaml:"on_stream_status"`
Channels []string `yaml:"channels"`
SendAllMessages bool `yaml:"send_all_messages"`
PreventPing bool `yaml:"prevent_ping"`
ShowBitGifters any `yaml:"show_bit_gifters"`
ShowHyperChat any `yaml:"show_hyber_chat"`
OutputLog bool `yaml:"output_log"`
ModActions bool `yaml:"mod_actions"`
FilterBadges []string `yaml:"filter_badges"`
FilterUsernames []string `yaml:"filter_usernames"`
Blacklist []string `yaml:"blacklist"`
FilterMessages []string `yaml:"filter_messages"`
GrabEmotes bool `yaml:"grab_emotes"`
UseExternalEmotes bool `yaml:"use_external_emotes"`
OnStreamStatus string `yaml:"on_stream_status"`
UserNoticeMessage []UserNoticeMessage `yaml:"user_notice_message"`

// Extras
ModTools ModTools `yaml:"mod_tools"`
}

type UserNoticeMessage struct {
Type string `yaml:"type"`
Min int `yaml:"min"`
}

type ModTools struct {
LogFirstMessages *int `yaml:"log_first_messages"`
}
Expand Down
100 changes: 97 additions & 3 deletions twitchBot/twitchBot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"runtime/debug"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -14,6 +15,7 @@ import (

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/webhook"
"github.com/dlclark/regexp2"
"github.com/gempir/go-twitch-irc/v4"
"github.com/nicklaw5/helix"
)
Expand All @@ -33,6 +35,7 @@ type bot struct {
overHeatAmount int
isOverHeated bool
messageHistory map[string][]twitch.PrivateMessage
channel Channel
}

func (b *bot) log(message string) {
Expand Down Expand Up @@ -154,7 +157,9 @@ func (b *bot) onClearChatMessage(message twitch.ClearChatMessage) {
return
}

b.log(fmt.Sprintf(message.Raw))
if b.config.OutputLog {
go b.log(fmt.Sprintf(message.Raw))
}

var timeoutMessage = "`User got banned permanently`"

Expand Down Expand Up @@ -212,7 +217,9 @@ func (b *bot) onClearMessage(deletedMessage twitch.ClearMessage) {
return
}

println(deletedMessage.Raw + "\n")
if b.config.OutputLog {
go b.log(fmt.Sprintf(deletedMessage.Raw))
}

for _, m := range b.messageHistory[deletedMessage.Channel] {
if m.ID == deletedMessage.TargetMsgID {
Expand All @@ -232,6 +239,72 @@ func (b *bot) onClearMessage(deletedMessage twitch.ClearMessage) {
}
}

func (b *bot) onUserNoticeMessage(message twitch.UserNoticeMessage) {

if b.config.OutputLog {
go b.log(fmt.Sprintf(message.Raw))
}

if len(b.config.UserNoticeMessage) == 0 {
return
}

re := regexp2.MustCompile(`\s#(\w+)`, regexp2.None)
match, err := re.FindStringMatch(message.Raw)
if err != nil {
b.errorLog(err)
return
}

channel := match.GroupByNumber(1).String()

go b.log(fmt.Sprintf("value: %v", message.Tags))

var webhookMessage discord.WebhookMessageCreate

msgId := message.Tags["msg-id"]

for _, config := range b.config.UserNoticeMessage {

switch config.Type {

case "raid":

if config.Type != msgId {
continue
}

var raidersAmmount int

if value, ok := message.Tags["msg-param-viewerCount"]; ok {
raidersAmmount, _ = strconv.Atoi(value)
if raidersAmmount < config.Min {
return
}
}

webhookMessage.Content = fmt.Sprintf("%d raiders just arrived", raidersAmmount)

webhookMessage.AvatarURL = b.getTwitchAvatarUrl(message.User.Name)

webhookMessage.Username = fmt.Sprintf(
"%s [%s chat]",
message.User.DisplayName,
utils.PluralSufixParser(channel),
)

_, err = b.webhookClient.CreateMessage(webhookMessage)

if err != nil {
b.errorLog(err)
}

default:
return
}
}
}

func (b *bot) loadConfiguration() error {
client := twitch.NewClient(b.config.TwitchUsername, "oauth:"+b.config.OauthPassword)

Expand All @@ -252,6 +325,25 @@ func (b *bot) loadConfiguration() error {
return fmt.Errorf("Couldn't start webhook client: %v", err)
}

var pong = false

client.OnPingSent(func() {
go func() {
time.Sleep(3 * time.Second)
if !pong && b.channel.IsOk {
b.errorLog(fmt.Errorf("Connection timeout reached!"))
close(b.channel.Channel)
return
}
pong = false
}()

})

client.OnPongMessage(func(message twitch.PongMessage) {
pong = true
})

client.OnConnect(b.onConnect)

client.OnPrivateMessage(b.onPrivateMessage)
Expand All @@ -260,6 +352,8 @@ func (b *bot) loadConfiguration() error {

client.OnClearMessage(b.onClearMessage)

client.OnUserNoticeMessage(b.onUserNoticeMessage)

return nil
}

Expand All @@ -274,6 +368,7 @@ func LaunchNewBot(filePath string, channel *Channel) {
sendMessageLock: sync.Mutex{},
firstMessages: map[string]int{},
messageHistory: map[string][]twitch.PrivateMessage{},
channel: *channel,
}

defer recover()
Expand Down Expand Up @@ -367,7 +462,6 @@ func (b *bot) getTwitchUserId(name string) string {

return ""
}

func (b *bot) getTwitchAvatarUrl(name string) string {

if userInfo, err := b.helixApi.GetUsers(&helix.UsersParams{Logins: []string{name}}); err == nil && len(userInfo.Data.Users) > 0 {
Expand Down

0 comments on commit e281853

Please sign in to comment.