// ── The voice the module's own lines are said in (phase 17, D140) ────────── // // One fleet setting (§33.4 reading 5): the name of a permission group that has // a BetterChat style, or nothing for plain chat. News lines and `rust.announce` // lines are then said in that group's title and colours — composed here, said by // our plugin with no player as the sender, so they look right whether or not // BetterChat is loaded. // // The style is read at the moment a line is said, never copied into the // setting: an operator who recolours the group changes the voice with it, and a // group whose style is taken away stops being a voice rather than leaving a // stale one behind. const settingsDb = require('../visibility/visibility.db') const chatStyle = require('./chatStyle') const db = require('./permissions.db') const model = require('./permissions.model') const VOICE_KEY = 'announce.voice' /** The chosen group's name, or '' for plain chat. */ async function chosen() { return (await settingsDb.getSetting(VOICE_KEY)) || '' } /** * The format a line is said in right now, or null for plain chat — which is * also the answer when the chosen group has since lost its style or gone. */ async function currentFormat() { const group = await chosen() if (!group) return null const fields = await db.getGroupChat(group) return fields ? chatStyle.voiceFormat(fields) : null } /** The setting, and every group that could be a voice, for the admin page. */ async function describe() { const [voice, rows] = await Promise.all([chosen(), db.listGroupChat()]) const options = [] for (const [group, fields] of model.chatByGroup(rows)) { const format = chatStyle.voiceFormat(fields) if (format) options.push({ group, title: fields.Title || group, format }) } return { voice, options: options.sort((a, b) => a.group.localeCompare(b.group)) } } /** * Choose the voice. A group is accepted only when it has a style a voice can be * made from; '' goes back to plain chat. Resolves `{ ok }` or * `{ ok: false, message }`. */ async function choose(group, userId = null) { const name = model.normaliseName(group) if (name) { const fields = await db.getGroupChat(name) if (!fields || !chatStyle.voiceFormat(fields)) { return { ok: false, message: `The group "${name}" has no chat style, so it cannot be a voice. Give it one under Permissions first.` } } } await settingsDb.setSetting(VOICE_KEY, name, userId) return { ok: true, voice: name } } module.exports = { VOICE_KEY, chosen, currentFormat, describe, choose }