Add enum provider for /emote

This commit is contained in:
Cohee
2024-06-15 18:21:44 +03:00
parent 3ca6795cde
commit 101c735d91

View File

@ -10,6 +10,7 @@ import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
import { SlashCommand } from '../../slash-commands/SlashCommand.js'; import { SlashCommand } from '../../slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument } from '../../slash-commands/SlashCommandArgument.js'; import { ARGUMENT_TYPE, SlashCommandArgument } from '../../slash-commands/SlashCommandArgument.js';
import { isFunctionCallingSupported } from '../../openai.js'; import { isFunctionCallingSupported } from '../../openai.js';
import { SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js';
export { MODULE_NAME }; export { MODULE_NAME };
const MODULE_NAME = 'expressions'; const MODULE_NAME = 'expressions';
@ -87,6 +88,7 @@ function getFallbackExpression() {
*/ */
function toggleTalkingHeadCommand(_) { function toggleTalkingHeadCommand(_) {
setTalkingHeadState(!extension_settings.expressions.talkinghead); setTalkingHeadState(!extension_settings.expressions.talkinghead);
return String(extension_settings.expressions.talkinghead);
} }
function isVisualNovelMode() { function isVisualNovelMode() {
@ -914,6 +916,7 @@ async function setSpriteSetCommand(_, folder) {
// moduleWorker(); // moduleWorker();
const vnMode = isVisualNovelMode(); const vnMode = isVisualNovelMode();
await sendExpressionCall(folder, lastExpression, true, vnMode); await sendExpressionCall(folder, lastExpression, true, vnMode);
return '';
} }
async function classifyCommand(_, text) { async function classifyCommand(_, text) {
@ -935,7 +938,7 @@ async function classifyCommand(_, text) {
async function setSpriteSlashCommand(_, spriteId) { async function setSpriteSlashCommand(_, spriteId) {
if (!spriteId) { if (!spriteId) {
console.log('No sprite id provided'); console.log('No sprite id provided');
return; return '';
} }
spriteId = spriteId.trim().toLowerCase(); spriteId = spriteId.trim().toLowerCase();
@ -955,7 +958,7 @@ async function setSpriteSlashCommand(_, spriteId) {
if (!spriteItem) { if (!spriteItem) {
console.log('No sprite found for search term ' + spriteId); console.log('No sprite found for search term ' + spriteId);
return; return '';
} }
label = spriteItem.label; label = spriteItem.label;
@ -963,6 +966,7 @@ async function setSpriteSlashCommand(_, spriteId) {
const vnMode = isVisualNovelMode(); const vnMode = isVisualNovelMode();
await sendExpressionCall(spriteFolderName, label, true, vnMode); await sendExpressionCall(spriteFolderName, label, true, vnMode);
return label;
} }
/** /**
@ -1330,10 +1334,18 @@ async function renderFallbackExpressionPicker() {
} }
} }
function getCachedExpressions() {
if (!Array.isArray(expressionsList)) {
return [];
}
return [...expressionsList, ...extension_settings.expressions.custom].filter(onlyUnique);
}
async function getExpressionsList() { async function getExpressionsList() {
// Return cached list if available // Return cached list if available
if (Array.isArray(expressionsList)) { if (Array.isArray(expressionsList)) {
return [...expressionsList, ...extension_settings.expressions.custom].filter(onlyUnique); return getCachedExpressions();
} }
/** /**
@ -2037,17 +2049,23 @@ function migrateSettings() {
}); });
eventSource.on(event_types.MOVABLE_PANELS_RESET, updateVisualNovelModeDebounced); eventSource.on(event_types.MOVABLE_PANELS_RESET, updateVisualNovelModeDebounced);
eventSource.on(event_types.GROUP_UPDATED, updateVisualNovelModeDebounced); eventSource.on(event_types.GROUP_UPDATED, updateVisualNovelModeDebounced);
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'sprite', SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'sprite',
aliases: ['emote'], aliases: ['emote'],
callback: setSpriteSlashCommand, callback: setSpriteSlashCommand,
unnamedArgumentList: [ unnamedArgumentList: [
new SlashCommandArgument( SlashCommandArgument.fromProps({
'spriteId', [ARGUMENT_TYPE.STRING], true, description: 'spriteId',
), typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
enumProvider: () => getCachedExpressions().map((x) => new SlashCommandEnumValue(x)),
}),
], ],
helpString: 'Force sets the sprite for the current character.', helpString: 'Force sets the sprite for the current character.',
returns: 'label',
})); }));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'spriteoverride', SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'spriteoverride',
aliases: ['costume'], aliases: ['costume'],
callback: setSpriteSetCommand, callback: setSpriteSetCommand,
unnamedArgumentList: [ unnamedArgumentList: [
@ -2057,8 +2075,9 @@ function migrateSettings() {
], ],
helpString: 'Sets an override sprite folder for the current character. If the name starts with a slash or a backslash, selects a sub-folder in the character-named folder. Empty value to reset to default.', helpString: 'Sets an override sprite folder for the current character. If the name starts with a slash or a backslash, selects a sub-folder in the character-named folder. Empty value to reset to default.',
})); }));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'lastsprite', SlashCommandParser.addCommandObject(SlashCommand.fromProps({
callback: (_, value) => lastExpression[value.trim()] ?? '', name: 'lastsprite',
callback: (_, value) => lastExpression[String(value).trim()] ?? '',
returns: 'sprite', returns: 'sprite',
unnamedArgumentList: [ unnamedArgumentList: [
new SlashCommandArgument( new SlashCommandArgument(
@ -2067,12 +2086,15 @@ function migrateSettings() {
], ],
helpString: 'Returns the last set sprite / expression for the named character.', helpString: 'Returns the last set sprite / expression for the named character.',
})); }));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'th', SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'th',
callback: toggleTalkingHeadCommand, callback: toggleTalkingHeadCommand,
aliases: ['talkinghead'], aliases: ['talkinghead'],
helpString: 'Character Expressions: toggles <i>Image Type - talkinghead (extras)</i> on/off.', helpString: 'Character Expressions: toggles <i>Image Type - talkinghead (extras)</i> on/off.',
returns: ARGUMENT_TYPE.BOOLEAN,
})); }));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'classify', SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'classify',
callback: classifyCommand, callback: classifyCommand,
unnamedArgumentList: [ unnamedArgumentList: [
new SlashCommandArgument( new SlashCommandArgument(