This repository was archived by the owner on Mar 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
275 lines (225 loc) · 5.59 KB
/
client.go
File metadata and controls
275 lines (225 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package client
import (
"fmt"
"selfbotat-v2/bot"
"selfbotat-v2/bot/config"
"selfbotat-v2/bot/types"
"selfbotat-v2/bot/utils"
//"slices"
"strings"
"sync"
db "selfbotat-v2/bot/database"
Log "selfbotat-v2/bot/logger"
"github.com/Potat-Industries/go-potatFilters"
"github.com/douglascdev/buttifier"
"github.com/gempir/go-twitch-irc/v4"
)
const (
ensurePoolSize = 50
maxClientSize = 100
maxMessageSize = 500
)
var (
totalJoined = make(map[string]bool)
clientCount = 1
ClientPool = make(map[int]*ChatClient)
lastClientIndex int
)
type ChatClient struct {
*twitch.Client
joinedChannels map[string]bool
}
func Create() {
ensurePool()
}
func ensurePool() {
var wg sync.WaitGroup
var poolMutex sync.Mutex
for i := 0; i < ensurePoolSize; i++ {
wg.Add(1)
go func(clientID int) {
defer wg.Done()
poolMutex.Lock()
client := createClient(clientID)
poolMutex.Unlock()
err := client.Connect()
if err != nil {
Log.Error.Println("Error connecting to Twitch", err)
}
}(i + 1)
}
wg.Wait()
}
func createClient(clientID int) *ChatClient {
client := &ChatClient{
twitch.NewClient(
config.Config.Twitch.Login,
fmt.Sprintf("oauth:%s", config.Config.Twitch.Password),
),
make(map[string]bool),
}
ClientPool[clientID] = client
applyListeners(client, clientID)
return client
}
func applyListeners(client *ChatClient, clientID int) {
client.OnPrivateMessage(func(msg twitch.PrivateMessage) {
parseMessage(msg)
})
client.OnSelfJoinMessage(func(msg twitch.UserJoinMessage) {
Log.Info.Printf("Joined #%s", msg.Channel)
client.joinedChannels[msg.Channel] = true
totalJoined[msg.Channel] = true
})
client.OnSelfPartMessage(func(msg twitch.UserPartMessage) {
Log.Info.Printf("Parted #%s", msg.Channel)
client.joinedChannels[msg.Channel] = false
totalJoined[msg.Channel] = false
})
client.OnConnect(func() {
clientCount += 1
if clientID == ensurePoolSize {
Log.Debug.Printf("IRC connected")
joinChannels()
}
})
client.SetJoinRateLimiter(twitch.CreateVerifiedRateLimiter())
}
func joinChannels() {
channels, err := db.GetChannels()
if err != nil {
Log.Error.Print("Error getting channels", err)
return
}
// Join("hash_table")
for _, channel := range channels {
Join(channel.Login)
}
}
func Join(channel string) bool {
if totalJoined[channel] {
return false
}
joined := false
for _, client := range ClientPool {
if len(client.joinedChannels) < maxClientSize {
client.Join(channel)
joined = true
break
}
}
if !joined {
client := createClient(clientCount + 1)
ClientPool[clientCount+1].Join(channel)
err := client.Connect()
if err != nil {
Log.Error.Print("Error connecting to Twitch", err)
}
}
return true
}
func Part(channel string) bool {
parted := false
for _, client := range ClientPool {
if client.joinedChannels[channel] {
client.Depart(channel)
parted = true
}
}
return parted
}
func Say(channel, message string) {
lastClientIndex = (lastClientIndex + 1) % (len(ClientPool))
client := ClientPool[lastClientIndex+1]
if len(message) > maxMessageSize {
message = message[:maxMessageSize]
}
if potatFilters.Test(message, potatFilters.FilterAll) {
Log.Warn.Printf("Message filtered in channel '%s': '%s'", channel, message)
client.Say(channel, "⚠ Message withheld for containing a banned phrase...")
return
}
client.Say(channel, message)
}
func parseMessage(msg twitch.PrivateMessage) {
_, found := db.GetUser(msg.User.ID, false)
if !found {
db.NewUser(
msg.User.ID,
msg.User.Name,
msg.User.DisplayName,
)
}
user := types.User{
ID: msg.User.ID,
Login: msg.User.Name,
Name: msg.User.DisplayName,
}
channel := types.Channel{
ID: msg.RoomID,
Login: msg.Channel,
}
rawText := strings.TrimPrefix(msg.Message, config.Config.Prefix)
parts := strings.Split(strings.TrimSpace(rawText), " ")
cmd := parts[0]
args := parts[1:]
params := utils.CreateParams(args)
hashtags := utils.CreateHashtags(msg.Message)
handleMessage(&types.MessageData{
User: user,
Channel: channel,
Text: msg.Message,
Command: cmd,
Args: args,
Params: params,
Hashtags: hashtags,
Raw: msg,
})
}
// temp handler while I figure some structure
func handleMessage(msg *types.MessageData) {
if config.Config.Buttsbot.Enabled && msg.User.ID != config.Config.Twitch.Id {
butter, err := buttifier.New()
if err != nil {
Log.Error.Fatal(err)
}
butter.ButtificationProbability = config.Config.Buttsbot.ButtificationProbability
butter.ButtificationRate = config.Config.Buttsbot.ButtificationRate
butter.ButtWord = config.Config.Buttsbot.ButtWord
buttedMsg, buttHasButted := butter.ButtifySentence(msg.Text)
if buttHasButted {
// prefix with invisible character to avoid running other bot commands with mod privilege
Say(msg.Channel.Login, "�"+buttedMsg)
}
}
if msg.User.ID != config.Config.Twitch.Id {
return
}
// check prefix
if !strings.HasPrefix(msg.Text, config.Config.Prefix) {
return
}
// find command
cmd := bot.FindCmd(msg.Command)
if cmd == nil {
return
}
// ignore self and non-whitelisted users
isSelf := msg.User.ID == config.Config.Twitch.Id
// isWhitelisted := slices.Contains(cmd.Whitelist, msg.User.ID)
if !isSelf {
return
}
// remove params from command arguments
// for key, _ := range cmd.Params {
// for _, arg := range msg.Args {
// isStringParam := strings.HasPrefix(arg, key + ":")
// isBoolParam := strings.HasPrefix(arg, "-" + key)
// if isStringParam || isBoolParam {
// msg.Args = slices.Remove(msg.Args, arg)
// }
// }
// }
// execute command
cmd.Execute(msg)
}