1
1
const { StaticAuthProvider } = require ( '@twurple/auth' ) ;
2
2
const { ChatClient } = require ( '@twurple/chat' ) ;
3
3
const { PubSubClient } = require ( '@twurple/pubsub' ) ;
4
-
4
+ const { ApiClient } = require ( '@twurple/api' ) ;
5
+ const { EventSubWsListener } = require ( '@twurple/eventsub-ws' ) ;
5
6
const EventQueue = require ( './components/base/eventQueue' ) ;
6
7
7
8
const readline = require ( 'readline' ) ;
@@ -17,7 +18,7 @@ const versionNumber = "3.0b";
17
18
* INDEXES
18
19
*/
19
20
20
- let client , pubSubClient ;
21
+ let client , pubSubClient , apiClient , eventListener ;
21
22
let listeners = [ ] ;
22
23
let cooldowns = { } ;
23
24
let units = {
@@ -82,7 +83,7 @@ const performCustomCommand = (command, {type, coolDown, target}, botContext) =>
82
83
// Define configuration options for chat bot
83
84
const startBot = async ( botConfig ) => {
84
85
try {
85
- let { accessToken, clientId, twitchChannel, devMode} = botConfig ;
86
+ let { accessToken, clientId, twitchChannel, devMode, broadcasterId } = botConfig ;
86
87
let botContext = { } ;
87
88
88
89
let plugins = [ deathCounterPlugin , requestPlugin , cameraObscura , modTools ] ;
@@ -213,16 +214,34 @@ const startBot = async (botConfig) => {
213
214
}
214
215
}
215
216
217
+ const onFollow = async ( followEvent ) => {
218
+ try {
219
+ // Run through redemption plugin hooks
220
+ for ( let plugin of plugins ) {
221
+ if ( plugin . followHook ) {
222
+ plugin . followHook ( followEvent , botContext ) ;
223
+ }
224
+ }
225
+ } catch ( error ) {
226
+ console . error ( "FOLLOW FAILURE: " + error ) ;
227
+ }
228
+ }
229
+
216
230
const rl = readline . createInterface ( {
217
231
input : process . stdin ,
218
232
output : process . stdout ,
219
233
terminal : true
220
234
} ) ;
221
235
222
- const authProvider = new StaticAuthProvider ( clientId , accessToken , [ "chat:read" , "chat:edit" , "channel:read:redemptions" , "channel:read:subscriptions" , "bits:read" , "channel_subscriptions" ] , "user" ) ;
236
+ const authProvider = new StaticAuthProvider ( clientId , accessToken , [ "chat:read" , "chat:edit" , "channel:read:redemptions" , "channel:read:subscriptions" , "bits:read" , "moderator:read:followers" , "channel_subscriptions" ] , "user" ) ;
237
+ apiClient = new ApiClient ( { authProvider} ) ;
238
+
223
239
client = new ChatClient ( { authProvider, channels : [ twitchChannel ] } ) ;
224
- pubSubClient = new PubSubClient ( ) ;
225
- const userId = await pubSubClient . registerUserListener ( authProvider ) ;
240
+
241
+ pubSubClient = new PubSubClient ( { authProvider} ) ;
242
+
243
+ apiClient . eventSub . deleteAllSubscriptions ( ) ;
244
+ eventListener = new EventSubWsListener ( { apiClient } ) ;
226
245
227
246
rl . on ( 'line' , onConsoleCommand ) ;
228
247
@@ -232,16 +251,21 @@ const startBot = async (botConfig) => {
232
251
} ) ;
233
252
client . onConnect ( onConnectedHandler ) ;
234
253
client . onRaid ( ( channel , username , { viewerCount} ) => { onRaid ( channel , username , viewerCount ) } ) ;
235
- let subListener = await pubSubClient . onSubscription ( userId , onSubscription ) ;
236
- let cheerListener = await pubSubClient . onBits ( userId , onBits ) ;
237
- let redemptionListener = await pubSubClient . onRedemption ( userId , onRedemption ) ;
254
+
255
+ let subListener = await pubSubClient . onSubscription ( broadcasterId , onSubscription ) ;
256
+ let cheerListener = await pubSubClient . onBits ( broadcasterId , onBits ) ;
257
+ let redemptionListener = await pubSubClient . onRedemption ( broadcasterId , onRedemption ) ;
258
+
259
+ eventListener . onChannelFollow ( broadcasterId , broadcasterId , onFollow ) ;
238
260
239
261
listeners = [ subListener , cheerListener , redemptionListener ] ;
240
262
241
263
// Connect to twitch chat and pubsub
242
- client . connect ( ) ;
264
+ await client . connect ( ) ;
265
+ await eventListener . start ( ) ;
243
266
} catch ( error ) {
244
267
console . error ( `* Failed to start bot: ${ error } ` ) ;
268
+ throw error ;
245
269
}
246
270
} ;
247
271
0 commit comments