NodeJS 如何从命令列表的末尾删除逗号?DiscordJS v14

hof1towb  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(115)

我试图删除逗号从命令列表的末尾,当我选择一个类别从菜单中,我已经尝试了一些东西,从帮助服务器,但他们没有工作,所以我诉诸于帮助,从你们这里的stackoverflow。

IMAGE

命令

const { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, Client } = require("discord.js");
const botconfig = require("../../botconfig");
const fs = require("fs");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("help")
    .setDescription("Sends the command list!"),
  async execute(interaction, client, args) {
    const dirs = fs.readdirSync("./commands");
    const slashCommands = await client.application.commands.fetch();

    const Buttons = new ActionRowBuilder().addComponents(
      new ButtonBuilder()
        .setCustomId("left")
        .setLabel("❮")
        .setStyle(ButtonStyle.Primary),
        
      new ButtonBuilder()
        .setCustomId("right")
        .setLabel("❯")
        .setStyle(ButtonStyle.Primary)
    );

    const embedMsg = new EmbedBuilder()
      .setTitle("__A1's Help Menu__")
      .setDescription(`>>> Prefix: \`${botconfig.defaultPrefix}\`\nVersion: \`${botconfig.version}\`\nCommands: \`${slashCommands.size}\``)
      .setThumbnail(client.user.displayAvatarURL())
      .addFields([
        {
          name: "🤗 - **Action**",
          value: "🔑 - **Administration**"
        },
        {
          name: "🎂 - **Birthday**",
          value: "🎮 - **Fun**"
        },
        {
          name: "🖼️ - **Image**",
          value: "💯 - **Level**"
        },
        {
          name: "🎲 - **Minigame**",
          value: "⚙️ - **Miscellaneous**"
        },
        {
          name: "🛡️ - **Moderation**",
          value: "🎫 - **Ticket**"
        }
      ])
      .setColor("#EE1C25")
      .setFooter({ text: "Navigate through the embeds using the provided menu below.", iconURL: interaction.user.displayAvatarURL() })

    let helpMenu = new ActionRowBuilder().addComponents(
      new StringSelectMenuBuilder()
        .setCustomId("helpMenu")
        .setMaxValues(1)
        .setMinValues(1)
        .setPlaceholder("Select Command Category")
    );

    fs.readdirSync("./commands").forEach((command) => {
      helpMenu.components[0].addOptions({
        label: `${command}`,
        description: `${command} commands`,
        value: `${command}`,
      });
    });

    // for (const folders of commandFolder) {
    //   helpMenu.components[0].addOptions({
    //     label: `${folders}`,
    //     description: `Command list for ${folders}`,
    //     value: `${folders}`,
    //   });
    // }

    interaction.reply({
      embeds: [embedMsg],
      components: [helpMenu, Buttons],
    });
  },
};

互动创建

const { Client, ChatInputCommandInteraction, InteractionType, EmbedBuilder } = require("discord.js");
  const fs = require("fs");
  
  module.exports = {
    name: "interactionCreate",
    async execute(interaction, client) {
      if (interaction.isSelectMenu) {
        if (interaction.customId === "helpMenu") {
          const selection = interaction.values[0];
          const commands = fs
            .readdirSync(`./commands/${selection}`)
            .filter((file) => file.endsWith(".js"))
            .join(" ")
            .split(".js");
  
          function capitalizeFL(str) {
            const capitalized = str.charAt(0).toUpperCase() + str.slice(1);
            return capitalized;
          }
  
          const embed = new EmbedBuilder()
            .setTitle(`Command list for ${selection}`)
            .setDescription(`\`\`\`${commands}\`\`\``)
            .setColor("Blurple")
            .addFields({
              name: `Command Count`,
              value: `${commands.length - 1} command(s)`,
            });
  
          await interaction.reply({
            embeds: [embed],
            ephemeral: true,
          });
        }
      }
    },
  };

我厌倦了其他人在支持不和谐的服务器的方法,当附近所有的人都没有工作,然后他们只是停止回答,是的,就像什么达赫克队友。

v1uwarro

v1uwarro1#

当你用一个数组测试这个问题时,你可以看到这个问题:

const files = ["test.js", "b.js", "add.js"]

const fileNames = files.filter((file) => file.endsWith(".js")).join(" ").split(".js");

console.log(fileNames);

在数组的末尾有一个空字符串,如果你在数组上调用join(),这个元素也会被连接起来,结果是在末尾有一个逗号。因为你检查文件是否以.js结尾,你可以只删除结尾的3个字符。

const files = ["test.js", "b.js", "add.js"]

const fileNames = files.filter((file) => file.endsWith(".js")).map((file) => file.slice(0, -3));

console.log(fileNames);

相关问题