存在 - 完整API参考
管理在线状态,跟踪跨设备的活动,并同步存在信息。
聊天命令
查看状态
/presence 显示您的状态
/presence who 谁在线
/presence activity 近期活动
设置状态
/presence online 设置在线
/presence away 设置离开
/presence dnd 请勿打扰
/presence offline 显示离线
/presence status "在会议中" 自定义状态
设备
/presence devices 您连接的设备
/presence sync 强制同步所有设备
TypeScript API参考
创建存在服务
import { createPresenceService } from 'clodds/presence';
const presence = createPresenceService({
// 更新间隔
heartbeatIntervalMs: 30000,
// 空闲后自动离开
awayAfterMs: 300000, // 5分钟空闲
// 存储
storage: 'redis', // 'redis' | 'memory'
redisUrl: process.env.REDIS_URL,
});
获取状态
// 获取自己的状态
const status = await presence.getStatus(userId);
console.log(`状态: ${status.status}`); // 'online' | 'away' | 'dnd' | 'offline'
console.log(`自定义: ${status.customStatus}`);
console.log(`最后在线: ${status.lastSeen}`);
console.log(`设备: ${status.activeDevice}`);
// 获取多个用户
const statuses = await presence.getStatuses(['user-1', 'user-2', 'user-3']);
设置状态
// 设置状态
await presence.setStatus(userId, 'online');
await presence.setStatus(userId, 'away');
await presence.setStatus(userId, 'dnd');
await presence.setStatus(userId, 'offline');
// 设置自定义状态消息
await presence.setCustomStatus(userId, '交易BTC');
// 清除自定义状态
await presence.clearCustomStatus(userId);
活动跟踪
// 记录活动
await presence.recordActivity(userId, {
type: 'message',
channelId: 'telegram-123',
timestamp: Date.now(),
});
// 获取近期活动
const activity = await presence.getActivity(userId, {
limit: 10,
since: Date.now() - 3600000, // 上一小时
});
for (const event of activity) {
console.log(`${event.type} 在 ${event.timestamp}`);
console.log(` 频道: ${event.channelId}`);
}
设备存在
// 获取用户的设备
const devices = await presence.getDevices(userId);
for (const device of devices) {
console.log(`${device.id}: ${device.name}`);
console.log(` 状态: ${device.status}`);
console.log(` 最后在线: ${device.lastSeen}`);
console.log(` 活跃: ${device.isActive}`);
}
// 设置设备状态
await presence.setDeviceStatus(userId, deviceId, 'online');
谁在线
// 获取在线用户
const online = await presence.getOnlineUsers({
channelId: 'telegram-123', // 可选:按频道过滤
});
for (const user of online) {
console.log(`${user.name}: ${user.status}`);
}
事件处理程序
// 状态变化
presence.on('statusChange', (userId, oldStatus, newStatus) => {
console.log(`${userId}: ${oldStatus} -> ${newStatus}`);
});
// 用户上线
presence.on('online', (userId) => {
console.log(`${userId} 现在在线`);
});
// 用户下线
presence.on('offline', (userId) => {
console.log(`${userId} 下线了`);
});
跨设备同步
// 强制同步
await presence.sync(userId);
// 获取同步状态
const syncStatus = await presence.getSyncStatus(userId);
console.log(`设备已同步: ${syncStatus.synced}/${syncStatus.total}`);
console.log(`最后同步: ${syncStatus.lastSync}`);
状态类型
| 状态 | 描述 |
|---|---|
online |
活跃且可用 |
away |
空闲/不活跃 |
dnd |
请勿打扰 |
offline |
不可用 |
自动离开
存在在不活动后自动变为away:
const presence = createPresenceService({
awayAfterMs: 300000, // 5分钟空闲 -> 离开
offlineAfterMs: 3600000, // 1小时空闲 -> 离线
});
多设备同步
当用户在多个设备上活跃时:
- 最近的活动决定主设备
- 状态在所有设备间同步
- 自定义状态在各处共享
最佳实践
- 使用心跳 — 保持状态准确
- 适当设置离开 — 不要频繁更改状态
- 自定义状态 — 让他人知道你在做什么
- 审查设备 — 保持设备列表清洁
- DND以专注 — 在交易期间静音通知