#1853 Add WI/Script link by entry automation id

This commit is contained in:
Cohee
2024-02-24 17:22:51 +02:00
parent 7b8ac8f4c4
commit 3441667336
10 changed files with 90 additions and 5 deletions

View File

@@ -191,6 +191,7 @@ export class QuickReplyApi {
* @param {Boolean} [props.executeOnAi] whether to execute the quick reply after the AI has sent a message
* @param {Boolean} [props.executeOnChatChange] whether to execute the quick reply when a new chat is loaded
* @param {Boolean} [props.executeOnGroupMemberDraft] whether to execute the quick reply when a group member is selected
* @param {String} [props.automationId] when not empty, the quick reply will be executed when the WI with the given automation ID is activated
* @returns {QuickReply} the new quick reply
*/
createQuickReply(setName, label, {
@@ -202,6 +203,7 @@ export class QuickReplyApi {
executeOnAi,
executeOnChatChange,
executeOnGroupMemberDraft,
automationId,
} = {}) {
const set = this.getSetByName(setName);
if (!set) {
@@ -217,6 +219,7 @@ export class QuickReplyApi {
qr.executeOnAi = executeOnAi ?? false;
qr.executeOnChatChange = executeOnChatChange ?? false;
qr.executeOnGroupMemberDraft = executeOnGroupMemberDraft ?? false;
qr.automationId = automationId ?? '';
qr.onUpdate();
return qr;
}
@@ -236,6 +239,7 @@ export class QuickReplyApi {
* @param {Boolean} [props.executeOnAi] whether to execute the quick reply after the AI has sent a message
* @param {Boolean} [props.executeOnChatChange] whether to execute the quick reply when a new chat is loaded
* @param {Boolean} [props.executeOnGroupMemberDraft] whether to execute the quick reply when a group member is selected
* @param {String} [props.automationId] when not empty, the quick reply will be executed when the WI with the given automation ID is activated
* @returns {QuickReply} the altered quick reply
*/
updateQuickReply(setName, label, {
@@ -248,6 +252,7 @@ export class QuickReplyApi {
executeOnAi,
executeOnChatChange,
executeOnGroupMemberDraft,
automationId,
} = {}) {
const qr = this.getQrByLabel(setName, label);
if (!qr) {
@@ -262,6 +267,7 @@ export class QuickReplyApi {
qr.executeOnAi = executeOnAi ?? qr.executeOnAi;
qr.executeOnChatChange = executeOnChatChange ?? qr.executeOnChatChange;
qr.executeOnGroupMemberDraft = executeOnGroupMemberDraft ?? qr.executeOnGroupMemberDraft;
qr.automationId = automationId ?? qr.automationId;
qr.onUpdate();
return qr;
}

View File

@@ -70,6 +70,10 @@
<input type="checkbox" id="qr--executeOnGroupMemberDraft">
<span><i class="fa-solid fa-fw fa-people-group"></i> Execute before group member message</span>
</label>
<div class="flex-container alignItemsBaseline" title="Activate this quick reply when a World Info entry with the same Automation ID is triggered.">
<small>Automation ID</small>
<input type="text" id="qr--automationId" class="text_pole flex1" placeholder="( None )">
</div>
</div>

View File

@@ -104,6 +104,7 @@ const loadSets = async () => {
qr.executeOnAi = slot.autoExecute_botMessage ?? false;
qr.executeOnChatChange = slot.autoExecute_chatLoad ?? false;
qr.executeOnGroupMemberDraft = slot.autoExecute_groupMemberDraft ?? false;
qr.automationId = slot.automationId ?? false;
qr.contextList = (slot.contextMenu ?? []).map(it=>({
set: it.preset,
isChained: it.chain,
@@ -244,3 +245,8 @@ const onGroupMemberDraft = async () => {
await autoExec.handleGroupMemberDraft();
};
eventSource.on(event_types.GROUP_MEMBER_DRAFTED, (...args) => executeIfReadyElseQueue(onGroupMemberDraft, args));
const onWIActivation = async (entries) => {
await autoExec.handleWIActivation(entries);
};
eventSource.on(event_types.WORLD_INFO_ACTIVATED, (...args) => executeIfReadyElseQueue(onWIActivation, args));

View File

@@ -82,4 +82,20 @@ export class AutoExecuteHandler {
];
await this.performAutoExecute(qrList);
}
/**
* @param {any[]} entries Set of activated entries
*/
async handleWIActivation(entries) {
if (!this.checkExecute() || !Array.isArray(entries) || entries.length === 0) return;
const automationIds = entries.map(entry => entry.automationId).filter(Boolean);
if (automationIds.length === 0) return;
const qrList = [
...this.settings.config.setList.map(link=>link.set.qrList.filter(qr=>qr.automationId && automationIds.includes(qr.automationId))).flat(),
...(this.settings.chatConfig?.setList?.map(link=>link.set.qrList.filter(qr=>qr.automationId && automationIds.includes(qr.automationId)))?.flat() ?? []),
];
await this.performAutoExecute(qrList);
}
}

View File

@@ -31,6 +31,7 @@ export class QuickReply {
/**@type {Boolean}*/ executeOnAi = false;
/**@type {Boolean}*/ executeOnChatChange = false;
/**@type {Boolean}*/ executeOnGroupMemberDraft = false;
/**@type {String}*/ automationId = '';
/**@type {Function}*/ onExecute;
/**@type {Function}*/ onDelete;
@@ -359,6 +360,13 @@ export class QuickReply {
this.executeOnGroupMemberDraft = executeOnGroupMemberDraft.checked;
this.updateContext();
});
/**@type {HTMLInputElement}*/
const automationId = dom.querySelector('#qr--automationId');
automationId.value = this.automationId;
automationId.addEventListener('input', () => {
this.automationId = automationId.value;
this.updateContext();
});
/**@type {HTMLElement}*/
const executeErrors = dom.querySelector('#qr--modal-executeErrors');
@@ -492,6 +500,7 @@ export class QuickReply {
executeOnAi: this.executeOnAi,
executeOnChatChange: this.executeOnChatChange,
executeOnGroupMemberDraft: this.executeOnGroupMemberDraft,
automationId: this.automationId,
};
}
}

View File

@@ -150,6 +150,7 @@ export class SlashCommandHandler {
executeOnAi: isTrueBoolean(args.bot),
executeOnChatChange: isTrueBoolean(args.load),
executeOnGroupMemberDraft: isTrueBoolean(args.group),
automationId: args.automationId ?? '',
},
);
} catch (ex) {
@@ -171,6 +172,7 @@ export class SlashCommandHandler {
executeOnAi: args.bot === undefined ? undefined : isTrueBoolean(args.bot),
executeOnChatChange: args.load === undefined ? undefined : isTrueBoolean(args.load),
executeOnGroupMemberDraft: args.group === undefined ? undefined : isTrueBoolean(args.group),
automationId: args.automationId ?? '',
},
);
} catch (ex) {