Add a name argument for /getchatbook Closes #2599

e960ae64c541f4aeffab2033daf5dc0afa444c37

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +28 -3Ignore whitespace
public/scripts/world-info.js+28 -3
@@ -930,7 +930,12 @@ function registerWorldInfoSlashCommands() {
930 return entries;930 return entries;
931 }931 }
932932
933 async function getChatBookCallback() {933 /**
934 * Gets the name of the chat-bound lorebook. Creates a new one if it doesn't exist.
935 * @param {import('./slash-commands/SlashCommandParser.js').NamedArguments} args Named arguments
936 * @returns
937 */
938 async function getChatBookCallback(args) {
934 const chatId = getCurrentChatId();939 const chatId = getCurrentChatId();
935940
936 if (!chatId) {941 if (!chatId) {
@@ -942,8 +947,19 @@ function registerWorldInfoSlashCommands() {
942 return chat_metadata[METADATA_KEY];947 return chat_metadata[METADATA_KEY];
943 }948 }
944949
945 // Replace non-alphanumeric characters with underscores, cut to 64 characters950 const name = (() => {
946 const name = `Chat Book ${getCurrentChatId()}`.replace(/[^a-z0-9]/gi, '_').replace(/_{2,}/g, '_').substring(0, 64);951 // Use the provided name if it's not in use
952 if (typeof args.name === 'string') {
953 const name = String(args.name);
954 if (world_names.includes(name)) {
955 throw new Error('This World Info file name is already in use');
956 }
957 return name;
958 }
959
960 // Replace non-alphanumeric characters with underscores, cut to 64 characters
961 return `Chat Book ${getCurrentChatId()}`.replace(/[^a-z0-9]/gi, '_').replace(/_{2,}/g, '_').substring(0, 64);
962 })();
947 await createNewWorldInfo(name);963 await createNewWorldInfo(name);
948964
949 chat_metadata[METADATA_KEY] = name;965 chat_metadata[METADATA_KEY] = name;
@@ -1289,6 +1305,15 @@ function registerWorldInfoSlashCommands() {
1289 callback: getChatBookCallback,1305 callback: getChatBookCallback,
1290 returns: 'lorebook name',1306 returns: 'lorebook name',
1291 helpString: 'Get a name of the chat-bound lorebook or create a new one if was unbound, and pass it down the pipe.',1307 helpString: 'Get a name of the chat-bound lorebook or create a new one if was unbound, and pass it down the pipe.',
1308 namedArgumentList: [
1309 SlashCommandNamedArgument.fromProps({
1310 name: 'name',
1311 description: 'lorebook name if creating a new one, will be auto-generated otherwise',
1312 typeList: [ARGUMENT_TYPE.STRING],
1313 isRequired: false,
1314 acceptsMultiple: false,
1315 }),
1316 ],
1292 aliases: ['getchatlore', 'getchatwi'],1317 aliases: ['getchatlore', 'getchatwi'],
1293 }));1318 }));
12941319