Add a name argument for /getchatbook Closes #2599
| @@ -930,7 +930,12 @@ function registerWorldInfoSlashCommands() { | ||
| 930 | 930 | return entries; |
| 931 | 931 | } |
| 932 | 932 | |
| 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 | 939 | const chatId = getCurrentChatId(); |
| 935 | 940 | |
| 936 | 941 | if (!chatId) { |
| @@ -942,8 +947,19 @@ function registerWorldInfoSlashCommands() { | ||
| 942 | 947 | return chat_metadata[METADATA_KEY]; |
| 943 | 948 | } |
| 944 | 949 | |
| 945 | - // Replace non-alphanumeric characters with underscores, cut to 64 characters | |
| 950 | + 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 | 963 | await createNewWorldInfo(name); |
| 948 | 964 | |
| 949 | 965 | chat_metadata[METADATA_KEY] = name; |
| @@ -1289,6 +1305,15 @@ function registerWorldInfoSlashCommands() { | ||
| 1289 | 1305 | callback: getChatBookCallback, |
| 1290 | 1306 | returns: 'lorebook name', |
| 1291 | 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 | 1317 | aliases: ['getchatlore', 'getchatwi'], |
| 1293 | 1318 | })); |
| 1294 | 1319 | |