mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Merge pull request #3409 from Succubyss/member-get
Adds /member-get via enhancing findGroupMemberId
This commit is contained in:
@@ -292,10 +292,11 @@ export function getGroupNames() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds the character ID for a group member.
|
* Finds the character ID for a group member.
|
||||||
* @param {string} arg 0-based member index or character name
|
* @param {number|string} arg 0-based member index or character name
|
||||||
* @returns {number} 0-based character ID
|
* @param {Boolean} full Whether to return a key-value object containing extra data
|
||||||
|
* @returns {number|Object} 0-based character ID or key-value object if full is true
|
||||||
*/
|
*/
|
||||||
export function findGroupMemberId(arg) {
|
export function findGroupMemberId(arg, full = false) {
|
||||||
arg = arg?.trim();
|
arg = arg?.trim();
|
||||||
|
|
||||||
if (!arg) {
|
if (!arg) {
|
||||||
@@ -311,15 +312,19 @@ export function findGroupMemberId(arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const index = parseInt(arg);
|
const index = parseInt(arg);
|
||||||
const searchByName = isNaN(index);
|
const searchByString = isNaN(index);
|
||||||
|
|
||||||
if (searchByName) {
|
if (searchByString) {
|
||||||
const memberNames = group.members.map(x => ({ name: characters.find(y => y.avatar === x)?.name, index: characters.findIndex(y => y.avatar === x) }));
|
const memberNames = group.members.map(x => ({
|
||||||
const fuse = new Fuse(memberNames, { keys: ['name'] });
|
avatar: x,
|
||||||
|
name: characters.find(y => y.avatar === x)?.name,
|
||||||
|
index: characters.findIndex(y => y.avatar === x),
|
||||||
|
}));
|
||||||
|
const fuse = new Fuse(memberNames, { keys: ['avatar', 'name'] });
|
||||||
const result = fuse.search(arg);
|
const result = fuse.search(arg);
|
||||||
|
|
||||||
if (!result.length) {
|
if (!result.length) {
|
||||||
console.warn(`WARN: No group member found with name ${arg}`);
|
console.warn(`WARN: No group member found using string ${arg}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,9 +335,11 @@ export function findGroupMemberId(arg) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Triggering group member ${chid} (${arg}) from search result`, result[0]);
|
console.log(`Targeting group member ${chid} (${arg}) from search result`, result[0]);
|
||||||
return chid;
|
|
||||||
} else {
|
return !full ? chid : { ...{ id: chid }, ...result[0].item };
|
||||||
|
}
|
||||||
|
else {
|
||||||
const memberAvatar = group.members[index];
|
const memberAvatar = group.members[index];
|
||||||
|
|
||||||
if (memberAvatar === undefined) {
|
if (memberAvatar === undefined) {
|
||||||
@@ -347,8 +354,14 @@ export function findGroupMemberId(arg) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Triggering group member ${memberAvatar} at index ${index}`);
|
console.log(`Targeting group member ${memberAvatar} at index ${index}`);
|
||||||
return chid;
|
|
||||||
|
return !full ? chid : {
|
||||||
|
id: chid,
|
||||||
|
avatar: memberAvatar,
|
||||||
|
name: characters.find(y => y.avatar === memberAvatar)?.name,
|
||||||
|
index: index,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -733,6 +733,57 @@ export function initDefaultSlashCommands() {
|
|||||||
],
|
],
|
||||||
helpString: 'Unhides a message from the prompt.',
|
helpString: 'Unhides a message from the prompt.',
|
||||||
}));
|
}));
|
||||||
|
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||||
|
name: 'member-get',
|
||||||
|
aliases: ['getmember', 'memberget'],
|
||||||
|
callback: (async ({ field = 'name' }, arg) => {
|
||||||
|
if (!selected_group) {
|
||||||
|
toastr.warning('Cannot run /member-get command outside of a group chat.');
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
if (field === '') {
|
||||||
|
toastr.warning('\'/member-get field=\' argument required!');
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
field = field.toString();
|
||||||
|
arg = arg.toString();
|
||||||
|
if (!['name', 'index', 'id', 'avatar'].includes(field)) {
|
||||||
|
toastr.warning('\'/member-get field=\' argument required!');
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
const isId = !isNaN(parseInt(arg));
|
||||||
|
const groupMember = findGroupMemberId(arg, true);
|
||||||
|
if (!groupMember) {
|
||||||
|
toastr.warn(`No group member found using ${isId ? 'id' : 'string'} ${arg}`);
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return groupMember[field];
|
||||||
|
}),
|
||||||
|
namedArgumentList: [
|
||||||
|
SlashCommandNamedArgument.fromProps({
|
||||||
|
name: 'field',
|
||||||
|
description: 'Whether to retrieve the name, index, id, or avatar.',
|
||||||
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
|
isRequired: true,
|
||||||
|
defaultValue: 'name',
|
||||||
|
enumList: [
|
||||||
|
new SlashCommandEnumValue('name', 'Character name'),
|
||||||
|
new SlashCommandEnumValue('index', 'Group member index'),
|
||||||
|
new SlashCommandEnumValue('avatar', 'Character avatar'),
|
||||||
|
new SlashCommandEnumValue('id', 'Character index'),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
unnamedArgumentList: [
|
||||||
|
SlashCommandArgument.fromProps({
|
||||||
|
description: 'member index (starts with 0), name, or avatar',
|
||||||
|
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.STRING],
|
||||||
|
isRequired: true,
|
||||||
|
enumProvider: commonEnumProviders.groupMembers(),
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
helpString: 'Retrieves a group member\'s name, index, id, or avatar.',
|
||||||
|
}));
|
||||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||||
name: 'member-disable',
|
name: 'member-disable',
|
||||||
callback: disableGroupMemberCallback,
|
callback: disableGroupMemberCallback,
|
||||||
@@ -843,7 +894,8 @@ export function initDefaultSlashCommands() {
|
|||||||
helpString: 'Moves a group member down in the group chat list.',
|
helpString: 'Moves a group member down in the group chat list.',
|
||||||
}));
|
}));
|
||||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||||
name: 'peek',
|
name: 'member-peek',
|
||||||
|
aliases: ['peek', 'memberpeek', 'peekmember'],
|
||||||
callback: peekCallback,
|
callback: peekCallback,
|
||||||
unnamedArgumentList: [
|
unnamedArgumentList: [
|
||||||
SlashCommandArgument.fromProps({
|
SlashCommandArgument.fromProps({
|
||||||
@@ -3047,7 +3099,7 @@ function performGroupMemberAction(chid, action) {
|
|||||||
|
|
||||||
async function disableGroupMemberCallback(_, arg) {
|
async function disableGroupMemberCallback(_, arg) {
|
||||||
if (!selected_group) {
|
if (!selected_group) {
|
||||||
toastr.warning('Cannot run /disable command outside of a group chat.');
|
toastr.warning('Cannot run /member-disable command outside of a group chat.');
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3064,7 +3116,7 @@ async function disableGroupMemberCallback(_, arg) {
|
|||||||
|
|
||||||
async function enableGroupMemberCallback(_, arg) {
|
async function enableGroupMemberCallback(_, arg) {
|
||||||
if (!selected_group) {
|
if (!selected_group) {
|
||||||
toastr.warning('Cannot run /enable command outside of a group chat.');
|
toastr.warning('Cannot run /member-enable command outside of a group chat.');
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3081,7 +3133,7 @@ async function enableGroupMemberCallback(_, arg) {
|
|||||||
|
|
||||||
async function moveGroupMemberUpCallback(_, arg) {
|
async function moveGroupMemberUpCallback(_, arg) {
|
||||||
if (!selected_group) {
|
if (!selected_group) {
|
||||||
toastr.warning('Cannot run /memberup command outside of a group chat.');
|
toastr.warning('Cannot run /member-up command outside of a group chat.');
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3098,7 +3150,7 @@ async function moveGroupMemberUpCallback(_, arg) {
|
|||||||
|
|
||||||
async function moveGroupMemberDownCallback(_, arg) {
|
async function moveGroupMemberDownCallback(_, arg) {
|
||||||
if (!selected_group) {
|
if (!selected_group) {
|
||||||
toastr.warning('Cannot run /memberdown command outside of a group chat.');
|
toastr.warning('Cannot run /member-down command outside of a group chat.');
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3115,12 +3167,12 @@ async function moveGroupMemberDownCallback(_, arg) {
|
|||||||
|
|
||||||
async function peekCallback(_, arg) {
|
async function peekCallback(_, arg) {
|
||||||
if (!selected_group) {
|
if (!selected_group) {
|
||||||
toastr.warning('Cannot run /peek command outside of a group chat.');
|
toastr.warning('Cannot run /member-peek command outside of a group chat.');
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_group_generating) {
|
if (is_group_generating) {
|
||||||
toastr.warning('Cannot run /peek command while the group reply is generating.');
|
toastr.warning('Cannot run /member-peek command while the group reply is generating.');
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3137,7 +3189,7 @@ async function peekCallback(_, arg) {
|
|||||||
|
|
||||||
async function removeGroupMemberCallback(_, arg) {
|
async function removeGroupMemberCallback(_, arg) {
|
||||||
if (!selected_group) {
|
if (!selected_group) {
|
||||||
toastr.warning('Cannot run /memberremove command outside of a group chat.');
|
toastr.warning('Cannot run /member-remove command outside of a group chat.');
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user