mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-03 03:12:51 +08:00
fix(open-protocol): restore prefix-only command parsing and declare command_prefix in list_ack
This commit is contained in:
1 parent
263c1ba6da
commit
69b311283a
6 files changed
+26
-92
No files matched your search
@@ -1,7 +1,6 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
parseCommand,
|
||||
parseCommandWithFallback,
|
||||
canRunCommand,
|
||||
isAdminCommand,
|
||||
} from "./commands.js";
|
||||
@@ -98,31 +97,3 @@ describe("canRunCommand", () => {
|
||||
expect(canRunCommand("clear", [], [6])).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseCommandWithFallback", () => {
|
||||
it("accepts both the configured primary prefix and slash fallback", () => {
|
||||
expect(parseCommandWithFallback("!play 晴天", "!")).toEqual({
|
||||
name: "play",
|
||||
args: "晴天",
|
||||
rawArgs: ["晴天"],
|
||||
flags: new Set(),
|
||||
});
|
||||
expect(parseCommandWithFallback("/play 晴天", "!")).toEqual({
|
||||
name: "play",
|
||||
args: "晴天",
|
||||
rawArgs: ["晴天"],
|
||||
flags: new Set(),
|
||||
});
|
||||
});
|
||||
|
||||
it("applies aliases on slash fallback and ignores bare slash", () => {
|
||||
expect(parseCommandWithFallback("/p 稻香", "!", { p: "play" })).toEqual({
|
||||
name: "play",
|
||||
args: "稻香",
|
||||
rawArgs: ["稻香"],
|
||||
flags: new Set(),
|
||||
});
|
||||
expect(parseCommandWithFallback("/", "!")).toBeNull();
|
||||
expect(parseCommandWithFallback("plain chat", "!")).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -55,23 +55,6 @@ export function parseCommand(
|
||||
};
|
||||
}
|
||||
|
||||
export function parseCommandWithFallback(
|
||||
message: string,
|
||||
primaryPrefix: string,
|
||||
aliases: Record<string, string> = {},
|
||||
fallbackPrefixes: readonly string[] = ["/"],
|
||||
): ParsedCommand | null {
|
||||
const direct = parseCommand(message, primaryPrefix, aliases);
|
||||
if (direct) return direct;
|
||||
|
||||
for (const fallback of fallbackPrefixes) {
|
||||
if (!fallback || fallback === primaryPrefix) continue;
|
||||
const parsed = parseCommand(message, fallback, aliases);
|
||||
if (parsed) return parsed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function isAdminCommand(commandName: string): boolean {
|
||||
return ADMIN_COMMANDS.has(commandName);
|
||||
}
|
||||
|
||||
@@ -1949,18 +1949,21 @@ describe("BotInstance trackEnd — stale playback sessions", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("BotInstance — OpenProtocol & slash command routing", () => {
|
||||
it("accepts / commands when commandPrefix is '!' and routes private replies to invokerId", async () => {
|
||||
describe("BotInstance — OpenProtocol & command routing", () => {
|
||||
it("ignores / commands when commandPrefix is '!' and routes private replies to invokerId", async () => {
|
||||
const ctx = makeGateCtx({ adminGroups: [] });
|
||||
ctx.executeCommand = vi.fn(async (parsed: { name: string; args: string }) =>
|
||||
`ok:${parsed.name}:${parsed.args}`,
|
||||
);
|
||||
|
||||
await handleTextMessage.call(ctx, makeMsg("/play 晴天"));
|
||||
expect(ctx.executeCommand).not.toHaveBeenCalled();
|
||||
|
||||
const dm: TS3TextMessage = {
|
||||
invokerName: "FlSpeakUser",
|
||||
invokerId: "12",
|
||||
invokerUid: "uid-fl",
|
||||
message: "/play 晴天",
|
||||
message: "!play 晴天",
|
||||
targetMode: 1,
|
||||
invokerGroups: [],
|
||||
};
|
||||
@@ -1971,7 +1974,7 @@ describe("BotInstance — OpenProtocol & slash command routing", () => {
|
||||
expect(ctx.tsClient.sendTextMessage).toHaveBeenCalledWith("ok:play:晴天", 1, 12);
|
||||
|
||||
ctx.tsClient.sendTextMessage.mockClear();
|
||||
await handleTextMessage.call(ctx, makeMsg("/now"));
|
||||
await handleTextMessage.call(ctx, makeMsg("!now"));
|
||||
expect(ctx.tsClient.sendTextMessage).toHaveBeenCalledWith("ok:now:");
|
||||
});
|
||||
|
||||
@@ -1981,7 +1984,7 @@ describe("BotInstance — OpenProtocol & slash command routing", () => {
|
||||
invokerName: "Guest",
|
||||
invokerId: "22",
|
||||
invokerUid: "uid-guest",
|
||||
message: "/stop",
|
||||
message: "!stop",
|
||||
targetMode: 1,
|
||||
invokerGroups: [],
|
||||
};
|
||||
@@ -1993,7 +1996,7 @@ describe("BotInstance — OpenProtocol & slash command routing", () => {
|
||||
okCtx.executeCommand = vi.fn(async () => {
|
||||
throw new Error("boom");
|
||||
});
|
||||
await handleTextMessage.call(okCtx, { ...dmStop, message: "/play x" });
|
||||
await handleTextMessage.call(okCtx, { ...dmStop, message: "!play x" });
|
||||
expect(okCtx.tsClient.sendTextMessage).toHaveBeenCalledWith("Error: boom", 1, 22);
|
||||
});
|
||||
|
||||
|
||||
+2
-2
@@ -12,7 +12,7 @@ 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 {
|
||||
parseCommandWithFallback,
|
||||
parseCommand,
|
||||
canRunCommand,
|
||||
type ParsedCommand,
|
||||
} from "./commands.js";
|
||||
@@ -823,7 +823,7 @@ export class BotInstance extends EventEmitter {
|
||||
}
|
||||
|
||||
private async handleTextMessage(msg: TS3TextMessage): Promise<void> {
|
||||
const parsed = parseCommandWithFallback(
|
||||
const parsed = parseCommand(
|
||||
msg.message,
|
||||
this.config.commandPrefix,
|
||||
this.config.commandAliases,
|
||||
|
||||
Reference in new issue
Block a user