【问题标题】:Add Field data from an array to an Embed once created创建后将数组中的字段数据添加到嵌入
【发布时间】:2020-11-07 05:20:27
【问题描述】:

编辑:已经修复。感谢大家

我正在尝试将字段添加到从 for 创建的嵌入中,因为我想添加保存在数组中的值。

我已经尝试了下一个代码,没有一个成功

for(let i = 0; i < nombres.length; i++){
            embed.addField(nombres[i], descripciones[i], true)
        }
for(let i = 0; i < nombres.length; i++){
            embed = {
                fields: {
                    name: nombres[i],
                    value: descripciones[i],
                    inline: true,
                },
            };
        }
for(let i = 0; i < nombres.length; i++){
            embed.fields(nombres[i], descripciones[i], true)
        }
for(let i = 0; i < nombres.length; i++){
            embed.addFields({name: nombres[i], value: descripciones[i], inline: true});
        }

问题是,我想做这样的事情,但可行

let nombres = [];
let descripciones = [];

const embed = new Discord.MessageEmbed()
.setDescription('All commands available.')

for(let i = 0; i < nombres.length; i++){
   embed.addField(nombres[i], descripciones[i], true)
}

而我得到的只是这个

【问题讨论】:

  • 两个数组是如何构建的?我的意思是它们是常量变量,还是您在使用它们之前就构建它们?
  • @Tenclea 是的,在执行我发布的代码并使用 console.log 检查之前,我会在它们上保存值。问题是,一旦我写了另一件事与嵌入无关,我不知道如何将新字段添加到嵌入中。
  • 这有点奇怪。我也认为这会起作用..我会自己做一些研究,如果我找到任何东西告诉你

标签: javascript discord discord.js


【解决方案1】:

试试这个代码,这应该可以工作

const { MessageEmbed } = require("discord.js")

let array1 = ["1", "2", "3", "4"]
let array2 = ["one", "two", "three", "four"]

var embed = new MessageEmbed();

embed.setDescription('test')

for (let i = 0; i < array1.length; i++) 
{
   embed.addField(array1[i], array2[i], false)
}

message.channel.send(embed)

// imports
const Discord = require("discord.js");
const fs = require("fs").promises;
const fss = require("fs");
const path = require("path");

// exports
module.exports = {
    name: "commands",
    alias: ["commands"],
    description: "",
    run: (client, message, args) => {

        let nombres = [],
            descripciones = [];

        fs.readdir(path.join(__dirname))
            .then(filess => {
                x = 0;
                filess.forEach(fil => {
                    if (!fil.endsWith('.js')) return;
                    let commandName = fil.substring(0, fil.indexOf('.js'));
                    let commandModule = require(path.join(__dirname, commandName));

                    nombres[x] = commandName;
                    descripciones[x] = commandModule.description;
                    x++
                    console.log(x, nombres[x], descripciones[x])
                })
            })


        var embed = new Discord.MessageEmbed();

        embed.setDescription('All commands available.')

        for (let i = 0; i < nombres.length; i++) {
            embed.addField(nombres[i], descripciones[i], false)
        }

        console.log({ embed })
        message.channel.send(embed)
    }
}

【讨论】:

  • 这是所有代码。是的,由于某种原因,这些值没有加入数组。 gyazo.com/666541fcb6604e63be74e1b603982f17 在控制台显示时显示未定义
  • 好的,你试过我建议的代码了吗?如果不是,我已将完整的命令代码添加到答案中。 (module.exports.description 留空)
猜你喜欢
  • 2016-10-16
  • 2021-10-29
  • 1970-01-01
  • 2019-09-26
  • 2018-10-27
  • 2014-05-18
  • 1970-01-01
  • 2021-10-20
  • 2022-07-02
相关资源
最近更新 更多