簡體   English   中英

我的 Discord 機器人的 /rolemenuremove 命令沒有按預期工作,請幫幫我

[英]My Discord bot's /rolemenuremove command isn't working as expected, please help me out

我的 Discord 機器人的/rolemenuremove命令沒有按預期工作。 當我嘗試刪除菜單中的角色時,它返回一條消息,指出該角色已被刪除,但是當我檢查菜單時,另一個角色已被刪除。 (不是我想要它刪除的那個。)

這是該消息的圖像:

在此處輸入圖片說明

這是我的命令代碼:

const {MessageEmbed, MessageActionRow, MessageSelectMenu} = require('discord.js');

module.exports = {
  name: 'rolemenuremove',
  description: 'Removes a role from an existing rolemenu.',
  options: [
    {
      name: 'channel',
      description: 'The channel in which the rolemenu is.',
      type: 'CHANNEL',
      required: true
    },
    {
      name: 'messageid',
      description: 'The ID of the rolemenu message.',
      type: 'STRING',
      required: true
    },
    {
      name: 'role',
      description: 'The role to remove from the rolemenu.',
      type: 'ROLE',
      required: true
    }
  ],
  async execute(interaction) {
    const channel = interaction.options.getChannel('channel')

    if (
      channel.type !== 'GUILD_TEXT' &&
      channel.type !== 'GUILD_NEWS' &&
      channel.type !== 'GUILD_NEWS_THREAD' &&
      channel.type !== 'GUILD_PUBLIC_THREAD' &&
      channel.type !== 'GUILD_PRIVATE_THREAD'
    )
      return interaction.reply({content: 'That is not a valid channel type.', ephemeral: true});

    const messageid = interaction.options.getString('messageid')

    const role = interaction.options.getRole('role')

    const message = await channel.messages.fetch(messageid, {cache: true, force: true})

    if (!message) return interaction.reply('Invalid message ID.')
    if (message.author.id !== interaction.client.user.id) return interaction.reply(`Please provide a message sent by <@${interaction.client.user.id}>`)

    let row = message.components[0]
    if (!row) return interaction.reply('Invalid message.')

    const option = [{
      label: role.name,
      description: `Get the ${role.name} role.`,
      value: role.id
    }]

    let menu = row.components[0]

    if (menu) {
      if (!menu.options.some(option => option.value === role.id)) {
            return interaction.reply({
              content: `<@&${option[0].value}> is not a part of this menu.`,
              allowedMentions: {
                roles: []
              },
              ephemeral: true
            })
          }

      menu.spliceOptions(menu.options.lastIndexOf(option), 1)
      menu.setMaxValues(menu.options.length)

      if (menu.opions.length === 0) return interaction.reply('There needs to be at least one role in the menu.')
    } else {
      return interaction.reply("Invalid message.")
    }

    await message.edit({
      components: [row],
      content: message.content
    })

    await interaction.reply({
      content: `Removed <@&${role.id}> from the menu.`, allowedMentions: {roles: []},
      ephemeral: true
    })

  },
};

請幫助我,並提前致謝。 :)

編輯:更新了我的代碼並解決了原始問題,但現在menu.spliceOptions部分不起作用。 編輯 2:更新了menu.spliceOptions行,仍然無法正常工作

弄清楚了。 這不是代碼的問題,而是我測試的角色的問題。 不知何故,該選項被破壞了,並制作了一個全新的菜單來修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM