mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-02 04:52:50 +08:00
chore: format
This commit is contained in:
1 parent
4244695075
commit
05f090d83a
1 file changed
+23
-70
+23
-70
@@ -73,10 +73,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
private httpQuery: TS6HttpQuery | null = null;
|
private httpQuery: TS6HttpQuery | null = null;
|
||||||
private udpErrorTimer: ReturnType<typeof setTimeout> | null = null;
|
private udpErrorTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
constructor(
|
constructor(private options: TS3ClientOptions, logger: Logger) {
|
||||||
private options: TS3ClientOptions,
|
|
||||||
logger: Logger,
|
|
||||||
) {
|
|
||||||
super();
|
super();
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
|
||||||
@@ -113,10 +110,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
const addr = `${this.options.host}:${this.options.port}`;
|
const addr = `${this.options.host}:${this.options.port}`;
|
||||||
|
|
||||||
// Detect or use forced protocol
|
// Detect or use forced protocol
|
||||||
if (
|
if (this.options.serverProtocol && this.options.serverProtocol !== "unknown") {
|
||||||
this.options.serverProtocol &&
|
|
||||||
this.options.serverProtocol !== "unknown"
|
|
||||||
) {
|
|
||||||
this.detectedProtocol = this.options.serverProtocol;
|
this.detectedProtocol = this.options.serverProtocol;
|
||||||
this.logger.info(
|
this.logger.info(
|
||||||
{ addr, protocol: this.detectedProtocol },
|
{ addr, protocol: this.detectedProtocol },
|
||||||
@@ -139,11 +133,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
this.logger.info(
|
this.logger.info(
|
||||||
{
|
{ addr, protocol: this.detectedProtocol, queryPort: detection.queryPort },
|
||||||
addr,
|
|
||||||
protocol: this.detectedProtocol,
|
|
||||||
queryPort: detection.queryPort,
|
|
||||||
},
|
|
||||||
`Server protocol detected: ${this.detectedProtocol.toUpperCase()}`,
|
`Server protocol detected: ${this.detectedProtocol.toUpperCase()}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -151,8 +141,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
|
|
||||||
// Set up TS6 HTTP Query if applicable
|
// Set up TS6 HTTP Query if applicable
|
||||||
if (this.detectedProtocol === "ts6") {
|
if (this.detectedProtocol === "ts6") {
|
||||||
const queryPort =
|
const queryPort = this.options.queryPort !== 10011 ? this.options.queryPort : 10080;
|
||||||
this.options.queryPort !== 10011 ? this.options.queryPort : 10080;
|
|
||||||
this.httpQuery = new TS6HttpQuery({
|
this.httpQuery = new TS6HttpQuery({
|
||||||
host: this.options.host,
|
host: this.options.host,
|
||||||
port: queryPort,
|
port: queryPort,
|
||||||
@@ -163,9 +152,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
// Guard against calling connect() while already connected.
|
// Guard against calling connect() while already connected.
|
||||||
// Save detectedProtocol first because disconnect() resets it.
|
// Save detectedProtocol first because disconnect() resets it.
|
||||||
if (this.client) {
|
if (this.client) {
|
||||||
this.logger.warn(
|
this.logger.warn("connect() called while already connected, disconnecting first");
|
||||||
"connect() called while already connected, disconnecting first",
|
|
||||||
);
|
|
||||||
const savedProtocol = this.detectedProtocol;
|
const savedProtocol = this.detectedProtocol;
|
||||||
const savedHttpQuery = this.httpQuery;
|
const savedHttpQuery = this.httpQuery;
|
||||||
this.disconnect();
|
this.disconnect();
|
||||||
@@ -192,9 +179,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
if (this.udpErrorTimer) clearTimeout(this.udpErrorTimer);
|
if (this.udpErrorTimer) clearTimeout(this.udpErrorTimer);
|
||||||
this.udpErrorTimer = setTimeout(() => {
|
this.udpErrorTimer = setTimeout(() => {
|
||||||
if (udpErrorCount > 1) {
|
if (udpErrorCount > 1) {
|
||||||
this.logger.warn(
|
this.logger.warn(`udp send error (repeated ${udpErrorCount} times, connection may be lost)`);
|
||||||
`udp send error (repeated ${udpErrorCount} times, connection may be lost)`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
udpErrorCount = 0;
|
udpErrorCount = 0;
|
||||||
this.udpErrorTimer = null;
|
this.udpErrorTimer = null;
|
||||||
@@ -205,11 +190,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
this.logger.warn(msg);
|
this.logger.warn(msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.client = new TS3FullClient(
|
this.client = new TS3FullClient(this.identity, addr, this.options.nickname, {
|
||||||
this.identity,
|
|
||||||
addr,
|
|
||||||
this.options.nickname,
|
|
||||||
{
|
|
||||||
// Forward server password to the protocol library so it can be
|
// Forward server password to the protocol library so it can be
|
||||||
// included in clientinit for password-protected servers
|
// included in clientinit for password-protected servers
|
||||||
serverPassword: this.options.serverPassword,
|
serverPassword: this.options.serverPassword,
|
||||||
@@ -219,8 +200,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
warn: throttledWarn,
|
warn: throttledWarn,
|
||||||
error: (msg) => this.logger.error(msg),
|
error: (msg) => this.logger.error(msg),
|
||||||
},
|
},
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
this.client.on("textMessage", (msg: TextMessage) => {
|
this.client.on("textMessage", (msg: TextMessage) => {
|
||||||
const tsMsg: TS3TextMessage = {
|
const tsMsg: TS3TextMessage = {
|
||||||
@@ -242,7 +222,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
this.client.on("clientEnter", (info: ClientInfo) => {
|
this.client.on("clientEnter", (info: ClientInfo) => {
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
{ nickname: info.nickname, id: info.id },
|
{ nickname: info.nickname, id: info.id },
|
||||||
"Client entered",
|
"Client entered"
|
||||||
);
|
);
|
||||||
this.emit("clientEnter", info);
|
this.emit("clientEnter", info);
|
||||||
});
|
});
|
||||||
@@ -255,7 +235,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
this.client.on("clientMoved", (ev: ClientMovedEvent) => {
|
this.client.on("clientMoved", (ev: ClientMovedEvent) => {
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
{ id: ev.id, targetChannelID: ev.targetChannelID.toString() },
|
{ id: ev.id, targetChannelID: ev.targetChannelID.toString() },
|
||||||
"Client moved",
|
"Client moved"
|
||||||
);
|
);
|
||||||
this.emit("clientMoved", ev);
|
this.emit("clientMoved", ev);
|
||||||
});
|
});
|
||||||
@@ -277,14 +257,11 @@ export class TS3Client extends EventEmitter {
|
|||||||
|
|
||||||
// Join channel by numeric ID (takes precedence) or by name
|
// Join channel by numeric ID (takes precedence) or by name
|
||||||
if (this.options.channelId) {
|
if (this.options.channelId) {
|
||||||
await this.joinChannel(
|
await this.joinChannel(this.options.channelId, this.options.channelPassword);
|
||||||
this.options.channelId,
|
|
||||||
this.options.channelPassword,
|
|
||||||
);
|
|
||||||
} else if (this.options.defaultChannel) {
|
} else if (this.options.defaultChannel) {
|
||||||
await this.joinChannel(
|
await this.joinChannel(
|
||||||
this.options.defaultChannel,
|
this.options.defaultChannel,
|
||||||
this.options.channelPassword,
|
this.options.channelPassword
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,12 +274,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
const isNumeric = /^\d+$/.test(channelName);
|
const isNumeric = /^\d+$/.test(channelName);
|
||||||
if (isNumeric) {
|
if (isNumeric) {
|
||||||
try {
|
try {
|
||||||
await clientMove(
|
await clientMove(this.client, this.clientId, BigInt(channelName), password);
|
||||||
this.client,
|
|
||||||
this.clientId,
|
|
||||||
BigInt(channelName),
|
|
||||||
password,
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error({ err, channelName }, "Failed to join channel");
|
this.logger.error({ err, channelName }, "Failed to join channel");
|
||||||
}
|
}
|
||||||
@@ -312,14 +284,16 @@ export class TS3Client extends EventEmitter {
|
|||||||
try {
|
try {
|
||||||
const channels = await listChannels(this.client);
|
const channels = await listChannels(this.client);
|
||||||
const channel = channels.find((ch) => ch.name === channelName);
|
const channel = channels.find((ch) => ch.name === channelName);
|
||||||
|
|
||||||
if (!channel) {
|
if (!channel) {
|
||||||
this.logger.warn({ channelName }, "Channel not found");
|
this.logger.warn({ channelName }, "Channel not found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await clientMove(this.client, this.clientId, channel.id, password);
|
await clientMove(this.client, this.clientId, channel.id, password);
|
||||||
this.logger.info(
|
this.logger.info(
|
||||||
{ channelName, cid: channel.id.toString() },
|
{ channelName, cid: channel.id.toString() },
|
||||||
"Joined channel",
|
"Joined channel"
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error({ err, channelName }, "Failed to join channel");
|
this.logger.error({ err, channelName }, "Failed to join channel");
|
||||||
@@ -328,7 +302,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
|
|
||||||
async sendTextMessage(
|
async sendTextMessage(
|
||||||
message: string,
|
message: string,
|
||||||
targetMode: number = 2,
|
targetMode: number = 2
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!this.client) return;
|
if (!this.client) return;
|
||||||
// targetMode 2 = channel, target 0 = current channel
|
// targetMode 2 = channel, target 0 = current channel
|
||||||
@@ -360,9 +334,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
await this.client.sendCommandNoWait(cmd);
|
await this.client.sendCommandNoWait(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
async execCommandWithResponse(
|
async execCommandWithResponse(cmd: string): Promise<Record<string, string>[]> {
|
||||||
cmd: string,
|
|
||||||
): Promise<Record<string, string>[]> {
|
|
||||||
if (!this.client) throw new Error("Not connected");
|
if (!this.client) throw new Error("Not connected");
|
||||||
return this.client.execCommandWithResponse(cmd);
|
return this.client.execCommandWithResponse(cmd);
|
||||||
}
|
}
|
||||||
@@ -375,28 +347,15 @@ export class TS3Client extends EventEmitter {
|
|||||||
overwrite = true,
|
overwrite = true,
|
||||||
): Promise<FileUploadInfo> {
|
): Promise<FileUploadInfo> {
|
||||||
if (!this.client) throw new Error("Not connected");
|
if (!this.client) throw new Error("Not connected");
|
||||||
return this.client.fileTransferInitUpload(
|
return this.client.fileTransferInitUpload(channelID, path, password, size, overwrite);
|
||||||
channelID,
|
|
||||||
path,
|
|
||||||
password,
|
|
||||||
size,
|
|
||||||
overwrite,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async uploadFileData(
|
async uploadFileData(host: string, info: FileUploadInfo, data: Readable): Promise<void> {
|
||||||
host: string,
|
|
||||||
info: FileUploadInfo,
|
|
||||||
data: Readable,
|
|
||||||
): Promise<void> {
|
|
||||||
if (!this.client) throw new Error("Not connected");
|
if (!this.client) throw new Error("Not connected");
|
||||||
await this.client.uploadFileData(host, info, data);
|
await this.client.uploadFileData(host, info, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fileTransferDeleteFile(
|
async fileTransferDeleteFile(channelID: bigint, paths: string[]): Promise<void> {
|
||||||
channelID: bigint,
|
|
||||||
paths: string[],
|
|
||||||
): Promise<void> {
|
|
||||||
if (!this.client) throw new Error("Not connected");
|
if (!this.client) throw new Error("Not connected");
|
||||||
await fileTransferDeleteFile(this.client, channelID, paths);
|
await fileTransferDeleteFile(this.client, channelID, paths);
|
||||||
}
|
}
|
||||||
@@ -420,10 +379,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
this.client.sendVoice(opusFrame, 5);
|
this.client.sendVoice(opusFrame, 5);
|
||||||
this.voiceFramesSent++;
|
this.voiceFramesSent++;
|
||||||
if (this.voiceFramesSent === 1) {
|
if (this.voiceFramesSent === 1) {
|
||||||
this.logger.info(
|
this.logger.info({ opusBytes: opusFrame.length, clientId: this.clientId }, "First voice packet sent to TeamSpeak");
|
||||||
{ opusBytes: opusFrame.length, clientId: this.clientId },
|
|
||||||
"First voice packet sent to TeamSpeak",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (this.voiceFramesSent === 0) {
|
if (this.voiceFramesSent === 0) {
|
||||||
@@ -444,10 +400,7 @@ export class TS3Client extends EventEmitter {
|
|||||||
if (this.client && !this.disconnecting) {
|
if (this.client && !this.disconnecting) {
|
||||||
this.disconnecting = true;
|
this.disconnecting = true;
|
||||||
const client = this.client;
|
const client = this.client;
|
||||||
client
|
client.disconnect().catch(() => {}).finally(() => {
|
||||||
.disconnect()
|
|
||||||
.catch(() => {})
|
|
||||||
.finally(() => {
|
|
||||||
if (this.client === client) {
|
if (this.client === client) {
|
||||||
this.client = null;
|
this.client = null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user