时间 / 条件函数
时间
| 函数 | 返回 | 说明 |
|---|---|---|
now() | Long | 当前时间戳(毫秒) |
date() | String | 当前日期 "yyyy-MM-dd" |
time() | String | 当前时间 "HH:mm:ss" |
datetime() | String | 完整时间 "yyyy-MM-dd HH:mm:ss" |
hour() | Integer | 当前小时(0–23) |
minute() | Integer | 当前分钟(0–59) |
dayofweek() | Integer | 星期几(1=周一, 7=周日,ISO 标准) |
dayofmonth() | Integer | 当月第几天(1–31) |
month() | Integer | 当前月份(1–12) |
year() | Integer | 当前年份 |
yaml
# 时段判断
是周末 = dayofweek() >= 6 # 周六、周日
是工作日 = dayofweek() <= 5
是夜晚 = hour() >= 20 || hour() < 6
# 时间戳
过去毫秒 = now() - 上次时间
# 字符串形式
当前日期 = date() # "2026-04-17"
完整时间 = datetime() # "2026-04-17 14:30:45"没有 second()
IM 不提供 second() 函数。要取秒数,可解析 time():
yaml
秒数 = split(time(), ":", 2) # "45"dayofweek 对照表
| 返回值 | 星期 |
|---|---|
| 1 | 周一 |
| 2 | 周二 |
| 3 | 周三 |
| 4 | 周四 |
| 5 | 周五 |
| 6 | 周六 |
| 7 | 周日 |
条件 / 空值
| 函数 | 参数 | 返回 | 说明 |
|---|---|---|---|
clamp(value, min, max) | Number×3 | Number | 限制在范围内(详见 数学函数) |
between(value, min, max) | Number×3 | Boolean | 是否在 [min, max] 范围内 |
coalesce(a, b, ...) | Any... | Any | 返回第一个非 null / 非 0 的值 |
ifnull(value, default) | Any×2 | Any | 值为 null 时返回默认值 |
typeof(value) | Any | String | 返回类型名 |
typeof 返回值
| 返回 | 类型 |
|---|---|
"null" | null |
"boolean" | 布尔 |
"number" | 数字 |
"string" | 字符串 |
"list" | 列表 |
"item" | 物品 ItemStack |
"map" | 映射 |
"object" | 其他对象 |
yaml
# 取名称,没有就用默认值
名称 = ifnull(@input.name, "未知物品")
# 取第一个非空的
显示名 = coalesce(nbt_displayName, lore_firstLine, material_name)
# 类型判断
if typeof(数据) == "list" {
大小 = list_size(数据)
}
if typeof(@input) == "item" {
// 槽位有物品
}何时用 ifnull vs coalesce
| 场景 | 推荐 |
|---|---|
| 一个值 + 一个默认 | ifnull(值, 默认) |
| 多个候选值,取第一个有效的 | coalesce(a, b, c, ...) |
| 数值范围夹断 | clamp(v, min, max) |
| 区间判断返回布尔 | between(v, min, max) |