69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
import { TEMPLATES } from './ack-templates.mjs';
|
||
|
||
function pickRandom(arr, randomEnabled) {
|
||
if (!randomEnabled || arr.length === 1) return arr[0];
|
||
return arr[Math.floor(Math.random() * arr.length)];
|
||
}
|
||
|
||
function withNickname(text, nickname, nicknameEnabled) {
|
||
if (!nicknameEnabled || !nickname) return text;
|
||
return `${nickname},${text}`;
|
||
}
|
||
|
||
function buildText(pool, ctx) {
|
||
const text = pickRandom(pool, ctx.randomEnabled);
|
||
return withNickname(text, ctx.nickname, ctx.nicknameEnabled);
|
||
}
|
||
|
||
const ImageBuilder = {
|
||
priority: 80,
|
||
support: (ctx) => ctx.msgType === 'image',
|
||
build: (ctx) => buildText(TEMPLATES.image, ctx),
|
||
};
|
||
|
||
const FileBuilder = {
|
||
priority: 80,
|
||
support: (ctx) => ctx.msgType === 'file',
|
||
build: (ctx) => buildText(TEMPLATES.file, ctx),
|
||
};
|
||
|
||
const VoiceBuilder = {
|
||
priority: 80,
|
||
support: (ctx) => ctx.msgType === 'voice',
|
||
build: (ctx) => buildText(TEMPLATES.voice, ctx),
|
||
};
|
||
|
||
const LocationBuilder = {
|
||
priority: 80,
|
||
support: (ctx) => ctx.msgType === 'location',
|
||
build: (ctx) => buildText(TEMPLATES.location, ctx),
|
||
};
|
||
|
||
const LinkBuilder = {
|
||
priority: 80,
|
||
support: (ctx) => ctx.msgType === 'link',
|
||
build: (ctx) => buildText(TEMPLATES.link, ctx),
|
||
};
|
||
|
||
const IntentBuilder = {
|
||
priority: 50,
|
||
support: (ctx) => {
|
||
const task = ctx.intent?.task;
|
||
return task && task !== 'unknown' && TEMPLATES.intent[task];
|
||
},
|
||
build: (ctx) => buildText(TEMPLATES.intent[ctx.intent.task], ctx),
|
||
};
|
||
|
||
const DefaultBuilder = {
|
||
priority: 0,
|
||
support: () => true,
|
||
build: (ctx) => buildText(TEMPLATES.default, ctx),
|
||
};
|
||
|
||
const BUILDERS = [ImageBuilder, FileBuilder, VoiceBuilder, LocationBuilder, LinkBuilder, IntentBuilder, DefaultBuilder]
|
||
.sort((a, b) => b.priority - a.priority);
|
||
|
||
export function selectBuilder(ctx) {
|
||
return BUILDERS.find((b) => b.support(ctx)) ?? DefaultBuilder;
|
||
}
|