Respect tags for current char(s) selection - Respect tags for prefer current char(s) - Fix preferCurrentChar not being true by default

ca009dee594f4617964d5a9b6f267f50271b201b

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +18 -12Ignore whitespace
public/scripts/slash-commands.js+18 -12
@@ -182,7 +182,7 @@ export function initDefaultSlashCommands() {
182182 if (args.preferCurrent instanceof SlashCommandClosure || Array.isArray(args.preferCurrent)) throw new Error('preferCurrent cannot be a closure or array');
183183 if (args.quiet instanceof SlashCommandClosure || Array.isArray(args.quiet)) throw new Error('quiet cannot be a closure or array');
184184
185185 const char = findChar({ name: name, filteredByTags: validateArrayArgString(args.tag, 'tag'), preferCurrentChar: isTrueBoolean!isFalseBoolean(args.preferCurrent), quiet: isTrueBoolean(args.quiet) });
186186 return char?.avatar ?? '';
187187 },
188188 returns: 'the avatar key (unique identifier) of the character',
@@ -3229,17 +3229,6 @@ export function validateArrayArg(arg, name, { allowUndefined = true } = {}) {
32293229export function findChar({ name = null, allowAvatar = true, insensitive = true, filteredByTags = null, preferCurrentChar = true, quiet = false } = {}) {
32303230 const matches = (char) => (allowAvatar && char.avatar === name) || (insensitive ? equalsIgnoreCaseAndAccents(char.name, name) : char.name === name);
32313231
3232- // Get the current character(s)
3233- const currentChars = selected_group ? groups.find(group => group.id === selected_group)?.members.map(member => characters.find(char => char.avatar === member)) : [characters[this_chid]];
3234-
3235- // If we have a current char and prefer it, return that if it matches - unless tags are provided, they have precedence
3236- if (preferCurrentChar && !filteredByTags) {
3237- const preferredChar = currentChars.find(matches);
3238- if (preferredChar) {
3239- return preferredChar;
3240- }
3241- }
3242-
32433232 // Filter characters by tags if provided
32443233 let filteredCharacters = characters;
32453234 if (filteredByTags) {
@@ -3249,6 +3238,23 @@ export function findChar({ name = null, allowAvatar = true, insensitive = true,
32493238 });
32503239 }
32513240
3241+ // Get the current character(s)
3242+ /** @type {any[]} */
3243+ const currentChars = selected_group ? groups.find(group => group.id === selected_group)?.members.map(member => filteredCharacters.find(char => char.avatar === member))
3244+ : [filteredCharacters.find(char => characters[this_chid]?.avatar === char.avatar)];
3245+
3246+ // If we have a current char and prefer it, return that if it matches
3247+ if (preferCurrentChar) {
3248+ const preferredCharSearch = currentChars.filter(matches);
3249+ if (preferredCharSearch.length > 1) {
3250+ if (!quiet) toastr.warning(`Multiple characters found for name "${name}" and given conditions.`);
3251+ else console.warn(`Multiple characters found for name "${name}". Returning the first match.`);
3252+ }
3253+ if (preferredCharSearch.length) {
3254+ return preferredCharSearch[0];
3255+ }
3256+ }
3257+
32523258 // If allowAvatar is true, search by avatar first
32533259 if (allowAvatar && name) {
32543260 const characterByAvatar = filteredCharacters.find(char => char.avatar === name);