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() {
182 if (args.preferCurrent instanceof SlashCommandClosure || Array.isArray(args.preferCurrent)) throw new Error('preferCurrent cannot be a closure or array');182 if (args.preferCurrent instanceof SlashCommandClosure || Array.isArray(args.preferCurrent)) throw new Error('preferCurrent cannot be a closure or array');
183 if (args.quiet instanceof SlashCommandClosure || Array.isArray(args.quiet)) throw new Error('quiet cannot be a closure or array');183 if (args.quiet instanceof SlashCommandClosure || Array.isArray(args.quiet)) throw new Error('quiet cannot be a closure or array');
184184
185 const char = findChar({ name: name, filteredByTags: validateArrayArgString(args.tag, 'tag'), preferCurrentChar: isTrueBoolean(args.preferCurrent), quiet: isTrueBoolean(args.quiet) });185 const char = findChar({ name: name, filteredByTags: validateArrayArgString(args.tag, 'tag'), preferCurrentChar: !isFalseBoolean(args.preferCurrent), quiet: isTrueBoolean(args.quiet) });
186 return char?.avatar ?? '';186 return char?.avatar ?? '';
187 },187 },
188 returns: 'the avatar key (unique identifier) of the character',188 returns: 'the avatar key (unique identifier) of the character',
@@ -3229,17 +3229,6 @@ export function validateArrayArg(arg, name, { allowUndefined = true } = {}) {
3229export function findChar({ name = null, allowAvatar = true, insensitive = true, filteredByTags = null, preferCurrentChar = true, quiet = false } = {}) {3229export function findChar({ name = null, allowAvatar = true, insensitive = true, filteredByTags = null, preferCurrentChar = true, quiet = false } = {}) {
3230 const matches = (char) => (allowAvatar && char.avatar === name) || (insensitive ? equalsIgnoreCaseAndAccents(char.name, name) : char.name === name);3230 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
3243 // Filter characters by tags if provided3232 // Filter characters by tags if provided
3244 let filteredCharacters = characters;3233 let filteredCharacters = characters;
3245 if (filteredByTags) {3234 if (filteredByTags) {
@@ -3249,6 +3238,23 @@ export function findChar({ name = null, allowAvatar = true, insensitive = true,
3249 });3238 });
3250 }3239 }
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
3252 // If allowAvatar is true, search by avatar first3258 // If allowAvatar is true, search by avatar first
3253 if (allowAvatar && name) {3259 if (allowAvatar && name) {
3254 const characterByAvatar = filteredCharacters.find(char => char.avatar === name);3260 const characterByAvatar = filteredCharacters.find(char => char.avatar === name);