Reward / Power-up icon as a top-left sticker on the cassette and the video box

Twitch icons of channel point rewards (custom images) and Custom Power-ups are cached
(10 min, refreshed on unknown triggers, never holding an alert more than 2 s) and
attached as alert.icon; cassette.js and video.js show it as a white-outlined sticker
on the top-left corner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-22 16:14:39 +02:00
parent b3caac7a31
commit 8ac8fe024c
6 changed files with 63 additions and 7 deletions

View file

@ -93,6 +93,41 @@ async function addSpeech(v, alert) {
}
}
// Twitch icons of channel point rewards (custom images only) and Custom Power-ups.
// alert.icon -> top-left sticker on the cassette (cassette.js) and the video box (video.js).
// Cached; reloaded every 10 min, or (at most every 30 s) when an unknown trigger comes in.
const icons = new Map();
let iconsAt = 0, iconsLoading = null;
const bestImg = i => (i && (i.url_4x || i.url_2x || i.url_1x)) || null;
async function loadIcons() {
const twitch = require('./twitch');
const u = twitch.getUser();
if (!u) return;
const next = new Map();
try {
const r = await twitch.helix(`/channel_points/custom_rewards?broadcaster_id=${u.twitch_id}`);
if (r.ok) for (const x of (await r.json()).data || []) { const img = bestImg(x.image); if (img) next.set(`reward|${x.id}`, img); }
} catch (e) { console.warn('[redeems] reward icons:', e.message); }
try {
const r = await twitch.helix(`/bits/custom_power_ups?broadcaster_id=${u.twitch_id}`);
if (r.ok) for (const x of (await r.json()).data || []) {
const img = bestImg(x.image) || bestImg(x.icon) || bestImg(x.default_image) || x.image_url || null;
if (img) next.set(`custom_power_up|${x.id}`, img);
}
} catch (e) { console.warn('[redeems] power-up icons:', e.message); }
if (next.size || !icons.size) { icons.clear(); for (const [k, v] of next) icons.set(k, v); }
iconsAt = Date.now();
}
async function iconFor(kind, key) {
if (kind !== 'reward' && kind !== 'custom_power_up') return null;
const age = Date.now() - iconsAt;
if (age > 10 * 60000 || (!icons.has(`${kind}|${key}`) && age > 30000)) {
iconsLoading = iconsLoading || loadIcons().finally(() => { iconsLoading = null; });
await Promise.race([iconsLoading, new Promise(r => setTimeout(r, 2000))]); // never hold an alert long
}
return icons.get(`${kind}|${key}`) || null;
}
// legacy action event type for the actions system / activity log
const LEGACY = { reward: 'channel_points', auto_reward: 'auto_reward', power_up: 'power_up', custom_power_up: 'custom_power_up' };
@ -106,8 +141,9 @@ function handle(kind, key, title, { user, input, emote, bits }, raw) {
return null;
}
const alert = toAlert(v, { user, input, emote, bits });
if (v.tts_voice_id && input && !v.video) addSpeech(v, alert).then(() => alerts.send(alert, raw)); // speech first, then the alert
else alerts.send(alert, raw);
const ready = iconFor(kind, key).then(icon => { alert.icon = icon; });
if (v.tts_voice_id && input && !v.video) ready.then(() => addSpeech(v, alert)).then(() => alerts.send(alert, raw)); // speech first, then the alert
else ready.then(() => alerts.send(alert, raw));
return alert;
}
@ -117,6 +153,7 @@ async function rebuild(kind, key, { user, input, emote, bits }) {
const v = pick(db.getVariationsFor(kind, key));
if (!v) return null;
const alert = toAlert(v, { user, input, emote, bits });
alert.icon = await iconFor(kind, key);
if (v.tts_voice_id && input && !v.video) await addSpeech(v, alert);
return alert;
}
@ -138,8 +175,9 @@ function test({ id, kind, key }) {
: v.kind === 'reward' ? 'test input' : null;
const alert = toAlert({ ...v, enabled: 1 }, { user: 'ChickenWhisperer', input, emote: v.kind === 'power_up' ? '25' : null,
bits: v.kind === 'custom_power_up' ? 50 : null }); // 25 = Kappa
if (v.tts_voice_id && !v.video) addSpeech(v, alert).then(() => alerts.sendTestAlert(alert));
else alerts.sendTestAlert(alert);
const ready = iconFor(v.kind, v.key).then(icon => { alert.icon = icon; });
if (v.tts_voice_id && !v.video) ready.then(() => addSpeech(v, alert)).then(() => alerts.sendTestAlert(alert));
else ready.then(() => alerts.sendTestAlert(alert));
return true;
}