mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-02 04:52:50 +08:00
fix: address Phase 2 review — error handlers, drain queue, prevent listener accumulation
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
a6bfd9d32c
commit
d66355a829
3 files changed
+39
-25
No files matched your search
+29
-19
@@ -49,6 +49,35 @@ export class TS3Client extends EventEmitter {
|
|||||||
port: options.port,
|
port: options.port,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.connection.on("error", (err) => {
|
||||||
|
this.logger.error({ err }, "TCP connection error");
|
||||||
|
this.emit("error", err);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.voice.on("error", (err) => {
|
||||||
|
this.logger.error({ err }, "UDP voice error");
|
||||||
|
this.emit("error", err);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.connection.on(
|
||||||
|
"notify:textmessage",
|
||||||
|
(data: Record<string, string>) => {
|
||||||
|
const msg: TS3TextMessage = {
|
||||||
|
invokerName: data.invokername ?? "",
|
||||||
|
invokerId: data.invokerid ?? "",
|
||||||
|
invokerUid: data.invokeruid ?? "",
|
||||||
|
message: data.msg ?? "",
|
||||||
|
targetMode: parseInt(data.targetmode ?? "0", 10),
|
||||||
|
};
|
||||||
|
this.emit("textMessage", msg);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
this.connection.on("close", () => {
|
||||||
|
this.logger.warn("Connection closed");
|
||||||
|
this.emit("disconnected");
|
||||||
|
});
|
||||||
|
|
||||||
if (options.identity) {
|
if (options.identity) {
|
||||||
this.identity = importIdentity(options.identity);
|
this.identity = importIdentity(options.identity);
|
||||||
} else {
|
} else {
|
||||||
@@ -91,27 +120,8 @@ export class TS3Client extends EventEmitter {
|
|||||||
await this.sendCommand("servernotifyregister", { event: "textchannel" });
|
await this.sendCommand("servernotifyregister", { event: "textchannel" });
|
||||||
await this.sendCommand("servernotifyregister", { event: "textprivate" });
|
await this.sendCommand("servernotifyregister", { event: "textprivate" });
|
||||||
|
|
||||||
this.connection.on(
|
|
||||||
"notify:textmessage",
|
|
||||||
(data: Record<string, string>) => {
|
|
||||||
const msg: TS3TextMessage = {
|
|
||||||
invokerName: data.invokername ?? "",
|
|
||||||
invokerId: data.invokerid ?? "",
|
|
||||||
invokerUid: data.invokeruid ?? "",
|
|
||||||
message: data.msg ?? "",
|
|
||||||
targetMode: parseInt(data.targetmode ?? "0", 10),
|
|
||||||
};
|
|
||||||
this.emit("textMessage", msg);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
this.keepAliveInterval = setInterval(() => {
|
this.keepAliveInterval = setInterval(() => {
|
||||||
this.voice.sendKeepAlive();
|
this.voice.sendKeepAlive();
|
||||||
}, 5000);
|
|
||||||
|
|
||||||
this.connection.on("close", () => {
|
|
||||||
this.logger.warn("Connection closed");
|
|
||||||
this.emit("disconnected");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.emit("connected");
|
this.emit("connected");
|
||||||
|
|||||||
@@ -62,12 +62,6 @@ export function decodeResponse(raw: string): Record<string, string>[] {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TS3Response {
|
|
||||||
errorId: number;
|
|
||||||
errorMessage: string;
|
|
||||||
data: Record<string, string>[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseErrorLine(line: string): { id: number; msg: string } {
|
export function parseErrorLine(line: string): { id: number; msg: string } {
|
||||||
const decoded = decodeResponse(line.replace(/^error\s+/, ""))[0];
|
const decoded = decodeResponse(line.replace(/^error\s+/, ""))[0];
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ export class TS3Connection extends EventEmitter {
|
|||||||
});
|
});
|
||||||
this.socket.on("close", () => {
|
this.socket.on("close", () => {
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
|
this.drainCommandQueue(new Error("Connection closed"));
|
||||||
this.emit("close");
|
this.emit("close");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -126,6 +127,15 @@ export class TS3Connection extends EventEmitter {
|
|||||||
this.socket = null;
|
this.socket = null;
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
}
|
}
|
||||||
|
this.drainCommandQueue(new Error("Disconnected"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private drainCommandQueue(error: Error): void {
|
||||||
|
for (const pending of this.commandQueue) {
|
||||||
|
pending.reject(error);
|
||||||
|
}
|
||||||
|
this.commandQueue = [];
|
||||||
|
this.responseLines = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
isConnected(): boolean {
|
isConnected(): boolean {
|
||||||
|
|||||||
Reference in new issue
Block a user