【问题标题】:Can .addField value write an array that contains file "names"?.addField 值可以写入包含文件“名称”的数组吗?
【发布时间】:2020-09-15 21:05:40
【问题描述】:

所以我目前正在制作一个不和谐的机器人,我制作了一个 ~play 命令,它从文件夹(歌曲)中读取文件并将它们存储在一个数组中,然后它会选择一个随机文件并在语音通道中播放它。

现在我想制作一首 ~songs,让机器人在聊天中发送一个嵌入内容,其中包含 .setTitle("These are that I have:"),然后是 .addField({name: "songs:", value: "*all the songs in the array"})

我想要一个字段值中的所有歌曲的原因是歌曲太多,我只能使用 25 个字段。我在值和字段外都尝试了forEach,但没有一个效果很好。

这是我的代码:

const discord = require("discord.js");
const fs = require("fs");

module.exports.run = async (bot, message, args) => {
  const songFiles = fs
    .readdirSync("./commands/Workout/")
    .filter((file) => file.endsWith(".mp3"));

  let songsEmbed = new discord.MessageEmbed()
    .setColor("RANDOM")
    .setTitle("These are that I have:");

  //.addField('Songname:',commandFiles.forEach())//doesn't work

  for (i = 0; i < commandFiles.length; i++) {
    songsEmbed.addField({ name: "Song name:", value: `${commandFiles[i]},` });
  } //That just create field for every song

  message.reply(songsEmbed);
};

module.exports.help = {
  name: "songs",
};

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    您可以使用.join() 函数创建包含条目的字符串。

    假设我们有这个数组:

    const songArray = [
      "Toto - Africa.mp3",
      "Rick Astley - Never Gonna Give You Up.mp3"
      "Bag Raiders - Shooting Stars.mp3"
    ]
    

    然后我们可以运行.join() 来为该数组中的每首歌曲创建一个字符串。请注意,\n 表示新行。

    songArray.join('\n')
    
    /*
    Toto - Africa.mp3
    Rick Astley - Never Gonna Give You Up.mp3
    Bag Raiders - Shooting Stars.mp3
    */
    

    所以你的代码应该是这样的:

    .addField('Songname:', commandFiles.join('\n'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 2020-06-08
      相关资源
      最近更新 更多