-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugins_core.go
231 lines (207 loc) · 7.66 KB
/
plugins_core.go
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
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gopkg.in/irc.v4"
"github.com/Luzifer/go_helpers/v2/backoff"
"github.com/Luzifer/go_helpers/v2/fieldcollection"
"github.com/Luzifer/go_helpers/v2/str"
"github.com/Luzifer/twitch-bot/v3/internal/actors/announce"
"github.com/Luzifer/twitch-bot/v3/internal/actors/ban"
"github.com/Luzifer/twitch-bot/v3/internal/actors/clip"
"github.com/Luzifer/twitch-bot/v3/internal/actors/clipdetector"
"github.com/Luzifer/twitch-bot/v3/internal/actors/commercial"
"github.com/Luzifer/twitch-bot/v3/internal/actors/counter"
"github.com/Luzifer/twitch-bot/v3/internal/actors/delay"
deleteactor "github.com/Luzifer/twitch-bot/v3/internal/actors/delete"
"github.com/Luzifer/twitch-bot/v3/internal/actors/eventmod"
"github.com/Luzifer/twitch-bot/v3/internal/actors/filesay"
"github.com/Luzifer/twitch-bot/v3/internal/actors/linkdetector"
"github.com/Luzifer/twitch-bot/v3/internal/actors/linkprotect"
logActor "github.com/Luzifer/twitch-bot/v3/internal/actors/log"
"github.com/Luzifer/twitch-bot/v3/internal/actors/marker"
"github.com/Luzifer/twitch-bot/v3/internal/actors/messagehook"
"github.com/Luzifer/twitch-bot/v3/internal/actors/modchannel"
"github.com/Luzifer/twitch-bot/v3/internal/actors/nuke"
"github.com/Luzifer/twitch-bot/v3/internal/actors/punish"
"github.com/Luzifer/twitch-bot/v3/internal/actors/quotedb"
"github.com/Luzifer/twitch-bot/v3/internal/actors/raw"
"github.com/Luzifer/twitch-bot/v3/internal/actors/respond"
"github.com/Luzifer/twitch-bot/v3/internal/actors/shield"
"github.com/Luzifer/twitch-bot/v3/internal/actors/shoutout"
"github.com/Luzifer/twitch-bot/v3/internal/actors/spotify"
"github.com/Luzifer/twitch-bot/v3/internal/actors/stopexec"
"github.com/Luzifer/twitch-bot/v3/internal/actors/timeout"
"github.com/Luzifer/twitch-bot/v3/internal/actors/variables"
"github.com/Luzifer/twitch-bot/v3/internal/actors/vip"
"github.com/Luzifer/twitch-bot/v3/internal/actors/whisper"
"github.com/Luzifer/twitch-bot/v3/internal/apimodules/customevent"
"github.com/Luzifer/twitch-bot/v3/internal/apimodules/kofi"
"github.com/Luzifer/twitch-bot/v3/internal/apimodules/msgformat"
"github.com/Luzifer/twitch-bot/v3/internal/apimodules/overlays"
"github.com/Luzifer/twitch-bot/v3/internal/apimodules/raffle"
"github.com/Luzifer/twitch-bot/v3/internal/service/access"
"github.com/Luzifer/twitch-bot/v3/internal/template/api"
"github.com/Luzifer/twitch-bot/v3/internal/template/date"
"github.com/Luzifer/twitch-bot/v3/internal/template/numeric"
"github.com/Luzifer/twitch-bot/v3/internal/template/random"
"github.com/Luzifer/twitch-bot/v3/internal/template/slice"
"github.com/Luzifer/twitch-bot/v3/internal/template/strings"
"github.com/Luzifer/twitch-bot/v3/internal/template/subscriber"
twitchFns "github.com/Luzifer/twitch-bot/v3/internal/template/twitch"
"github.com/Luzifer/twitch-bot/v3/internal/template/userstate"
"github.com/Luzifer/twitch-bot/v3/pkg/database"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
const ircHandleWaitRetries = 10
var (
corePluginRegistrations = []plugins.RegisterFunc{
// Actors
announce.Register,
ban.Register,
clip.Register,
clipdetector.Register,
commercial.Register,
counter.Register,
delay.Register,
deleteactor.Register,
eventmod.Register,
filesay.Register,
linkdetector.Register,
linkprotect.Register,
logActor.Register,
marker.Register,
messagehook.Register,
modchannel.Register,
nuke.Register,
punish.Register,
quotedb.Register,
raw.Register,
respond.Register,
shield.Register,
shoutout.Register,
stopexec.Register,
timeout.Register,
variables.Register,
vip.Register,
whisper.Register,
// Template functions
api.Register,
date.Register,
numeric.Register,
random.Register,
slice.Register,
spotify.Register,
strings.Register,
subscriber.Register,
twitchFns.Register,
userstate.Register,
// API-only modules
customevent.Register,
kofi.Register,
msgformat.Register,
overlays.Register,
raffle.Register,
}
knownModules []string
)
func initCorePlugins() error {
args := getRegistrationArguments()
for idx, rf := range corePluginRegistrations {
if err := rf(args); err != nil {
return errors.Wrapf(err, "registering core plugin %d", idx)
}
}
return nil
}
func registerRoute(route plugins.HTTPRouteRegistrationArgs) error {
r := router.
PathPrefix(fmt.Sprintf("/%s/", route.Module)).
Subrouter()
if !str.StringInSlice(route.Module, knownModules) {
knownModules = append(knownModules, route.Module)
}
var hdl http.Handler = route.HandlerFunc
switch {
case route.RequiresEditorsAuth:
hdl = writeAuthMiddleware(hdl, moduleConfigEditor)
case route.RequiresWriteAuth:
hdl = writeAuthMiddleware(hdl, route.Module)
}
var muxRoute *mux.Route
if route.IsPrefix {
muxRoute = r.PathPrefix(route.Path).Handler(hdl)
} else {
muxRoute = r.Handle(route.Path, hdl)
}
if route.Method != "" {
muxRoute.Methods(route.Method)
}
if !route.SkipDocumentation {
return errors.Wrap(registerSwaggerRoute(route), "registering documentation")
}
return nil
}
func getRegistrationArguments() plugins.RegistrationArguments {
return plugins.RegistrationArguments{
FormatMessage: formatMessage,
FrontendNotify: func(mt string) { frontendNotifyHooks.Ping(mt) },
GetBaseURL: func() string { return cfg.BaseURL },
GetDatabaseConnector: func() database.Connector { return db },
GetLogger: func(moduleName string) *log.Entry { return log.WithField("module", moduleName) },
GetTwitchClient: func() *twitch.Client { return twitchClient },
HasAnyPermissionForChannel: accessService.HasAnyPermissionForChannel,
HasPermissionForChannel: accessService.HasPermissionsForChannel,
RegisterActor: registerAction,
RegisterActorDocumentation: registerActorDocumentation,
RegisterAPIRoute: registerRoute,
RegisterCron: cronService.AddFunc,
RegisterCopyDatabaseFunc: registerDatabaseCopyFunc,
RegisterEventHandler: registerEventHandlers,
RegisterMessageModFunc: registerChatcommand,
RegisterRawMessageHandler: registerRawMessageHandler,
RegisterTemplateFunction: tplFuncs.Register,
SendMessage: sendMessage,
ValidateToken: authService.ValidateTokenFor,
CreateEvent: func(evt string, eventData *fieldcollection.FieldCollection) error {
handleMessage(ircHdl.Client(), nil, &evt, eventData)
return nil
},
GetModuleConfigForChannel: func(module, channel string) *fieldcollection.FieldCollection {
return config.ModuleConfig.GetChannelConfig(module, channel)
},
GetTwitchClientForChannel: func(channel string) (*twitch.Client, error) {
return accessService.GetTwitchClientForChannel(channel, access.ClientConfig{
TwitchClient: cfg.TwitchClient,
TwitchClientSecret: cfg.TwitchClientSecret,
})
},
}
}
func sendMessage(m *irc.Message) error {
err := handleChatcommandModifications(m)
switch {
case err == nil:
// There was no error, the message should be sent normally
case errors.Is(err, plugins.ErrSkipSendingMessage):
// One chatcommand handler cancelled sending the message
// (probably because it was handled otherwise)
return nil
default:
// Something in a chatcommand handler went wrong
return errors.Wrap(err, "handling chat commands")
}
if err = backoff.NewBackoff().WithMaxIterations(ircHandleWaitRetries).Retry(func() error {
if ircHdl == nil {
return errors.New("irc handle not available")
}
return nil
}); err != nil {
return errors.Wrap(err, "waiting for IRC connection")
}
return ircHdl.SendMessage(m)
}