Files
FWS-DiscordBot/src/ui/buttons.js
T

100 lines
3.3 KiB
JavaScript

import { ActionRowBuilder, ButtonBuilder, ButtonStyle, UserSelectMenuBuilder } from 'discord.js';
/** Discord label and custom id limits. */
const LABEL_MAX = 80;
export const POLL_CHOICES_MAX = 10;
function truncate(text, max) {
return text.length > max ? `${text.slice(0, max - 1)}…` : text;
}
/** Builds a row of link buttons, skipping links without a URL (M12-T03). */
export function linkButtons(links) {
const buttons = links
.filter((link) => link.url)
.slice(0, 5)
.map((link) => new ButtonBuilder().setStyle(ButtonStyle.Link).setLabel(link.label).setURL(link.url));
return buttons.length > 0 ? [new ActionRowBuilder().addComponents(buttons)] : [];
}
/** Claim / close controls posted in a ticket channel (M6-T05 / M6-T06). */
export function ticketButtons() {
return [
new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('ticket:claim')
.setLabel('Prendre en charge')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder().setCustomId('ticket:close').setLabel('Fermer').setStyle(ButtonStyle.Danger),
),
];
}
/**
* Control panel posted in a temporary voice channel (M14-T05):
* buttons for the whole channel, select menus for a specific member.
*/
export function voicePanelComponents({ locked = false } = {}) {
return [
new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('voice:rename')
.setLabel('Renommer')
.setEmoji('✏️')
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId('voice:limit')
.setLabel('Limiter')
.setEmoji('👥')
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId('voice:lock')
.setLabel(locked ? 'Déverrouiller' : 'Verrouiller')
.setEmoji(locked ? '🔓' : '🔒')
.setStyle(locked ? ButtonStyle.Success : ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId('voice:claim')
.setLabel('Réclamer')
.setEmoji('👑')
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('voice:delete')
.setLabel('Supprimer')
.setEmoji('🗑️')
.setStyle(ButtonStyle.Danger),
),
memberSelect('voice:permit', 'Autoriser un membre à rejoindre...'),
memberSelect('voice:kick', 'Exclure un membre du salon...'),
memberSelect('voice:transfer', 'Transférer la propriété du salon...'),
];
}
function memberSelect(customId, placeholder) {
return new ActionRowBuilder().addComponents(
new UserSelectMenuBuilder()
.setCustomId(customId)
.setPlaceholder(placeholder)
.setMinValues(1)
.setMaxValues(1),
);
}
/** One button per choice, 5 per row at most (M10-T02). */
export function pollButtons(pollId, choices) {
const rows = [];
for (let start = 0; start < Math.min(choices.length, POLL_CHOICES_MAX); start += 5) {
rows.push(
new ActionRowBuilder().addComponents(
choices.slice(start, start + 5).map((choice, offset) =>
new ButtonBuilder()
.setCustomId(`poll:${pollId}:${start + offset}`)
.setLabel(truncate(choice, LABEL_MAX))
.setStyle(ButtonStyle.Secondary),
),
),
);
}
return rows;
}