feat(voice): support several trigger channels with their own category
A server needs one "create your own room" channel per theme, which a single trigger id could not express. Triggers are now stored as a list, each with its own home category, name template and default limit. Joining a trigger creates the channel at the bottom of that trigger's category, and a member keeps one channel per trigger instead of one room for the whole server. Triggers declared by the single-trigger version and CHANNEL_VOICE_TRIGGER_ID still work as fallbacks, so no server has to be reconfigured.
This commit is contained in:
8 files changed
+787
-112
No files matched your search
+173
-48
@@ -4,16 +4,19 @@ import { UserFacingError } from '../../core/errors.js';
|
||||
import {
|
||||
activeChannelCount,
|
||||
claimOwnership,
|
||||
getVoiceSettings,
|
||||
findTrigger,
|
||||
kickUser,
|
||||
listTriggerSummaries,
|
||||
NAME_MAX,
|
||||
permitUser,
|
||||
postSetupPanel,
|
||||
rejectUser,
|
||||
removeChannel,
|
||||
removeVoiceTriggerConfig,
|
||||
renameChannel,
|
||||
saveVoiceSettings,
|
||||
saveVoiceTrigger,
|
||||
setChannelLimit,
|
||||
setTriggerEnabled,
|
||||
showInfo,
|
||||
toggleLock,
|
||||
transferOwnership,
|
||||
@@ -21,17 +24,27 @@ import {
|
||||
import { baseEmbed, successEmbed } from '../../ui/embeds.js';
|
||||
|
||||
export const access = ACCESS_LEVELS.USER;
|
||||
export const subcommandAccess = { setup: ACCESS_LEVELS.STAFF, panel: ACCESS_LEVELS.STAFF };
|
||||
export const subcommandAccess = {
|
||||
setup: ACCESS_LEVELS.STAFF,
|
||||
panel: ACCESS_LEVELS.STAFF,
|
||||
list: ACCESS_LEVELS.STAFF,
|
||||
remove: ACCESS_LEVELS.STAFF,
|
||||
enable: ACCESS_LEVELS.STAFF,
|
||||
disable: ACCESS_LEVELS.STAFF,
|
||||
};
|
||||
|
||||
const voiceChannelOption = (option) => option.addChannelTypes(ChannelType.GuildVoice);
|
||||
|
||||
/** Longest embed listing worth sending before it gets unreadable. */
|
||||
const MAX_LISTED_TRIGGERS = 20;
|
||||
|
||||
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')
|
||||
.setDescription('Ajouter ou modifier un salon déclencheur')
|
||||
.addChannelOption((option) =>
|
||||
voiceChannelOption(
|
||||
option
|
||||
@@ -40,10 +53,13 @@ export const data = new SlashCommandBuilder()
|
||||
.setRequired(true),
|
||||
),
|
||||
)
|
||||
.addStringOption((option) =>
|
||||
option.setName('label').setDescription('Nom du déclencheur (ex. « Dev », « SIO »)').setMaxLength(40),
|
||||
)
|
||||
.addChannelOption((option) =>
|
||||
option
|
||||
.setName('category')
|
||||
.setDescription('Catégorie d’accueil des salons créés')
|
||||
.setDescription('Catégorie d’accueil des salons créés (défaut : celle du déclencheur)')
|
||||
.addChannelTypes(ChannelType.GuildCategory),
|
||||
)
|
||||
.addStringOption((option) =>
|
||||
@@ -60,12 +76,47 @@ export const data = new SlashCommandBuilder()
|
||||
.setMaxValue(99),
|
||||
)
|
||||
.addBooleanOption((option) =>
|
||||
option.setName('panel').setDescription('Publier le panneau d’explication dans le salon déclencheur'),
|
||||
option.setName('panel').setDescription('Publier le panneau d’explication dans le salon'),
|
||||
),
|
||||
)
|
||||
.addSubcommand((sub) => sub.setName('config').setDescription('Afficher la configuration actuelle'))
|
||||
.addSubcommand((sub) => sub.setName('config').setDescription('Afficher les déclencheurs configurés'))
|
||||
.addSubcommand((sub) =>
|
||||
sub.setName('panel').setDescription('Republier le panneau d’explication dans le salon déclencheur'),
|
||||
sub
|
||||
.setName('panel')
|
||||
.setDescription('Republier le panneau d’explication dans les salons déclencheurs')
|
||||
.addChannelOption((option) =>
|
||||
voiceChannelOption(option.setName('trigger').setDescription('Un seul déclencheur (défaut : tous)')),
|
||||
),
|
||||
)
|
||||
.addSubcommandGroup((group) =>
|
||||
group
|
||||
.setName('triggers')
|
||||
.setDescription('Gérer les salons déclencheurs')
|
||||
.addSubcommand((sub) => sub.setName('list').setDescription('Lister les déclencheurs'))
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('remove')
|
||||
.setDescription('Supprimer un déclencheur')
|
||||
.addChannelOption((option) =>
|
||||
voiceChannelOption(option.setName('trigger').setDescription('Salon').setRequired(true)),
|
||||
),
|
||||
)
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('disable')
|
||||
.setDescription('Désactiver un déclencheur sans le supprimer')
|
||||
.addChannelOption((option) =>
|
||||
voiceChannelOption(option.setName('trigger').setDescription('Salon').setRequired(true)),
|
||||
),
|
||||
)
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
.setName('enable')
|
||||
.setDescription('Réactiver un déclencheur')
|
||||
.addChannelOption((option) =>
|
||||
voiceChannelOption(option.setName('trigger').setDescription('Salon').setRequired(true)),
|
||||
),
|
||||
),
|
||||
)
|
||||
.addSubcommand((sub) =>
|
||||
sub
|
||||
@@ -134,66 +185,133 @@ export const data = new SlashCommandBuilder()
|
||||
),
|
||||
);
|
||||
|
||||
const ORIGIN_LABELS = {
|
||||
env: 'variables d’environnement',
|
||||
settings: 'anciens réglages du bot',
|
||||
};
|
||||
|
||||
/** One embed field per trigger, shared by `/voice config`, `/voice triggers list` and `/voice setup`. */
|
||||
function describeTrigger(trigger, index = null) {
|
||||
const lines = [
|
||||
`**Salon :** <#${trigger.triggerId}>`,
|
||||
`**Catégorie :** ${trigger.categoryId ? `<#${trigger.categoryId}>` : 'celle du salon déclencheur'}`,
|
||||
`**Modèle :** \`${trigger.template}\``,
|
||||
`**Limite :** ${trigger.userLimit ? `${trigger.userLimit} places` : 'illimitée'}`,
|
||||
`**Salons en cours :** ${trigger.activeChannels ?? 0}`,
|
||||
];
|
||||
const origin = ORIGIN_LABELS[trigger.source];
|
||||
if (origin) lines.push(`**Origine :** ${origin}`);
|
||||
|
||||
return {
|
||||
name: `${index === null ? '' : `${index + 1}. `}${trigger.label ? `${trigger.label} · ` : ''}${
|
||||
trigger.enabled ? '✅ Actif' : '⛔ Désactivé'
|
||||
}`,
|
||||
value: lines.join('\n'),
|
||||
};
|
||||
}
|
||||
|
||||
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 },
|
||||
const triggers = listTriggerSummaries(guildId);
|
||||
const embed = baseEmbed(COLORS.primary).setTitle('🎧 Salons vocaux temporaires');
|
||||
|
||||
if (triggers.length === 0) {
|
||||
return embed.setDescription(
|
||||
'Aucun déclencheur pour le moment : tu peux en ajouter un avec `/voice setup` (un par thème, ex. « Dev », « SIO »).',
|
||||
);
|
||||
}
|
||||
|
||||
embed.setDescription(
|
||||
`**${triggers.length}** déclencheur(s) · **${activeChannelCount(guildId)}** salon(s) temporaire(s) en cours\n` +
|
||||
'Chaque déclencheur crée ses salons en bas de sa propre catégorie, et ils disparaissent dès qu’ils se vident.',
|
||||
);
|
||||
embed.addFields(triggers.slice(0, MAX_LISTED_TRIGGERS).map((trigger, i) => describeTrigger(trigger, i)));
|
||||
if (triggers.length > MAX_LISTED_TRIGGERS) {
|
||||
embed.addFields({
|
||||
name: 'Autres déclencheurs',
|
||||
value: `… et ${triggers.length - MAX_LISTED_TRIGGERS} autre(s), visibles sur le serveur.`,
|
||||
});
|
||||
}
|
||||
return embed;
|
||||
}
|
||||
|
||||
async function runSetup(interaction) {
|
||||
const trigger = interaction.options.getChannel('trigger', true);
|
||||
if (trigger.type !== ChannelType.GuildVoice) {
|
||||
const channel = interaction.options.getChannel('trigger', true);
|
||||
if (channel.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(
|
||||
const existing = findTrigger(interaction.guildId, channel.id);
|
||||
const template = interaction.options.getString('template');
|
||||
const limit = interaction.options.getInteger('limit');
|
||||
const label = interaction.options.getString('label');
|
||||
|
||||
const trigger = saveVoiceTrigger(
|
||||
interaction.guildId,
|
||||
{
|
||||
trigger: trigger.id,
|
||||
...(category && { category: category.id }),
|
||||
...(template && { template }),
|
||||
...(limit !== null && { limit }),
|
||||
triggerId: channel.id,
|
||||
...(label !== null && { label }),
|
||||
// A trigger with no explicit category hosts its channels in its own category.
|
||||
categoryId: category?.id ?? existing?.categoryId ?? channel.parentId ?? null,
|
||||
...(template !== null && { template }),
|
||||
...(limit !== null && { userLimit: limit }),
|
||||
},
|
||||
interaction.user.id,
|
||||
);
|
||||
|
||||
await interaction.reply({
|
||||
embeds: [successEmbed(`Salon déclencheur : <#${trigger.id}>`)],
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
if (!trigger) {
|
||||
throw new UserFacingError('Le déclencheur n’a pas pu être enregistré, réessaie.');
|
||||
}
|
||||
|
||||
const embed = baseEmbed(COLORS.success)
|
||||
.setTitle(existing ? '✅ Déclencheur mis à jour' : '✅ Déclencheur ajouté')
|
||||
.addFields(describeTrigger(trigger))
|
||||
.setFooter({ text: 'Les salons créés depuis ce déclencheur apparaîtront en bas de sa catégorie.' });
|
||||
|
||||
await interaction.reply({ embeds: [embed], flags: MessageFlags.Ephemeral });
|
||||
|
||||
const wantsPanel = interaction.options.getBoolean('panel') ?? true;
|
||||
if (wantsPanel) await postSetupPanel(interaction).catch(() => {});
|
||||
if (wantsPanel) await postSetupPanel(interaction, { triggerId: channel.id }).catch(() => {});
|
||||
}
|
||||
|
||||
/** `/voice triggers …` — declaration and lifecycle of the trigger channels (M14-T01). */
|
||||
async function runTriggerGroup(interaction, subcommand) {
|
||||
const guildId = interaction.guildId;
|
||||
|
||||
if (subcommand === 'list') {
|
||||
await interaction.reply({ embeds: [configEmbed(guildId)], flags: MessageFlags.Ephemeral });
|
||||
return;
|
||||
}
|
||||
|
||||
const target = interaction.options.getChannel('trigger', true);
|
||||
if (subcommand === 'remove') {
|
||||
removeVoiceTriggerConfig(guildId, target.id);
|
||||
await interaction.reply({
|
||||
embeds: [
|
||||
successEmbed(
|
||||
`C’est fait : <#${target.id}> n’est plus un déclencheur. Les salons déjà créés depuis lui restent en place jusqu’à ce qu’ils se vident.`,
|
||||
),
|
||||
],
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const trigger = setTriggerEnabled(guildId, target.id, subcommand === 'enable');
|
||||
await interaction.reply({
|
||||
embeds: [
|
||||
successEmbed(
|
||||
trigger.enabled
|
||||
? `Bon retour ! Le déclencheur <#${trigger.triggerId}> crée à nouveau des salons.`
|
||||
: `Le déclencheur <#${trigger.triggerId}> est en pause : y entrer ne crée plus de salon, et sa configuration est conservée.`,
|
||||
),
|
||||
],
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
}
|
||||
|
||||
/** Every subcommand acts on the caller's temporary channel, checked by the service (M14-T06). */
|
||||
@@ -201,12 +319,19 @@ export async function execute(interaction) {
|
||||
const subcommand = interaction.options.getSubcommand();
|
||||
const channel = interaction.options.getChannel('channel');
|
||||
|
||||
if (interaction.options.getSubcommandGroup(false) === 'triggers') {
|
||||
await runTriggerGroup(interaction, subcommand);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (subcommand) {
|
||||
case 'setup':
|
||||
await runSetup(interaction);
|
||||
return;
|
||||
case 'panel':
|
||||
await postSetupPanel(interaction);
|
||||
await postSetupPanel(interaction, {
|
||||
triggerId: interaction.options.getChannel('trigger')?.id ?? null,
|
||||
});
|
||||
return;
|
||||
case 'config':
|
||||
await interaction.reply({ embeds: [configEmbed(interaction.guildId)], flags: MessageFlags.Ephemeral });
|
||||
|
||||
Reference in new issue
Block a user