获取脚本
提示
请务必先阅读如何正确使用酒馆助手
本文档介绍如何获取酒馆助手脚本树、脚本按钮和脚本信息。
getScriptTrees
获取酒馆助手脚本列表,支持获取全局脚本、预设脚本或角色卡脚本。
ts
function getScriptTrees(option: ScriptTreesOptions): ScriptTree[];ts
type ScriptTreesOptions = {
/** 对全局脚本 ('global')、当前预设脚本 ('preset') 或当前角色卡脚本 ('character') 进行操作 */
type: 'global' | 'preset' | 'character';
};ts
type ScriptTree = Script | ScriptFolder;ts
type Script = {
type: 'script';
enabled: boolean;
name: string;
id: string;
content: string;
info: string;
button: {
enabled: boolean;
buttons: Array<ScriptButton>;
};
data: Record<string, any>;
};ts
type ScriptFolder = {
type: 'folder';
enabled: boolean;
name: string;
id: string;
icon: string;
color: string;
scripts: Script[];
};ts
type ScriptButton = {
name: string;
visible: boolean;
};参数
option.type
- 类型:
'global' | 'preset' | 'character' - 描述: 要操作的脚本类型
'global'- 全局脚本,对所有角色卡和聊天生效'preset'- 当前预设脚本,绑定到当前使用的预设'character'- 当前角色卡脚本,绑定到当前角色卡
- 必需: 是
返回值
- 脚本树数组:
ScriptTree[]
示例
ts
const globalScripts = getScriptTrees({ type: 'global' });
console.log('全局脚本:', globalScripts);ts
const presetScripts = getScriptTrees({ type: 'preset' });
console.log('预设脚本:', presetScripts);ts
const characterScripts = getScriptTrees({ type: 'character' });
console.log('角色卡脚本:', characterScripts);ts
const scripts = getScriptTrees({ type: 'global' });
scripts.forEach(item => {
if (item.type === 'script') {
console.log('脚本:', item.name);
console.log('启用状态:', item.enabled);
console.log('按钮数量:', item.button.buttons.length);
} else if (item.type === 'folder') {
console.log('文件夹:', item.name);
item.scripts.forEach(script => {
console.log('脚本:', script.name);
});
}
});getAllEnabledScriptButtons
获取所有处于启用状态的酒馆助手脚本按钮,主要是方便 QR 助手等兼容脚本按钮。
ts
function getAllEnabledScriptButtons(): {
[script_id: string]: { button_id: string; button_name: string }[]
};返回值
- 所有启用的脚本按钮:
{ [script_id: string]: { button_id: string; button_name: string }[] }
示例
ts
const enabledButtons = getAllEnabledScriptButtons();
console.log('所有启用的按钮:', enabledButtons);
// 遍历所有脚本的按钮
for (const [scriptId, buttons] of Object.entries(enabledButtons)) {
console.log(`脚本 ${scriptId} 的按钮:`, buttons);
}getScriptButtons 🚫TavernHelper
获取当前脚本的按钮列表,只能在脚本中使用。
ts
function getScriptButtons(): ScriptButton[];ts
type ScriptButton = {
name: string; // 按钮名称
visible: boolean; // 按钮是否可见
};返回值
- 脚本按钮数组:
ScriptButton[]
示例
ts
const buttons = getScriptButtons();
console.log('当前按钮:', buttons);
// 检查是否有特定按钮
const hasStartButton = buttons.some(btn => btn.name === '开始游戏');
console.log('是否有开始游戏按钮:', hasStartButton);getButtonEvent 🚫TavernHelper
获取按钮对应的事件类型,只能在脚本中使用。
ts
function getButtonEvent(button_name: string): string;参数
button_name
- 类型:
string - 描述: 按钮名称
- 必需: 是
返回值
- 事件类型:
string
示例
ts
const event_type = getButtonEvent('按钮名');
eventOn(event_type, () => {
console.log('按钮被点击了');
});ts
const buttons = ['开始', '继续', '重新开始'];
buttons.forEach(buttonName => {
const eventType = getButtonEvent(buttonName);
eventOn(eventType, () => {
console.log(`${buttonName} 按钮被点击`);
});
});getScriptName 🚫TavernHelper
获取当前脚本的名称,只能在脚本中使用。
ts
function getScriptName(): string;返回值
- 脚本名称:
string
示例
ts
const scriptName = getScriptName();
console.log('当前脚本名称:', scriptName);getScriptInfo 🚫TavernHelper
获取当前脚本的作者注释,只能在脚本中使用。
ts
function getScriptInfo(): string;返回值
- 脚本作者注释:
string
示例
ts
const name = getScriptName();
const info = getScriptInfo();
console.log(`脚本: ${name}`);
console.log(`说明: ${info}`);