Files
2026-10-22 17:07:08 +08:00

51 lines
2.0 KiB
TypeScript

import {get_bot_context_status} from "../../../bot/instance";
import type {Socket} from "socket.io-client";
import {console_print} from "../../../utils/console_print.ts";
import {bot_status} from "../../../types/bot/instance/bot_runtime.ts";
export async function update_all_bot_status(socket: Socket, node_id: number) {
const bot_status = await get_bot_context_status();
if (!socket.connected) return;
try {
const response = await socket
.timeout(5000)
.emitWithAck('update_all_bot_status', {
id: node_id,
bot_status,
});
if (!response || (response.status !== 'success' && response.status !== 'error')) {
throw new Error('响应格式无效');
}
if (response.status === 'error') {
throw new Error(`收到错误码: ${response.data}`);
}
console_print.log(console_print.success("机器人状态更新成功"))
} catch (error) {
throw new Error(`机器人状态上报失败: ${error instanceof Error ? error.message : String(error)}`);
}
}
export async function update_one_bot_status(socket: Socket, node_id: number, bot_id: number, user_id: number, status: bot_status) {
if (!socket.connected) return;
try {
const response = await socket
.timeout(5000)
.emitWithAck('update_one_bot_status', {
id: node_id,
user_id,
bot_id,
status,
});
if (!response || (response.status !== 'success' && response.status !== 'error')) {
throw new Error('响应格式无效');
}
if (response.status === 'error') {
throw new Error(`收到错误码: ${response.data}`);
}
console_print.log(console_print.success(`机器人 ${bot_id} 状态更新成功 更新为 ${status}`));
} catch (error) {
throw new Error(`机器人 ${bot_id} 状态上报失败: ${error instanceof Error ? error.message : String(error)}`);
}
}