删除角色的按钮

编程入门 行业动态 更新时间:2024-10-04 19:25:20

删除角色的<a href=https://www.elefans.com/category/jswz/34/1771318.html style=按钮"/>

删除角色的按钮

我有一个带有角色设置的菜单,一个菜单角色,有一个角色设置和删除一个角色,这里是删除角色的代码,第一次对我有效,第二次出现错误。

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: ' 
          

更多推荐

删除角色的按钮

本文发布于:2024-05-30 05:34:18,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   角色

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!