feat(open-protocol): add OpenProtocol v1 discovery and slash-command support

This commit is contained in:
Dr1mH4X committed 2026-11-14 18:43:00 +08:00
1 parent 17008ae52e
commit 263c1ba6da
16 files changed
+1359 -183

No files matched your search

+42 -8
View File
@@ -2,15 +2,17 @@ import { EventEmitter } from "node:events";
import {
TS3Client,
type TS3ClientOptions,
type TS3PluginCommand,
type TS3TextMessage,
type TS3VoiceActivity,
type TS3VoiceSendFailure,
} from "../ts-protocol/client.js";
import { OpenProtocolManager } from "../ts-protocol/open-protocol.js";
import { AudioPlayer } from "../audio/player.js";
import { PlayQueue, PlayMode, type QueuedSong } from "../audio/queue.js";
import type { MusicProvider, Platform, Song } from "../music/provider.js";
import {
parseCommand,
parseCommandWithFallback,
canRunCommand,
type ParsedCommand,
} from "./commands.js";
@@ -154,11 +156,27 @@ export interface BotStatus {
effectiveDuration?: number;
}
async function sendTextReply(
tsClient: Pick<TS3Client, "sendTextMessage">,
msg: TS3TextMessage,
text: string,
): Promise<void> {
if (msg.targetMode === 1) {
const targetClientId = Number(msg.invokerId);
if (Number.isInteger(targetClientId) && targetClientId > 0) {
await tsClient.sendTextMessage(text, 1, targetClientId);
}
return;
}
await tsClient.sendTextMessage(text);
}
export class BotInstance extends EventEmitter {
readonly id: string;
name: string;
private tsClient: TS3Client;
private openProtocol: OpenProtocolManager;
private player: AudioPlayer;
private voiceDucking: VoiceDuckingController;
private managedVoiceClients: ManagedVoiceClientRegistry;
@@ -230,6 +248,14 @@ export class BotInstance extends EventEmitter {
this.avatarStore = options.avatarStore;
this.tsClient = new TS3Client(options.tsOptions, this.logger);
this.openProtocol = new OpenProtocolManager({
transport: this.tsClient,
getConfig: () => ({
locale: this.config.locale,
commandPrefix: this.config.commandPrefix,
}),
logger: this.logger,
});
this.player = new AudioPlayer(this.logger);
this.voiceDucking = new VoiceDuckingController(
this.player,
@@ -446,6 +472,12 @@ export class BotInstance extends EventEmitter {
});
});
this.tsClient.on("pluginCommand", (cmd: TS3PluginCommand) => {
this.openProtocol?.handlePluginCommand(cmd).catch((err) => {
this.logger.debug({ err }, "OpenProtocol plugin command handler failed");
});
});
this.tsClient.on("disconnected", () => {
// Always reset local state — covers the case where connect() never
// completed (hanging handshake → 60s library idle timeout) and
@@ -453,6 +485,7 @@ export class BotInstance extends EventEmitter {
// short-circuited on !this.connected, leaving player stuck as "playing".
this.lifecycleGeneration++;
this.connected = false;
this.openProtocol?.reset();
this._cancelIdleTimer();
this.unregisterManagedVoiceClient(MANAGED_VOICE_CLIENT_RELEASE_GRACE_MS);
this.voiceDucking.reset(true);
@@ -487,6 +520,7 @@ export class BotInstance extends EventEmitter {
this.autoPaused = false;
this._startIdlePoller();
this._startJellyfinReportPoller();
this.openProtocol?.scheduleAnnounce();
});
this.tsClient.on("voiceActivity", (activity: TS3VoiceActivity) => {
@@ -525,6 +559,7 @@ export class BotInstance extends EventEmitter {
this.profileManager.onChannelMoved(event.targetChannelID).catch((err) => {
this.logger.warn({ err }, "Channel description move update failed");
});
this.openProtocol?.scheduleAnnounce();
} else {
this.voiceDucking.removeSpeaker(event.id);
}
@@ -667,6 +702,7 @@ export class BotInstance extends EventEmitter {
this.disconnectEmitted = true;
this.emit("disconnected");
}
this.openProtocol?.reset();
this.tsClient.disconnect();
// Stop outbound PCM and initiate the TeamSpeak disconnect before removing
// our id from the shared registry, minimizing the window in which another
@@ -787,10 +823,10 @@ export class BotInstance extends EventEmitter {
}
private async handleTextMessage(msg: TS3TextMessage): Promise<void> {
const parsed = parseCommand(
const parsed = parseCommandWithFallback(
msg.message,
this.config.commandPrefix,
this.config.commandAliases
this.config.commandAliases,
);
if (!parsed) return;
@@ -800,7 +836,7 @@ export class BotInstance extends EventEmitter {
"Command denied: invoker not in adminGroups"
);
try {
await this.tsClient.sendTextMessage(COMMAND_DENIED_MESSAGE);
await sendTextReply(this.tsClient, msg, COMMAND_DENIED_MESSAGE);
} catch (sendErr) {
this.logger.error({ err: sendErr }, "Failed to send permission-denied message to chat");
}
@@ -818,15 +854,13 @@ export class BotInstance extends EventEmitter {
// A single long reply (e.g. full lyrics) would exceed TeamSpeak's
// per-message byte cap, so split it and send the chunks in order.
for (const chunk of splitTextIntoChunks(response)) {
await this.tsClient.sendTextMessage(chunk);
await sendTextReply(this.tsClient, msg, chunk);
}
}
} catch (err) {
this.logger.error({ err, command: parsed.name }, "Command execution error");
try {
await this.tsClient.sendTextMessage(
`Error: ${(err as Error).message}`
);
await sendTextReply(this.tsClient, msg, `Error: ${(err as Error).message}`);
} catch (sendErr) {
this.logger.error({ err: sendErr }, "Failed to send error message to chat");
}