feat: prevent recording of audio from bot users

This commit is contained in:
MythEclipse
2026-05-16 15:52:10 +07:00
parent d50ce8698f
commit 8b33af8286
2 changed files with 52 additions and 9 deletions
+2
View File
@@ -92,6 +92,8 @@ export async function startRecording(
if (userId === client.user?.id) return; if (userId === client.user?.id) return;
const userMetadata = await collectUserMetadata(client, userId, channel); const userMetadata = await collectUserMetadata(client, userId, channel);
if (userMetadata.bot) return;
logger.info( logger.info(
{ userId, username: userMetadata.username }, { userId, username: userMetadata.username },
"Voice activity detected", "Voice activity detected",
+50 -9
View File
@@ -1,5 +1,5 @@
import { EventEmitter } from "node:events"; import { EventEmitter } from "node:events";
import { describe, expect, it, vi } from "vitest"; import { beforeEach, describe, expect, it, vi } from "vitest";
const speaking = new EventEmitter(); const speaking = new EventEmitter();
const subscribe = vi.fn(); const subscribe = vi.fn();
@@ -13,6 +13,21 @@ const joinVoiceChannel = vi.fn(() => ({
destroy: vi.fn(), destroy: vi.fn(),
})); }));
function createChannel() {
return {
id: "voice-channel",
name: "Voice",
guild: {
id: "guild",
voiceAdapterCreator: {},
members: {
cache: new Map(),
fetch: vi.fn(async () => null),
},
},
};
}
vi.mock("@discordjs/voice", async () => { vi.mock("@discordjs/voice", async () => {
const actual = const actual =
await vi.importActual<typeof import("@discordjs/voice")>( await vi.importActual<typeof import("@discordjs/voice")>(
@@ -26,19 +41,17 @@ vi.mock("@discordjs/voice", async () => {
}); });
describe("startRecording", () => { describe("startRecording", () => {
beforeEach(() => {
subscribe.mockClear();
speaking.removeAllListeners();
});
it("does not subscribe to the bot user's own audio", async () => { it("does not subscribe to the bot user's own audio", async () => {
const { startRecording } = await import("../src/recorder"); const { startRecording } = await import("../src/recorder");
const client = { const client = {
user: { id: "bot-user" }, user: { id: "bot-user" },
}; };
const channel = { const channel = createChannel();
id: "voice-channel",
name: "Voice",
guild: {
id: "guild",
voiceAdapterCreator: {},
},
};
await startRecording(client as never, channel as never); await startRecording(client as never, channel as never);
speaking.emit("start", "bot-user"); speaking.emit("start", "bot-user");
@@ -46,4 +59,32 @@ describe("startRecording", () => {
expect(subscribe).not.toHaveBeenCalled(); expect(subscribe).not.toHaveBeenCalled();
}); });
it("does not subscribe to other bot users", async () => {
const { startRecording } = await import("../src/recorder");
const client = {
user: { id: "self-user" },
users: {
cache: new Map([
[
"music-bot",
{
id: "music-bot",
username: "Jockie Music",
tag: "Jockie Music#8158",
bot: true,
displayAvatarURL: vi.fn(() => "https://example.com/avatar.png"),
},
],
]),
fetch: vi.fn(async () => null),
},
};
await startRecording(client as never, createChannel() as never);
speaking.emit("start", "music-bot");
await new Promise((resolve) => setImmediate(resolve));
expect(subscribe).not.toHaveBeenCalled();
});
}); });