【问题标题】:How to respond with embeds如何使用嵌入响应
【发布时间】:2021-02-13 20:16:29
【问题描述】:

目前我正在尝试使用我的机器人并更新它的响应。现在我正在尝试使用下面的代码使其响应我制作的随机嵌入。出于某种原因,在使用此代码时,机器人会以“NaN”或“3”响应?

const embeds = [`${yellowinfocommandembed}`, `${whiteinfocommandembed}`, `${redinfocommandembed}`, `${purpleinfocommandembed}`, `${pinkinfocommandembed}`, `${orangeinfocommandembed}`,`${limeinfocommandembed}`,`${greeninfocommandembed}`,`${cyaninfocommandembed}`,`${browninfocommandembed}`,`${blueinfocommandembed}`,`${blackinfocommandembed}` ];
const embed = Math.floor(Math.random() * embeds.length);
    if(message.channel.type == "dm") {
        message.channel.send(embed);
    }
        else {
            message.delete ();
            message.channel.send(embed);
        }
}

【问题讨论】:

  • 老实说,我不确定使用随机性对我来说是全新的,但它实际上是在做随机数学而不是随机化响应?
  • 请不要用 java 标记 javascript 问题。它们是两种不同的语言。

标签: javascript discord discord.js bots


【解决方案1】:

目的:

Math.floor(Math.random() * embeds.length);

是生成一个介于 0 和数组长度(下限)之间的伪随机整数。这不提供数组中的随机元素。您可以使用此数字通过括号语法从数组中获取一个元素。

const arr = ['Peach', 'Pear', 'Plum', 'Pomegranate'];
const num = Math.floor(Math.random() * arr.length);

console.log(`The random number: ${num}`);
console.log(`The corresponding array element: ${arr[num]}`);

编辑:

您必须了解模板文字才能了解您的机器人发送[object Object] 的原因。模板文字(${variable})主要用于两个目的

  • 将字符串和变量或多个变量合并在一起
  • 将变量的数据类型强制为字符串

这是第二个目的的示例。

// normal usage: (`1 + 1 is ${1 + 1}`) => 1 + 1 is 2

console.log(typeof 10); // just the number 10
console.log(typeof `${10}`); // but if I put it in a teplate literal, it coerces itself to a string

// of course, this also applies for concatenation using the + operator

// normal usage: ('1 + 1 is ' + (1 + 1)) => 1 + 1 is 2
console.log(typeof 10); // just the number two
console.log(typeof ('' + 10)); // but if I merge it with an empty string, it coerces itself to a string

为什么这很重要?例如,orangeinfocommandembedMessageEmbed 类的一个实例。所以当它被强制转换成一个字符串时,它就变成了[object Object]

const obj = { foo: 'bar' };
console.log(`${obj}`); // [object Object]

抱歉,对于一个相对简单的修复,这是一个很长的解释。只需删除模板文字并仅使用基本变量。

const embeds = [
 yellowinfocommandembed,
 whiteinfocommandembed,
 redinfocommandembed,
 purpleinfocommandembed,
 pinkinfocommandembed,
 orangeinfocommandembed,
 limeinfocommandembed,
 greeninfocommandembed,
 cyaninfocommandembed,
 browninfocommandembed,
 blueinfocommandembed,
 blackinfocommandembed,
];

【讨论】:

  • 感谢您再次回答我的一个问题!再次帮助了我,但现在我的问题是它只是说“[object Object]”我假设因为我称之为 emebeds 的方式不能以这种方式完成。它是否可以发送嵌入而不是仅仅说 "[object Object]" ` if(message.channel.type == "dm") { message.channel.send(${embeds[embed]}); } 其他 { 消息。删除(); message.channel.send(${embeds[embed]}); } }``
  • 我将编辑我的问题以说明为什么会发生这种情况以及如何解决它。
【解决方案2】:
const embeds = [yellowinfocommandembed, whiteinfocommandembed, redinfocommandembed, purpleinfocommandembed, pinkinfocommandembed, orangeinfocommandembed, limeinfocommandembed, greeninfocommandembed, cyaninfocommandembed, browninfocommandembed, blueinfocommandembed, blackinfocommandembed];
const embed = embeds[Math.floor(Math.random() * embeds.length)];

if(message.channel.type != "dm"){
    message.delete();
}

message.channel.send(embed);

2 个问题:

  1. 在“嵌入”数组中,您使用 ` 将嵌入保存为字符串,而不是嵌入。
  2. 在“嵌入”中,您只生成了一个随机数,而不是从嵌入数组中获取随机元素,您可以在我提供的代码中查看如何操作。

【讨论】:

  • 谢谢,所以这是我调用嵌入的方式?
  • 是的,要从数组中获取元素,您调用数组并在方括号中指定元素的索引(在这种情况下是随机的)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
  • 2012-08-23
  • 2017-02-02
相关资源
最近更新 更多