add 'full' parameter to findGroupMemberId

b8821acb953a045798dd9383e6ec056e92022026

Succubyss <87207237+Succubyss@users.noreply.github.com>

1 files changed, +27 -14Showing whitespace changes
public/scripts/group-chats.js+27 -14
@@ -292,10 +292,11 @@ export function getGroupNames() {
292292
293/**293/**
294 * Finds the character ID for a group member.294 * Finds the character ID for a group member.
295 * @param {string} arg 0-based member index or character name295 * @param {number|string} arg 0-based member index or character name
296 * @returns {number} 0-based character ID296 * @param {Boolean} full Whether to return a key-value object containing extra data
297 * @returns {number|Object} 0-based character ID or key-value object if full is true
297 */298 */
298export function findGroupMemberId(arg) {299export function findGroupMemberId(arg, full) {
299 arg = arg?.trim();300 arg = arg?.trim();
300301
301 if (!arg) {302 if (!arg) {
@@ -311,15 +312,19 @@ export function findGroupMemberId(arg) {
311 }312 }
312313
313 const index = parseInt(arg);314 const index = parseInt(arg);
314 const searchByName = isNaN(index);315 const searchByString = isNaN(index);
315316
316 if (searchByName) {317 if (searchByString) {
317 const memberNames = group.members.map(x => ({ name: characters.find(y => y.avatar === x)?.name, index: characters.findIndex(y => y.avatar === x) }));318 const memberNames = group.members.map(x => ({
318 const fuse = new Fuse(memberNames, { keys: ['name'] });319 avatar: x,
320 name: characters.find(y => y.avatar === x)?.name,
321 index: characters.findIndex(y => y.avatar === x),
322 }));
323 const fuse = new Fuse(memberNames, { keys: ['avatar', 'name'] });
319 const result = fuse.search(arg);324 const result = fuse.search(arg);
320325
321 if (!result.length) {326 if (!result.length) {
322 console.warn(`WARN: No group member found with name ${arg}`);327 console.warn(`WARN: No group member found using string ${arg}`);
323 return;328 return;
324 }329 }
325330
@@ -330,9 +335,11 @@ export function findGroupMemberId(arg) {
330 return;335 return;
331 }336 }
332337
333 console.log(`Triggering group member ${chid} (${arg}) from search result`, result[0]);338 console.log(`Targeting group member ${chid} (${arg}) from search result`, result[0]);
334 return chid;339
335 } else {340 return !full ? chid : { ...{ id: chid }, ...result[0].item };
341 }
342 else {
336 const memberAvatar = group.members[index];343 const memberAvatar = group.members[index];
337344
338 if (memberAvatar === undefined) {345 if (memberAvatar === undefined) {
@@ -347,8 +354,14 @@ export function findGroupMemberId(arg) {
347 return;354 return;
348 }355 }
349356
350 console.log(`Triggering group member ${memberAvatar} at index ${index}`);357 console.log(`Targeting group member ${memberAvatar} at index ${index}`);
351 return chid;358
359 return !full ? chid : {
360 id: chid,
361 avatar: memberAvatar,
362 name: characters.find(y => y.avatar === memberAvatar)?.name,
363 index: index,
364 };
352 }365 }
353}366}
354367