Update gitignore

This commit is contained in:
Bashy 2026-11-20 18:53:12 +01:00
commit 9e0ddb8182
32 changed files with 3863 additions and 0 deletions

118
src/eventsub.js Normal file
View file

@ -0,0 +1,118 @@
const { WebSocket } = require('ws');
const dispatch = require('./dispatch');
const EVENTSUB_URL = 'wss://eventsub.wss.twitch.tv/ws';
const CLIENT_ID = process.env.TWITCH_CLIENT_ID;
let ws = null;
let shouldRun = false;
let sessionId = null;
let _accessToken = null;
let _broadcasterId = null;
function start(accessToken, broadcasterId) {
_accessToken = accessToken;
_broadcasterId = broadcasterId;
shouldRun = true;
connect();
console.log(`[eventsub] Starting — broadcaster: ${broadcasterId}, base_url: ${process.env.APP_BASE_URL}`);
}
function stop() {
shouldRun = false;
if (ws) ws.close();
console.log('[eventsub] Stopped');
}
function connect() {
console.log('[eventsub] Connecting...');
ws = new WebSocket(EVENTSUB_URL);
ws.on('open', () => console.log('[eventsub] Connected to Twitch EventSub'));
ws.on('message', (raw) => handleMessage(JSON.parse(raw)));
ws.on('close', () => {
console.log('[eventsub] Disconnected');
if (shouldRun) setTimeout(connect, 10_000);
});
ws.on('error', (e) => console.error('[eventsub] Error:', e.message));
}
async function handleMessage(msg) {
const type = msg.metadata?.message_type;
if (type === 'session_welcome') {
sessionId = msg.payload.session.id;
console.log('[eventsub] Session established:', sessionId);
await subscribeAll();
} else if (type === 'session_reconnect') {
console.log('[eventsub] Reconnect requested');
ws.close();
} else if (type === 'notification') {
handleNotification(msg.payload);
}
}
async function subscribeAll() {
const subs = [
{ type: 'channel.subscribe', version: '1', condition: { broadcaster_user_id: _broadcasterId } },
{ type: 'channel.subscription.message', version: '1', condition: { broadcaster_user_id: _broadcasterId } },
{ type: 'channel.subscription.gift', version: '1', condition: { broadcaster_user_id: _broadcasterId } },
{ type: 'channel.cheer', version: '1', condition: { broadcaster_user_id: _broadcasterId } },
{ type: 'channel.channel_points_custom_reward_redemption.add',version: '1', condition: { broadcaster_user_id: _broadcasterId } },
{ type: 'channel.follow', version: '2', condition: { broadcaster_user_id: _broadcasterId, moderator_user_id: _broadcasterId } },
{ type: 'channel.raid', version: '1', condition: { to_broadcaster_user_id: _broadcasterId } },
];
for (const sub of subs) {
try {
const res = await fetch('https://api.twitch.tv/helix/eventsub/subscriptions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${_accessToken}`,
'Client-Id': CLIENT_ID,
'Content-Type': 'application/json',
},
body: JSON.stringify({ ...sub, transport: { method: 'websocket', session_id: sessionId } }),
});
if (res.status === 202) {
console.log(`[eventsub] Subscribed: ${sub.type}`);
} else {
const body = await res.text();
console.warn(`[eventsub] Subscribe ${sub.type} → ${res.status}: ${body}`);
}
} catch (e) {
console.error(`[eventsub] Subscribe ${sub.type} failed:`, e.message);
}
}
}
function handleNotification(payload) {
const sub = payload.subscription;
const event = payload.event;
const username = event.user_login || event.from_broadcaster_user_login || null;
console.log(`[eventsub] Notification: ${sub.type} — user: ${username}`);
switch (sub.type) {
case 'channel.subscribe':
case 'channel.subscription.message':
dispatch.dispatchEvent('sub', event.tier, username, event);
break;
case 'channel.subscription.gift':
dispatch.dispatchEvent('gift_sub', event.tier, username, event);
break;
case 'channel.cheer':
dispatch.dispatchEvent('bits', String(event.bits), username, event);
break;
case 'channel.channel_points_custom_reward_redemption.add':
dispatch.dispatchEvent('channel_points', event.reward?.id, username, event);
break;
case 'channel.follow':
dispatch.dispatchEvent('follow', null, username, event);
break;
case 'channel.raid':
dispatch.dispatchEvent('raid', null, username, event);
break;
}
}
module.exports = { start, stop };