Merge pull request #3409 from Succubyss/member-get Adds /member-get via enhancing findGroupMemberId
Signed| @@ -292,10 +292,11 @@ export function getGroupNames() { | ||
| 292 | 292 | |
| 293 | 293 | /** |
| 294 | 294 | * Finds the character ID for a group member. |
| 295 | 295 | * @param {number|string} arg 0-based member index or character name |
| 296 | - * @returns {number} 0-based character ID | |
| 296 | + * @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 | */ |
| 298 | 299 | export function findGroupMemberId(arg, full = false) { |
| 299 | 300 | arg = arg?.trim(); |
| 300 | 301 | |
| 301 | 302 | if (!arg) { |
| @@ -311,15 +312,19 @@ export function findGroupMemberId(arg) { | ||
| 311 | 312 | } |
| 312 | 313 | |
| 313 | 314 | const index = parseInt(arg); |
| 314 | 315 | const searchByNamesearchByString = isNaN(index); |
| 315 | 316 | |
| 316 | 317 | if (searchByNamesearchByString) { |
| 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 | 324 | const result = fuse.search(arg); |
| 320 | 325 | |
| 321 | 326 | if (!result.length) { |
| 322 | 327 | console.warn(`WARN: No group member found withusing namestring ${arg}`); |
| 323 | 328 | return; |
| 324 | 329 | } |
| 325 | 330 | |
| @@ -330,9 +335,11 @@ export function findGroupMemberId(arg) { | ||
| 330 | 335 | return; |
| 331 | 336 | } |
| 332 | 337 | |
| 333 | 338 | console.log(`TriggeringTargeting 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 | 343 | const memberAvatar = group.members[index]; |
| 337 | 344 | |
| 338 | 345 | if (memberAvatar === undefined) { |
| @@ -347,8 +354,14 @@ export function findGroupMemberId(arg) { | ||
| 347 | 354 | return; |
| 348 | 355 | } |
| 349 | 356 | |
| 350 | 357 | console.log(`TriggeringTargeting 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 | } |
| 354 | 367 | |
| @@ -734,6 +734,57 @@ export function initDefaultSlashCommands() { | ||
| 734 | 734 | helpString: 'Unhides a message from the prompt.', |
| 735 | 735 | })); |
| 736 | 736 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 737 | + name: 'member-get', | |
| 738 | + aliases: ['getmember', 'memberget'], | |
| 739 | + callback: (async ({ field = 'name' }, arg) => { | |
| 740 | + if (!selected_group) { | |
| 741 | + toastr.warning('Cannot run /member-get command outside of a group chat.'); | |
| 742 | + return ''; | |
| 743 | + } | |
| 744 | + if (field === '') { | |
| 745 | + toastr.warning('\'/member-get field=\' argument required!'); | |
| 746 | + return ''; | |
| 747 | + } | |
| 748 | + field = field.toString(); | |
| 749 | + arg = arg.toString(); | |
| 750 | + if (!['name', 'index', 'id', 'avatar'].includes(field)) { | |
| 751 | + toastr.warning('\'/member-get field=\' argument required!'); | |
| 752 | + return ''; | |
| 753 | + } | |
| 754 | + const isId = !isNaN(parseInt(arg)); | |
| 755 | + const groupMember = findGroupMemberId(arg, true); | |
| 756 | + if (!groupMember) { | |
| 757 | + toastr.warn(`No group member found using ${isId ? 'id' : 'string'} ${arg}`); | |
| 758 | + return ''; | |
| 759 | + } | |
| 760 | + return groupMember[field]; | |
| 761 | + }), | |
| 762 | + namedArgumentList: [ | |
| 763 | + SlashCommandNamedArgument.fromProps({ | |
| 764 | + name: 'field', | |
| 765 | + description: 'Whether to retrieve the name, index, id, or avatar.', | |
| 766 | + typeList: [ARGUMENT_TYPE.STRING], | |
| 767 | + isRequired: true, | |
| 768 | + defaultValue: 'name', | |
| 769 | + enumList: [ | |
| 770 | + new SlashCommandEnumValue('name', 'Character name'), | |
| 771 | + new SlashCommandEnumValue('index', 'Group member index'), | |
| 772 | + new SlashCommandEnumValue('avatar', 'Character avatar'), | |
| 773 | + new SlashCommandEnumValue('id', 'Character index'), | |
| 774 | + ], | |
| 775 | + }), | |
| 776 | + ], | |
| 777 | + unnamedArgumentList: [ | |
| 778 | + SlashCommandArgument.fromProps({ | |
| 779 | + description: 'member index (starts with 0), name, or avatar', | |
| 780 | + typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.STRING], | |
| 781 | + isRequired: true, | |
| 782 | + enumProvider: commonEnumProviders.groupMembers(), | |
| 783 | + }), | |
| 784 | + ], | |
| 785 | + helpString: 'Retrieves a group member\'s name, index, id, or avatar.', | |
| 786 | + })); | |
| 787 | + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | |
| 737 | 788 | name: 'member-disable', |
| 738 | 789 | callback: disableGroupMemberCallback, |
| 739 | 790 | aliases: ['disable', 'disablemember', 'memberdisable'], |
| @@ -843,7 +894,8 @@ export function initDefaultSlashCommands() { | ||
| 843 | 894 | helpString: 'Moves a group member down in the group chat list.', |
| 844 | 895 | })); |
| 845 | 896 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 846 | 897 | name: 'member-peek', |
| 898 | + aliases: ['peek', 'memberpeek', 'peekmember'], | |
| 847 | 899 | callback: peekCallback, |
| 848 | 900 | unnamedArgumentList: [ |
| 849 | 901 | SlashCommandArgument.fromProps({ |
| @@ -3047,7 +3099,7 @@ function performGroupMemberAction(chid, action) { | ||
| 3047 | 3099 | |
| 3048 | 3100 | async function disableGroupMemberCallback(_, arg) { |
| 3049 | 3101 | if (!selected_group) { |
| 3050 | 3102 | toastr.warning('Cannot run /member-disable command outside of a group chat.'); |
| 3051 | 3103 | return ''; |
| 3052 | 3104 | } |
| 3053 | 3105 | |
| @@ -3064,7 +3116,7 @@ async function disableGroupMemberCallback(_, arg) { | ||
| 3064 | 3116 | |
| 3065 | 3117 | async function enableGroupMemberCallback(_, arg) { |
| 3066 | 3118 | if (!selected_group) { |
| 3067 | 3119 | toastr.warning('Cannot run /member-enable command outside of a group chat.'); |
| 3068 | 3120 | return ''; |
| 3069 | 3121 | } |
| 3070 | 3122 | |
| @@ -3081,7 +3133,7 @@ async function enableGroupMemberCallback(_, arg) { | ||
| 3081 | 3133 | |
| 3082 | 3134 | async function moveGroupMemberUpCallback(_, arg) { |
| 3083 | 3135 | if (!selected_group) { |
| 3084 | 3136 | toastr.warning('Cannot run /memberupmember-up command outside of a group chat.'); |
| 3085 | 3137 | return ''; |
| 3086 | 3138 | } |
| 3087 | 3139 | |
| @@ -3098,7 +3150,7 @@ async function moveGroupMemberUpCallback(_, arg) { | ||
| 3098 | 3150 | |
| 3099 | 3151 | async function moveGroupMemberDownCallback(_, arg) { |
| 3100 | 3152 | if (!selected_group) { |
| 3101 | 3153 | toastr.warning('Cannot run /memberdownmember-down command outside of a group chat.'); |
| 3102 | 3154 | return ''; |
| 3103 | 3155 | } |
| 3104 | 3156 | |
| @@ -3115,12 +3167,12 @@ async function moveGroupMemberDownCallback(_, arg) { | ||
| 3115 | 3167 | |
| 3116 | 3168 | async function peekCallback(_, arg) { |
| 3117 | 3169 | if (!selected_group) { |
| 3118 | 3170 | toastr.warning('Cannot run /member-peek command outside of a group chat.'); |
| 3119 | 3171 | return ''; |
| 3120 | 3172 | } |
| 3121 | 3173 | |
| 3122 | 3174 | if (is_group_generating) { |
| 3123 | 3175 | toastr.warning('Cannot run /member-peek command while the group reply is generating.'); |
| 3124 | 3176 | return ''; |
| 3125 | 3177 | } |
| 3126 | 3178 | |
| @@ -3137,7 +3189,7 @@ async function peekCallback(_, arg) { | ||
| 3137 | 3189 | |
| 3138 | 3190 | async function removeGroupMemberCallback(_, arg) { |
| 3139 | 3191 | if (!selected_group) { |
| 3140 | 3192 | toastr.warning('Cannot run /memberremovemember-remove command outside of a group chat.'); |
| 3141 | 3193 | return ''; |
| 3142 | 3194 | } |
| 3143 | 3195 | |