back to websockets (webhooks are a pain), new websocket EventSub fixer

This commit is contained in:
2025-07-04 16:36:06 +02:00
parent d4c5b29fe1
commit c0fc8bccf2
5 changed files with 46 additions and 57 deletions

View File

@@ -1,7 +1,4 @@
import { eventSub, streamerApi } from "..";
await streamerApi.eventSub.deleteAllSubscriptions();
console.info('Succesfully deleted all unused EventSub subscriptions');
import { eventSub, streamerApi, streamerId } from "..";
eventSub.onRevoke(event => {
console.info(`Successfully revoked EventSub subscription: ${event.id}`);
@@ -9,6 +6,7 @@ eventSub.onRevoke(event => {
eventSub.onSubscriptionCreateSuccess(event => {
console.info(`Successfully created EventSub subscription: ${event.id}`);
deleteDuplicateSubscriptions.refresh();
});
eventSub.onSubscriptionCreateFailure(event => {
@@ -32,3 +30,36 @@ for (const file of files) {
};
eventSub.start();
import { getAuthRecord } from "../db/dbAuth";
import { StaticAuthProvider } from "@twurple/auth";
import { ApiClient, HelixEventSubSubscription } from "@twurple/api";
const deleteDuplicateSubscriptions = setTimeout(async () => {
console.info('Deleting all double subscriptions');
const tokendata = await streamerApi.getTokenInfo();
const authdata = await getAuthRecord(streamerId, []);
const tempauth = new StaticAuthProvider(tokendata.clientId, authdata?.accesstoken.accessToken!);
const tempapi: ApiClient = new ApiClient({ authProvider: tempauth });
console.info('Created the temporary API client');
const subs = await tempapi.eventSub.getSubscriptionsForStatus('enabled');
const seen = new Map();
const duplicates: HelixEventSubSubscription[] = [];
for (const sub of subs.data) {
if (seen.has(sub.type)) {
if (!duplicates.some(o => o.type === sub.type)) {
duplicates.push(seen.get(sub.type));
};
} else {
seen.set(sub.type, sub);
};
};
for (const sub of duplicates) {
await tempapi.eventSub.deleteSubscription(sub.id);
console.info(`Deleted sub: id: ${sub.id}, type: ${sub.type}`);
};
if (duplicates.length === 0) console.info('No duplicate subscriptions found');
console.info('Removed temporary API client');
}, 5000);

View File

@@ -1,6 +1,6 @@
import { createAuthProvider } from "./auth";
import { ApiClient } from "@twurple/api";
import { EventSubHttpListener, ReverseProxyAdapter } from "@twurple/eventsub-http";
import { EventSubWsListener } from "@twurple/eventsub-ws";
import { addAdmin } from "./lib/admins";
const CHATTERINTENTS = ["user:read:chat", "user:write:chat", "user:bot"];
@@ -12,11 +12,6 @@ if (chatterId === "") { console.error('Please set a CHATTER_ID in the .env'); pr
export const streamerId = process.env.STREAMER_ID ?? "";
if (streamerId === "") { console.error('Please set a STREAMER_ID in the .env'); process.exit(1); };
const hostName = process.env.EVENTSUB_HOSTNAME ?? "";
if (hostName === "") { console.error('Please set a EVENTSUB_HOSTNAME in the .env'); process.exit(1); };
const port = Number(process.env.EVENTSUB_PORT) ?? 0;
if (port === 0) { console.error('Please set a EVENTSUB_PORT in the .env'); process.exit(1); };
export const chatterAuthProvider = await createAuthProvider(chatterId, singleUserMode ? CHATTERINTENTS.concat(STREAMERINTENTS) : CHATTERINTENTS);
export const streamerAuthProvider = singleUserMode ? undefined : await createAuthProvider(streamerId, STREAMERINTENTS, true);
@@ -27,11 +22,7 @@ export const chatterApi = new ApiClient({ authProvider: chatterAuthProvider });
export const streamerApi = streamerAuthProvider ? new ApiClient({ authProvider: streamerAuthProvider }) : chatterApi; // if there is no streamer user, use the chatter user
/** As the streamerApi has either the streamer or the chatter if the chatter IS the streamer this has streamer permissions */
export const eventSub = new EventSubHttpListener({
apiClient: streamerApi,
adapter: new ReverseProxyAdapter({ hostName, port }),
secret: Bun.randomUUIDv7()
});
export const eventSub = new EventSubWsListener({ apiClient: streamerApi });
export const commandPrefix = process.env.COMMAND_PREFIX ?? "!";