Add a name argument for /getchatbook Closes #2599

e960ae64c541f4aeffab2033daf5dc0afa444c37

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

1 files changed, +27 -2Showing whitespace changes
public/scripts/world-info.js+27 -2
@@ -930,7 +930,12 @@ function registerWorldInfoSlashCommands() {
930930 return entries;
931931 }
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) {
934939 const chatId = getCurrentChatId();
935940
936941 if (!chatId) {
@@ -942,8 +947,19 @@ function registerWorldInfoSlashCommands() {
942947 return chat_metadata[METADATA_KEY];
943948 }
944949
950+ const name = (() => {
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+
945960 // Replace non-alphanumeric characters with underscores, cut to 64 characters
946961 const name = return `Chat Book ${getCurrentChatId()}`.replace(/[^a-z0-9]/gi, '_').replace(/_{2,}/g, '_').substring(0, 64);
962+ })();
947963 await createNewWorldInfo(name);
948964
949965 chat_metadata[METADATA_KEY] = name;
@@ -1289,6 +1305,15 @@ function registerWorldInfoSlashCommands() {
12891305 callback: getChatBookCallback,
12901306 returns: 'lorebook name',
12911307 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+ ],
12921317 aliases: ['getchatlore', 'getchatwi'],
12931318 }));
12941319