命令我的机器人如何让我的机器人随机回复?

时间:2020-09-09 00:47:41

标签: javascript node.js discord.js

我希望在我命令我的机器人时,他回复随机消息。

这正确吗?

if(command === "memes") {

let replies = ["meme 1", "meme 2", "meme 3"];

let random = Math.floor(Math.random() * 3)

message.channel.send[random]

1 个答案:

答案 0 :(得分:1)

message.channel.send()是一个函数,而不是可以与[](数组标识符)一起使用的变量。您要执行的操作是从数组中的随机数发送字符串。尝试以下代码:

if(command === "memes") {

  // List Replies
  let replies = ["meme 1", "meme 2", "meme 3"];

  // Get a random number from `replies.length` instead so it'll be dynamic.
  // `replies.length` will return the length of the array, in this case `3`, so we can use it as such.
  let random = Math.floor(Math.random() * replies.length);

  // Send the message using `replies` number `random`
  message.channel.send(replies[random]);

}

如果您仍然需要帮助,请尝试查看以下链接: