跳到主要内容

脚本定义

脚本定义文件是 script/ 目录下的 YAML 文件,每个文件描述一个可执行脚本。

文件 ID 规则

脚本 ID 默认从文件的相对路径推导:去掉扩展名,将 / 替换为 .

文件路径推导出的 ID
script/hello.ymlhello
script/example/hello.ymlexample.hello
script/combat/damage/calc.ymlcombat.damage.calc

你也可以在定义中通过 id 字段手动指定,覆盖自动推导的值。

完整字段

id: example.hello
enabled: true
preheat: false
async-allowed: true
timeout-ms: 0
tags:
- example
metadata:
author: test
parameters:
player: Player
amount: Double
condition: check.permission
deny: notify.no-permission
functions:
helper: example.helper-script
on-timeout: notify.timeout
on-exception: notify.error
return-conversion: String
source:
type: fluxon
content: |
print "Hello, world!"

字段说明

字段类型默认值说明
idString从文件路径推导脚本唯一标识
enabledBooleantrue是否启用此脚本
preheatBooleanfalse是否在加载时预编译(而非首次执行时编译)
async-allowedBooleantrue是否允许异步执行
timeout-msLong0执行超时时间(毫秒),0 表示不限制
tagsSet<String>分类标签,用于筛选和管理
metadataMap任意键值对元数据
parametersMap参数声明,格式为 参数名: Applicative 类型名
conditionString前置条件脚本 ID,返回 false 时拒绝执行
denyString条件不满足时执行的脚本 ID
functionsMap自定义函数映射,格式为 函数名: 脚本 ID
on-timeoutString超时回调脚本 ID
on-exceptionString异常回调脚本 ID
return-conversionString返回值类型转换(Applicative 类型名)
source.typeStringfluxon脚本类型
source.contentString脚本内容(内联 Fluxon 代码)

关于 Applicative 类型名

parametersreturn-conversion 里出现的类型名,来自 Monoceros 的 Applicative 转换系统。

如果你想进一步理解这些类型名是怎么注册、怎么转换的,可以继续看 ../api/applicative.md

前置条件与拒绝回调

你可以通过 condition 指定一个前置条件脚本。执行前会先运行该脚本,如果返回 false,则主脚本不会执行,转而执行 deny 指定的脚本(如果配置了的话)。

condition: check.vip-level
deny: notify.vip-required

超时与异常处理

  • timeout-ms 设置执行超时,超时后自动终止并触发 on-timeout 回调
  • 执行过程中抛出异常时触发 on-exception 回调

这两个回调都是可选的,不配置则静默处理。