mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-02 04:52:50 +08:00
Merge pull request #6 from ZHANGTIANYAO1/claude/setup-teamspeak-bot-QBfaS
Add comprehensive error handling to prevent process crashes
This commit is contained in:
6 files changed
+32
-2
No files matched your search
@@ -128,6 +128,14 @@ export class AudioPlayer extends EventEmitter {
|
|||||||
this.logger.info({ ffmpeg: ffmpegBin }, "Using ffmpeg binary");
|
this.logger.info({ ffmpeg: ffmpegBin }, "Using ffmpeg binary");
|
||||||
this.ffmpeg = spawn(ffmpegBin, args, { stdio: ["ignore", "pipe", "pipe"] });
|
this.ffmpeg = spawn(ffmpegBin, args, { stdio: ["ignore", "pipe", "pipe"] });
|
||||||
|
|
||||||
|
// Prevent stream errors from crashing the process
|
||||||
|
this.ffmpeg.stdout!.on("error", (err) => {
|
||||||
|
this.logger.warn({ err }, "FFmpeg stdout error");
|
||||||
|
});
|
||||||
|
this.ffmpeg.stderr!.on("error", (err) => {
|
||||||
|
this.logger.warn({ err }, "FFmpeg stderr error");
|
||||||
|
});
|
||||||
|
|
||||||
let gotFirstData = false;
|
let gotFirstData = false;
|
||||||
this.ffmpeg.stdout!.on("data", (chunk: Buffer) => {
|
this.ffmpeg.stdout!.on("data", (chunk: Buffer) => {
|
||||||
if (!gotFirstData) {
|
if (!gotFirstData) {
|
||||||
|
|||||||
+3
-1
@@ -99,7 +99,9 @@ export class BotInstance extends EventEmitter {
|
|||||||
|
|
||||||
private setupTsEvents(): void {
|
private setupTsEvents(): void {
|
||||||
this.tsClient.on("textMessage", (msg: TS3TextMessage) => {
|
this.tsClient.on("textMessage", (msg: TS3TextMessage) => {
|
||||||
this.handleTextMessage(msg);
|
this.handleTextMessage(msg).catch((err) => {
|
||||||
|
this.logger.error({ err }, "Unhandled error in text message handler");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
this.tsClient.on("disconnected", () => {
|
this.tsClient.on("disconnected", () => {
|
||||||
|
|||||||
@@ -25,6 +25,14 @@ async function main() {
|
|||||||
saveConfig(CONFIG_PATH, config);
|
saveConfig(CONFIG_PATH, config);
|
||||||
|
|
||||||
const logger = createLogger(LOG_DIR);
|
const logger = createLogger(LOG_DIR);
|
||||||
|
|
||||||
|
// Prevent unhandled errors from crashing the process
|
||||||
|
process.on("uncaughtException", (err) => {
|
||||||
|
logger.error({ err }, "Uncaught exception");
|
||||||
|
});
|
||||||
|
process.on("unhandledRejection", (reason) => {
|
||||||
|
logger.error({ reason }, "Unhandled promise rejection");
|
||||||
|
});
|
||||||
const db = createDatabase(DB_PATH);
|
const db = createDatabase(DB_PATH);
|
||||||
|
|
||||||
const apiServer = createApiServerManager(
|
const apiServer = createApiServerManager(
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
await this.client.connect();
|
await this.client.connect();
|
||||||
await this.client.waitConnected();
|
await this.client.waitConnected();
|
||||||
this.clientId = this.client.clientID();
|
this.clientId = this.client.clientID();
|
||||||
|
this.voiceFramesSent = 0;
|
||||||
this.logger.info({ clientId: this.clientId }, "Logged in (visible client)");
|
this.logger.info({ clientId: this.clientId }, "Logged in (visible client)");
|
||||||
|
|
||||||
// Join default channel if specified
|
// Join default channel if specified
|
||||||
|
|||||||
@@ -67,7 +67,14 @@ export function createWebServer(options: WebServerOptions): WebServer {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
server.on("error", (err) => {
|
||||||
|
logger.error({ err }, "HTTP server error");
|
||||||
|
});
|
||||||
|
|
||||||
const wss = new WebSocketServer({ server, path: "/ws" });
|
const wss = new WebSocketServer({ server, path: "/ws" });
|
||||||
|
wss.on("error", (err) => {
|
||||||
|
logger.error({ err }, "WebSocket server error");
|
||||||
|
});
|
||||||
const cleanupWs = setupWebSocket(wss, options.botManager, logger);
|
const cleanupWs = setupWebSocket(wss, options.botManager, logger);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -39,7 +39,11 @@ export function setupWebSocket(
|
|||||||
const message = JSON.stringify(data);
|
const message = JSON.stringify(data);
|
||||||
for (const client of clients) {
|
for (const client of clients) {
|
||||||
if (client.readyState === WebSocket.OPEN) {
|
if (client.readyState === WebSocket.OPEN) {
|
||||||
client.send(message);
|
try {
|
||||||
|
client.send(message);
|
||||||
|
} catch {
|
||||||
|
clients.delete(client);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user