improve duplicate deleter

This commit is contained in:
2025-07-27 19:16:45 +02:00
parent 15c8abc2c3
commit a340b004a0

View File

@@ -33,35 +33,29 @@ for (const file of files) {
eventSub.start(); eventSub.start();
import { getAuthRecord } from "db/dbAuth"; import { HelixEventSubSubscription } from "@twurple/api";
import { StaticAuthProvider } from "@twurple/auth";
import { ApiClient, HelixEventSubSubscription } from "@twurple/api";
const deleteDuplicateSubscriptions = setTimeout(async () => { const deleteDuplicateSubscriptions = setTimeout(async () => {
logger.info('Deleting all double subscriptions'); logger.info('Deleting all double subscriptions');
const tokendata = await streamerApi.getTokenInfo(); await streamerApi.asUser(streamerId, async tempapi => {
const authdata = await getAuthRecord(streamerId, []); const subs = await tempapi.eventSub.getSubscriptionsForStatus("enabled");
const tempauth = new StaticAuthProvider(tokendata.clientId, authdata?.accesstoken.accessToken!);
const tempapi: ApiClient = new ApiClient({ authProvider: tempauth });
logger.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) { const seen = new Map();
if (seen.has(sub.type)) { const duplicates: HelixEventSubSubscription[] = [];
if (!duplicates.some(o => o.type === sub.type)) {
duplicates.push(seen.get(sub.type)); for (const sub of subs.data) {
if (seen.has(sub.type)) {
duplicates.push(sub);
} else {
seen.set(sub.type, sub);
}; };
} else {
seen.set(sub.type, sub);
}; };
};
for (const sub of duplicates) { for (const sub of duplicates) {
await tempapi.eventSub.deleteSubscription(sub.id); await tempapi.eventSub.deleteSubscription(sub.id);
logger.ok(`Deleted sub: id: ${sub.id}, type: ${sub.type}`); logger.ok(`Deleted sub: id: ${sub.id}, type: ${sub.type}`);
}; };
if (duplicates.length === 0) logger.ok('No duplicate subscriptions found'); if (duplicates.length === 0) logger.ok('No duplicate subscriptions found');
else logger.ok('Deleted all duplicate EventSub subscriptions'); else logger.ok('Deleted all duplicate EventSub subscriptions');
});
}, 5000); }, 5000);