feat(voice): guarantee an empty generated channel disappears

A generated channel could survive with nobody in it: the row was dropped before
the channel was really gone, so a refused delete call left a room nothing could
track anymore, and an event missed while the bot was down was never retried.

The row is now kept until Discord confirms the channel is gone, and a sweep at
startup then every minute deletes whatever the event missed. The grace period
is tunable with VOICE_EMPTY_GRACE_MS, 0 deleting as soon as the room empties.
This commit is contained in:
BlackAngelTV committed 2026-09-15 16:11:30 +02:00
1 parent 836abeafd5
commit 76e2269555
5 files changed
+227 -36

No files matched your search

+122
View File
@@ -22,7 +22,9 @@ import {
bottomPosition,
buildChannelName,
clampLimit,
cleanupVoiceChannel,
createFromTrigger,
deleteVoiceChannel,
findTrigger,
getVoiceTrigger,
getVoiceTriggers,
@@ -32,6 +34,8 @@ import {
sanitizeChannelName,
saveVoiceTrigger,
setTriggerEnabled,
startVoiceJanitor,
sweepVoiceChannels,
} from '../src/services/voice/voice-service.js';
const member = (roleIds = [], { id = 'u1', guildOwnerId = 'someone-else' } = {}) => ({
@@ -349,6 +353,124 @@ describe('voice channel creation from a trigger', () => {
});
});
describe('empty generated channels', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(() => {
getDatabase().exec('DELETE FROM voice_channels');
});
const track = (channelId, ownerId = 'u1', triggerId = 'dev') =>
addVoiceChannel({ channelId, guildId: 'g', ownerId, triggerId, name: `salon-${channelId}` });
const voiceChannel = (id, { members = [] } = {}) => ({
id,
parentId: 'cat',
members: new Collection(members.map((memberId) => [memberId, { id: memberId }])),
deleted: false,
failure: null,
async delete() {
if (this.failure) throw this.failure;
this.deleted = true;
},
});
/** Guild mock whose `fetch` only knows the cached channels, like a channel deleted by hand. */
const makeGuild = (channels) => {
const cache = new Collection(channels.map((channel) => [channel.id, channel]));
const guild = { id: 'g', channels: { cache, fetch: async (id) => cache.get(id) ?? null } };
for (const channel of cache.values()) channel.guild = guild;
return guild;
};
const clientOf = (...guilds) => ({ guilds: { cache: new Collection(guilds.map((g) => [g.id, g])) } });
it('deletes a generated channel as soon as nobody is left in it', async () => {
track('c1');
const channel = voiceChannel('c1');
await cleanupVoiceChannel(makeGuild([channel]), 'c1', { graceMs: 0 });
assert.equal(channel.deleted, true);
assert.equal(getVoiceChannel('c1'), null, 'the row is forgotten too');
});
it('keeps a channel that still has members', async () => {
track('c1');
const channel = voiceChannel('c1', { members: ['u2'] });
await cleanupVoiceChannel(makeGuild([channel]), 'c1', { graceMs: 0 });
assert.equal(channel.deleted, false);
assert.ok(getVoiceChannel('c1'));
});
it('leaves a grace period during which the owner can come back', async () => {
track('c1');
const channel = voiceChannel('c1');
const pending = cleanupVoiceChannel(makeGuild([channel]), 'c1', { graceMs: 30 });
channel.members.set('u1', { id: 'u1' });
await pending;
await new Promise((resolve) => setTimeout(resolve, 60));
assert.equal(channel.deleted, false, 'the customised channel survives a quick move');
assert.ok(getVoiceChannel('c1'));
});
it('deletes the empty channels and forgets the vanished ones in one sweep', async () => {
track('vide');
track('occupe', 'u2');
track('disparu', 'u3', 'sio');
const empty = voiceChannel('vide');
const busy = voiceChannel('occupe', { members: ['u2'] });
await sweepVoiceChannels(clientOf(makeGuild([empty, busy])));
assert.equal(empty.deleted, true);
assert.equal(busy.deleted, false);
assert.equal(getVoiceChannel('vide'), null);
assert.equal(getVoiceChannel('disparu'), null, 'a channel deleted by hand is forgotten');
assert.ok(getVoiceChannel('occupe'));
});
it('runs that sweep on an interval', async () => {
track('c1');
const channel = voiceChannel('c1');
const timer = startVoiceJanitor(clientOf(makeGuild([channel])), 20);
await new Promise((resolve) => setTimeout(resolve, 60));
clearInterval(timer);
assert.equal(channel.deleted, true);
assert.equal(getVoiceChannel('c1'), null);
});
it('keeps the row when the deletion fails, so the sweep can retry', async () => {
track('c1');
const channel = voiceChannel('c1');
channel.failure = Object.assign(new Error('Missing Permissions'), { code: 50013, status: 403 });
makeGuild([channel]);
assert.equal(await deleteVoiceChannel(channel, 'test'), false);
assert.ok(getVoiceChannel('c1'), 'the channel is not leaked');
channel.failure = null;
assert.equal(await deleteVoiceChannel(channel, 'test'), true);
assert.equal(getVoiceChannel('c1'), null);
});
it('forgets a channel that Discord has already deleted', async () => {
track('c1');
const channel = voiceChannel('c1');
channel.failure = Object.assign(new Error('Unknown Channel'), { code: 10003 });
makeGuild([channel]);
assert.equal(await deleteVoiceChannel(channel, 'test'), true);
assert.equal(getVoiceChannel('c1'), null);
});
});
describe('voice helpers', () => {
it('normalises a channel name', () => {
assert.equal(sanitizeChannelName(' Salon de Léa '), 'Salon de Léa');