Skip to content

Commit 2467527

Browse files
Updated twurple
1 parent 0522dcf commit 2467527

File tree

6 files changed

+165
-344
lines changed

6 files changed

+165
-344
lines changed

electron/bot/bot.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const { StaticAuthProvider } = require('@twurple/auth');
22
const { ChatClient } = require('@twurple/chat');
33
const { PubSubClient } = require('@twurple/pubsub');
4-
4+
const { ApiClient } = require('@twurple/api');
5+
const { EventSubWsListener } = require('@twurple/eventsub-ws');
56
const EventQueue = require('./components/base/eventQueue');
67

78
const readline = require('readline');
@@ -17,7 +18,7 @@ const versionNumber = "3.0b";
1718
* INDEXES
1819
*/
1920

20-
let client, pubSubClient;
21+
let client, pubSubClient, apiClient, eventListener;
2122
let listeners = [];
2223
let cooldowns = {};
2324
let units = {
@@ -82,7 +83,7 @@ const performCustomCommand = (command, {type, coolDown, target}, botContext) =>
8283
// Define configuration options for chat bot
8384
const startBot = async (botConfig) => {
8485
try {
85-
let {accessToken, clientId, twitchChannel, devMode} = botConfig;
86+
let {accessToken, clientId, twitchChannel, devMode, broadcasterId} = botConfig;
8687
let botContext = {};
8788

8889
let plugins = [deathCounterPlugin, requestPlugin, cameraObscura, modTools];
@@ -213,16 +214,34 @@ const startBot = async (botConfig) => {
213214
}
214215
}
215216

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+
216230
const rl = readline.createInterface({
217231
input: process.stdin,
218232
output: process.stdout,
219233
terminal: true
220234
});
221235

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+
223239
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 });
226245

227246
rl.on('line', onConsoleCommand);
228247

@@ -232,16 +251,21 @@ const startBot = async (botConfig) => {
232251
});
233252
client.onConnect(onConnectedHandler);
234253
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);
238260

239261
listeners = [subListener, cheerListener, redemptionListener];
240262

241263
// Connect to twitch chat and pubsub
242-
client.connect();
264+
await client.connect();
265+
await eventListener.start();
243266
} catch (error) {
244267
console.error(`* Failed to start bot: ${error}`);
268+
throw error;
245269
}
246270
};
247271

electron/bot/botPlugins/cameraObscura.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ exports.init = async (botContext) => {}
182182
exports.followHook = async ({userName}, botContext) => {
183183
const {enabled, messageTemplate} = botContext.botConfig.alertConfigs.followAlert;
184184

185+
console.log("FOLLOW EVENT: " + userName);
186+
185187
let alertMessage = `${userName} just followed!`;
186188
if (messageTemplate) {
187189
alertMessage = messageTemplate.replace("${username}", userName);

electron/main.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,13 @@ const MEDIA_DIRECTORY = path.join(HOME, "media");
1919
const CONFIG_FILE = path.join(HOME, "config.json");
2020
const MIGRATION_FILE = path.join(HOME, "migration.json");
2121
const USER_DATA_FILE = path.join(HOME, "config.json");
22-
const REACT_APP_LOCATION = `file://${path.join(__dirname, '../build/index.html')}`
2322
const DEFAULT_FILE_SERVER_PORT = "8080";
2423
const ENV = process.env.RUNTIME_ENV || "prod";
2524

25+
console.log("ENV: " + ENV);
2626
console.log("HOME: " + HOME);
2727
console.log("CONFIG FILE: " + CONFIG_FILE);
2828

29-
let isDev = false;
30-
try {
31-
isDev = require('electron-is-dev');
32-
} catch (e) {
33-
console.log("Running in production mode using react app at: " + REACT_APP_LOCATION);
34-
}
35-
3629
if (!fs.existsSync(HOME)) {
3730
console.log("HOME NOT FOUND");
3831
fs.mkdirSync(HOME, {recursive: true});
@@ -64,6 +57,8 @@ if (!fs.existsSync(CONFIG_FILE)) {
6457
"profileImage": "",
6558
"accessToken": "",
6659
"refreshToken": "",
60+
"clientId": "z9sxe9lmchklcfoqpcaoe1fo8a660e",
61+
"clientSecret": "ebtdjo3gx50d5qyk331mn6rrzh5wlx",
6762
}));
6863
}
6964

@@ -122,7 +117,7 @@ const createWindow = async () => {
122117
const url = require('url').format({
123118
protocol: 'file',
124119
slashes: true,
125-
pathname: require('path').join(__dirname, 'build/index.html')
120+
pathname: require('path').join(__dirname, '../build/index.html')
126121
})
127122
win.loadURL(url)
128123
} else {
@@ -145,11 +140,6 @@ const createWindow = async () => {
145140
console.error('Failed to register protocol');
146141
}
147142
});
148-
149-
// Open the DevTools.
150-
if (isDev) {
151-
win.webContents.openDevTools({ mode: 'detach' });
152-
}
153143
};
154144

155145
// This method will be called when Electron has finished
@@ -200,7 +190,7 @@ const twitchRefresh = async () => {
200190
const twitchLogin = async () => {
201191
try {
202192
let token = await twitchOauth.getAccessToken({
203-
scope: 'chat:read chat:edit channel:read:redemptions channel:read:subscriptions bits:read channel:manage:redemptions'
193+
scope: 'chat:read chat:edit channel:read:redemptions channel:read:subscriptions bits:read channel:manage:redemptions moderator:read:followers'
204194
});
205195
let {twitchChannel, profileImage, id} = await getTwitchUser(null, token.access_token);
206196
config.accessToken = token.access_token;

0 commit comments

Comments
 (0)