按钮"/>
删除角色的按钮
我有一个带有角色设置的菜单,一个菜单角色,有一个角色设置和删除一个角色,这里是删除角色的代码,第一次对我有效,第二次出现错误。
const {
ButtonBuilder,
ActionRowBuilder,
StringSelectMenuBuilder,
ButtonStyle,
} = require('discord.js');
const database = require('../../database');
async function removeRoleButton(buttonInteraction) {
const guildId = buttonInteraction.guild.id;
const userId = buttonInteraction.user.id;
// Get personal roles from the database
const personalRoleIds = await database.getPersonalRoleFromDB2(guildId, userId);
const personalRoles = personalRoleIds && personalRoleIds.length > 0
? (await Promise.all(personalRoleIds.map(roleId => buttonInteraction.guild.roles.fetch(roleId).catch(() => null)))).filter(role => role)
: [];
if (personalRoles.length === 0) {
await buttonInteraction.Reply({
content: 'У вас нет личной роли для удаления.',
ephemeral: true,
});
} else {
// Create role selector menu
const roleSelector = generateRoleSelector(personalRoles);
const roleSelectActionRow = new ActionRowBuilder().addComponents(roleSelector);
const removeButton = new ButtonBuilder()
.setCustomId('removeRole')
.setLabel('Удалить роль')
.setStyle(ButtonStyle.Danger);
const removeActionRow = new ActionRowBuilder().addComponents(removeButton);
await buttonInteraction.reply({
content: 'Выберите личную роль для удаления:',
components: [roleSelectActionRow, removeActionRow],
ephemeral: true,
});
function generateRoleSelector(personalRoles) {
const roleSelector = new StringSelectMenuBuilder()
.setCustomId('roleSelector')
.setPlaceholder('Выберите личную роль для удаления')
.setMaxValues(1);
const options = personalRoles.map((role) => ({
label: role.name,
value: role.id,
description: `ID роли: ${role.id}`,
}));
roleSelector.addOptions(options);
return roleSelector;
}
async function handleRoleSelectorInteraction(selectInteraction) {
// Check if the user who caused the interaction is the same as the one who clicked the remove button
if (selectInteraction.user.id !== buttonInteraction.user.id) return;
const selectedRoleId = selectInteraction.values[0];
const selectedRole = selectInteraction.guild.roles.cache.get(selectedRoleId);
// Remove role
const removeRoleSelector = new StringSelectMenuBuilder()
.setCustomId('removeRoleSelector')
.setPlaceholder('Выберите, что делать с участниками этой роли')
.addOptions([
{
label: 'Удалить роль у всех участников',
value: 'remove',
description: 'Это действие удалит выбранную личную роль у всех участников, у которых она есть.',
emoji: '
更多推荐
删除角色的按钮
发布评论