Quick Replies: Add auto-execute on new chat

06d300ad3691ab144186e136ecc3c0c33685958d

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

5 files changed, +33 -0Ignore whitespace
public/scripts/extensions/quick-reply/api/QuickReplyApi.js+6 -0
@@ -204,6 +204,7 @@ export class QuickReplyApi {
204204 * @param {boolean} [props.executeOnAi] whether to execute the quick reply after the AI has sent a message
205205 * @param {boolean} [props.executeOnChatChange] whether to execute the quick reply when a new chat is loaded
206206 * @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
207208 * @param {string} [props.automationId] when not empty, the quick reply will be executed when the WI with the given automation ID is activated
208209 * @returns {QuickReply} the new quick reply
209210 */
@@ -218,6 +219,7 @@ export class QuickReplyApi {
218219 executeOnAi,
219220 executeOnChatChange,
220221 executeOnGroupMemberDraft,
222+ executeOnNewChat,
221223 automationId,
222224 } = {}) {
223225 const set = this.getSetByName(setName);
@@ -236,6 +238,7 @@ export class QuickReplyApi {
236238 qr.executeOnAi = executeOnAi ?? false;
237239 qr.executeOnChatChange = executeOnChatChange ?? false;
238240 qr.executeOnGroupMemberDraft = executeOnGroupMemberDraft ?? false;
241+ qr.executeOnNewChat = executeOnNewChat ?? false;
239242 qr.automationId = automationId ?? '';
240243 qr.onUpdate();
241244 return qr;
@@ -258,6 +261,7 @@ export class QuickReplyApi {
258261 * @param {boolean} [props.executeOnAi] whether to execute the quick reply after the AI has sent a message
259262 * @param {boolean} [props.executeOnChatChange] whether to execute the quick reply when a new chat is loaded
260263 * @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
261265 * @param {string} [props.automationId] when not empty, the quick reply will be executed when the WI with the given automation ID is activated
262266 * @returns {QuickReply} the altered quick reply
263267 */
@@ -273,6 +277,7 @@ export class QuickReplyApi {
273277 executeOnAi,
274278 executeOnChatChange,
275279 executeOnGroupMemberDraft,
280+ executeOnNewChat,
276281 automationId,
277282 } = {}) {
278283 const qr = this.getQrByLabel(setName, label);
@@ -290,6 +295,7 @@ export class QuickReplyApi {
290295 qr.executeOnAi = executeOnAi ?? qr.executeOnAi;
291296 qr.executeOnChatChange = executeOnChatChange ?? qr.executeOnChatChange;
292297 qr.executeOnGroupMemberDraft = executeOnGroupMemberDraft ?? qr.executeOnGroupMemberDraft;
298+ qr.executeOnNewChat = executeOnNewChat ?? qr.executeOnNewChat;
293299 qr.automationId = automationId ?? qr.automationId;
294300 qr.onUpdate();
295301 return qr;
public/scripts/extensions/quick-reply/html/qrEditor.html+4 -0
@@ -109,6 +109,10 @@
109109 <span><i class="fa-solid fa-fw fa-message"></i><span data-i18n="Execute on chat change">Execute on chat change</span></span>
110110 </label>
111111 <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">
112116 <input type="checkbox" id="qr--executeOnGroupMemberDraft">
113117 <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>
114118 </label>
public/scripts/extensions/quick-reply/index.js+6 -0
@@ -105,6 +105,7 @@ const loadSets = async () => {
105105 qr.executeOnAi = slot.autoExecute_botMessage ?? false;
106106 qr.executeOnChatChange = slot.autoExecute_chatLoad ?? false;
107107 qr.executeOnGroupMemberDraft = slot.autoExecute_groupMemberDraft ?? false;
108+ qr.executeOnNewChat = slot.autoExecute_newChat ?? false;
108109 qr.automationId = slot.automationId ?? '';
109110 qr.contextList = (slot.contextMenu ?? []).map(it=>({
110111 set: it.preset,
@@ -260,3 +261,8 @@ const onWIActivation = async (entries) => {
260261 await autoExec.handleWIActivation(entries);
261262};
262263eventSource.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));
public/scripts/extensions/quick-reply/src/AutoExecuteHandler.js+9 -0
@@ -83,6 +83,15 @@ export class AutoExecuteHandler {
8383 await this.performAutoExecute(qrList);
8484 }
8585
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+
8695 /**
8796 * @param {any[]} entries Set of activated entries
8897 */
public/scripts/extensions/quick-reply/src/QuickReply.js+8 -0
@@ -44,6 +44,7 @@ export class QuickReply {
4444 /**@type {boolean}*/ executeOnAi = false;
4545 /**@type {boolean}*/ executeOnChatChange = false;
4646 /**@type {boolean}*/ executeOnGroupMemberDraft = false;
47+ /**@type {boolean}*/ executeOnNewChat = false;
4748 /**@type {string}*/ automationId = '';
4849
4950 /**@type {function}*/ onExecute;
@@ -1061,6 +1062,13 @@ export class QuickReply {
10611062 this.updateContext();
10621063 });
10631064 /**@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}*/
10641072 const automationId = dom.querySelector('#qr--automationId');
10651073 automationId.value = this.automationId;
10661074 automationId.addEventListener('input', () => {