Quick Replies: Add auto-execute on new chat
| @@ -204,6 +204,7 @@ export class QuickReplyApi { | ||
| 204 | 204 | * @param {boolean} [props.executeOnAi] whether to execute the quick reply after the AI has sent a message |
| 205 | 205 | * @param {boolean} [props.executeOnChatChange] whether to execute the quick reply when a new chat is loaded |
| 206 | 206 | * @param {boolean} [props.executeOnGroupMemberDraft] whether to execute the quick reply when a group member is selected |
| 207 | + * @param {boolean} [props.executeOnNewChat] whether to execute the quick reply when a new chat is created | |
| 207 | 208 | * @param {string} [props.automationId] when not empty, the quick reply will be executed when the WI with the given automation ID is activated |
| 208 | 209 | * @returns {QuickReply} the new quick reply |
| 209 | 210 | */ |
| @@ -218,6 +219,7 @@ export class QuickReplyApi { | ||
| 218 | 219 | executeOnAi, |
| 219 | 220 | executeOnChatChange, |
| 220 | 221 | executeOnGroupMemberDraft, |
| 222 | + executeOnNewChat, | |
| 221 | 223 | automationId, |
| 222 | 224 | } = {}) { |
| 223 | 225 | const set = this.getSetByName(setName); |
| @@ -236,6 +238,7 @@ export class QuickReplyApi { | ||
| 236 | 238 | qr.executeOnAi = executeOnAi ?? false; |
| 237 | 239 | qr.executeOnChatChange = executeOnChatChange ?? false; |
| 238 | 240 | qr.executeOnGroupMemberDraft = executeOnGroupMemberDraft ?? false; |
| 241 | + qr.executeOnNewChat = executeOnNewChat ?? false; | |
| 239 | 242 | qr.automationId = automationId ?? ''; |
| 240 | 243 | qr.onUpdate(); |
| 241 | 244 | return qr; |
| @@ -258,6 +261,7 @@ export class QuickReplyApi { | ||
| 258 | 261 | * @param {boolean} [props.executeOnAi] whether to execute the quick reply after the AI has sent a message |
| 259 | 262 | * @param {boolean} [props.executeOnChatChange] whether to execute the quick reply when a new chat is loaded |
| 260 | 263 | * @param {boolean} [props.executeOnGroupMemberDraft] whether to execute the quick reply when a group member is selected |
| 264 | + * @param {boolean} [props.executeOnNewChat] whether to execute the quick reply when a new chat is created | |
| 261 | 265 | * @param {string} [props.automationId] when not empty, the quick reply will be executed when the WI with the given automation ID is activated |
| 262 | 266 | * @returns {QuickReply} the altered quick reply |
| 263 | 267 | */ |
| @@ -273,6 +277,7 @@ export class QuickReplyApi { | ||
| 273 | 277 | executeOnAi, |
| 274 | 278 | executeOnChatChange, |
| 275 | 279 | executeOnGroupMemberDraft, |
| 280 | + executeOnNewChat, | |
| 276 | 281 | automationId, |
| 277 | 282 | } = {}) { |
| 278 | 283 | const qr = this.getQrByLabel(setName, label); |
| @@ -290,6 +295,7 @@ export class QuickReplyApi { | ||
| 290 | 295 | qr.executeOnAi = executeOnAi ?? qr.executeOnAi; |
| 291 | 296 | qr.executeOnChatChange = executeOnChatChange ?? qr.executeOnChatChange; |
| 292 | 297 | qr.executeOnGroupMemberDraft = executeOnGroupMemberDraft ?? qr.executeOnGroupMemberDraft; |
| 298 | + qr.executeOnNewChat = executeOnNewChat ?? qr.executeOnNewChat; | |
| 293 | 299 | qr.automationId = automationId ?? qr.automationId; |
| 294 | 300 | qr.onUpdate(); |
| 295 | 301 | return qr; |
| @@ -109,6 +109,10 @@ | ||
| 109 | 109 | <span><i class="fa-solid fa-fw fa-message"></i><span data-i18n="Execute on chat change">Execute on chat change</span></span> |
| 110 | 110 | </label> |
| 111 | 111 | <label class="checkbox_label"> |
| 112 | + <input type="checkbox" id="qr--executeOnNewChat"> | |
| 113 | + <span><i class="fa-solid fa-fw fa-comments"></i><span data-i18n="Execute on new chat">Execute on new chat</span></span> | |
| 114 | + </label> | |
| 115 | + <label class="checkbox_label"> | |
| 112 | 116 | <input type="checkbox" id="qr--executeOnGroupMemberDraft"> |
| 113 | 117 | <span><i class="fa-solid fa-fw fa-people-group"></i><span data-i18n="Execute on group member draft">Execute on group member draft</span></span> |
| 114 | 118 | </label> |
| @@ -105,6 +105,7 @@ const loadSets = async () => { | ||
| 105 | 105 | qr.executeOnAi = slot.autoExecute_botMessage ?? false; |
| 106 | 106 | qr.executeOnChatChange = slot.autoExecute_chatLoad ?? false; |
| 107 | 107 | qr.executeOnGroupMemberDraft = slot.autoExecute_groupMemberDraft ?? false; |
| 108 | + qr.executeOnNewChat = slot.autoExecute_newChat ?? false; | |
| 108 | 109 | qr.automationId = slot.automationId ?? ''; |
| 109 | 110 | qr.contextList = (slot.contextMenu ?? []).map(it=>({ |
| 110 | 111 | set: it.preset, |
| @@ -260,3 +261,8 @@ const onWIActivation = async (entries) => { | ||
| 260 | 261 | await autoExec.handleWIActivation(entries); |
| 261 | 262 | }; |
| 262 | 263 | eventSource.on(event_types.WORLD_INFO_ACTIVATED, (...args) => executeIfReadyElseQueue(onWIActivation, args)); |
| 264 | + | |
| 265 | +const onNewChat = async () => { | |
| 266 | + await autoExec.handleNewChat(); | |
| 267 | +}; | |
| 268 | +eventSource.on(event_types.CHAT_CREATED, (...args) => executeIfReadyElseQueue(onNewChat, args)); | |
| @@ -83,6 +83,15 @@ export class AutoExecuteHandler { | ||
| 83 | 83 | await this.performAutoExecute(qrList); |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | + async handleNewChat() { | |
| 87 | + if (!this.checkExecute()) return; | |
| 88 | + const qrList = [ | |
| 89 | + ...this.settings.config.setList.map(link=>link.set.qrList.filter(qr=>qr.executeOnNewChat)).flat(), | |
| 90 | + ...(this.settings.chatConfig?.setList?.map(link=>link.set.qrList.filter(qr=>qr.executeOnNewChat))?.flat() ?? []), | |
| 91 | + ]; | |
| 92 | + await this.performAutoExecute(qrList); | |
| 93 | + } | |
| 94 | + | |
| 86 | 95 | /** |
| 87 | 96 | * @param {any[]} entries Set of activated entries |
| 88 | 97 | */ |
| @@ -44,6 +44,7 @@ export class QuickReply { | ||
| 44 | 44 | /**@type {boolean}*/ executeOnAi = false; |
| 45 | 45 | /**@type {boolean}*/ executeOnChatChange = false; |
| 46 | 46 | /**@type {boolean}*/ executeOnGroupMemberDraft = false; |
| 47 | + /**@type {boolean}*/ executeOnNewChat = false; | |
| 47 | 48 | /**@type {string}*/ automationId = ''; |
| 48 | 49 | |
| 49 | 50 | /**@type {function}*/ onExecute; |
| @@ -1061,6 +1062,13 @@ export class QuickReply { | ||
| 1061 | 1062 | this.updateContext(); |
| 1062 | 1063 | }); |
| 1063 | 1064 | /**@type {HTMLInputElement}*/ |
| 1065 | + const executeOnNewChat = dom.querySelector('#qr--executeOnNewChat'); | |
| 1066 | + executeOnNewChat.checked = this.executeOnNewChat; | |
| 1067 | + executeOnNewChat.addEventListener('click', ()=>{ | |
| 1068 | + this.executeOnNewChat = executeOnNewChat.checked; | |
| 1069 | + this.updateContext(); | |
| 1070 | + }); | |
| 1071 | + /**@type {HTMLInputElement}*/ | |
| 1064 | 1072 | const automationId = dom.querySelector('#qr--automationId'); |
| 1065 | 1073 | automationId.value = this.automationId; |
| 1066 | 1074 | automationId.addEventListener('input', () => { |