Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit 41d2add

Browse files
committed
Update twurple from v5 to latest v7
1 parent 7966552 commit 41d2add

File tree

13 files changed

+168
-229
lines changed

13 files changed

+168
-229
lines changed

nodecg-io-core/dashboard/bundles.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ export function renderInstanceSelector(): void {
7777
return;
7878
}
7979

80-
const currentInstance = config.data.bundles[bundle]?.find((dep) => dep.serviceType === serviceType)
81-
?.serviceInstance;
80+
const currentInstance = config.data.bundles[bundle]?.find(
81+
(dep) => dep.serviceType === serviceType,
82+
)?.serviceInstance;
8283

8384
let index = 0;
8485
for (let i = 0; i < selectBundleInstance.options.length; i++) {

package-lock.json

+95-192
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/twitch-api/extension/index.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,24 @@ module.exports = function (nodecg: NodeCG.ServerAPI) {
99

1010
twitchApi?.onAvailable(async (client) => {
1111
nodecg.log.info("Twitch api client has been updated, getting user info.");
12-
const user = await client.helix.users.getMe();
13-
const follows = await user.getFollows();
14-
const stream = await user.getStream();
12+
const tokenInfo = await client.getTokenInfo();
13+
const userId = tokenInfo.userId;
14+
15+
if (!userId) {
16+
nodecg.log.info("Unable to determine authenticated user");
17+
return;
18+
}
19+
20+
const user = await client.users.getUserById(userId);
21+
if (!user) {
22+
nodecg.log.info("Unable to get user info");
23+
return;
24+
}
25+
26+
const follows = await user?.getFollowedChannels();
27+
const stream = await user?.getStream();
1528
nodecg.log.info(
16-
`You are user "${user.name}", follow ${follows.total} people and are${
17-
stream === null ? " not" : ""
18-
} streaming.`,
29+
`You are user "${user.name}", follow ${follows.total} people and are${stream === null ? " not" : ""} streaming.`,
1930
);
2031
});
2132

samples/twitch-chat/extension/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module.exports = function (nodecg: NodeCG.ServerAPI) {
99
const twitch = requireService<TwitchChatServiceClient>(nodecg, "twitch-chat");
1010

1111
// Hardcoded channels for testing purposes.
12-
// Note that this does need a # before the channel name and is case-insensitive.
13-
const twitchChannels = ["#skate702", "#daniel0611"];
12+
// Note that this is case-insensitive.
13+
const twitchChannels = ["skate702", "hlxid"];
1414

1515
// Once the service instance has been set we add listeners for messages in the corresponding channels.
1616
twitch?.onAvailable((client) => {

samples/twitch-pubsub/extension/index.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ module.exports = function (nodecg: NodeCG.ServerAPI) {
88
const pubsub = requireService<TwitchPubSubServiceClient>(nodecg, "twitch-pubsub");
99

1010
pubsub?.onAvailable((client) => {
11+
const channelName = "skate702";
12+
1113
nodecg.log.info("PubSub client has been updated, adding handlers for messages.");
12-
client.onSubscription((message) => {
14+
client.onSubscription(channelName, (message) => {
1315
nodecg.log.info(`${message.userDisplayName} just subscribed (${message.cumulativeMonths} months)`);
1416
});
15-
client.onBits((message) => {
17+
client.onBits(channelName, (message) => {
1618
nodecg.log.info(`${message.userName} cheered ${message.bits} Bits`);
1719
});
18-
client.onBitsBadgeUnlock((message) => {
20+
client.onBitsBadgeUnlock(channelName, (message) => {
1921
nodecg.log.info(`${message.userName} just unlocked the ${message.badgeTier} Badge`);
2022
});
21-
client.onRedemption((message) => {
23+
client.onRedemption(channelName, (message) => {
2224
nodecg.log.info(`${message.userDisplayName} redeemed ${message.rewardTitle} (${message.message})`);
2325
});
2426
});

services/nodecg-io-twitch-api/extension/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import NodeCG from "@nodecg/types";
2-
import { Result, emptySuccess, success, ServiceBundle, Logger } from "nodecg-io-core";
32
import { ApiClient } from "@twurple/api";
3+
import { Result, emptySuccess, success, ServiceBundle, Logger } from "nodecg-io-core";
44
import { createAuthProvider, getTokenInfo, TwitchServiceConfig } from "nodecg-io-twitch-auth";
55

66
export type TwitchApiServiceClient = ApiClient;

services/nodecg-io-twitch-api/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@
3939
"dependencies": {
4040
"nodecg-io-core": "^0.3.0",
4141
"nodecg-io-twitch-auth": "^0.3.0",
42-
"@twurple/api": "^5.3.4"
42+
"@twurple/api": "^7.2.0"
4343
}
4444
}

services/nodecg-io-twitch-chat/extension/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class TwitchService extends ServiceBundle<TwitchServiceConfig, TwitchChatService
2424
}
2525

2626
stopClient(client: TwitchChatServiceClient, logger: Logger): void {
27-
client.quit().then(() => logger.info("Successfully stopped twitch client."));
27+
client.quit();
28+
logger.info("Successfully stopped twitch client.");
2829
}
2930

3031
removeHandlers(client: TwitchChatServiceClient): void {

services/nodecg-io-twitch-chat/extension/twitchClient.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ export class TwitchChatServiceClient extends ChatClient {
1111

1212
// Create the actual chat client and connect
1313
const chatClient = new TwitchChatServiceClient({ authProvider });
14-
await chatClient.connect();
14+
chatClient.connect();
1515

16-
// This also waits till it has registered itself at the IRC server, which is needed to do anything.
17-
await new Promise((resolve, _reject) => {
18-
chatClient.onRegister(() => resolve(undefined));
16+
// This also waits till it has connected and registered itself at the IRC server, which is needed to do anything.
17+
await new Promise((resolve, reject) => {
18+
chatClient.onConnect(() => resolve(undefined));
19+
chatClient.onAuthenticationFailure(() => {
20+
reject("Authentication failed");
21+
});
1922
});
2023

2124
return chatClient;
@@ -31,7 +34,7 @@ export class TwitchChatServiceClient extends ChatClient {
3134
* @param channel the channel to join
3235
*/
3336
join(channel: string): Promise<void> {
34-
this.onRegister(() => {
37+
this.onConnect(() => {
3538
this.join(channel);
3639
});
3740

services/nodecg-io-twitch-chat/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@
3939
"dependencies": {
4040
"nodecg-io-core": "^0.3.0",
4141
"nodecg-io-twitch-auth": "^0.3.0",
42-
"@twurple/chat": "^5.3.4"
42+
"@twurple/chat": "^7.2.0"
4343
}
4444
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
import { BasicPubSubClient, SingleUserPubSubClient } from "@twurple/pubsub";
1+
import { BasicPubSubClient, PubSubClient } from "@twurple/pubsub";
22
import { createAuthProvider, TwitchServiceConfig } from "nodecg-io-twitch-auth";
3-
import { AuthProvider } from "@twurple/auth";
3+
import { PubSubClientConfig } from "@twurple/pubsub/lib/PubSubClient";
44

5-
export class TwitchPubSubServiceClient extends SingleUserPubSubClient {
5+
export class TwitchPubSubServiceClient extends PubSubClient {
66
private basicClient: BasicPubSubClient;
7-
constructor(auth: AuthProvider, basicClient: BasicPubSubClient) {
8-
super({ authProvider: auth, pubSubClient: basicClient });
9-
this.basicClient = basicClient;
7+
8+
constructor(config: PubSubClientConfig) {
9+
super(config);
10+
11+
// Get reference to underlying client.
12+
// Very ugly but not possible differently.
13+
// This is a private field and may change but we need it
14+
// to add listeners for disconnect/connection failures
15+
// to the underlying basic client and force connection to
16+
// ensure valid credentials.
17+
//@ts-expect-error private field
18+
this.basicClient = this._basicClient;
1019
}
1120

1221
/**
@@ -17,14 +26,23 @@ export class TwitchPubSubServiceClient extends SingleUserPubSubClient {
1726
const authProvider = await createAuthProvider(cfg);
1827

1928
// Create the actual pubsub client and connect
20-
const basicClient = new BasicPubSubClient();
21-
const pubSubClient = new TwitchPubSubServiceClient(authProvider, basicClient);
29+
const pubSubClient = new TwitchPubSubServiceClient({ authProvider });
2230

23-
await basicClient.connect();
31+
pubSubClient.basicClient.connect();
32+
await new Promise((resolve, reject) => {
33+
pubSubClient.basicClient.onConnect(() => resolve(null));
34+
// 15 second timeout
35+
setTimeout(() => reject("Timeout for PubSub connection was exceeded"), 15000);
36+
});
2437
return pubSubClient;
2538
}
2639

27-
disconnect(): Promise<void> {
28-
return this.basicClient.disconnect();
40+
async disconnect(): Promise<void> {
41+
this.basicClient.disconnect();
42+
await new Promise((resolve, reject) => {
43+
this.basicClient.onDisconnect(() => resolve(null));
44+
// 15 second timeout
45+
setTimeout(() => reject("Timeout for PubSub disconnection was exceeded"), 15000);
46+
});
2947
}
3048
}

services/nodecg-io-twitch-pubsub/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"dependencies": {
4040
"nodecg-io-core": "^0.3.0",
4141
"nodecg-io-twitch-auth": "^0.3.0",
42-
"@twurple/api": "^5.3.4",
43-
"@twurple/pubsub": "^5.3.4"
42+
"@twurple/api": "^7.2.0",
43+
"@twurple/pubsub": "^7.2.0"
4444
}
4545
}

utils/nodecg-io-twitch-auth/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
"typescript": "^5.4.3"
2222
},
2323
"dependencies": {
24-
"@twurple/auth": "^5.3.4"
24+
"@twurple/auth": "^7.2.0"
2525
}
2626
}

0 commit comments

Comments
 (0)