Wire it up: Twitch events, dashboard, overlay parts, layout editor removed

Twitch / server
- bits via channel.bits.use (cheers + Power-ups, replaces channel.cheer); built-in
  channel point rewards; polls; Hype Train v2; outgoing raids. New scopes:
  channel:read:polls, channel:read:hype_train, channel:manage:raids (log in again).
- Hype Train: alerts on start / each new level / end, plus the progress bar.
- Raid send-off: start/cancel raids from the dashboard (Helix /raids); the card
  stays until channel.raid reports the raid happened.
- StreamElements client started on boot; tip alerts.

Dashboard
- Twitch + StreamElements status, test buttons (poll, Hype Train bar, raid card),
  Raid box, activity Reload + auto-refresh, Redeems in the nav.
- Two-click delete instead of browser confirm() on Media and Actions.

Overlay
- ?parts= splits the overlay into OBS sources: chat, alerts, poll, hypebar, raid,
  or exclusions like ?parts=nochat. Recommended: ?parts=chat (audio Monitor Only,
  chat sounds stay off stream) + ?parts=nochat.
- Now playing switched off (commented, see CLAUDE.md to re-enable).

Removed the layout editor and preview page; chat position/style is baked into
overlay.ejs from the last saved layout (overlay_layout table kept, unused).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-20 23:18:15 +02:00
parent b28e56f991
commit cdcd3737a7
17 changed files with 589 additions and 140 deletions

View file

@ -20,6 +20,9 @@ function legacy(alert) {
case 'gift': return ['gift_sub', alert.tier];
case 'bits': return ['bits', String(alert.bits)];
case 'streak': return ['watch_streak', String(alert.streak)];
case 'hype': return ['hype_train', alert.phase];
case 'tip': return ['tip', `${alert.amount} ${alert.currency}`];
case 'redeem': return [{ reward: 'channel_points', auto_reward: 'auto_reward', power_up: 'power_up' }[alert.kind], alert.key];
default: return [alert.type, null];
}
}
@ -44,6 +47,11 @@ function fromCheer(e) {
return { type: 'bits', user: e.is_anonymous ? null : (e.user_name || e.user_login), bits: e.bits, message: e.message || null };
}
// channel.bits.use with type 'cheer' (Power-ups are handled by redeems.js)
function fromBitsUse(e) {
return { type: 'bits', user: e.user_name || e.user_login || null, bits: e.bits, message: text(e.message) };
}
// channel.chat.notification: one event type that carries all sub/gift/raid details
// (Prime flag, months bought at once, gift recipient, resub message, community gift totals).
function fromChatNotification(e) {
@ -72,6 +80,77 @@ function fromChatNotification(e) {
}
}
// --- StreamElements tip (Astro channel.tips message data) ---
function fromTip(d) {
const t = d.donation || {};
return { type: 'tip', user: t.user?.username || 'Someone', amount: Number(t.amount) || 0, currency: t.currency || 'USD',
message: (t.message || '').trim() || null };
}
// --- Hype Train (v2): alert on start, on each NEW level, and at the end ---
const hypeLevels = new Map(); // train id -> last announced level
function fromHypeTrain(phase, e) {
const base = { type: 'hype', golden: e.type === 'golden_kappa', treasure: e.type === 'treasure', shared: !!e.is_shared_train, level: e.level };
if (phase === 'begin') {
hypeLevels.set(e.id, e.level);
return { ...base, phase: 'begin' };
}
if (phase === 'progress') {
if ((hypeLevels.get(e.id) || 0) >= e.level) return null; // every contribution sends progress: only level-ups alert
hypeLevels.set(e.id, e.level);
return { ...base, phase: 'level' };
}
hypeLevels.delete(e.id);
const top = (e.top_contributions || []).map(c => c.user_name || c.user_login).filter(Boolean);
return { ...base, phase: 'end', top: [...new Set(top)].slice(0, 3) };
}
// --- Hype Train progress bar (persistent while the train runs, outside the queue) ---
function sendHypeBar(phase, e) {
ws.broadcast({ type: 'hype_bar', phase, train: {
id: e.id, level: e.level, progress: e.progress ?? null, goal: e.goal ?? null, total: e.total,
expires_at: e.expires_at || null, golden: e.type === 'golden_kappa', treasure: e.type === 'treasure' } });
}
// --- outgoing raid send-off card (persistent, silent, outside the queue) ---
// Twitch has no "raid started" event: the card appears when the raid is started from the dashboard
// (routers/alerts.js -> Helix POST /raids) and stays until channel.raid reports the raid happened.
let pendingRaid = null; // { target, avatar, viewers, started_at }
function raidCard(phase, raid) { ws.broadcast({ type: 'raid_card', phase, raid }); }
function raidStarted(raid) { pendingRaid = { ...raid, started_at: new Date().toISOString() }; raidCard('start', pendingRaid); }
function raidCancelled() { if (pendingRaid) raidCard('cancel', pendingRaid); pendingRaid = null; }
function getPendingRaid() { return pendingRaid; }
// channel.raid from us: the raid happened (started from the dashboard or straight on Twitch)
async function raidHappened(e) {
let raid = pendingRaid;
if (!raid) { // started on Twitch directly: show the card briefly
let avatar = null;
try {
const res = await require('./twitch').helix(`/users?id=${encodeURIComponent(e.to_broadcaster_user_id)}`);
if (res.ok) avatar = (await res.json()).data?.[0]?.profile_image_url || null;
} catch { /* fallback art */ }
raid = { target: e.to_broadcaster_user_name || e.to_broadcaster_user_login, avatar };
}
raidCard('done', { ...raid, viewers: e.viewers });
pendingRaid = null;
dispatch.dispatchEvent('raid_out', String(e.viewers), e.to_broadcaster_user_name, e);
}
// --- polls: a live card on the overlay, outside the alert queue ---
function pollShape(phase, e) {
return {
id: e.id, title: e.title, status: e.status || null, ends_at: e.ends_at || null,
choices: (e.choices || []).map(c => ({ id: c.id, title: c.title, votes: c.votes || 0 })),
};
}
function sendPoll(phase, e) {
ws.broadcast({ type: 'poll', phase, poll: pollShape(phase, e) });
if (phase !== 'progress') {
const detail = phase === 'end' ? e.status : null;
dispatch.dispatchEvent(`poll_${phase}`, detail, null, e); // activity log (begin + end only)
}
}
// --- watch streaks, reported by the overlay (they only exist as a chat USERNOTICE) ---
const seenNotices = new Map(); // message id -> time; several overlays may report the same notice
function fromOverlayNotice(n) {
@ -105,14 +184,63 @@ const SAMPLES = {
'bits-120': { type: 'bits', user: 'LurkingLynx', bits: 120 },
'bits-200': { type: 'bits', user: 'xX_Lurker_Xx', bits: 200, message: 'RUN' },
'bits-500': { type: 'bits', user: 'BobTheBuilder99', bits: 500, message: 'Tom da Tank says hi' },
'hype-begin':{ type: 'hype', phase: 'begin', level: 1, golden: false },
'hype-level':{ type: 'hype', phase: 'level', level: 3, golden: false },
'hype-end': { type: 'hype', phase: 'end', level: 4, golden: false, top: ['ChickenWhisperer', 'PixelPotato', 'LurkingLynx'] },
'hype-golden':{ type: 'hype', phase: 'begin', level: 1, golden: true },
'tip-5': { type: 'tip', user: 'PixelPotato', amount: 5, currency: 'EUR', message: 'for the hydration fund' },
'tip-25': { type: 'tip', user: 'ChickenWhisperer', amount: 25, currency: 'EUR', message: 'great stream today, have a coffee on me ♡' },
'tip-100': { type: 'tip', user: 'BobTheBuilder99', amount: 100, currency: 'USD', message: null },
};
const TEST_KEYS = [...Object.keys(SAMPLES), 'queue-test'];
const TEST_KEYS = [...Object.keys(SAMPLES), 'queue-test', 'poll-test', 'hype-bar-test', 'raid-card-test'];
// fake Hype Train bar: starts at level 1, fills up, levels up twice, ends
let hypeTimers = [];
function testHypeBar() {
hypeTimers.forEach(clearTimeout); hypeTimers = [];
const t = { id: 'test-' + Date.now(), level: 1, progress: 0, goal: 1000, total: 0, type: 'regular' };
const send = phase => sendHypeBar(phase, { ...t, expires_at: new Date(Date.now() + 300000).toISOString() });
send('begin');
for (let i = 1; i <= 12; i++) hypeTimers.push(setTimeout(() => {
t.progress += 300; t.total += 300;
if (t.progress >= t.goal) { t.progress -= t.goal; t.level++; t.goal += 500; }
send('progress');
}, i * 800));
hypeTimers.push(setTimeout(() => send('end'), 11000));
}
// fake poll: begins, votes come in over ~9 s, then it ends with a winner
let pollTimers = [];
function testPoll() {
pollTimers.forEach(clearTimeout); pollTimers = [];
const ends = new Date(Date.now() + 10000).toISOString();
const poll = { id: 'test-' + Date.now(), title: 'Which map next?', ends_at: ends, status: null,
choices: [{ id: 'a', title: 'Customs', votes: 0 }, { id: 'b', title: 'Woods', votes: 0 }, { id: 'c', title: 'Reserve', votes: 0 }, { id: 'd', title: 'Factory (chaos)', votes: 0 }] };
const send = phase => ws.broadcast({ type: 'poll', phase, poll: JSON.parse(JSON.stringify(poll)) });
send('begin');
for (let i = 1; i <= 8; i++) pollTimers.push(setTimeout(() => {
poll.choices.forEach((c, k) => { c.votes += Math.floor(Math.random() * (k === 2 ? 6 : 4)); });
send('progress');
}, i * 1000));
pollTimers.push(setTimeout(() => { poll.status = 'completed'; send('end'); }, 10000));
}
function sendTestAlert(alert) {
ws.broadcast({ type: 'alert', alert: { ...alert, test: true } });
}
// Test alerts go straight to the overlay; they are not logged and do not fire actions.
// test:true makes the overlay drop what it is showing (fast checking);
// test:true replaces another test alert at once (fast checking) but never cuts off a real alert;
// 'queue-test' sends four alerts that queue like real ones.
function sendTest(key) {
if (key === 'poll-test') { testPoll(); return true; }
if (key === 'hype-bar-test') { testHypeBar(); return true; }
if (key === 'raid-card-test') { // countdown card, then "raid sent" after 12 s
raidCard('start', { target: 'CozyStreamer', avatar: null, viewers: 42, started_at: new Date().toISOString() });
setTimeout(() => raidCard('done', { target: 'CozyStreamer', avatar: null, viewers: 42 }), 12000);
return true;
}
if (key === 'queue-test') {
ws.broadcast({ type: 'alert', alert: { ...SAMPLES['follow'], test: true } });
for (const k of ['bits-60', 'gift-10', 'streak']) ws.broadcast({ type: 'alert', alert: { ...SAMPLES[k], test: 'queue' } });
@ -124,4 +252,4 @@ function sendTest(key) {
return true;
}
module.exports = { send, sendTest, SAMPLES, TEST_KEYS, fromFollow, fromCheer, fromChatNotification, fromOverlayNotice };
module.exports = { send, sendTest, sendTestAlert, sendPoll, sendHypeBar, raidStarted, raidCancelled, raidHappened, getPendingRaid, fromHypeTrain, fromTip, SAMPLES, TEST_KEYS, fromFollow, fromCheer, fromBitsUse, fromChatNotification, fromOverlayNotice };