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