{ "version": 3, "sources": ["../../../server/chat-plugins/daily-spotlight.ts"], "sourcesContent": ["import { FS, Utils } from '../../lib';\n\nconst DAY = 24 * 60 * 60 * 1000;\nconst SPOTLIGHT_FILE = 'config/chat-plugins/spotlights.json';\nconst NUMBER_REGEX = /^\\s*[0-9]+\\s*$/;\n\n/** legacy - string = just url, arr is [url, width, height] */\ntype StoredImage = string | [string, number, number];\n\ninterface Spotlight {\n\timage?: StoredImage;\n\tdescription: string;\n\ttime: number;\n}\n\nexport let spotlights: {\n\t[roomid: string]: { [k: string]: Spotlight[] },\n} = {};\n\ntry {\n\tspotlights = JSON.parse(FS(SPOTLIGHT_FILE).readIfExistsSync() || \"{}\");\n\tfor (const roomid in spotlights) {\n\t\tfor (const k in spotlights[roomid]) {\n\t\t\tfor (const spotlight of spotlights[roomid][k]) {\n\t\t\t\tif (!spotlight.time) {\n\t\t\t\t\tspotlight.time = Date.now();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n} catch (e: any) {\n\tif (e.code !== 'MODULE_NOT_FOUND' && e.code !== 'ENOENT') throw e;\n}\nif (!spotlights || typeof spotlights !== 'object') spotlights = {};\n\nfunction saveSpotlights() {\n\tFS(SPOTLIGHT_FILE).writeUpdate(() => JSON.stringify(spotlights));\n}\n\nfunction nextDaily() {\n\tfor (const roomid in spotlights) {\n\t\tfor (const key in spotlights[roomid]) {\n\t\t\tif (spotlights[roomid][key].length > 1) {\n\t\t\t\tspotlights[roomid][key].shift();\n\t\t\t}\n\t\t}\n\t}\n\n\tsaveSpotlights();\n\ttimeout = setTimeout(nextDaily, DAY);\n}\n\nconst midnight = new Date();\nmidnight.setHours(24, 0, 0, 0);\nlet timeout = setTimeout(nextDaily, midnight.getTime() - Date.now());\n\nexport async function renderSpotlight(roomid: RoomID, key: string, index: number) {\n\tlet imgHTML = '';\n\tconst { image, description } = spotlights[roomid][key][index];\n\n\tif (image) {\n\t\tif (Array.isArray(image)) {\n\t\t\timgHTML = `
${Chat.formatText(description, true)} | ${imgHTML}
This room has no daily spotlights.
${key}: | `;\n\t\t\t\tconst keys = Utils.sortBy(spotlights[room.roomid][key].slice(), spotlight => {\n\t\t\t\t\tswitch (sortType) {\n\t\t\t\t\tcase 'time':\n\t\t\t\t\t\treturn -spotlight.time;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\treturn spotlight.description;\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tfor (const [i] of keys.entries()) {\n\t\t\t\t\tconst html = await renderSpotlight(room.roomid, key, i);\n\t\t\t\t\tbuf += `|
---|---|
${i ? i : 'Current'} | ${html} |
/setdaily ${key},${image ? `${image},` : ''}${code}
/daily [name]
: shows the daily spotlight.!daily [name]
: shows the daily spotlight to everyone. Requires: + % @ # ~/setdaily [name], [image], [description]
: sets the daily spotlight. Image can be left out. Requires: % @ # ~/queuedaily [name], [image], [description]
: queues a daily spotlight. At midnight, the spotlight with this name will automatically switch to the next queued spotlight. Image can be left out. Requires: % @ # ~/queuedailyat [name], [queue number], [image], [description]
: inserts a daily spotlight into the queue at the specified number (starting from 1). Requires: % @ # ~/replacedaily [name], [queue number], [image], [description]
: replaces the daily spotlight queued at the specified number. Requires: % @ # ~/removedaily [name][, queue number]
: if no queue number is provided, deletes all queued and current spotlights with the given name. If a number is provided, removes a specific future spotlight from the queue. Requires: % @ # ~/swapdaily [name], [queue number], [queue number]
: swaps the two queued spotlights at the given queue numbers. Requires: % @ # ~/viewspotlights [sorter]
: shows all current spotlights in the room. For staff, also shows queued spotlights.` +\n\t\t\t`[sorter] can either be unset, 'time', or 'alphabet'. These sort by either the time added, or alphabetical order.` +\n\t\t\t`