Skip to content

修改脚本

提示

请务必先阅读如何正确使用酒馆助手

本文档介绍如何创建、修改酒馆助手脚本树、脚本按钮和脚本信息。

点击查看对应类型定义文件

replaceScriptTrees

完全替换酒馆助手脚本列表。

警告

  • 这是完全替换操作,会清空现有的脚本列表并替换为新列表。请谨慎使用
ts
function replaceScriptTrees(
  script_trees: PartialDeep<ScriptTree>[],
  option: ScriptTreesOptions
): void;
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;
};

参数

script_trees

  • 类型: PartialDeep<ScriptTree>[]
  • 描述: 要用于替换的脚本树列表

option.type

  • 类型: 'global' | 'preset' | 'character'
  • 描述: 要操作的脚本类型
    • 'global' - 全局脚本
    • 'preset' - 当前预设脚本
    • 'character' - 当前角色卡脚本

示例

ts
const newScripts = [
  {
    type: 'script',
    name: '新脚本',
    enabled: true,
    content: 'console.log("Hello");'
  }
];
replaceScriptTrees(newScripts, { type: 'global' });
ts
const characterScripts = [
  {
    type: 'folder',
    name: '角色专用脚本',
    enabled: true,
    scripts: [
      {
        type: 'script',
        name: '角色初始化',
        enabled: true,
        content: '// 初始化代码'
      }
    ]
  }
];
replaceScriptTrees(characterScripts, { type: 'character' });
ts
// 传入空数组可以清空脚本列表
replaceScriptTrees([], { type: 'preset' });

updateScriptTreesWith

使用更新函数更新酒馆助手脚本列表,支持同步和异步更新。

ts
function updateScriptTreesWith(
  updater: (script_trees: ScriptTree[]) => PartialDeep<ScriptTree>[],
  option: ScriptTreesOptions
): ScriptTree[];

function updateScriptTreesWith(
  updater: (script_trees: ScriptTree[]) => Promise<PartialDeep<ScriptTree>[]>,
  option: ScriptTreesOptions
): Promise<ScriptTree[]>;

参数

updater

  • 类型: (script_trees: ScriptTree[]) => PartialDeep<ScriptTree>[](script_trees: ScriptTree[]) => Promise<PartialDeep<ScriptTree>[]>
  • 描述: 更新函数,接收当前脚本树列表,返回更新后的列表

option.type

  • 类型: 'global' | 'preset' | 'character'
  • 描述: 要操作的脚本类型

返回值

  • 更新后的脚本树列表: ScriptTree[]Promise<ScriptTree[]>

示例

ts
updateScriptTreesWith((scripts) => {
  scripts.forEach(item => {
    if (item.type === 'script' && item.name.includes('测试')) {
      item.enabled = true;
    }
  });
  return scripts;
}, { type: 'global' });
ts
updateScriptTreesWith((scripts) => {
  scripts.forEach(item => {
    if (item.type === 'script') {
      item.name = '[修改] ' + item.name;
    } else if (item.type === 'folder') {
      item.name = '[文件夹] ' + item.name;
    }
  });
  return scripts;
}, { type: 'character' });
ts
updateScriptTreesWith((scripts) => {
  scripts.forEach(item => {
    item.enabled = false;
    if (item.type === 'folder') {
      item.scripts.forEach(script => {
        script.enabled = false;
      });
    }
  });
  return scripts;
}, { type: 'global' });
ts
await updateScriptTreesWith(async (scripts) => {
  // 执行异步操作,例如从服务器获取配置
  const config = await fetch('/api/script-config').then(r => r.json());

  // 根据配置更新脚本
  scripts.forEach(item => {
    if (item.type === 'script' && config.enabledScripts.includes(item.name)) {
      item.enabled = true;
    }
  });

  return scripts;
}, { type: 'preset' });
ts
updateScriptTreesWith((scripts) => {
  return [
    ...scripts,
    {
      type: 'script',
      name: '新增脚本',
      enabled: true,
      content: 'console.log("新脚本");'
    }
  ];
}, { type: 'global' });

replaceScriptButtons 🚫TavernHelper

完全替换当前脚本的按钮列表,只能在脚本中使用

ts
function replaceScriptButtons(buttons: ScriptButton[]): void;
ts
type ScriptButton = {
  name: string;
  visible: boolean;
};

参数

buttons

  • 类型: ScriptButton[]
  • 描述: 新的按钮数组,将完全替换现有按钮

示例

ts
replaceScriptButtons([{ name: '开始游戏', visible: true }]);
ts
eventOnButton("前往地点", () => {
  replaceScriptButtons([
    { name: '学校', visible: true },
    { name: '商店', visible: true },
    { name: '公园', visible: true }
  ]);
});
ts
replaceScriptButtons([]);
ts
const locations = ['家', '学校', '商店', '公园', '图书馆'];
const buttons = locations.map(loc => ({
  name: loc,
  visible: true
}));
replaceScriptButtons(buttons);

updateScriptButtonsWith 🚫TavernHelper

使用更新函数更新脚本按钮列表,只能在脚本中使用

ts
function updateScriptButtonsWith(
  updater: (buttons: ScriptButton[]) => ScriptButton[]
): ScriptButton[];

function updateScriptButtonsWith(
  updater: (buttons: ScriptButton[]) => Promise<ScriptButton[]>
): Promise<ScriptButton[]>;

参数

updater

  • 类型: (buttons: ScriptButton[]) => ScriptButton[](buttons: ScriptButton[]) => Promise<ScriptButton[]>
  • 描述: 用于更新脚本按钮列表的函数。它应该接收脚本按钮列表作为参数,并返回更新后的脚本按钮列表

返回值

  • 更新后的脚本按钮列表: ScriptButton[]Promise<ScriptButton[]>

示例

ts
updateScriptButtonsWith(buttons => [
  ...buttons,
  { name: '新按钮', visible: true }
]);
ts
updateScriptButtonsWith(buttons => {
  return buttons.map(btn => ({
    ...btn,
    visible: false
  }));
});
ts
updateScriptButtonsWith(buttons => {
  return buttons.map(btn => ({
    ...btn,
    visible: !btn.visible
  }));
});
ts
updateScriptButtonsWith(buttons => {
  return buttons.filter(btn => btn.name !== '要删除的按钮');
});
ts
updateScriptButtonsWith(buttons => {
  return buttons.map(btn => {
    if (btn.name === '旧名称') {
      return { ...btn, name: '新名称' };
    }
    return btn;
  });
});

appendInexistentScriptButtons 🚫TavernHelper

为脚本按钮列表末尾添加不存在的按钮,不会重复添加同名按钮,只能在脚本中使用

ts
function appendInexistentScriptButtons(buttons: ScriptButton[]): void;

参数

buttons

  • 类型: ScriptButton[]
  • 描述: 要添加的按钮数组,只会添加当前不存在的按钮

示例

ts
appendInexistentScriptButtons([{ name: '重新开始', visible: true }]);
ts
appendInexistentScriptButtons([
  { name: '保存进度', visible: true },
  { name: '读取进度', visible: true },
  { name: '返回主菜单', visible: true }
]);
// 即使用户删除了这些按钮,下次运行时也会自动添加回来

replaceScriptInfo 🚫TavernHelper

替换当前脚本的作者注释,只能在脚本中使用

ts
function replaceScriptInfo(info: string): void;

参数

info

  • 类型: string
  • 描述: 新的作者注释

示例

ts
replaceScriptInfo('这是一个游戏脚本,用于管理游戏流程');
ts
const version = '1.0.0';
const author = '作者名';
const description = '脚本描述';

replaceScriptInfo(`
版本: ${version}
作者: ${author}
描述: ${description}
最后更新: ${new Date().toLocaleDateString()}
`);
ts
replaceScriptInfo(`
【使用说明】
1. 点击"开始游戏"按钮开始
2. 选择地点进行探索
3. 点击"保存进度"保存当前状态

【注意事项】
- 请勿在对话中途切换角色卡
- 建议定期保存进度
`);

作者:KAKAA, 青空莉想做舞台少女的狗