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 | * 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 name | 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 | export function findGroupMemberId(arg) { | 299 | export function findGroupMemberId(arg, full = false) { |
| 299 | arg = arg?.trim(); | 300 | arg = arg?.trim(); |
| 300 | 301 | ||
| 301 | if (!arg) { | 302 | if (!arg) { |
| @@ -311,15 +312,19 @@ export function findGroupMemberId(arg) { | |||
| 311 | } | 312 | } |
| 312 | 313 | ||
| 313 | const index = parseInt(arg); | 314 | const index = parseInt(arg); |
| 314 | const searchByName = isNaN(index); | 315 | const searchByString = isNaN(index); |
| 315 | 316 | ||
| 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); |
| 320 | 325 | ||
| 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 | } |
| 325 | 330 | ||
| @@ -330,9 +335,11 @@ export function findGroupMemberId(arg) { | |||
| 330 | return; | 335 | return; |
| 331 | } | 336 | } |
| 332 | 337 | ||
| 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]; |
| 337 | 344 | ||
| 338 | if (memberAvatar === undefined) { | 345 | if (memberAvatar === undefined) { |
| @@ -347,8 +354,14 @@ export function findGroupMemberId(arg) { | |||
| 347 | return; | 354 | return; |
| 348 | } | 355 | } |
| 349 | 356 | ||
| 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 | } |
| 354 | 367 | ||
| @@ -734,6 +734,57 @@ export function initDefaultSlashCommands() { | |||
| 734 | helpString: 'Unhides a message from the prompt.', | 734 | helpString: 'Unhides a message from the prompt.', |
| 735 | })); | 735 | })); |
| 736 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | 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 | name: 'member-disable', | 788 | name: 'member-disable', |
| 738 | callback: disableGroupMemberCallback, | 789 | callback: disableGroupMemberCallback, |
| 739 | aliases: ['disable', 'disablemember', 'memberdisable'], | 790 | aliases: ['disable', 'disablemember', 'memberdisable'], |
| @@ -843,7 +894,8 @@ export function initDefaultSlashCommands() { | |||
| 843 | helpString: 'Moves a group member down in the group chat list.', | 894 | helpString: 'Moves a group member down in the group chat list.', |
| 844 | })); | 895 | })); |
| 845 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | 896 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 846 | name: 'peek', | 897 | name: 'member-peek', |
| 898 | aliases: ['peek', 'memberpeek', 'peekmember'], | ||
| 847 | callback: peekCallback, | 899 | callback: peekCallback, |
| 848 | unnamedArgumentList: [ | 900 | unnamedArgumentList: [ |
| 849 | SlashCommandArgument.fromProps({ | 901 | SlashCommandArgument.fromProps({ |
| @@ -3047,7 +3099,7 @@ function performGroupMemberAction(chid, action) { | |||
| 3047 | 3099 | ||
| 3048 | async function disableGroupMemberCallback(_, arg) { | 3100 | async function disableGroupMemberCallback(_, arg) { |
| 3049 | if (!selected_group) { | 3101 | if (!selected_group) { |
| 3050 | toastr.warning('Cannot run /disable command outside of a group chat.'); | 3102 | toastr.warning('Cannot run /member-disable command outside of a group chat.'); |
| 3051 | return ''; | 3103 | return ''; |
| 3052 | } | 3104 | } |
| 3053 | 3105 | ||
| @@ -3064,7 +3116,7 @@ async function disableGroupMemberCallback(_, arg) { | |||
| 3064 | 3116 | ||
| 3065 | async function enableGroupMemberCallback(_, arg) { | 3117 | async function enableGroupMemberCallback(_, arg) { |
| 3066 | if (!selected_group) { | 3118 | if (!selected_group) { |
| 3067 | toastr.warning('Cannot run /enable command outside of a group chat.'); | 3119 | toastr.warning('Cannot run /member-enable command outside of a group chat.'); |
| 3068 | return ''; | 3120 | return ''; |
| 3069 | } | 3121 | } |
| 3070 | 3122 | ||
| @@ -3081,7 +3133,7 @@ async function enableGroupMemberCallback(_, arg) { | |||
| 3081 | 3133 | ||
| 3082 | async function moveGroupMemberUpCallback(_, arg) { | 3134 | async function moveGroupMemberUpCallback(_, arg) { |
| 3083 | if (!selected_group) { | 3135 | if (!selected_group) { |
| 3084 | toastr.warning('Cannot run /memberup command outside of a group chat.'); | 3136 | toastr.warning('Cannot run /member-up command outside of a group chat.'); |
| 3085 | return ''; | 3137 | return ''; |
| 3086 | } | 3138 | } |
| 3087 | 3139 | ||
| @@ -3098,7 +3150,7 @@ async function moveGroupMemberUpCallback(_, arg) { | |||
| 3098 | 3150 | ||
| 3099 | async function moveGroupMemberDownCallback(_, arg) { | 3151 | async function moveGroupMemberDownCallback(_, arg) { |
| 3100 | if (!selected_group) { | 3152 | if (!selected_group) { |
| 3101 | toastr.warning('Cannot run /memberdown command outside of a group chat.'); | 3153 | toastr.warning('Cannot run /member-down command outside of a group chat.'); |
| 3102 | return ''; | 3154 | return ''; |
| 3103 | } | 3155 | } |
| 3104 | 3156 | ||
| @@ -3115,12 +3167,12 @@ async function moveGroupMemberDownCallback(_, arg) { | |||
| 3115 | 3167 | ||
| 3116 | async function peekCallback(_, arg) { | 3168 | async function peekCallback(_, arg) { |
| 3117 | if (!selected_group) { | 3169 | if (!selected_group) { |
| 3118 | toastr.warning('Cannot run /peek command outside of a group chat.'); | 3170 | toastr.warning('Cannot run /member-peek command outside of a group chat.'); |
| 3119 | return ''; | 3171 | return ''; |
| 3120 | } | 3172 | } |
| 3121 | 3173 | ||
| 3122 | if (is_group_generating) { | 3174 | if (is_group_generating) { |
| 3123 | toastr.warning('Cannot run /peek command while the group reply is generating.'); | 3175 | toastr.warning('Cannot run /member-peek command while the group reply is generating.'); |
| 3124 | return ''; | 3176 | return ''; |
| 3125 | } | 3177 | } |
| 3126 | 3178 | ||
| @@ -3137,7 +3189,7 @@ async function peekCallback(_, arg) { | |||
| 3137 | 3189 | ||
| 3138 | async function removeGroupMemberCallback(_, arg) { | 3190 | async function removeGroupMemberCallback(_, arg) { |
| 3139 | if (!selected_group) { | 3191 | if (!selected_group) { |
| 3140 | toastr.warning('Cannot run /memberremove command outside of a group chat.'); | 3192 | toastr.warning('Cannot run /member-remove command outside of a group chat.'); |
| 3141 | return ''; | 3193 | return ''; |
| 3142 | } | 3194 | } |
| 3143 | 3195 | ||