mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-02 04:52:50 +08:00
fix: address Phase 6 review — error handling on player routes, SPA fallback scope, WS cleanup
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
0b54809113
commit
2a966e914c
3 files changed
+51
-58
No files matched your search
+42
-54
@@ -57,55 +57,46 @@ export function createPlayerRouter(
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/:botId/pause", async (req, res) => {
|
||||
const bot = (req as any).bot;
|
||||
const cmd = parseCommand("!pause", "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
});
|
||||
const simpleCommand = (cmdStr: string) => async (req: any, res: any) => {
|
||||
try {
|
||||
const bot = req.bot;
|
||||
const cmd = parseCommand(cmdStr, "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: (err as Error).message });
|
||||
}
|
||||
};
|
||||
|
||||
router.post("/:botId/resume", async (req, res) => {
|
||||
const bot = (req as any).bot;
|
||||
const cmd = parseCommand("!resume", "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
});
|
||||
|
||||
router.post("/:botId/next", async (req, res) => {
|
||||
const bot = (req as any).bot;
|
||||
const cmd = parseCommand("!next", "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
});
|
||||
|
||||
router.post("/:botId/prev", async (req, res) => {
|
||||
const bot = (req as any).bot;
|
||||
const cmd = parseCommand("!prev", "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
});
|
||||
|
||||
router.post("/:botId/stop", async (req, res) => {
|
||||
const bot = (req as any).bot;
|
||||
const cmd = parseCommand("!stop", "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
});
|
||||
router.post("/:botId/pause", simpleCommand("!pause"));
|
||||
router.post("/:botId/resume", simpleCommand("!resume"));
|
||||
router.post("/:botId/next", simpleCommand("!next"));
|
||||
router.post("/:botId/prev", simpleCommand("!prev"));
|
||||
router.post("/:botId/stop", simpleCommand("!stop"));
|
||||
router.post("/:botId/clear", simpleCommand("!clear"));
|
||||
|
||||
router.post("/:botId/volume", async (req, res) => {
|
||||
const bot = (req as any).bot;
|
||||
const { volume } = req.body;
|
||||
const cmd = parseCommand(`!vol ${volume}`, "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
try {
|
||||
const bot = (req as any).bot;
|
||||
const { volume } = req.body;
|
||||
const cmd = parseCommand(`!vol ${volume}`, "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: (err as Error).message });
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/:botId/mode", async (req, res) => {
|
||||
const bot = (req as any).bot;
|
||||
const { mode } = req.body;
|
||||
const cmd = parseCommand(`!mode ${mode}`, "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
try {
|
||||
const bot = (req as any).bot;
|
||||
const { mode } = req.body;
|
||||
const cmd = parseCommand(`!mode ${mode}`, "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: (err as Error).message });
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/:botId/queue", (req, res) => {
|
||||
@@ -113,18 +104,15 @@ export function createPlayerRouter(
|
||||
res.json({ queue: bot.getQueue(), status: bot.getStatus() });
|
||||
});
|
||||
|
||||
router.post("/:botId/clear", async (req, res) => {
|
||||
const bot = (req as any).bot;
|
||||
const cmd = parseCommand("!clear", "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
});
|
||||
|
||||
router.delete("/:botId/queue/:index", async (req, res) => {
|
||||
const bot = (req as any).bot;
|
||||
const cmd = parseCommand(`!remove ${req.params.index}`, "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
try {
|
||||
const bot = (req as any).bot;
|
||||
const cmd = parseCommand(`!remove ${req.params.index}`, "!")!;
|
||||
const response = await bot.executeCommand(cmd);
|
||||
res.json({ message: response });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: (err as Error).message });
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/:botId/playlist", async (req, res) => {
|
||||
|
||||
+3
-2
@@ -56,13 +56,13 @@ export function createWebServer(options: WebServerOptions): WebServer {
|
||||
|
||||
if (options.staticDir) {
|
||||
app.use(express.static(options.staticDir));
|
||||
app.get("*", (_req, res) => {
|
||||
app.get(/^(?!\/api|\/ws)/, (_req, res) => {
|
||||
res.sendFile(path.join(options.staticDir!, "index.html"));
|
||||
});
|
||||
}
|
||||
|
||||
const wss = new WebSocketServer({ server, path: "/ws" });
|
||||
setupWebSocket(wss, options.botManager, logger);
|
||||
const cleanupWs = setupWebSocket(wss, options.botManager, logger);
|
||||
|
||||
return {
|
||||
async start(): Promise<void> {
|
||||
@@ -74,6 +74,7 @@ export function createWebServer(options: WebServerOptions): WebServer {
|
||||
});
|
||||
},
|
||||
stop(): void {
|
||||
cleanupWs();
|
||||
wss.close();
|
||||
server.close();
|
||||
},
|
||||
|
||||
@@ -6,7 +6,7 @@ export function setupWebSocket(
|
||||
wss: WebSocketServer,
|
||||
botManager: BotManager,
|
||||
logger: Logger
|
||||
): void {
|
||||
): () => void {
|
||||
const clients = new Set<WebSocket>();
|
||||
|
||||
wss.on("connection", (ws) => {
|
||||
@@ -65,6 +65,10 @@ export function setupWebSocket(
|
||||
}
|
||||
};
|
||||
|
||||
setInterval(attachBotListeners, 5000);
|
||||
const intervalId = setInterval(attachBotListeners, 5000);
|
||||
attachBotListeners();
|
||||
|
||||
return () => {
|
||||
clearInterval(intervalId);
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user