mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-02 19:02:50 +08:00
fix(teamspeak): fence chat commands to their connection
This commit is contained in:
1 parent
4f026aa65f
commit
17b092f2e4
2 files changed
+44
-1
No files matched your search
@@ -1975,6 +1975,41 @@ describe("BotInstance — OpenProtocol & command routing", () => {
|
||||
expect(ctx.tsClient.sendTextMessage).toHaveBeenCalledWith("ok:now:");
|
||||
});
|
||||
|
||||
it("drops a private command reply when the connection changed during execution", async () => {
|
||||
const ctx = makeGateCtx({});
|
||||
ctx.lifecycleGeneration = 1;
|
||||
const executed = deferred();
|
||||
const reply = deferred<string>();
|
||||
ctx.executeCommand = vi.fn(() => {
|
||||
executed.resolve();
|
||||
return reply.promise;
|
||||
});
|
||||
|
||||
const pending = handleTextMessage.call(ctx, { ...makeMsg("!now"), targetMode: 1 });
|
||||
await executed.promise;
|
||||
ctx.lifecycleGeneration++;
|
||||
reply.resolve("Prior connection's queue");
|
||||
await pending;
|
||||
|
||||
expect(ctx.tsClient.sendTextMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("drops a command from a previous connection after the group check", async () => {
|
||||
const ctx = makeGateCtx({ adminGroups: [6] });
|
||||
ctx.lifecycleGeneration = 1;
|
||||
const groups = deferred<string[]>();
|
||||
ctx.tsClient.getClientServerGroups.mockReturnValue(groups.promise);
|
||||
|
||||
const pending = handleTextMessage.call(ctx, { ...makeMsg("!stop"), targetMode: 1 });
|
||||
expect(ctx.tsClient.getClientServerGroups).toHaveBeenCalled();
|
||||
ctx.lifecycleGeneration++;
|
||||
groups.resolve(["6"]);
|
||||
await pending;
|
||||
|
||||
expect(ctx.executeCommand).not.toHaveBeenCalled();
|
||||
expect(ctx.tsClient.sendTextMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("routes permission denied and execution errors privately when triggered via DM", async () => {
|
||||
const ctx = makeGateCtx({ adminGroups: [6], lookupGroups: [] });
|
||||
const dmStop: TS3TextMessage = {
|
||||
|
||||
+9
-1
@@ -830,7 +830,13 @@ export class BotInstance extends EventEmitter {
|
||||
);
|
||||
if (!parsed) return;
|
||||
|
||||
if (!(await this.isCommandAllowed(parsed.name, msg))) {
|
||||
// A numeric invoker ID only belongs to this TeamSpeak connection.
|
||||
const generation = this.lifecycleGeneration;
|
||||
const isCurrent = () => this.lifecycleGeneration === generation;
|
||||
const allowed = await this.isCommandAllowed(parsed.name, msg);
|
||||
if (!isCurrent()) return;
|
||||
|
||||
if (!allowed) {
|
||||
this.logger.info(
|
||||
{ command: parsed.name, invoker: msg.invokerName },
|
||||
"Command denied: invoker not in adminGroups"
|
||||
@@ -854,10 +860,12 @@ 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)) {
|
||||
if (!isCurrent()) return;
|
||||
await sendTextReply(this.tsClient, msg, chunk);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (!isCurrent()) return;
|
||||
this.logger.error({ err, command: parsed.name }, "Command execution error");
|
||||
try {
|
||||
await sendTextReply(this.tsClient, msg, `Error: ${(err as Error).message}`);
|
||||
|
||||
Reference in new issue
Block a user