From 9e356b087584f5d5c47f116b6bba1e367b046243 Mon Sep 17 00:00:00 2001 From: Bashy Date: Sat, 21 Nov 2026 15:09:12 +0200 Subject: [PATCH] TTS blocklist: starter list of slurs/TOS phrases, whole-word matching - src/tts-blocklist.txt: slurs, self-harm/threat phrases, sexual violence, hate slogans (no normal swearing); merged into Settings once (tts_blocklist_seeded), editable there afterwards - Matching is now whole words/phrases with plurals, look-alike swaps and spaced-out letters, so raccoon / spicy / therapist / grape are fine Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 5 ++-- src/db.js | 17 ++++++++++++- src/tts-blocklist.txt | 56 +++++++++++++++++++++++++++++++++++++++++++ src/tts.js | 21 ++++++++++------ views/settings.ejs | 2 +- 5 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 src/tts-blocklist.txt diff --git a/CLAUDE.md b/CLAUDE.md index 8d44027..c58fa05 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -139,8 +139,9 @@ Chat (`?parts=chat`) is `public/chat/chat.css` + `public/chat/chat.js` (window.H Builder page to make named TTS voices, used by redeem variations (Redeems page: "TTS: " per variation). - Redeem with a TTS voice + viewer text: `redeems.addSpeech()` cleans the text (`tts.prepareViewerText`: links out, - repeats collapsed, cut to `settings.tts_max_chars` at a word, `settings.tts_blocklist` -> nothing read, note says - "(message removed)"), synthesizes " says: " (`tts_say_name`), and only then sends the alert with + repeats collapsed, cut to `settings.tts_max_chars` at a word, `settings.tts_blocklist` -> nothing read, letter says + "(message removed)"; whole-word matching with plurals, look-alike swaps and spaced-out letters; starter list + `src/tts-blocklist.txt` is merged into the setting once, `tts_blocklist_seeded`), synthesizes " says: " (`tts_say_name`), and only then sends the alert with `tts` (URL), `tts_ms`, `tts_env` (loudness per 40 ms, `tts.loudness()`) and `tts_prefix` (words said before the message). Shown as the envelope letter (style `tts`, `public/alerts/fx.js` envelope(), CSS in fx.css): flap opens, the letter rises out and settles in front ~80px from the top, words ink in as spoken (timing estimated from tts_env; if the voice diff --git a/src/db.js b/src/db.js index 5085287..9db1ad6 100644 --- a/src/db.js +++ b/src/db.js @@ -12,6 +12,7 @@ function init() { createSchema(); migrate(); seedSettings(); + seedTtsBlocklist(); seedVariations(); console.log(`[db] Initialised — path: ${DB_PATH}`); } @@ -136,6 +137,20 @@ function migrate() { addColumn('settings', 'tts_max_chars', 'INTEGER DEFAULT 200'); // longer redeem text is cut addColumn('settings', 'tts_blocklist', "TEXT DEFAULT ''"); // words/phrases, one per line or comma-separated addColumn('settings', 'tts_say_name', 'INTEGER DEFAULT 1'); // " says: ..." before the message + addColumn('settings', 'tts_blocklist_seeded', 'INTEGER DEFAULT 0'); // starter list copied in once +} + +// Put the starter TTS blocklist (src/tts-blocklist.txt) into the settings ONCE; after that it is the user's list +// (clearing or editing it in Settings is respected). +function seedTtsBlocklist() { + const s = db.prepare('SELECT tts_blocklist, tts_blocklist_seeded FROM settings WHERE id = 1').get(); + if (!s || s.tts_blocklist_seeded) return; + const starter = require('fs').readFileSync(require('path').join(__dirname, 'tts-blocklist.txt'), 'utf8') + .split('\n').map(l => l.trim()).filter(l => l && !l.startsWith('#')); + const mine = String(s.tts_blocklist || '').split(/[\n,]/).map(w => w.trim()).filter(Boolean); + const merged = [...mine, ...starter.filter(w => !mine.some(m => m.toLowerCase() === w.toLowerCase()))]; + db.prepare('UPDATE settings SET tts_blocklist = ?, tts_blocklist_seeded = 1 WHERE id = 1').run(merged.join('\n')); + console.log(`[db] TTS blocklist: added ${merged.length - mine.length} starter words`); } function seedSettings() { @@ -190,7 +205,7 @@ function getSettings() { // what overlay pages get: everything except server-only values (the overlay URL is public) function getOverlaySettings() { - const { tts_blocklist, ...rest } = getSettings(); + const { tts_blocklist, tts_blocklist_seeded, ...rest } = getSettings(); return rest; } diff --git a/src/tts-blocklist.txt b/src/tts-blocklist.txt new file mode 100644 index 0000000..c9090d8 --- /dev/null +++ b/src/tts-blocklist.txt @@ -0,0 +1,56 @@ +# Starter TTS blocklist: slurs and phrases that can get a channel in trouble under Twitch's rules when read aloud. +# Copied ONCE into Settings -> TTS redeems -> Blocked words (on first start after this was added); edit it there. +# Matching (src/tts.js prepareViewerText): whole words, not case-sensitive, plurals (-s/-es/-z) included, +# simple swaps (0->o 1->i 3->e 4->a 5->s 7->t @->a $->s) and spaced-out letters (k y s) caught. +# Normal swearing is deliberately not here. + +# racial / ethnic slurs +nigger +nigga +sandnigger +chink +gook +spic +wetback +beaner +kike +raghead +towelhead +coon +jigaboo +porch monkey +paki + +# homophobic / transphobic +faggot +fag +dyke +tranny +shemale + +# ableist +retard +retarded + +# self-harm / threats +kys +kill yourself +kill urself +kill your self +neck yourself +hang yourself +go die + +# sexual violence / minors +rape +rapist +pedo +pedophile +child porn + +# hate slogans +heil hitler +sieg heil +1488 +white power +gas the jews diff --git a/src/tts.js b/src/tts.js index 1f07c27..a5a7503 100644 --- a/src/tts.js +++ b/src/tts.js @@ -87,7 +87,15 @@ function cleanText(text) { // Returns { text, blocked }. blocked = a blocklist word was found: do not read anything. const LEET = { 0: 'o', 1: 'i', 3: 'e', 4: 'a', 5: 's', 7: 't', '@': 'a', $: 's', '!': 'i' }; function parseBlocklist(list) { - return String(list || '').split(/[\n,]/).map(w => w.trim().toLowerCase()).filter(Boolean); + return String(list || '').split(/[\n,]/).map(w => w.trim().toLowerCase()).filter(w => w && !w.startsWith('#')); +} +// lowercase, look-alike swaps, spaced-out letters joined ("k y s" -> "kys"), only letters/digits, single spaces +function normalizeForMatch(text) { + return ' ' + String(text).toLowerCase() + .replace(/[013457@$!]/g, c => LEET[c] || c) + .replace(/(^|[^a-z0-9\u00c0-\u024f])((?:[a-z][\s.\-_*]+){2,}[a-z])(?![a-z])/g, (_, pre, letters) => pre + letters.replace(/[^a-z]/g, '')) + .replace(/([a-z])\1{2,}/g, '$1') // niiiice -> nice + .replace(/[^a-z0-9\u00c0-\u024f]+/g, ' ').trim() + ' '; } function prepareViewerText(input, { maxChars = 200, blocklist = '' } = {}) { let t = cleanText(input) @@ -98,13 +106,12 @@ function prepareViewerText(input, { maxChars = 200, blocklist = '' } = {}) { .replace(/\s+/g, ' ').trim(); const max = Math.max(20, Math.min(MAX_CHARS, maxChars | 0 || 200)); if (t.length > max) t = t.slice(0, max).replace(/\s+\S*$/, '') + '…'; - const plain = t.toLowerCase().replace(/[013457@$!]/g, c => LEET[c] || c); - const words = ' ' + plain.replace(/[^a-z0-9\u00c0-\u024f]+/g, ' ') + ' '; - const squashed = plain.replace(/[^a-z\u00c0-\u024f]+/g, ''); + // whole words/phrases only (so "raccoon", "spicy", "therapist" are fine), plurals included + const hay = normalizeForMatch(t); const blocked = parseBlocklist(blocklist).some(w => { - const ww = w.replace(/[013457@$!]/g, c => LEET[c] || c); - return ww.length >= 4 ? squashed.includes(ww.replace(/[^a-z\u00c0-\u024f]+/g, '')) // longer words: also when spaced out - : words.includes(' ' + ww.replace(/[^a-z0-9\u00c0-\u024f]+/g, ' ').trim() + ' '); + const term = normalizeForMatch(w).trim(); + if (!term) return false; + return new RegExp(` ${term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?:s|es|z)? `).test(hay); }); return { text: t, blocked }; } diff --git a/views/settings.ejs b/views/settings.ejs index 725fe22..5d4843a 100644 --- a/views/settings.ejs +++ b/views/settings.ejs @@ -74,7 +74,7 @@

Custom CSS / JS