feat(voice): add temporary voice channel manager
Joining the trigger channel must feel instant and never leave orphan channels behind, so the ownership is stored in SQLite, empty channels are deleted after a short grace period and a startup sweep cleans up whatever a restart left behind.
This commit is contained in:
12 files changed
+1277
-1
No files matched your search
@@ -0,0 +1,248 @@
|
||||
import { ChannelType, MessageFlags, SlashCommandBuilder } from 'discord.js';
|
||||
import { ACCESS_LEVELS, COLORS } from '../../config/constants.js';
|
||||
import { UserFacingError } from '../../core/errors.js';
|
||||
import {
|
||||
activeChannelCount,
|
||||
claimOwnership,
|
||||
getVoiceSettings,
|
||||
kickUser,
|
||||
NAME_MAX,
|
||||
permitUser,
|
||||
postSetupPanel,
|
||||
rejectUser,
|
||||
removeChannel,
|
||||
renameChannel,
|
||||
saveVoiceSettings,
|
||||
setChannelLimit,
|
||||
showInfo,
|
||||
toggleLock,
|
||||
transferOwnership,
|
||||
} from '../../services/voice/voice-service.js';
|
||||
import { baseEmbed, successEmbed } from '../../ui/embeds.js';
|
||||
|
||||
export const access = ACCESS_LEVELS.USER;
|
||||
export const subcommandAccess = { setup: ACCESS_LEVELS.STAFF, panel: ACCESS_LEVELS.STAFF };
|
||||
|
||||
const voiceChannelOption = (option) => option.addChannelTypes(ChannelType.GuildVoice);
|
||||
|
||||
export const data = new SlashCommandBuilder()
|
||||
.setName('voice')
|
||||
.setDescription('Gestion des salons vocaux temporaires')
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('setup')
|
||||
.setDescription('Configurer le salon déclencheur et les salons créés')
|
||||
.addChannelOption((option) =>
|
||||
voiceChannelOption(
|
||||
option
|
||||
.setName('trigger')
|
||||
.setDescription('Salon vocal à rejoindre pour créer un salon')
|
||||
.setRequired(true),
|
||||
),
|
||||
)
|
||||
.addChannelOption((option) =>
|
||||
option
|
||||
.setName('category')
|
||||
.setDescription('Catégorie d’accueil des salons créés')
|
||||
.addChannelTypes(ChannelType.GuildCategory),
|
||||
)
|
||||
.addStringOption((option) =>
|
||||
option
|
||||
.setName('template')
|
||||
.setDescription('Modèle du nom, avec {user} (ex. « 🔊 Salon de {user} »)')
|
||||
.setMaxLength(NAME_MAX),
|
||||
)
|
||||
.addIntegerOption((option) =>
|
||||
option
|
||||
.setName('limit')
|
||||
.setDescription('Places par défaut (0 = illimité)')
|
||||
.setMinValue(0)
|
||||
.setMaxValue(99),
|
||||
)
|
||||
.addBooleanOption((option) =>
|
||||
option.setName('panel').setDescription('Publier le panneau d’explication dans le salon déclencheur'),
|
||||
),
|
||||
)
|
||||
.addSubcommand((sub) => sub.setName('config').setDescription('Afficher la configuration actuelle'))
|
||||
.addSubcommand((sub) =>
|
||||
sub.setName('panel').setDescription('Republier le panneau d’explication dans le salon déclencheur'),
|
||||
)
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('name')
|
||||
.setDescription('Renommer ton salon')
|
||||
.addStringOption((option) =>
|
||||
option.setName('name').setDescription('Nouveau nom').setRequired(true).setMaxLength(NAME_MAX),
|
||||
),
|
||||
)
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('limit')
|
||||
.setDescription('Limiter le nombre de places (0 = illimité)')
|
||||
.addIntegerOption((option) =>
|
||||
option
|
||||
.setName('limit')
|
||||
.setDescription('Nombre de places')
|
||||
.setRequired(true)
|
||||
.setMinValue(0)
|
||||
.setMaxValue(99),
|
||||
),
|
||||
)
|
||||
.addSubcommand((sub) => sub.setName('lock').setDescription('Verrouiller ton salon'))
|
||||
.addSubcommand((sub) => sub.setName('unlock').setDescription('Déverrouiller ton salon'))
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('permit')
|
||||
.setDescription('Autoriser un membre à rejoindre ton salon')
|
||||
.addUserOption((option) => option.setName('user').setDescription('Membre').setRequired(true)),
|
||||
)
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('reject')
|
||||
.setDescription('Interdire l’accès à un membre')
|
||||
.addUserOption((option) => option.setName('user').setDescription('Membre').setRequired(true)),
|
||||
)
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('kick')
|
||||
.setDescription('Exclure un membre du salon')
|
||||
.addUserOption((option) => option.setName('user').setDescription('Membre').setRequired(true)),
|
||||
)
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('transfer')
|
||||
.setDescription('Transférer la propriété du salon')
|
||||
.addUserOption((option) =>
|
||||
option.setName('user').setDescription('Nouveau propriétaire').setRequired(true),
|
||||
),
|
||||
)
|
||||
.addSubcommand((sub) => sub.setName('claim').setDescription('Récupérer un salon abandonné'))
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('delete')
|
||||
.setDescription('Supprimer un salon temporaire')
|
||||
.addChannelOption((option) =>
|
||||
voiceChannelOption(option.setName('channel').setDescription('Salon (staff uniquement)')),
|
||||
),
|
||||
)
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('info')
|
||||
.setDescription('Détails du salon temporaire')
|
||||
.addChannelOption((option) =>
|
||||
voiceChannelOption(option.setName('channel').setDescription('Salon (staff uniquement)')),
|
||||
),
|
||||
);
|
||||
|
||||
function configEmbed(guildId) {
|
||||
const settings = getVoiceSettings(guildId);
|
||||
return baseEmbed(COLORS.primary)
|
||||
.setTitle('Salons vocaux temporaires')
|
||||
.addFields(
|
||||
{
|
||||
name: 'Salon déclencheur',
|
||||
value: settings.triggerId ? `<#${settings.triggerId}>` : '❌ Non configuré',
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Catégorie',
|
||||
value: settings.categoryId ? `<#${settings.categoryId}>` : 'Aucune',
|
||||
inline: true,
|
||||
},
|
||||
{ name: 'Modèle du nom', value: `\`${settings.template}\``, inline: true },
|
||||
{
|
||||
name: 'Limite par défaut',
|
||||
value: settings.defaultLimit ? `${settings.defaultLimit} places` : 'Illimitée',
|
||||
inline: true,
|
||||
},
|
||||
{ name: 'Salons actifs', value: String(activeChannelCount(guildId)), inline: true },
|
||||
);
|
||||
}
|
||||
|
||||
async function runSetup(interaction) {
|
||||
const trigger = interaction.options.getChannel('trigger', true);
|
||||
if (trigger.type !== ChannelType.GuildVoice) {
|
||||
throw new UserFacingError('Le salon déclencheur doit être un salon **vocal**.');
|
||||
}
|
||||
|
||||
const category = interaction.options.getChannel('category');
|
||||
const template = interaction.options.getString('template');
|
||||
const limit = interaction.options.getInteger('limit');
|
||||
|
||||
if (category && category.type !== ChannelType.GuildCategory) {
|
||||
throw new UserFacingError('La catégorie d’accueil doit être une **catégorie**.');
|
||||
}
|
||||
if (trigger.id === category?.id) {
|
||||
throw new UserFacingError('Le salon déclencheur ne peut pas être sa propre catégorie d’accueil.');
|
||||
}
|
||||
|
||||
saveVoiceSettings(
|
||||
interaction.guildId,
|
||||
{
|
||||
trigger: trigger.id,
|
||||
...(category && { category: category.id }),
|
||||
...(template && { template }),
|
||||
...(limit !== null && { limit }),
|
||||
},
|
||||
interaction.user.id,
|
||||
);
|
||||
|
||||
await interaction.reply({
|
||||
embeds: [successEmbed(`Salon déclencheur : <#${trigger.id}>`)],
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
|
||||
const wantsPanel = interaction.options.getBoolean('panel') ?? true;
|
||||
if (wantsPanel) await postSetupPanel(interaction).catch(() => {});
|
||||
}
|
||||
|
||||
/** Every subcommand acts on the caller's temporary channel, checked by the service (M14-T06). */
|
||||
export async function execute(interaction) {
|
||||
const subcommand = interaction.options.getSubcommand();
|
||||
const channel = interaction.options.getChannel('channel');
|
||||
|
||||
switch (subcommand) {
|
||||
case 'setup':
|
||||
await runSetup(interaction);
|
||||
return;
|
||||
case 'panel':
|
||||
await postSetupPanel(interaction);
|
||||
return;
|
||||
case 'config':
|
||||
await interaction.reply({ embeds: [configEmbed(interaction.guildId)], flags: MessageFlags.Ephemeral });
|
||||
return;
|
||||
case 'name':
|
||||
await renameChannel(interaction, interaction.options.getString('name', true));
|
||||
return;
|
||||
case 'limit':
|
||||
await setChannelLimit(interaction, interaction.options.getInteger('limit', true));
|
||||
return;
|
||||
case 'lock':
|
||||
case 'unlock':
|
||||
await toggleLock(interaction, { locked: subcommand === 'lock' });
|
||||
return;
|
||||
case 'permit':
|
||||
await permitUser(interaction, interaction.options.getUser('user', true).id);
|
||||
return;
|
||||
case 'reject':
|
||||
await rejectUser(interaction, interaction.options.getUser('user', true).id);
|
||||
return;
|
||||
case 'kick':
|
||||
await kickUser(interaction, interaction.options.getUser('user', true).id);
|
||||
return;
|
||||
case 'transfer':
|
||||
await transferOwnership(interaction, interaction.options.getUser('user', true).id);
|
||||
return;
|
||||
case 'claim':
|
||||
await claimOwnership(interaction);
|
||||
return;
|
||||
case 'delete':
|
||||
await removeChannel(interaction, { channelId: channel?.id ?? null });
|
||||
return;
|
||||
case 'info':
|
||||
await showInfo(interaction, { channelId: channel?.id ?? null });
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user