Skip to content

Instantly share code, notes, and snippets.

@timcole
Created December 15, 2020 12:34
Show Gist options
  • Save timcole/e2fe6c7a98c9b8d72f74d7de7f0aecdb to your computer and use it in GitHub Desktop.
Save timcole/e2fe6c7a98c9b8d72f74d7de7f0aecdb to your computer and use it in GitHub Desktop.
Clear all events in Twitch's Eventsub API
import fetch from 'node-fetch';
async function main(): Promise<void> {
let cursor: string | undefined;
do {
const { pagination, total, data } = await getEvents(cursor);
cursor = pagination?.cursor;
console.log(cursor, total, data.length);
await Promise.all(data.map(({ id }: { id: string }) => delEvents(id)));
} while (typeof cursor !== 'undefined');
}
async function getEvents(cursor?: string) {
return await fetch(
`https://api.twitch.tv/helix/eventsub/subscriptions${
cursor ? `?after=${cursor}` : ''
}`,
{
headers: {
Authorization: `Bearer ${process.env.TWITCH_EVENTSUB!}`,
'Client-ID': process.env.TWITCH_CLIENT_ID!,
},
}
).then((data) => data.json());
}
async function delEvents(id: string): Promise<void> {
await fetch(`https://api.twitch.tv/helix/eventsub/subscriptions?id=${id}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${process.env.TWITCH_EVENTSUB!}`,
'Client-ID': process.env.TWITCH_CLIENT_ID!,
},
});
}
main().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment